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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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]
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Comments
Post a Comment
Leave Comment