Java Coding Online Test - MCQ Questions

Welcome to Java Coding Online Test!. Here, we will present 25 MCQs (Multiple-Choice Questions) containing code snippets to test your coding and logical skills.

You can select the correct answer for each question and submit the test. You will get your online test score after finishing the complete test.

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

public class Main {
    public static void main(String[] args) {
        int a = 10;
        double b = a / 4;
        System.out.println(b);
    }
}
a) 2.5
b) 2.0
c) 2
d) Compilation error

2. What will the following Java code print?

public class Main {
    public static void main(String[] args) {
        int x = 5, y = 10;
        int z = x++ * --y;
        System.out.println(z);
    }
}
a) 45
b) 50
c) 40
d) 55

3. What does the following Java code output?

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

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

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

5. What does the following Java code print?

public class Main {
    public static void main(String[] args) {
        int[] array = {1, 2, 3, 4, 5};
        System.out.println(array[4]);
    }
}
a) 5
b) 4
c) 3
d) ArrayIndexOutOfBoundsException

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

public class Main {
    public static void main(String[] args) {
        String s = "Java";
        s.concat(" SE 8");
        System.out.println(s);
    }
}
a) Java SE 8
b) Java
c) SE 8
d) NullPointerException

7. What does this Java code output?

public class Main {
    public static void main(String[] args) {
        int x = 10;
        System.out.println(x > 10 ? "Greater" : "Not greater");
    }
}
a) Greater
b) Not greater
c) 10
d) Compilation error

8. What will the following Java code snippet output?

public class Main {
    public static void main(String[] args) {
        final int x;
        x = 20;
        x = 30;
        System.out.println(x);
    }
}
a) 20
b) 30
c) Compilation error
d) None of these

9. What does the following Java code print?

public class Main {
    public static void main(String[] args) {
        try {
            int[] arr = new int[5];
            System.out.println(arr[5]);
            } catch (Exception e) {
                System.out.println("Exception caught");
            }
        }
    }
a) Exception caught
b) 0
c) ArrayIndexOutOfBoundsException
d) No output

10. What is the result of executing the following Java code?

public class Main {
    public static void main(String[] args) {
        String x = new String("Java");
        String y = new String("Java");
        System.out.println(x == y);
    }
}
a) true
b) false
c) Error
d) None of these

11. What will this Java function output?

public class Main {
    public static int add(int x, int y) {
        return x + y;
    }
    public static void main(String[] args) {
        System.out.println(add(5, 3));
    }
}
a) 8
b) 15
c) 5
d) None of these

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

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

13. What does the following Java code return?

public class Main {
    public static void main(String[] args) {
        int[] array = new int[10];
        System.out.println(array.length);
    }
}
a) 0
b) 10
c) null
d) None of these

14. What is the output of this Java program?

public class Main {
    public static void main(String[] args) {
        Integer x = 128;
        Integer y = 128;
        System.out.println(x == y);
    }
}
a) true
b) false
c) Compilation error
d) Exception thrown

15. What will the following Java code snippet output?

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

16. What does this Java code print?

public class Main {
    public static void main(String[] args) {
        System.out.println(Math.max(13, 42));
    }
}
a) 13
b) 42
c) 0
d) 1

17. What is the outcome of the following Java code?

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello World".substring(6, 11));
    }
}
a) World
b) Hello
c) Hello World
d) Error

18. What will this Java code output?

public class Main {
    public static void main(String[] args) {
        String s = null;
        System.out.println(s.length());
    }
}
a) 0
b) null
c) NullPointerException
d) -1

19. What does the following Java code return?

public class Main {
    public static void main(String[] args) {
        int result = 1 + 2;
        System.out.println(result);
    }
}
a) 1
b) 2
c) 3
d) None of these

20. What is the result of executing the following Java code?

public class Main {
    public static void main(String[] args) {
        int[] nums = {1, 2, 3, 4, 5};
        System.out.println(nums[4]);
    }
}
a) 1
b) 5
c) Error
d) None of these

21. What will the following Java code snippet output?

public class Main {
    public static void main(String[] args) {
        int x = 10;
        int y = new Integer(10);
        System.out.println(x == y);
    }
}
a) true
b) false
c) NullPointerException
d) Compilation error

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

public class Main {
    public static void main(String[] args) {
        double x = 0.0;
        double y = -0.0;
        System.out.println(x == y);
    }
}
a) true
b) false
c) NaN
d) Exception

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

public class Main {
    public static void main(String[] args) {
        try {
            int[] a = {1, 2, 3};
            System.out.println(a[3]);
            } catch (ArrayIndexOutOfBoundsException e) {
                System.out.println("Exception caught");
            }
        }
    }
a) 1
b) 2
c) 3
d) Exception caught

24. What does this Java code output?

public class Main {
    public static void main(String[] args) {
        Integer a = 100;
        Integer b = 100;
        System.out.println(a == b);
    }
}
a) true
b) false
c) Exception
d) None of these

25. What is the result of this Java function?

public class Main {
    public static boolean isPositive(int num) {
        return num > 0;
    }
    public static void main(String[] args) {
        System.out.println(isPositive(-1));
    }
}
a) true
b) false
c) Error
d) None

Comments