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);
Answer:
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);
Answer:
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]);
Answer:
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.");
}
Answer:
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);
}
Answer:
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);
Answer:
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);
Answer:
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++;
}
Answer:
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));
Answer:
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);
}
}
Answer:
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");
Answer:
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 + " ");
}
Answer:
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));
}
}
Answer:
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 + " ");
}
Answer:
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);
Answer:
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);
Answer:
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();
}
}
Answer:
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);
}
}
Answer:
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");
}
Answer:
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");
}
Answer:
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);
Answer:
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);
Answer:
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();
}
}
Answer:
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++;
}
Answer:
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() + " ");
}
Answer:
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);
Answer:
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);
}
}
Answer:
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);
Answer:
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");
}
Answer:
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);
Answer:
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]);
Answer:
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();
}
}
Answer:
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));
}
}
Answer:
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");
}
Answer:
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);
Answer:
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);
}
Answer:
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);
}
}
Answer:
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);
Answer:
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);
Answer:
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));
}
Answer:
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));
Answer:
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));
}
}
Answer:
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");
}
Answer:
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]);
}
Answer:
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);
Answer:
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);
Answer:
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);
}
}
Answer:
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));
Answer:
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 + " ");
}
});
Answer:
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);
Answer:
Explanation:
The enhanced for loop iterates through each element in the array and sums them up, resulting in a total of 15.
Comments
Post a Comment
Leave Comment