ProgrammingVery hardProduct company OA
What is printed?
What is printed?
for (int i = 0; i < 3; i++) {
try {
if (i == 1) continue;
System.out.print(i);
} finally {
System.out.print("f");
}
}- A. 0ff2f
- B. 0f1f2f
- C. 012f
- D. 0f f2f (continue skips finally)
Correct answer: A. 0ff2f
i=0: prints 0, finally prints f -> "0f". i=1: continue skips the print of i but finally still runs before the loop continues -> "f". i=2: prints 2, finally prints f -> "2f". Concatenated: 0f + f + 2f = "0ff2f".
More OOP with C++ and Java questions
- What is printed? java try { try { throw new java.io.IOException("io-fail"); } catch (java.io.IOException e) { throw new…
- What happens when this runs? java class Test { static void m() { throw new ArithmeticException("boom"); } public static void main(String[]…
- What is printed? java static void m(int x) throws java.io.IOException, InterruptedException { if (x == 0) throw new java.io.IOException()…
- What happens when this runs, assuming recurse() causes infinite recursion? java static void recurse() { recurse(); } public static void…
- What is printed? java class AEx extends Exception {} class BEx extends AEx {} // in main, wrapped in throws Exception: try { throw new…
- Does this compile (Java 7+)? java static void m() throws java.io.IOException, java.sql.SQLException { try { if (Math.random() < 2) throw…