Java Coding MCQ Questions

Welcome to Java Coding MCQ Questions. Here, we present 25 MCQs (Multiple-Choice Questions) with code snippets to test your understanding of Java coding and logical stills. Let's get started!

1. What will be the output of the following Java program?

public class Main {
    public static void main(String[] args) {
        int x = 10, y = 20;
        if(x++ > 10 && ++y > 20) {
            x = 40;
        }
        System.out.println(x + " " + y);
    }
}
a) 11 21
b) 11 20
c) 40 21
d) 40 20

2. What does the following Java code snippet print?

public class Test {
    public static void main(String[] args) {
        System.out.println(1 + 2 + "3");
    }
}
a) 6
b) 33
c) 123
d) 330

3. Consider the following Java program. What will it output?

public class MyClass {
    static void method(int x) {
        System.out.print("int ");
    }
    static void method(double x) {
        System.out.print("double ");
    }
    public static void main(String[] args) {
        method(99);
        method(99.0);
    }
}
a) int double
b) double int
c) int int
d) double double

4. What exception does the following Java code snippet throw?

public class ExceptionTest {
    public static void main(String[] args) {
        int[] numbers = new int[3];
        numbers[4] = 5;
    }
}
a) ArithmeticException
b) ArrayIndexOutOfBoundsException
c) NullPointerException
d) No exception is thrown

5. What will be the output of the following Java code?

public class Increment {
    public static void main(String[] args) {
        int x = 4;
        int y = x++;
        System.out.println(x + " " + y);
    }
}
a) 5 4
b) 4 5
c) 5 5
d) 4 4

6. What will be the output of the following Java code?

public class Main {
    public static void main(String[] args) {
        String s1 = new String("Hello");
        String s2 = new String("Hello");
        System.out.println(s1 == s2);
    }
}
a) true
b) false
c) Compilation error
d) Runtime error

7. Which of the following is true about the Java constructor?

a) A constructor must have the same name as the class.
b) A constructor cannot be overloaded.
c) A constructor must be declared public.
d) A constructor is used to destroy an object.

8. What will be the output of the following Java program?

public class BooleanTest {
    public static void main(String[] args) {
        boolean b = false;
        if (b = true) {
            System.out.println("True");
         } else {
                System.out.println("False");
         }
    }
}
a) True
b) False
c) Compilation error
d) No output

9. What is the output of this Java code snippet?

public class Output {
    public static void main(String[] args) {
        char c;
        for(c = 'A'; c <= 'E'; c++) {
            System.out.print(c);
        }
    }
}
a) ABCDE
b) ABCDEF
c) A-E
d) Compilation error

10. Which of the following options correctly describes the final keyword in Java?

a) A final class can be inherited.
b) A final method can be overridden.
c) A final variable can be initialized only once.
d) All of the above

11. What will be the output of the following Java code?

public class Test {
    public static void main(String[] args) {
        final int i;
        i = 20;
        System.out.println(i);
    }
}
a) 20
b) Compilation error
c) Runtime error
d) No output

12. Consider the following Java program. What will it output?

public class Main {
    public static void main(String[] args) {
        String str1 = "abc";
        String str2 = "abc";
        System.out.println(str1 == str2);
    }
}
a) true
b) false
c) Compilation error
d) Runtime error

13. What is the result of compiling and running the following Java code?

public class Main {
    public static void main(String[] args) {
        int[] data = {1, 2, 3};
        for(int i : data) {
            System.out.print(data[i]);
        }
    }
}
a) 123
b) 234
c) Compilation error
d) Runtime error

14. What is incorrect about the following Java code snippet?

public class Sample {
    public static void main(String[] args) {
        int num;
        System.out.println(num);
    }
}
a) The variable num is not initialized.
b) System.out.println cannot print integers.
c) There should be a semicolon at the end of the println statement.
d) The code is correct.

15. What does the following Java code snippet print?

public class Test {
    public static void main(String[] args) {
        boolean flag = false;
        if (flag = true) {
            System.out.println("True");
        } else {
                System.out.println("False");
        }
    }
}
a) True
b) False
c) Compilation error
d) No output

16. What is the output of the following Java code snippet?

public class Output {
    public static void main(String[] args) {
        int x = 10;
        change(x);
        System.out.println(x);
    }
    public static void change(int x) {
        x = 20;
    }
}
a) 10
b) 20
c) 0
d) None of the above

17. What will be the output of the following Java code?

public class Calculation {
    public static void main(String[] args) {
        int a = 2, b = 5;
        double result = b / a;
        System.out.println(result);
    }
}
a) 2.5
b) 2.0
c) 3
d) Compilation error

18. What exception is thrown by the following Java code?

public class Main {
    public static void main(String[] args) {
        int result = divide(10, 0);
        System.out.println(result);
    }
    static int divide(int x, int y) {
        return x / y;
    }
}
a) ArithmeticException
b) IllegalArgumentException
c) NullPointerException
d) No exception is thrown

19. What will be the output of the following Java program?

public class Test {
    public static void main(String[] args) {
        int x = 5;
        System.out.println(x > 2 ? x < 4 ? 10 : 8 : 7);
    }
}
a) 10
b) 8
c) 7
d) Compilation error

20. Consider the following Java class. What is the main issue with it?

public class Test {
    private int count;
    public void setCount(int count) {
        count = count;
    }
    public int getCount() {
        return count;
    }
}
a) The setCount method incorrectly assigns the parameter to itself instead of the field.
b) The class does not have a constructor.
c) The getCount method does not correctly return the private field.
d) There are no issues with the class.

21. What will the following Java code output?

public class Test {
    public static void main(String[] args) {
        int[] array = new int[3];
        for (int i = 0; i < array.length; i++) {
            array[i] = i * 2;
            System.out.print(array[i] + " ");
        }
    }
}
a) 0 1 2
b) 0 2 4
c) 1 2 3
d) Compilation error

22. What is the result of compiling and running the following Java code?

public class Main {
    public static void main(String[] args) {
        System.out.println('A' + '1' + "2");
    }
}
a) A12
b) 82
c) B2
d) 98

23. Analyze the following Java code snippet. What is the primary issue?

public class Sample {
    public static void main(String[] args) {
        float f = 3.14;
    }
}
a) The variable f should be of type double.
b) The literal 3.14 is of type double, causing a type mismatch.
c) There is no issue; the code will compile and run correctly.
d) The code will throw a runtime exception.

24. What is the output of the following Java code?

public class Loop {
    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            if (i % 3 == 0) {
                continue;
            }
            System.out.print(i + " ");
        }
    }
}
a) 0 1 2 3 4
b) 1 2 4
c) 1 2 3 4
d) 1 4

25. What does the following Java code snippet return?

public class Factorial {
    public static int factorial(int n) {
        if (n == 0) {
            return 1;
        }
        return n * factorial(n - 1);
    }
    public static void main(String[] args) {
        System.out.println(factorial(5));
    }
}
a) 120
b) 24
c) 0
d) 720

Comments