Python Java C C++ HTML CSS JS

Java Practice Questions

Topic-based coding problems with sample test cases — build confidence step by step.

Java Methods Practice

Defining and calling methods, parameters, return values and reusable logic.

1
Add Two Numbers (Method)
Easy
Write a method int add(int a, int b) and use it to print the sum of two integers.
A single line with two integers A and B.
A single integer: the sum.
Sample Input 1
5 7
Sample Output 1
12
Sample Input 2
-3 8
Sample Output 2
5
Sample Input 3
0 0
Sample Output 3
0
Sample Input 4
1000000000 1000000000
Sample Output 4
2000000000
Sample Input 5
-10 3
Sample Output 5
-7
-10^9
Define a static method and call it from main.
2
Is Even (Method)
Easy
Write a method boolean isEven(int n) and use it to print "Even" or "Odd".
A single integer N.
"Even" or "Odd".
Sample Input 1
7
Sample Output 1
Odd
Sample Input 2
12
Sample Output 2
Even
Sample Input 3
0
Sample Output 3
Even
Sample Input 4
-3
Sample Output 4
Odd
Sample Input 5
-2
Sample Output 5
Even
-10^6
return n % 2 == 0.
3
Square (Method)
Easy
Write a method int square(int n) and use it to print n squared.
A single integer N.
A single integer: N*N.
Sample Input 1
5
Sample Output 1
25
Sample Input 2
12
Sample Output 2
144
Sample Input 3
1
Sample Output 3
1
Sample Input 4
0
Sample Output 4
0
Sample Input 5
10000
Sample Output 5
100000000
1
return n * n.
4
Max of Two (Method)
Easy
Write a method int max(int a, int b) and use it to print the larger of two integers.
A single line with two integers A and B.
A single integer: the maximum.
Sample Input 1
5 9
Sample Output 1
9
Sample Input 2
-3 -7
Sample Output 2
-3
Sample Input 3
7 7
Sample Output 3
7
Sample Input 4
100 1
Sample Output 4
100
Sample Input 5
-100 -1
Sample Output 5
-1
-10^9
return (a > b) ? a : b.
5
Area of Rectangle (Method)
Easy
Write a method int area(int l, int b) and use it to print the area of a rectangle.
A single line with two integers L and B.
A single integer: L * B.
Sample Input 1
4 5
Sample Output 1
20
Sample Input 2
10 10
Sample Output 2
100
Sample Input 3
1 1
Sample Output 3
1
Sample Input 4
1000000 1
Sample Output 4
1000000
Sample Input 5
7 8
Sample Output 5
56
1
return l * b.
6
Perimeter of Rectangle (Method)
Easy
Write a method int perimeter(int l, int b) and use it to print the perimeter of a rectangle.
A single line with two integers L and B.
A single integer: 2 * (L + B).
Sample Input 1
4 5
Sample Output 1
18
Sample Input 2
10 10
Sample Output 2
40
Sample Input 3
1 1
Sample Output 3
4
Sample Input 4
1000000 1
Sample Output 4
2000002
Sample Input 5
3 7
Sample Output 5
20
1
return 2 * (l + b).
1
Factorial (Method)
Medium
Write a method long factorial(int n) and use it to print N!.
A single integer N.
A single integer: N!.
Sample Input 1
5
Sample Output 1
120
Sample Input 2
0
Sample Output 2
1
Sample Input 3
1
Sample Output 3
1
Sample Input 4
10
Sample Output 4
3628800
Sample Input 5
20
Sample Output 5
2432902008176640000
0
Loop inside the method.
2
Is Prime (Method)
Medium
Write a method boolean isPrime(int n) and use it to print "Prime" or "Not Prime".
A single integer N.
"Prime" or "Not Prime".
Sample Input 1
17
Sample Output 1
Prime
Sample Input 2
15
Sample Output 2
Not Prime
Sample Input 3
2
Sample Output 3
Prime
Sample Input 4
97
Sample Output 4
Prime
Sample Input 5
100
Sample Output 5
Not Prime
2
Test divisors up to sqrt(n).
3
GCD (Method)
Medium
Write a method int gcd(int a, int b) and use it to print the GCD of two numbers.
A single line with two integers A and B.
A single integer: gcd(A, B).
Sample Input 1
48 18
Sample Output 1
6
Sample Input 2
12 8
Sample Output 2
4
Sample Input 3
17 5
Sample Output 3
1
Sample Input 4
100 75
Sample Output 4
25
Sample Input 5
1000000 500000
Sample Output 5
500000
1
Euclidean algorithm in the method.
4
Reverse Number (Method)
Medium
Write a method int reverse(int n) and use it to print the reversed number (leading zeros dropped).
A single integer N.
A single integer: the reversed number.
Sample Input 1
12345
Sample Output 1
54321
Sample Input 2
100
Sample Output 2
1
Sample Input 3
1
Sample Output 3
1
Sample Input 4
9009
Sample Output 4
9009
Sample Input 5
123456789
Sample Output 5
987654321
1
Build the reverse in a loop.
5
Sum of Array (Method)
Medium
Write a method int sum(int[] arr) and use it to print the sum of N integers.
First line: N. Second line: N integers.
A single integer: the sum.
Sample Input 1
5
1 2 3 4 5
Sample Output 1
15
Sample Input 2
3
10 -2 4
Sample Output 2
12
Sample Input 3
1
7
Sample Output 3
7
Sample Input 4
4
-1 -1 -1 -1
Sample Output 4
-4
Sample Input 5
6
1 1 1 1 1 1
Sample Output 5
6
1
Loop in the method over arr.
6
Count Vowels (Method)
Medium
Write a method int countVowels(String s) and use it to print the vowel count of a string.
A single line containing a string S (lowercase letters).
A single integer: the vowel count.
Sample Input 1
hello
Sample Output 1
2
Sample Input 2
programming
Sample Output 2
3
Sample Input 3
aeiou
Sample Output 3
5
Sample Input 4
bcdfg
Sample Output 4
0
Sample Input 5
java
Sample Output 5
2
1
Loop over chars in the method.
7
Power (Method)
Medium
Write a method long power(int b, int e) and use it to print B^E.
A single line with two integers B and E.
A single integer: B^E.
Sample Input 1
2 10
Sample Output 1
1024
Sample Input 2
5 3
Sample Output 2
125
Sample Input 3
0 0
Sample Output 3
1
Sample Input 4
0 5
Sample Output 4
0
Sample Input 5
10 10
Sample Output 5
10000000000
0
Loop in the method.
8
Is Palindrome Number (Method)
Medium
Write a method boolean isPalindrome(int n) and use it to print "Palindrome" or "Not Palindrome".
A single integer N.
"Palindrome" or "Not Palindrome".
Sample Input 1
121
Sample Output 1
Palindrome
Sample Input 2
123
Sample Output 2
Not Palindrome
Sample Input 3
1
Sample Output 3
Palindrome
Sample Input 4
9009
Sample Output 4
Palindrome
Sample Input 5
12321
Sample Output 5
Palindrome
1
Compare the number with its reverse.
9
Largest Digit (Method)
Medium
Write a method int largestDigit(int n) and use it to print the largest digit of N.
A single integer N.
A single integer: the largest digit.
Sample Input 1
2913
Sample Output 1
9
Sample Input 2
100
Sample Output 2
1
Sample Input 3
1
Sample Output 3
1
Sample Input 4
987654321
Sample Output 4
9
Sample Input 5
50505
Sample Output 5
5
1
Extract digits in the method.
10
Convert to Uppercase (Method)
Medium
Write a method String toUpper(String s) and use it to print the uppercase version of a lowercase string.
A single line containing a string S.
The uppercase string.
Sample Input 1
hello
Sample Output 1
HELLO
Sample Input 2
java
Sample Output 2
JAVA
Sample Input 3
a
Sample Output 3
A
Sample Input 4
techbridge24
Sample Output 4
TECHBRIDGE24
Sample Input 5
xyz
Sample Output 5
XYZ
1
Loop over chars and subtract 32 (or use toUpperCase).
1
LCM (Method)
Hard
Write a method int lcm(int a, int b) (using your gcd) and use it to print the LCM of two numbers.
A single line with two integers A and B.
A single integer: lcm(A, B).
Sample Input 1
4 6
Sample Output 1
12
Sample Input 2
3 5
Sample Output 2
15
Sample Input 3
1 1
Sample Output 3
1
Sample Input 4
12 18
Sample Output 4
36
Sample Input 5
7 7
Sample Output 5
7
1
lcm = a / gcd(a, b) * b.
2
String Reverse (Method)
Hard
Write a method String reverse(String s) and use it to print a string reversed.
A single line containing a string S.
The reversed string.
Sample Input 1
hello
Sample Output 1
olleh
Sample Input 2
abc
Sample Output 2
cba
Sample Input 3
a
Sample Output 3
a
Sample Input 4
racecar
Sample Output 4
racecar
Sample Input 5
TechBridge24
Sample Output 5
42egdirBhceT
1
Iterate backwards and build the result.
3
Max of Array (Method)
Hard
Write a method int max(int[] arr) and use it to print the largest element of an array.
First line: N. Second line: N integers.
A single integer: the maximum.
Sample Input 1
5
3 7 2 9 4
Sample Output 1
9
Sample Input 2
4
-5 -2 -8 -1
Sample Output 2
-1
Sample Input 3
1
100
Sample Output 3
100
Sample Input 4
6
1 1 1 1 1 1
Sample Output 4
1
Sample Input 5
5
50 40 30 20 10
Sample Output 5
10
1
Loop in the method.
4
Count Primes in Range (Method)
Hard
Write a method boolean isPrime(int n) and use it to print how many primes exist in the range 2..N.
A single integer N.
A single integer: the prime count.
Sample Input 1
10
Sample Output 1
4
Sample Input 2
20
Sample Output 2
8
Sample Input 3
2
Sample Output 3
1
Sample Input 4
50
Sample Output 4
15
Sample Input 5
100
Sample Output 5
25
2
Loop from 2 to N and call isPrime.
5
Digit Sum (Method)
Hard
Write a method int digitSum(int n) and use it to print the sum of digits of N.
A single integer N.
A single integer: the digit sum.
Sample Input 1
1234
Sample Output 1
10
Sample Input 2
999
Sample Output 2
27
Sample Input 3
1
Sample Output 3
1
Sample Input 4
1000000000
Sample Output 4
1
Sample Input 5
987654321
Sample Output 5
45
1
Extract digits in the method.
6
Is Palindrome String (Method)
Hard
Write a method boolean isPalindrome(String s) and use it to print "Palindrome" or "Not Palindrome".
A single line containing a string S.
"Palindrome" or "Not Palindrome".
Sample Input 1
racecar
Sample Output 1
Palindrome
Sample Input 2
hello
Sample Output 2
Not Palindrome
Sample Input 3
a
Sample Output 3
Palindrome
Sample Input 4
abba
Sample Output 4
Palindrome
Sample Input 5
12321
Sample Output 5
Palindrome
1
Compare s with its reverse inside the method.
7
Average of Array (Method)
Hard
Write a method int average(int[] arr) and use it to print the floor of the average.
First line: N. Second line: N integers.
A single integer: floor(sum / N).
Sample Input 1
5
1 2 3 4 5
Sample Output 1
3
Sample Input 2
4
10 20 30 40
Sample Output 2
25
Sample Input 3
1
7
Sample Output 3
7
Sample Input 4
3
1 1 2
Sample Output 4
1
Sample Input 5
5
10 20 30 40 50
Sample Output 5
30
1
sum / arr.length using integer division.
8
Fibonacci Term (Method)
Hard
Write a method int fib(int n) that returns the n-th Fibonacci number using a loop, and use it to print F(N). F(0)=0, F(1)=1.
A single integer N.
A single integer: F(N).
Sample Input 1
6
Sample Output 1
8
Sample Input 2
10
Sample Output 2
55
Sample Input 3
0
Sample Output 3
0
Sample Input 4
1
Sample Output 4
1
Sample Input 5
15
Sample Output 5
610
0
Iterative loop inside the method.
9
Count Occurrences (Method)
Hard
Write a method int count(int[] arr, int x) and use it to print how many times X appears in the array.
First line: N. Second line: N integers. Third line: X.
A single integer: the count.
Sample Input 1
5
3 7 3 9 3
3
Sample Output 1
3
Sample Input 2
4
1 2 3 4
5
Sample Output 2
0
Sample Input 3
1
7
7
Sample Output 3
1
Sample Input 4
6
1 1 1 1 1 1
1
Sample Output 4
6
Sample Input 5
4
2 4 6 8
4
Sample Output 5
1
1
Loop and count in the method.
10
Circle Area (Method)
Hard
Write a method double area(int r) that returns 3.14 * r * r, and use it to print the area rounded DOWN to the nearest integer.
A single integer R.
A single integer: floor(3.14 * R * R).
Sample Input 1
5
Sample Output 1
78
Sample Input 2
2
Sample Output 2
12
Sample Input 3
1
Sample Output 3
3
Sample Input 4
10
Sample Output 4
314
Sample Input 5
100
Sample Output 5
31400
1
return 3.14 * r * r; then (int)Math.floor(...).
11
First N Primes (Method)
Hard
Write a method boolean isPrime(int n) and use it to print the first N prime numbers on one line, space-separated.
A single integer N.
The first N primes on one line.
Sample Input 1
5
Sample Output 1
2 3 5 7 11
Sample Input 2
3
Sample Output 2
2 3 5
Sample Input 3
1
Sample Output 3
2
Sample Input 4
10
Sample Output 4
2 3 5 7 11 13 17 19 23 29
Sample Input 5
7
Sample Output 5
2 3 5 7 11 13 17
1
Keep counting primes starting from 2.
12
Swap Array Ends (Method)
Hard
Write a method void swapEnds(int[] arr) that swaps the first and last elements, and use it to print the resulting array.
First line: N. Second line: N integers.
The array after swapping ends on one line.
Sample Input 1
5
1 2 3 4 5
Sample Output 1
5 2 3 4 1
Sample Input 2
3
10 20 30
Sample Output 2
30 20 10
Sample Input 3
1
7
Sample Output 3
7
Sample Input 4
4
1 2 3 4
Sample Output 4
4 2 3 1
Sample Input 5
6
-1 0 1 2 3 4
Sample Output 5
4 0 1 2 3 -1
1
Swap arr[0] and arr[n-1].
13
Compound Interest (Method)
Hard
Write a method long compound(int p, int r, int t) that returns p * (100 + r) * t / 100 (simple interest + principal), and use it to print the result. It is guaranteed to be a whole number.
A single line with three integers P, R, T.
A single integer: P + interest.
Sample Input 1
1000 5 2
Sample Output 1
1100
Sample Input 2
2000 10 1
Sample Output 2
2200
Sample Input 3
5000 8 5
Sample Output 3
7000
Sample Input 4
1 100 1
Sample Output 4
2
Sample Input 5
10000 3 3
Sample Output 5
10900
1
return p * (100 + r) * t / 100;
14
Are Anagrams (Method)
Hard
Write a method boolean areAnagrams(String a, String b) and use it to print "Anagram" or "Not Anagram".
Two lines: string A, then string B.
"Anagram" or "Not Anagram".
Sample Input 1
listen
silent
Sample Output 1
Anagram
Sample Input 2
hello
world
Sample Output 2
Not Anagram
Sample Input 3
a
a
Sample Output 3
Anagram
Sample Input 4
abc
cba
Sample Output 4
Anagram
Sample Input 5
aaa
aa
Sample Output 5
Not Anagram
1
Sort both strings and compare, or count frequencies.
Competitive MCQs — Java Methods
Code snippets, output prediction, concepts & error spotting. Pick an answer to see instant feedback.
Score 0/ 33
Q1
Which keyword defines a method that belongs to the class rather than an instance?
Correct!
Wrong — correct answer is .
static methods are called on the class.
Q2
What is the return type of a method that prints but returns nothing?
Correct!
Wrong — correct answer is .
void means no value is returned.
Q3
What is the output of this code?
java
1
2
3
4
static int add(int a, int b) {
    return a + b;
}
System.out.println(add(2, 3));
Correct!
Wrong — correct answer is .
add(2,3) returns 2+3 = 5.
Q4
What is the output of this code?
static int add(int a, int b) {
return a + b;
}
System.out.println(add(2, 3));
Correct!
Wrong — correct answer is .
add(2,3) returns 2+3 = 5.
Q5
What are the values called that a method receives when called?
Correct!
Wrong — correct answer is .
Values passed at the call site are arguments/actual parameters.
Q6
Which of these is method overloading?
Correct!
Wrong — correct answer is .
Overloading = same name, different parameter types/count, same class.
Q7
What is the output of this code?
java
1
2
3
4
static void greet(String name) {
    System.out.println("Hi " + name);
}
greet("Aya");
Correct!
Wrong — correct answer is .
The method prints "Hi Aya".
Q8
What is the output of this code?
static void greet(String name) {
System.out.println("Hi " + name);
}
greet("Aya");
Correct!
Wrong — correct answer is .
The method prints "Hi Aya".
Q9
Which keyword returns a value from a method?
Correct!
Wrong — correct answer is .
return sends the result back to the caller.
Q10
What happens if a method declared to return int has no return statement?
Correct!
Wrong — correct answer is .
A non-void method must return a value on every path.
Q11
What is the output of this code?
java
1
2
3
4
static int square(int n) {
    return n * n;
}
System.out.println(square(4));
Correct!
Wrong — correct answer is .
square(4) = 4 * 4 = 16.
Q12
What is the output of this code?
static int square(int n) {
return n * n;
}
System.out.println(square(4));
Correct!
Wrong — correct answer is .
square(4) = 4 * 4 = 16.
Q13
What does the this keyword refer to inside an instance method?
Correct!
Wrong — correct answer is .
this refers to the object on which the method was invoked.
Q14
What is the output of this code?
static int twice(int x) {
return x * 2;
}
System.out.println(twice(5));
Correct!
Wrong — correct answer is .
twice(5) returns 5 * 2 = 10.
Q15
What does method overloading mean?
Correct!
Wrong — correct answer is .
Overloading reuses a name with different parameters in the same class.
Q16
What is the output of this code?
static int add(int a, int b) {
return a + b;
}
static int add(int a, int b, int c) {
return a + b + c;
}
System.out.println(add(1, 2));
Correct!
Wrong — correct answer is .
The two-argument overload matches and returns 3.
Q17
What is a parameter?
Correct!
Wrong — correct answer is .
Parameters are declared in the method signature to accept inputs.
Q18
What is an argument?
Correct!
Wrong — correct answer is .
Arguments are the actual values supplied at the call site.
Q19
What is the output of this code?
static void greet() {
System.out.println("Hi");
}
greet();
Correct!
Wrong — correct answer is .
Calling greet() runs its body, printing "Hi".
Q20
Which keyword prevents a method from being overridden?
Correct!
Wrong — correct answer is .
final methods cannot be overridden by subclasses.
Q21
What is the output of this code?
static int f(int x) {
return x + 1;
}
System.out.println(f(f(5)));
Correct!
Wrong — correct answer is .
Inner f(5) = 6, outer f(6) = 7.
Q22
What does pass-by-value mean for primitives?
Correct!
Wrong — correct answer is .
Primitive changes inside a method do not affect the caller.
Q23
What is the output of this code?
static void change(int x) {
x = 100;
}
int a = 5;
change(a);
System.out.println(a);
Correct!
Wrong — correct answer is .
a is passed by value; the copy becomes 100 but a stays 5.
Q24
Which is a valid method signature?
Correct!
Wrong — correct answer is .
Modifiers, return type, name, and parameters follow Java syntax.
Q25
What is the output of this code?
static boolean isEven(int n) {
return n % 2 == 0;
}
System.out.println(isEven(8));
Correct!
Wrong — correct answer is .
8 % 2 == 0 is true, so isEven returns true.
Q26
Which is a recursive method?
Correct!
Wrong — correct answer is .
Recursion happens when a method invokes itself.
Q27
What is the output of this code?
static int sum(int a, int b) {
return a + b;
}
System.out.println(sum(sum(1, 2), sum(3, 4)));
Correct!
Wrong — correct answer is .
Inner sums are 3 and 7; the outer sum is 10.
Q28
What is method scope?
Correct!
Wrong — correct answer is .
Local variables live only within the method that declares them.
Q29
What is the output of this code?
static String shout(String s) {
return s.toUpperCase() + "!";
}
System.out.println(shout("hi"));
Correct!
Wrong — correct answer is .
The method uppercases "hi" to "HI" and appends "!".
Q30
Can a void method return early?
Correct!
Wrong — correct answer is .
A bare return exits a void method early.
Q31
What is the output of this code?
static int abs(int n) {
return n < 0 ? -n : n;
}
System.out.println(abs(-7));
Correct!
Wrong — correct answer is .
The ternary flips negatives, turning -7 into 7.
Q32
Which method header correctly returns the larger of two ints?
Correct!
Wrong — correct answer is .
It takes two ints and returns an int result.
Q33
What is the output of this code?
static int f(int a, int b) {
return a > b ? a : b;
}
System.out.println(f(3, 9));
Correct!
Wrong — correct answer is .
9 is larger, so the method returns 9.