java-programming-flow-control Related Question Answers

1. What will be the output of the program? int i = l, j = -1; switch (i) { case 0, 1: j = 1; / Line 4 / case 2: j = 2; default: j = 0; } System.out.println("j = " + j);





2. What will be the output of the program? int i = 1, j = 10; do { if(i > j) { break; } j--; } while (++i < 5); System.out.println("i = " + i + " and j = " + j);





3. What will be the output of the program? 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 "); } } } }





4. What will be the output of the program? 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; } }





5. What will be the output of the program? 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 / x = x + 100; else if ( b1 | b2 ) / Line 21 / x = x + 1000; } } System.out.println(x); } }





6. What will be the output of the program? public class If1 { static boolean b; public static void main(String [] args) { short hand = 42; if ( hand < 50 && !b ) / Line 7 / hand++; if ( hand > 50 ); / Line 9 / else if ( hand > 40 ) { hand += 7; hand++; } else --hand; System.out.println(hand); } }





7. What will be the output of the program? 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 ) ; } }





8. What will be the output of the program? int x = l, y = 6; while (y--) { x++; } System.out.println("x = " + x +" y = " + y);





9. What will be the output of the program? 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);





10. What will be the output of the program? for (int i = 0; i < 4; i += 2) { System.out.print(i + " "); } System.out.println(i); / Line 5 /





11. What will be the output of the program? int x = 3; int y = 1; if (x = y) / Line 3 / { System.out.println("x =" + x); }





12. What will be the output of the program? Float f = new Float("12"); switch (f) { case 12: System.out.println("Twelve"); case 0: System.out.println("Zero"); default: System.out.println("Default"); }





13. What will be the output of the program? int i = 0; while(1) { if(i == 4) { break; } ++i; } System.out.println("i = " + i);





14. What will be the output of the program? 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'); } } }





15. What will be the output of the program? 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");





16. What will be the output of the program? 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); } }





17. What will be the output of the program? boolean bool = true; if(bool = false) / Line 2 / { System.out.println("a"); } else if(bool) / Line 6 / { System.out.println("b"); } else if(!bool) / Line 10 / { System.out.println("c"); / Line 12 / } else { System.out.println("d"); }





18. What will be the output of the program? 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 "); } } } }





19. What will be the output of the program? int i = 0, j = 5; tp: for (;;) { i++; for (;;) { if(i > --j) { break tp; } } System.out.println("i =" + i + ", j = " + j);





20. What will be the output of the program? int I = 0; label: if (I < 2) { System.out.print("I is " + I); I++; continue label; }





21. public void foo( boolean a, boolean b) { if( a ) { System.out.println("A"); / Line 5 / } else if(a && b) / Line 7 / { System.out.println( "A && B"); } else / Line 11 / { if ( !b ) { System.out.println( "notB") ; } else { System.out.println( "ELSE" ) ; } } }





22. switch(x) { default: System.out.println("Hello"); } Which two are acceptable types for x? byte long char float Short Long





23. public void test(int x) { int odd = 1; if(odd) / Line 4 / { System.out.println("odd"); } else { System.out.println("even"); } } Which statement is true?





24. public class While { public void loop() { int x= 0; while ( 1 ) / Line 6 / { System.out.print("x plus one is " + (x + 1)); / Line 8 / } } } Which statement is true?





Terms And Service:We do not guarantee the accuracy of available data ..We Provide Information On Public Data.. Please consult an expert before using this data for commercial or personal use | Powered By:Omega Web Solutions
© 2002-2017 Omega Education PVT LTD...Privacy | Terms And Conditions
Question ANSWER With Solution