Welcome to the Java Methods MCQ Quiz! In this quiz, we have compiled the top 20 multiple-choice questions (MCQs) on Java methods, along with detailed answers and explanations.
The quiz covers all important concepts related to Java methods, including method declaration, method calling, parameters, return types, and many more.
Whether you are a student, beginner, or experienced programmer, this quiz will help you test your understanding of Java methods and strengthen your programming fundamentals. By practicing these MCQs, you can improve your logical thinking, coding skills, and preparation for interviews, academic exams, certification tests, and competitive programming.
There is no time limit for completing the quiz. Once you finish all the questions, you will receive a scorecard showing your performance and overall score.
Java Methods MCQ Quiz
Let’s begin the quiz and see how well you understand Java methods. Can you score 20/20 and achieve a perfect 100%? Good luck!
What will be the output of the following code?
public class Test {
static int add(int a, int b) { return a + b; }
public static void main(String[] args) {
System.out.println(add(5, 3));
}
}- The add() method takes two integers and returns their sum.
- add(5, 3) returns 5 + 3 = 8.
- In Java, you can call another method in the same class directly by its name.
- ‘this’ is optional but not required.
- In Java, the method signature consists of the method name and its parameter list (types and order).
- The return type is NOT part of the signature, which matters for overloading resolution.
What is the output of the following program?
public class A {
void show() { System.out.print("A "); }
void show(int x) { System.out.print("A" + x + " "); }
public static void main(String[] args) {
A obj = new A();
obj.show();
obj.show(10);
}
}- The first call invokes show() which prints “A “, the second call invokes show(int) which prints “A10 “.
- This is method overloading — two methods with the same name but different parameter lists.
- A method can have multiple return statements (e.g., in if-else branches), but only one executes per method call.
- Once a return is hit, the method exits immediately.
- Java is strictly pass-by-value.
- For primitives, the value itself is copied.
- For objects, the reference (memory address) is copied — so the object’s fields can be mutated, but reassigning the reference inside the method does not affect the caller’s reference.
What is the output of the following program code?
public class Test {
static void change(int x) { x = 100; }
public static void main(String[] args) {
int a = 10;
change(a);
System.out.println(a);
}
}- Java passes ‘a’ by value, so inside change(), ‘x’ is a copy of ‘a’.
- Modifying ‘x’ does not affect ‘a’.
- So ‘a’ remains 10.
What is the output of the following program?
public class Test {
static void modify(int[] arr) { arr[0] = 99; }
public static void main(String[] args) {
int[] nums = {1, 2, 3};
modify(nums);
System.out.println(nums[0]);
}
}- Although Java passes the array reference by value, both the caller and the method point to the same array object.
- Modifying arr[0] inside modify() changes the actual array, so nums[0] becomes 99.
- A varargs parameter is declared using ‘…’, e.g., void sum(int… nums).
- It is internally treated as an array.
- Only one varargs parameter is allowed per method, and it must be the last parameter.
What is the output of the following program code?
public class Test {
static void print(int... nums) {
System.out.println(nums.length);
}
public static void main(String[] args) {
print(1, 2, 3, 4, 5);
}
}- The varargs parameter ‘nums’ collects all passed arguments into an array.
- Passing 1, 2, 3, 4, 5 creates an array of length 5.
What is the output of the following program?
public class Test {
static int fact(int n) {
if (n == 0) return 1;
return n * fact(n - 1);
}
public static void main(String[] args) {
System.out.println(fact(5));
}
}- This is a classic recursive factorial method.
- The base case is n==0 returning 1, which stops the recursion.
- fact(5) = 5 × 4 × 3 × 2 × 1 × 1 = 120.
What is the output of the following program if no errors?
public class Test {
static int count = 0;
static void recurse(int n) {
if (n <= 0) return;
count++;
recurse(n - 2);
}
public static void main(String[] args) {
recurse(7);
System.out.println(count);
}
}- Call chain: recurse(7) → recurse(5) → recurse(3) → recurse(1) → recurse(-1).
- count increments 4 times (for n = 7, 5, 3, 1).
- When n = -1, the base case n <= 0 triggers and returns. Output: 4.
What is the output?
public class Test {
int x = 10;
void show(int x) {
System.out.println(x + " " + this.x);
}
public static void main(String[] args) {
new Test().show(20);
}
}- Inside show(), the parameter ‘x’ shadows the instance variable ‘x’, so plain ‘x’ refers to the parameter (20).
- ‘this.x’ explicitly refers to the instance variable, which is 10. Output: 20 10.
What will the output print?
public class Test {
static void check(int n) {
if (n < 0) return;
System.out.println(n);
}
public static void main(String[] args) {
check(-5);
check(10);
}
}- ‘return’ in a void method exits the method immediately without executing the remaining statements.
- check(-5) hits the return immediately and prints nothing.
- check(10) does not satisfy n < 0, so it prints 10.
- Private methods are accessible only within the declaring class.
- They are not inherited, so subclasses cannot override them.
- If a subclass defines a method with the same name, it is a completely new method, not an override.
Predict the output of the following program.
public class Test {
static int mystery(int n) {
return n == 1 ? 1 : n + mystery(n - 1);
}
public static void main(String[] args) {
System.out.println(mystery(4) - mystery(2));
}
}- mystery(4) = 4 + 3 + 2 + 1 = 10.
- mystery(2) = 2 + 1 = 3.
- 10 − 3 = 7. The trap is computing both sides independently before subtracting.
What will the Java program print?
public class Test {
static int val = 5;
static int compute(int x) {
val = val * 2;
return x + val;
}
public static void main(String[] args) {
int a = compute(val);
System.out.println(a);
}
}- The argument val is evaluated as 5 before the method runs.
- Inside compute(5): val becomes 5 × 2 = 10, then returns 5 + 10 = 15.
- The key trap: the argument is evaluated first, then the method body executes — so x holds the old value of val (5), not 10.






