Java Coding Questions and Answers

Welcome to the Java Coding Quiz. In this quiz, we present 50 coding MCQ questions to test your coding knowledge of Java programming. Each question has a correct and brief explanation.

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

int a = 10;
int b = ++a + a++ + --a;
System.out.println(b);
a) 32
b) 33
c) 30
d) 31

Answer:

b) 33

Explanation:

The expression is evaluated as follows: ++a (pre-increment, a becomes 11) + a++ (post-increment, uses 11 then a becomes 12) + --a (pre-decrement, a becomes 11). Hence, the expression is 11 + 11 + 11, which equals 33.

2. What does this Java code snippet output?

String str1 = "Java";
String str2 = new String("Java");
System.out.println(str1 == str2);
a) true
b) false
c) Java
d) Compilation error

Answer:

b) false

Explanation:

str1 refers to a string in the string pool, while str2 refers to an object in the heap. The == operator checks for reference equality, hence the result is false.

3. Identify the output of the following code:

int[] array = {1, 2, 3, 4, 5};
System.out.println(array[3]);
a) 1
b) 2
c) 3
d) 4

Answer:

d) 4

Explanation:

Array indices start at 0. Therefore, array[3] refers to the fourth element in the array, which is 4.

4. What will be printed by this Java code?

try {
    int[] myNumbers = {1, 2, 3};
    System.out.println(myNumbers[10]);
} catch (Exception e) {
    System.out.println("Something went wrong.");
}
a) Something went wrong.
b) An ArrayIndexOutOfBoundsException is thrown
c) 0
d) The program crashes with an error

Answer:

a) Something went wrong.

Explanation:

Accessing an array index out of bounds throws an ArrayIndexOutOfBoundsException, which is caught by the catch block printing "Something went wrong."

5. What does this code snippet output?

for (int i = 0; i < 5; i++) {
    if (i >= 2) {
        break;
    }
    System.out.println(i);
}
a) 0 1
b) 0 1 2
c) 0 1 2 3 4
d) 0 1 3 4

Answer:

a) 0 1

Explanation:

The break statement exits the loop when i is greater than or equal to 2. Thus, only 0 and 1 are printed.

6. What is the result of executing this code?

int x = 5;
int y = x * 2;
y += 10;
System.out.println(y);
a) 10
b) 15
c) 20
d) 25

Answer:

c) 20

Explanation:

1. int x = 5;: This initializes a variable x with the value 5. 

2. int y = x * 2;: This initializes a variable y with the result of doubling the value of x. So, y will be assigned the value 5 * 2, which is 10. 

3. y += 10;: This is a shorthand for y = y + 10;. It adds 10 to the current value of y. So, y becomes 10 + 10, which is 20. 

4. System.out.println(y);: This prints the value of y to the console. In this case, it will print 20.

7. What will the following Java code snippet output?

boolean a = true;
boolean b = !a;
System.out.println(a && b);
a) true
b) false
c) Compilation error
d) Runtime error

Answer:

b) false

Explanation:

b is the negation of a, so a && b (true AND false) evaluates to false.

8. What does the following code snippet print?

int i = 0;
while (i < 3) {
    System.out.println("Hi");
    i++;
}
a) Hi (printed once)
b) Hi (printed three times)
c) Hi (printed infinitely)
d) No output

Answer:

b) Hi (printed three times)

Explanation:

The loop prints "Hi" and increments i each time until i is less than 3.

9. Determine the output of this Java code:

String s1 = "Java";
String s2 = "Java";
String s3 = new String("Java");
System.out.println(s1 == s2 && s1.equals(s3));
a) true
b) false
c) Compilation error
d) Runtime error

Answer:

a) true

Explanation:

s1 and s2 refer to the same string in the string pool, so s1 == s2 is true. s1.equals(s3) checks for value equality, which is also true.

10. What is the result of the following code snippet?

class Test {
    public static void main(String[] args) {
        System.out.println(args.length);
    }
}
a) 0
b) 1
c) The length of the string provided as a command-line argument
d) Compilation error

Answer:

a) 0

Explanation:

If no command-line arguments are provided, the length of the args array is 0.

11. What will the following Java code snippet output?

int a = 10;
int b = 20;
System.out.println(a > b ? "A is greater" : "B is greater");
a) A is greater
b) B is greater
c) Compilation error
d) Runtime error

Answer:

b) B is greater

Explanation:

The ternary operator checks if a is greater than b. Since it's not, it prints "B is greater".

12. Identify the output of this code:

int[] numbers = {1, 2, 3, 4, 5};
for (int number : numbers) {
    if (number == 3) {
        continue;
    }
    System.out.print(number + " ");
}
a) 1 2 4 5
b) 1 2 3 4 5
c) 1 2 3 4
d) 1 2

Answer:

a) 1 2 4 5

Explanation:

The continue statement skips the current iteration when the number is 3. Thus, it prints all numbers except 3.

13. What does this Java code snippet output?

public class Test {
    static boolean isEven(int n) {
        return n % 2 == 0;
    }
    public static void main(String[] args) {
        System.out.println(isEven(5));
    }
}
a) true
b) false
c) Compilation error
d) Runtime error

Answer:

b) false

Explanation:

The isEven method returns true if the number is divisible by 2. Since 5 is not divisible by 2, it returns false.

14. What will be printed by this Java code?

String str = "Java";
for(int i = 0; i < str.length(); i++) {
    char ch = str.charAt(i);
    System.out.print(ch + " ");
}
a) J a v a
b) Java
c) Jav
d) Compilation error

Answer:

a) J a v a

Explanation:

The loop iterates over each character in the string "Java" and prints each character followed by a space.

15. What does this code snippet output?

int num = 5;
String result = num % 2 == 0 ? "Even" : "Odd";
System.out.println(result);
a) Even
b) Odd
c) 5
d) Compilation error

Answer:

b) Odd

Explanation:

The ternary operator checks if num is divisible by 2. Since 5 is not, it prints "Odd".

16. What is the result of executing this code?

int total = 0;
for (int i = 1; i <= 5; i++) {
    total += i;
}
System.out.println(total);
a) 10
b) 15
c) 20
d) 25

Answer:

b) 15

Explanation:

The loop calculates the sum of numbers from 1 to 5, resulting in 15.

17. What will the following Java code snippet output?

class Base {
    Base() {
        System.out.print("Base Constructor");
    }
}
class Derived extends Base {
    Derived() {
        System.out.print("Derived Constructor");
    }
}
public class Test {
    public static void main(String[] args) {
        Derived d = new Derived();
    }
}
a) Base Constructor Derived Constructor
b) Derived Constructor
c) Base Constructor
d) Derived Constructor

Answer:

a) Base Constructor Derived Constructor

Explanation:

When an instance of Derived is created, the constructor of Base is called first, followed by the constructor of Derived.

18. Identify the output of this code:

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

Answer:

a) 5 \n 4

Explanation:

x++ is post-increment, so y gets the original value of x (4), then x is incremented to 5.

19. What does this Java code snippet output?

int i = 5;
switch (i) {
    case 1: System.out.println("One"); break;
    case 5: System.out.println("Five"); break;
    default: System.out.println("Default");
}
a) One
b) Five
c) Default
d) No output

Answer:

b) Five

Explanation:

The switch statement matches the case with i = 5 and prints "Five".

20. What is the result of the following code snippet?

boolean flag1 = true, flag2 = false, flag3 = true;
if (flag1 || (flag2 && flag3)) {
    System.out.println("Condition is true");
} else {
    System.out.println("Condition is false");
}
a) Condition is true
b) Condition is false
c) Compilation error
d) Runtime error

Answer:

a) Condition is true

Explanation:

The if condition evaluates to true because flag1 is true. The rest of the condition is not evaluated due to short-circuiting.

21. What does the following Java code snippet output?

int number = 5;
String message = number > 10 ? "Greater than 10" : "Less than or equal to 10";
System.out.println(message);
a) Greater than 10
b) Less than or equal to 10
c) Compilation error
d) Runtime error

Answer:

b) Less than or equal to 10

Explanation:

The ternary operator checks if number is greater than 10. Since it's not, it prints "Less than or equal to 10".

22. Identify the output of this code:

int sum = 0;
for (int i = 0; i <= 10; i+=2) {
    sum += i;
}
System.out.println(sum);
a) 30
b) 25
c) 20
d) 55

Answer:

a) 30

Explanation:

The loop iterates and adds every even number from 0 to 10, resulting in a sum of 30.

23. What will be printed by this Java code?

class A {
    void print() {
        System.out.println("A");
    }
}
class B extends A {
    void print() {
        System.out.println("B");
    }
}
public class Test {
    public static void main(String[] args) {
        A obj = new B();
        obj.print();
    }
}
a) A
b) B
c) Compilation error
d) Runtime error

Answer:

b) B

Explanation:

Although obj is of type A, it refers to an instance of B. Thus, B's print method is called due to polymorphism.

24. What does this code snippet output?

int[] numbers = {1, 2, 3, 4, 5};
int index = 0;
while (index < numbers.length) {
    System.out.print(numbers[index] + " ");
    index++;
}
a) 1 2 3 4 5
b) 1 2 3 4
c) 0 1 2 3 4
d) An infinite loop

Answer:

a) 1 2 3 4 5

Explanation:

The while loop iterates through the array and prints each element until it reaches the length of the array.

25. What is the result of executing this code?

String[] array = {"A", "B", "C", "D"};
for (String element : array) {
    System.out.print(element.toLowerCase() + " ");
}
a) a b c d
b) A B C D
c) a b c
d) Compilation error

Answer:

a) a b c d

Explanation:

The enhanced for loop iterates over each element in the array and prints it in lowercase.

26. What will the following Java code snippet output?

int a = 5;
int b = 10;
int max = a > b ? a : b;
System.out.println(max);
a) 5
b) 10
c) Compilation error
d) Runtime error

Answer:

b) 10

Explanation:

The ternary operator checks which number is greater. Since b is greater than a, it prints 10.

27. Identify the output of this code:

class MyClass {
    static int a = 3;
    static int b;

    static {
        b = a * 4;
    }

    public static void main(String[] args) {
        System.out.println(MyClass.b);
    }
}
a) 0
b) 3
c) 12
d) Compilation error

Answer:

c) 12

Explanation:

The static block initializes b as 4 times a (which is 3), so b is 12.

28. What does this Java code snippet output?

List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
list.remove(1);
System.out.println(list);
a) [1, 2]
b) [1, 3]
c) [2, 3]
d) [1, 2, 3]

Answer:

b) [1, 3]

Explanation:

The remove method with an int argument removes the element at that index. Index 1 corresponds to the second element, which is 2.

29. What will be printed by this Java code?

boolean flag1 = false;
boolean flag2 = true;
if (flag1 || flag2) {
    System.out.println("True");
} else {
    System.out.println("False");
}
a) True
b) False
c) Compilation error
d) Runtime error

Answer:

a) True

Explanation:

The if condition checks if either flag1 or flag2 is true. Since flag2 is true, the output is "True".

30. What does this code snippet output?

int number = 0;
do {
    number++;
} while (number < 5);
System.out.println(number);
a) 4
b) 5
c) 6
d) Infinite loop

Answer:

b) 5

Explanation:

The do-while loop increments number until it is less than 5. It stops when number becomes 5.

31. What will the following Java code snippet output?

int[] arr = new int[3];
arr[0] = 5;
arr[1] = 10;
System.out.println(arr[2]);
a) 0
b) 5
c) 10
d) null

Answer:

a) 0

Explanation:

In Java, integer arrays are initialized with zeros. Since arr[2] is not explicitly assigned, its default value is 0.

32. Identify the output of this code:

class Base {
    public Base() {
        System.out.print("Base Constructor");
    }
}
class Derived extends Base {
    public Derived() {
        System.out.print("Derived Constructor");
    }
}
public class Test {
    public static void main(String[] args) {
        Base obj = new Derived();
    }
}
a) Base Constructor Derived Constructor
b) Derived Constructor
c) Base Constructor
d) Derived Constructor

Answer:

a) Base Constructor Derived Constructor

Explanation:

When creating an instance of Derived, the constructor of Base is called first, followed by the constructor of Derived.

33. What does this Java code snippet output?

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

Answer:

a) true

Explanation:

The equals method compares the contents of two strings. Since s1 and s2 contain the same characters, the result is true.

34. What will be printed by this Java code?

int i = 1;
switch (i) {
    case 1: System.out.println("One"); break;
    case 2: System.out.println("Two"); break;
    default: System.out.println("Other");
}
a) One
b) Two
c) Other
d) No output

Answer:

a) One

Explanation:

The switch statement matches the case with i = 1 and prints "One".

35. What does this code snippet output?

int i = 10;
i = i++ + i;
System.out.println(i);
a) 20
b) 21
c) 10
d) 11

Answer:

b) 21

Explanation:

The expression i = i++ + i is evaluated as 10 + 11. The post-increment i++ uses the current value (10) and then increments i to 11.

36. What is the result of executing this code?

List<String> items = Arrays.asList("Apple", "Banana", "Cherry");
for (String item : items) {
    if ("Banana".equals(item)) {
        continue;
    }
    System.out.print(item);
}
a) Apple
b) Apple Cherry
c) Banana
d) Apple

Answer:

b) Apple Cherry

Explanation:

The continue statement skips the current iteration when the item is "Banana". Thus, "Apple" and "Cherry" are printed.

37. What will the following Java code snippet output?

public class Test {
    public static void main(String[] args) {
        int x = 5;
        int y = 10;
        int z = (++x) + (y--);
        System.out.println(z);
    }
}
a) 15
b) 16
c) 14
d) 17

Answer:

b) 16

Explanation:

++x pre-increments x to 6, and y-- uses the value 10 before post-decrementing. Thus, z is 6 + 10 = 16.

38. Identify the output of this code:

int number = 3;
String result = switch (number) {
    case 1 -> "One";
    case 2 -> "Two";
    case 3 -> "Three";
    default -> "Other";
};
System.out.println(result);
a) One
b) Two
c) Three
d) Other

Answer:

c) Three

Explanation:

The switch expression matches number with case 3 and returns "Three".

39. What does this Java code snippet output?

int[] arr = {1, 2, 3, 4, 5};
System.out.println(arr.length);
a) 4
b) 5
c) 6
d) An error

Answer:

b) 5

Explanation:

The length property of an array returns the number of elements in the array, which is 5 in this case.

40. What is the result of the following code snippet?

String str = "Hello";
for (int i = str.length() - 1; i >= 0; i--) {
    System.out.print(str.charAt(i));
}
a) Hello
b) olleH
c) elloH
d) oellH

Answer:

b) olleH

Explanation:

The loop prints the characters of the string in reverse order, resulting in "olleH".

41. What will the following Java code snippet output?

int a = 5;
int b = a * 2;
System.out.println((a < b) && (b > a));
a) true
b) false
c) Compilation error
d) Runtime error

Answer:

a) true

Explanation:

Both conditions (a < b) and (b > a) are true as 5 is less than 10, resulting in the logical AND operator returning true.

42. Identify the output of this code:

class Calculator {
    int multiply(int a, int b) {
        return a * b;
    }
    double multiply(double a, double b) {
        return a * b;
    }
}
public class Test {
    public static void main(String[] args) {
        Calculator calc = new Calculator();
        System.out.println(calc.multiply(5, 2));
    }
}
a) 10
b) 10.0
c) Compilation error
d) Runtime error

Answer:

a) 10

Explanation:

The multiply(int, int) method is called, returning an int value 10.

43. What does this Java code snippet output?

int i = 5;
switch (i) {
    default: System.out.println("Default");
    case 1: System.out.println("One");
    case 2: System.out.println("Two");
}
a) Default
b) One
c) Two
d) Default One Two

Answer:

d) Default One Two

Explanation:

The switch statement starts at the default case and falls through the rest of the cases as there are no break statements.

44. What will be printed by this Java code?

String[] fruits = {"Apple", "Banana", "Cherry"};
for (int i = 0; i < fruits.length; i++) {
    if ("Banana".equals(fruits[i])) {
        continue;
    }
    System.out.println(fruits[i]);
}
a) Apple
b) Apple Cherry
c) Banana
d) Apple

Answer:

b) Apple Cherry

Explanation:

The continue statement skips the current iteration when the fruit is "Banana". Thus, it prints "Apple" and "Cherry".

45. What does this code snippet output?

boolean x = false;
boolean y = true;
boolean z = x || y;
System.out.println(z);
a) true
b) false
c) Compilation error
d) Runtime error

Answer:

a) true

Explanation:

The logical OR operator || returns true if either operand is true. Since y is true, the result is true.

46. What is the result of executing this code?

int sum = 0;
for (int i = 1; i <= 5; i++) {
    if (i % 2 == 0) {
        sum += i;
    }
}
System.out.println(sum);
a) 6
b) 9
c) 15
d) 10

Answer:

a) 6

Explanation:

The loop adds only even numbers (2 and 4) from 1 to 5, resulting in a sum of 6.

47. What will the following Java code snippet output?

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

Answer:

b) false

Explanation:

str1 and str2 reference different String objects, so str1 == str2 compares references and returns false.

48. Identify the output of this code:

int a = 5;
int b = 10;
System.out.println("Sum = " + (a + b));
a) Sum = 5 + 10
b) Sum = 15
c) Sum = 510
d) Compilation error

Answer:

b) Sum = 15

Explanation:

The expression (a + b) is evaluated as 15, and then concatenated with "Sum = ".

49. What does this Java code snippet output?

List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
list.forEach(n -> {
    if (n % 2 == 0) {
        System.out.print(n + " ");
    }
});
a) 1 2 3 4 5
b) 1 3 5
c) 2 4
d) 2 4 5

Answer:

c) 2 4

Explanation:

The lambda expression in forEach prints only the even numbers, which are 2 and 4.

50. What is the result of the following code snippet?

int[] numbers = {1, 2, 3, 4, 5};
int total = 0;
for (int num : numbers) {
    total += num;
}
System.out.println(total);
a) 10
b) 15
c) 20
d) 25

Answer:

b) 15

Explanation:

The enhanced for loop iterates through each element in the array and sums them up, resulting in a total of 15.

Comments