Module 4: Control Flow - Final Quiz MCQs
Solve topic-wise placement questions and improve your skills.
What is printed by the following Java 14+ switch expression?
int code = 2;
String message = switch (code) {
case 1 -> "Low";
case 2, 3 -> "Medium";
case 4 -> "High";
default -> "Unknown";
};
System.out.println(message);What is the output of the following do-while loop?
int count = 10;
do {
count += 5;
} while (count < 10);
System.out.println(count);What will be printed by the following labeled break snippet?
outer:
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (i == 2 && j == 2) {
break outer;
}
System.out.print(i + "" + j + " ");
}
}What will be printed by this enhanced for-each loop?
int[] nums = {2, 4, 6};
int sum = 0;
for (int n : nums) {
sum += n;
}
System.out.println(sum);goto statement?What is the output of the following continue statement?
int total = 0;
for (int i = 1; i <= 5; i++) {
if (i % 2 == 0) continue;
total += i;
}
System.out.println(total);What will be printed by the following switch fall-through snippet without break statements?
int score = 2;
int points = 0;
switch (score) {
case 1: points += 10;
case 2: points += 20;
case 3: points += 30;
default: points += 5;
}
System.out.println(points);What is the exact output of this while loop containing a postfix increment in its condition?
int i = 0;
while (i++ < 3) {
System.out.print(i + " ");
}What is the output of the following multi-variable for loop?
int count = 0;
for (int i = 0, j = 10; i < j; i += 2, j -= 2) {
count += (i + j);
}
System.out.println(count);What will be printed by the following labeled continue snippet?
int sum = 0;
outer:
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (j == 2) continue outer;
sum += (i * 10 + j);
}
}
System.out.println(sum);What will be printed after attempting to modify array elements inside an enhanced for-each loop?
int[] arr = {1, 2, 3};
for (int x : arr) {
x = x * 10;
}
System.out.println(arr[0] + " " + arr[1] + " " + arr[2]);What is the exact output of this do-while loop with postfix increment in the condition?
int k = 1;
do {
System.out.print(k + " ");
} while (k++ < 3);
System.out.print("final=" + k);