Curioustab
Aptitude
General Knowledge
Verbal Reasoning
Computer Science
Interview
Take Free Test
Aptitude
General Knowledge
Verbal Reasoning
Computer Science
Interview
Take Free Test
Flow Control Questions
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?
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.
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?
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.
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?
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.
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.
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.
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);
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");
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); } }
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);
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"); }
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);
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);
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); }
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; } }
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'); } } }
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
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); } }
1
2