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.
Input Format
A single line with two integers A and B.
Output Format
A single integer: the sum.
Sample Test Cases
|
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 |
Constraints
-10^9
Explanation
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".
Input Format
A single integer N.
Output Format
"Even" or "Odd".
Sample Test Cases
|
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 |
Constraints
-10^6
Explanation
return n % 2 == 0.
3
Square (Method)
Easy
Write a method int square(int n) and use it to print n squared.
Input Format
A single integer N.
Output Format
A single integer: N*N.
Sample Test Cases
|
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 |
Constraints
1
Explanation
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.
Input Format
A single line with two integers A and B.
Output Format
A single integer: the maximum.
Sample Test Cases
|
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 |
Constraints
-10^9
Explanation
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.
Input Format
A single line with two integers L and B.
Output Format
A single integer: L * B.
Sample Test Cases
|
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 |
Constraints
1
Explanation
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.
Input Format
A single line with two integers L and B.
Output Format
A single integer: 2 * (L + B).
Sample Test Cases
|
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 |
Constraints
1
Explanation
return 2 * (l + b).
1
Factorial (Method)
Medium
Write a method long factorial(int n) and use it to print N!.
Input Format
A single integer N.
Output Format
A single integer: N!.
Sample Test Cases
|
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 |
Constraints
0
Explanation
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".
Input Format
A single integer N.
Output Format
"Prime" or "Not Prime".
Sample Test Cases
|
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 |
Constraints
2
Explanation
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.
Input Format
A single line with two integers A and B.
Output Format
A single integer: gcd(A, B).
Sample Test Cases
|
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 |
Constraints
1
Explanation
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).
Input Format
A single integer N.
Output Format
A single integer: the reversed number.
Sample Test Cases
|
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 |
Constraints
1
Explanation
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.
Input Format
First line: N. Second line: N integers.
Output Format
A single integer: the sum.
Sample Test Cases
|
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 |
Constraints
1
Explanation
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.
Input Format
A single line containing a string S (lowercase letters).
Output Format
A single integer: the vowel count.
Sample Test Cases
|
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 |
Constraints
1
Explanation
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.
Input Format
A single line with two integers B and E.
Output Format
A single integer: B^E.
Sample Test Cases
|
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 |
Constraints
0
Explanation
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".
Input Format
A single integer N.
Output Format
"Palindrome" or "Not Palindrome".
Sample Test Cases
|
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 |
Constraints
1
Explanation
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.
Input Format
A single integer N.
Output Format
A single integer: the largest digit.
Sample Test Cases
|
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 |
Constraints
1
Explanation
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.
Input Format
A single line containing a string S.
Output Format
The uppercase string.
Sample Test Cases
|
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 |
Constraints
1
Explanation
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.
Input Format
A single line with two integers A and B.
Output Format
A single integer: lcm(A, B).
Sample Test Cases
|
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 |
Constraints
1
Explanation
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.
Input Format
A single line containing a string S.
Output Format
The reversed string.
Sample Test Cases
|
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 |
Constraints
1
Explanation
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.
Input Format
First line: N. Second line: N integers.
Output Format
A single integer: the maximum.
Sample Test Cases
|
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 |
Constraints
1
Explanation
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.
Input Format
A single integer N.
Output Format
A single integer: the prime count.
Sample Test Cases
|
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 |
Constraints
2
Explanation
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.
Input Format
A single integer N.
Output Format
A single integer: the digit sum.
Sample Test Cases
|
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 |
Constraints
1
Explanation
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".
Input Format
A single line containing a string S.
Output Format
"Palindrome" or "Not Palindrome".
Sample Test Cases
|
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 |
Constraints
1
Explanation
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.
Input Format
First line: N. Second line: N integers.
Output Format
A single integer: floor(sum / N).
Sample Test Cases
|
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 |
Constraints
1
Explanation
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.
Input Format
A single integer N.
Output Format
A single integer: F(N).
Sample Test Cases
|
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 |
Constraints
0
Explanation
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.
Input Format
First line: N. Second line: N integers. Third line: X.
Output Format
A single integer: the count.
Sample Test Cases
|
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 |
Constraints
1
Explanation
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.
Input Format
A single integer R.
Output Format
A single integer: floor(3.14 * R * R).
Sample Test Cases
|
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 |
Constraints
1
Explanation
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.
Input Format
A single integer N.
Output Format
The first N primes on one line.
Sample Test Cases
|
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 |
Constraints
1
Explanation
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.
Input Format
First line: N. Second line: N integers.
Output Format
The array after swapping ends on one line.
Sample Test Cases
|
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 |
Constraints
1
Explanation
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.
Input Format
A single line with three integers P, R, T.
Output Format
A single integer: P + interest.
Sample Test Cases
|
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 |
Constraints
1
Explanation
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".
Input Format
Two lines: string A, then string B.
Output Format
"Anagram" or "Not Anagram".
Sample Test Cases
|
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 |
Constraints
1
Explanation
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
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));
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
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");
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
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));
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));
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));
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();
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)));
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);
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));
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)));
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"));
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));
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));
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.