Flow Control Questions
Practice Flow Control MCQs with answers and explanations. Page 1 of 2.
Category
Java Programming
Topic
Flow Control
Page
1 / 2
Mode
Practice
Questions
Open any question to view the answer and explanation.
Java do-while with pre-increment in condition — compute final values of i and j.
i = 1; j = 10;
do {
if (i > j) {
break;
}
j--;
} while (++i < 5);
System.out.println("i = " + i + " and j = " + j);
What is printed?
Open
View answer
Java boolean assignments in if/else chain — what character is printed?
boolean bool = true;
if (bool = false) { // Line 2: assignment, not comparison
System.out.println("a");
}
else if (bool) {
System.out.println("b");
}
else if (!bool) {
System.out.println("c"); // Line 12
}
else {
System.out.println("d");
}
Choose the exact output.
Open
View answer
Java control flow and assignment in conditions — determine the final value printed.
public class If2
{
static boolean b1, b2;
public static void main(String [] args)
{
int x = 0;
if (!b1) // Line 7
{
if (!b2) // Line 9
{
b1 = true;
x++;
if (5 > 6) { x++; }
if (!b1)
x = x + 10;
else if (b2 = true) // Line 19: assignment returns true
x = x + 100;
else if (b1 | b2) // Line 21
x = x + 1000;
}
}
System.out.println(x);
}
}
What is the program output?
Open
View answer
Java labeled continue usage — does this snippet compile and, if so, what prints?
int I = 0;
label:
if (I < 2) {
System.out.print("I is " + I);
I++;
continue label;
}
Choose the correct outcome.
Open
View answer
Java switch without breaks (fall-through) — compute final j value.
public class Test
{
public static void main(String args[])
{
int i = 1, j = 0;
switch(i)
{
case 2: j += 6;
case 4: j += 1;
default: j += 2;
case 0: j += 4;
}
System.out.println("j = " + j);
}
}
What is printed?
Open
View answer
Java switch fall-through with constant short case labels — what sequence is printed?
public class Switch2
{
final static short x = 2;
public static int y = 0;
public static void main(String [] args)
{
for (int z = 0; z < 3; z++)
{
switch (z)
{
case x: System.out.print("0 ");
case x-1: System.out.print("1 ");
case x-2: System.out.print("2 ");
}
}
}
}
Choose the exact output.
Open
View answer
Java switch with default, break, and multiple passes — what is the full output for z from 0 to 3?
public class Switch2
{
final static short x = 2;
public static int y = 0;
public static void main(String [] args)
{
for (int z = 0; z < 4; z++)
{
switch (z)
{
case x: System.out.print("0 ");
default: System.out.print("def ");
case x-1: System.out.print("1 ");
break;
case x-2: System.out.print("2 ");
}
}
}
}
Select the correct sequence.
Open
View answer
Java labeled break exiting nested loops — what prints (if anything)?
i = 0; j = 5;
tp: for (;;) {
i++;
for (;;) {
if (i > --j) {
break tp; // exits the outer loop
}
}
System.out.println("i =" + i + ", j = " + j);
}
Determine the program’s behavior.
Open
View answer
In Java, consider the following loop. Predict the exact printed value of i after the loop terminates.
int i = 0;
while (1) // treated as constant true in many MCQ contexts; assume an endless loop with break
{
if (i == 4)
{
break;
}
++i;
}
System.out.println("i = " + i);
Open
View answer
In Java, analyze switch fall-through within a for loop and predict the exact output sequence.
for (int i = 0; i < 3; i++)
{
switch (i)
{
case 0: break;
case 1: System.out.print("one ");
case 2: System.out.print("two ");
case 3: System.out.print("three ");
}
}
System.out.println("done");
Open
View answer
In Java, interpret nested do/while and while statements formatted on separate lines. What is printed?
public class Test {
public static void main(String[] args) {
int I = 1;
do while (I < 1)
System.out.print("I is " + I);
while (I > 1);
}
}
Open
View answer
Identify the correct outcome of this Java switch with comma-separated labels (which are invalid in Java). What happens at compile time?
int i = l, j = -1; // Note: the source uses the letter l, not digit 1
switch (i)
{
case 0, 1: j = 1; // invalid Java syntax (comma in case label)
case 2: j = 2;
default: j = 0;
}
System.out.println("j = " + j);
Open
View answer
Switching on a Float wrapper reference in Java: determine what happens at compile time for the following.
Float f = new Float("12");
switch (f)
{
case 12: System.out.println("Twelve");
case 0: System.out.println("Zero");
default: System.out.println("Default");
}
Open
View answer
Labeled loops in Java (outer/inner): determine the printed value of I.
int I = 0;
outer:
while (true)
{
I++;
inner:
for (int j = 0; j < 10; j++)
{
I += j;
if (j == 3)
continue inner;
break outer;
}
continue outer;
}
System.out.println(I);
Open
View answer
Decrement operator in while condition and post-loop values: predict x and y.
int x = l, y = 6; // note: the source uses letter l (ell), not digit 1
while (y--)
{
x++;
}
System.out.println("x = " + x + " y = " + y);
Open
View answer
Assignment vs. comparison in an if condition: determine the compile-time outcome for this Java snippet.
int x = 3;
int y = 1;
if (x = y) // uses assignment, not comparison
{
System.out.println("x =" + x);
}
Open
View answer
Analyze this Java switch with fall-through and erroneous case label (letter l instead of 1). What is the compile-time result?
public class SwitchTest {
public static void main(String[] args) {
System.out.println("value =" + switchIt(4));
}
public static int switchIt(int x) {
int j = 1;
switch (x) {
case l: j++;
case 2: j++;
case 3: j++;
case 4: j++;
case 5: j++;
default: j++;
}
return j + x;
}
}
Open
View answer
For-loop evaluation order with function calls in header: predict the exact character sequence printed.
public class Delta {
static boolean foo(char c) {
System.out.print(c);
return true;
}
public static void main(String[] argv) {
int i = 0;
for (foo('A'); foo('B') && (i < 2); foo('C')) {
i++;
foo('D');
}
}
}
Open
View answer
In Java, what is the exact output and why? Consider scope of the for-loop variable and the print after the loop.
for (int i = 0; i < 4; i += 2)
{
System.out.print(i + " ");
}
System.out.println(i); // Line 5
Open
View answer
Trace Java if/else with a dangling semicolon and a default boolean: what value prints for hand?
public class If1 {
static boolean b; // default false
public static void main(String [] args) {
short hand = 42;
if (hand < 50 && !b) // Line 7
hand++;
if (hand > 50); // Line 9 (dangling semicolon)
else if (hand > 40) {
hand += 7;
hand++;
} else {
--hand;
}
System.out.println(hand);
}
}
Open
View answer
Practice smarter
Solve a few questions daily and revisit weak topics regularly to improve accuracy.