
Practice 100 Java Method Programming Questions
Q1. Write a Java program to create a method that takes two integers as input and returns their sum.
Input:
Enter first number: 5
Enter second number: 10
Expected Output:
The sum is: 15
Q2. Write a Java program to create a method that takes a string as input and returns its length.
Input:
Enter a string: HelloWorld
Expected Output:
The length of the string is: 10
Q3. Write a Java program to create a method that takes an integer as input and returns `true` if it is even, otherwise `false`.
Input:
Enter a number: 4
Expected Output:
The number is even: true
Q4. Write a Java program to create a method that calculates the factorial of a given number.
Input:
Enter a number: 5
Expected Output:
The factorial is: 120
Q5. Write a Java program to create a method that takes an array of integers and returns the largest number.
Input:
Enter array elements: 2, 8, 3, 7
Expected Output:
The largest number is: 8
Q6. Write a Java program to create a method that checks if a given string is a palindrome.
Input:
Enter a string: madam
Expected Output:
The string is a palindrome: true
Q7. Write a Java program to create a method that takes a character and returns whether it is a vowel or not.
Input:
Enter a character: e
Expected Output:
The character is a vowel: true
Q8. Write a Java program to create a method that swaps two numbers.
Input:
Enter two numbers: 3 7
Expected Output:
Before swap: a = 3, b = 7
After swap: a = 7, b = 3
Q9. Write a Java program to create a method that takes an array of integers and returns their average.
Input:
Enter array elements: 10, 20, 30, 40, 50
Expected Output:
The average is: 30.0
Q10. Write a Java program to create a method that prints the Fibonacci series up to a given number of terms.
Input:
Enter the number of terms: 5
Expected Output:
Fibonacci series: 0 1 1 2 3
Q11. Write a Java program to create a method that converts a given temperature in Celsius to Fahrenheit.
Input:
Enter temperature in Celsius: 25
Expected Output:
Temperature in Fahrenheit: 77.0
Q12. Write a Java program to create a method that calculates the power of a number using recursion.
Input:
Enter base: 2
Enter exponent: 3
Expected Output:
Result: 8
Q13. Write a Java program to create a method that reverses a given integer.
Input:
Enter an integer: 12345
Expected Output:
Reversed number: 54321
Q14. Write a Java program to create a method that checks if a number is a prime number.
Input:
Enter a number: 7
Expected Output:
The number is prime: true
Q15. Write a Java program to create a method that converts a given string to uppercase.
Input:
Enter a string: hello
Expected Output:
Uppercase string: HELLO
Q16. Write a Java program to create a method that takes a number as input and returns its square.
Input:
Enter a number: 6
Expected Output:
Square of the number: 36
Q17. Write a Java program to create a method that calculates the area of a circle given its radius.
Input:
Enter radius: 5
Expected Output:
Area of the circle: 78.54
Q18. Write a Java program to create a method that counts the number of vowels in a given string.
Input:
Enter a string: programming
Expected Output:
Number of vowels: 3
Q19. Write a Java program to create a method that takes an array and sorts it in ascending order.
Input:
Enter array elements: 5, 2, 8, 1
Expected Output:
Sorted array: 1, 2, 5, 8
Q20. Write a Java program to create a method that checks whether a year is a leap year or not.
Input:
Enter a year: 2024
Expected Output:
The year is a leap year: true
Q21. Write a Java program to create a method that concatenates two strings.
Input:
Enter first string: Hello
Enter second string: World
Expected Output:
Concatenated string: HelloWorld
Q22. Write a Java program to create a method that calculates the sum of the digits of a given number.
Input:
Enter a number: 123
Expected Output:
Sum of digits: 6
Q23. Write a Java program to create a method that converts a binary number to its decimal equivalent.
Input:
Enter binary number: 1011
Expected Output:
Decimal equivalent: 11
Q24. Write a Java program to create a method that finds the GCD of two numbers.
Input:
Enter two numbers: 12, 18
Expected Output:
GCD: 6
Q25. Write a Java program to create a method that calculates the compound interest.
Input:
Enter principal: 1000
Enter rate: 5
Enter time: 2
Expected Output:
Compound interest: 102.5
Q26. Write a Java program to create a method that takes a string and counts the number of words in it.
Input:
Enter a string: Java is fun
Expected Output:
Number of words: 3
Q27. Write a Java program to create a method that calculates the sum of an array of integers.
Input:
Enter array elements: 4, 5, 6
Expected Output:
Sum of array elements: 15
Q28. Write a Java program to create a method that reverses a string.
Input:
Enter a string: Java
Expected Output:
Reversed string: avaJ
Q29. Write a Java program to create a method that converts a given decimal number to binary.
Input:
Enter a decimal number: 10
Expected Output:
Binary equivalent: 1010
Q30. Write a Java program to create a method that checks if a number is an Armstrong number.
Input:
Enter a number: 153
Expected Output:
The number is an Armstrong number: true
Q31. Write a Java program to create a method that calculates the simple interest.
Input:
Enter principal: 5000
Enter rate: 4
Enter time: 3
Expected Output:
Simple interest: 600
Q32. Write a Java program to create a method that calculates the perimeter of a rectangle.
Input:
Enter length: 7
Enter breadth: 5
Expected Output:
Perimeter of rectangle: 24
Q33. Write a Java program to create a method that finds the second largest number in an array.
Input:
Enter array elements: 10, 20, 30, 40
Expected Output:
Second largest number: 30
Q34. Write a Java program to create a method that checks if a number is a perfect number.
Input:
Enter a number: 6
Expected Output:
The number is a perfect number: true
Q35. Write a Java program to create a method that takes a string and checks if it contains only digits.
Input:
Enter a string: 12345
Expected Output:
The string contains only digits: true
Q36. Write a Java program to create a method that finds the minimum number in an array.
Input:
Enter array elements: 12, 5, 8, 19
Expected Output:
Minimum number: 5
Q37. Write a Java program to create a method that calculates the product of two numbers.
Input:
Enter first number: 4
Enter second number: 7
Expected Output:
Product: 28
Q38. Write a Java program to create a method that converts a string to lowercase.
Input:
Enter a string: JAVA
Expected Output:
Lowercase string: java
Q39. Write a Java program to create a method that finds the HCF (Highest Common Factor) of two numbers.
Input:
Enter two numbers: 28, 42
Expected Output:
HCF: 14
Q40. Write a Java program to create a method that checks whether a character is an alphabet.
Input:
Enter a character: a
Expected Output:
The character is an alphabet: true
Q41. Write a Java program to create a method that returns the absolute value of a number.
Input:
Enter a number: -15
Expected Output:
Absolute value: 15
Q42. Write a Java program to create a method that counts the occurrences of a specific character in a string.
Input:
Enter a string: programming
Enter a character: g
Expected Output:
Occurrences of 'g': 2
Q43. Write a Java program to create a method that finds the smallest and largest number in an array.
Input:
Enter array elements: 3, 9, 1, 5
Expected Output:
Smallest: 1
Largest: 9
Q44. Write a Java program to create a method that calculates the sum of squares of the first N natural numbers.
Input:
Enter a number: 5
Expected Output:
Sum of squares: 55
Q45. Write a Java program to create a method that checks if a string starts with a specific prefix.
Input:
Enter a string: programming
Enter a prefix: pro
Expected Output:
The string starts with the prefix: true
Q46. Write a Java program to create a method that calculates the factorial of a number using iteration.
Input:
Enter a number: 5
Expected Output:
Factorial: 120
Q47. Write a Java program to create a method that converts a given character to its ASCII value.
Input:
Enter a character: A
Expected Output:
ASCII value: 65
Q48. Write a Java program to create a method that takes a string and checks if it ends with a specific suffix.
Input:
Enter a string: learning
Enter a suffix: ing
Expected Output:
The string ends with the suffix: true
Q49. Write a Java program to create a method that finds the length of the longest word in a sentence.
Input:
Enter a sentence: Java programming is fun
Expected Output:
Length of the longest word: 11
Q50. Write a Java program to create a method that converts a decimal number to its hexadecimal equivalent.
Input:
Enter a decimal number: 255
Expected Output:
Hexadecimal equivalent: FF
Q51. Write a Java program to create a method that removes all vowels from a string.
Input:
Enter a string: beautiful
Expected Output:
String without vowels: btfl
Q52. Write a Java program to create a method that calculates the total surface area of a cube given its side length.
Input:
Enter side length: 3
Expected Output:
Surface area of cube: 54
Q53. Write a Java program to create a method that checks if an integer is even or odd.
Input:
Enter a number: 7
Expected Output:
The number is odd: true
Q54. Write a Java program to create a method that counts the number of consonants in a string.
Input:
Enter a string: education
Expected Output:
Number of consonants: 5
Q55. Write a Java program to create a method that calculates the area of a triangle given its base and height.
Input:
Enter base: 5
Enter height: 4
Expected Output:
Area of triangle: 10.0
Q56. Write a Java program to create a method that takes two numbers and returns their greatest number.
Input:
Enter two numbers: 10, 20
Expected Output:
Greatest number: 20
Q57. Write a Java program to create a method that reverses the words in a sentence.
Input:
Enter a sentence: Java is awesome
Expected Output:
Reversed sentence: awesome is Java
Q58. Write a Java program to create a method that checks if a string contains only alphabets.
Input:
Enter a string: Hello123
Expected Output:
The string contains only alphabets: false
Q59. Write a Java program to create a method that calculates the cube of a number.
Input:
Enter a number: 3
Expected Output:
Cube: 27
Q60. Write a Java program to create a method that calculates the sum of natural numbers up to a given limit.
Input:
Enter a number: 10
Expected Output:
Sum of natural numbers: 55
Q61. Write a Java program to create a method that checks if a year is a leap year.
Input:
Enter a year: 2024
Expected Output:
The year is a leap year: true
Q62. Write a Java program to create a method that finds the average of three numbers.
Input:
Enter three numbers: 10, 20, 30
Expected Output:
Average: 20.0
Q63. Write a Java program to create a method that swaps two numbers without using a third variable.
Input:
Enter first number: 5
Enter second number: 10
Expected Output:
Swapped numbers: First = 10, Second = 5
Q64. Write a Java program to create a method that calculates the sum of even numbers up to a given limit.
Input:
Enter a limit: 10
Expected Output:
Sum of even numbers: 30
Q65. Write a Java program to create a method that converts a string to title case (capitalize the first letter of each word).
Input:
Enter a string: java programming is fun
Expected Output:
Title case string: Java Programming Is Fun
Q66. Write a Java program to create a method that calculates the circumference of a circle.
Input:
Enter radius: 7
Expected Output:
Circumference of circle: 43.96
Q67. Write a Java program to create a method that takes a number and returns its reverse.
Input:
Enter a number: 1234
Expected Output:
Reversed number: 4321
Q68. Write a Java program to create a method that removes duplicate characters from a string.
Input:
Enter a string: programming
Expected Output:
String without duplicates: programin
Q69. Write a Java program to create a method that checks if a number is divisible by both 3 and 5.
Input:
Enter a number: 15
Expected Output:
The number is divisible by both 3 and 5: true
Q70. Write a Java program to create a method that calculates the power of a number using a loop.
Input:
Enter base: 2
Enter exponent: 3
Expected Output:
Result: 8
Q71. Write a Java program to create a method that prints the Fibonacci series up to a given term.
Input:
Enter number of terms: 5
Expected Output:
Fibonacci series: 0, 1, 1, 2, 3
Q72. Write a Java program to create a method that concatenates two strings.
Input:
Enter first string: Hello
Enter second string: World
Expected Output:
Concatenated string: HelloWorld
Q73. Write a Java program to create a method that checks if a given number is prime.
Input:
Enter a number: 11
Expected Output:
The number is prime: true
Q74. Write a Java program to create a method that calculates the GCD (Greatest Common Divisor) of two numbers.
Input:
Enter two numbers: 54, 24
Expected Output:
GCD: 6
Q75. Write a Java program to create a method that prints the multiplication table of a given number.
Input:
Enter a number: 5
Expected Output:
Multiplication table:
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
...
5 x 10 = 50
Q76. Write a Java program to create a method that finds the largest digit in a given number.
Input:
Enter a number: 5931
Expected Output:
Largest digit: 9
Q77. Write a Java program to create a method that checks if a string is a palindrome (case-insensitive).
Input:
Enter a string: Racecar
Expected Output:
The string is a palindrome: true
Q78. Write a Java program to create a method that converts a temperature from Celsius to Fahrenheit.
Input:
Enter temperature in Celsius: 25
Expected Output:
Temperature in Fahrenheit: 77.0
Q79. Write a Java program to create a method that sorts an array in ascending order.
Input:
Enter array elements: 5, 2, 9, 1
Expected Output:
Sorted array: 1, 2, 5, 9
Q80. Write a Java program to create a method that calculates the simple interest given principal, rate, and time.
Input:
Enter principal: 1000
Enter rate: 5
Enter time: 2
Expected Output:
Simple interest: 100.0
Q81. Write a Java program to create a method that converts a binary number to decimal.
Input:
Enter a binary number: 1101
Expected Output:
Decimal equivalent: 13
Q82. Write a Java program to create a method that checks if a number is a perfect square.
Input:
Enter a number: 16
Expected Output:
The number is a perfect square: true
Q83. Write a Java program to create a method that counts the number of vowels in a string.
Input:
Enter a string: education
Expected Output:
Number of vowels: 5
Q84. Write a Java program to create a method that checks if a number is a power of 2.
Input:
Enter a number: 8
Expected Output:
The number is a power of 2: true
Q85. Write a Java program to create a method that finds the second largest number in an array.
Input:
Enter array elements: 10, 20, 30, 40
Expected Output:
Second largest number: 30
Q86. Write a Java program to create a method that removes all spaces from a string.
Input:
Enter a string: Hello World
Expected Output:
String without spaces: HelloWorld
Q87. Write a Java program to create a method that calculates the sum of digits in a given number.
Input:
Enter a number: 1234
Expected Output:
Sum of digits: 10
Q88. Write a Java program to create a method that converts all vowels in a string to uppercase.
Input:
Enter a string: programming
Expected Output:
Modified string: prOgrAmmIng
Q89. Write a Java program to create a method that checks if a number is an Armstrong number.
Input:
Enter a number: 153
Expected Output:
The number is an Armstrong number: true
Q90. Write a Java program to create a method that generates the first N prime numbers.
Input:
Enter N: 5
Expected Output:
First 5 prime numbers: 2, 3, 5, 7, 11
Q91. Write a Java program to create a method that finds the factorial of a given number using recursion.
Input:
Enter a number: 5
Expected Output:
Factorial: 120
Q92. Write a Java program to create a method that converts a decimal number to binary.
Input:
Enter a decimal number: 10
Expected Output:
Binary equivalent: 1010
Q93. Write a Java program to create a method that counts the number of words in a string.
Input:
Enter a string: Java is fun
Expected Output:
Number of words: 3
Q94. Write a Java program to create a method that calculates the sum of all odd numbers up to a given limit.
Input:
Enter a limit: 10
Expected Output:
Sum of odd numbers: 25
Q95. Write a Java program to create a method that checks if two strings are anagrams.
Input:
Enter first string: listen
Enter second string: silent
Expected Output:
The strings are anagrams: true
Q96. Write a Java program to create a method that generates a random number between two given numbers.
Input:
Enter lower limit: 5
Enter upper limit: 15
Expected Output:
Random number: 8
Q97. Write a Java program to create a method that calculates the harmonic sum up to a given number.
Input:
Enter a number: 4
Expected Output:
Harmonic sum: 2.083333333333333
Q98. Write a Java program to create a method that reverses an array.
Input:
Enter array elements: 1, 2, 3, 4, 5
Expected Output:
Reversed array: 5, 4, 3, 2, 1
Q99. Write a Java program to create a method that prints the prime factors of a given number.
Input:
Enter a number: 28
Expected Output:
Prime factors: 2, 2, 7
Q100. Write a Java program to create a method that finds the longest word in a sentence.
Input:
Enter a sentence: Java is a powerful programming language
Expected Output:
Longest word: programming