Top

# Flow Control - Finding the output

1.

What will be the output of the program?

int i = 1, j = 10;
do
{
if(i > j)
{
break;
}
j--;
} while (++i < 5 i = " + i + " xss=removed>
 
 A.i = 6 and j = 5 B.i = 5 and j = 5 C.i = 6 and j = 4 D.i = 5 and j = 6 
 Answer: D This loop is a do-while loop, which always executes the code block within the block at least once, due to the testing condition being at the end of the loop, rather than at the beginning. This particular loop is exited prematurely if i becomes greater than j. The order is, test i against j, if bigger, it breaks from the loop, decrements j by one, and then tests the loop condition, where a pre-incremented by one i is tested for being lower than 5. The test is at the end of the loop, so i can reach the value of 5 before it fails. So it goes, start: 1, 10 2, 9 3, 8 4, 7 5, 6 loop condition fails. Enter details here Report type Wrong answer Wrong question Wrong answer description Wrong topic section / subject 
 
 2. 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> A.0 1 2 B.0 1 2 1 2 2 C.Compilation fails at line 11. D.Compilation fails at line 12. Answer: C Case expressions must be constant expressions. Since x is marked final, lines 12 and 13 are legal; however y is not a final so the compiler will fail at line 11. Enter details here Report type Wrong answer Wrong question Wrong answer description Wrong topic section / subject 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> A.0 1 2 B.0 1 2 1 2 2 C.2 1 0 1 0 0 D.2 1 2 0 1 2 Answer: D The case expressions are all legal because x is marked final, which means the expressions can be evaluated at compile time. In the first iteration of the for loop case x-2 matches, so 2 is printed. In the second iteration, x-1 is matched so 1 and 2 are printed (remember, once a match is found all remaining statements are executed until a break statement is encountered). In the third iteration, x is matched. So 0 1 and 2 are printed. Enter details here Report type Wrong answer Wrong question Wrong answer description Wrong topic section / subject 4. 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); } } A.00 B.1 C.101 D.111 Answer: C As instance variables, b1 and b2 are initialized to false. The if tests on lines 7 and 9 are successful so b1 is set to true and x is incremented. The next if test to succeed is on line 19 (note that the code is not testing to see if b2 is true, it is setting b2 to be true). Since line 19 was successful, subsequent else-if's (line 21) will be skipped. Enter details here Report type Wrong answer Wrong question Wrong answer description Wrong topic section / subject 5. 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); A.j = -1 B.j = 0 C.j = 1 D.Compilation fails. Answer: D The case statement takes only a single argument. The case statement on line 4 is given two arguments so the compiler complains. Enter details here Report type Wrong answer Wrong question Wrong answer description Wrong topic section / subject 6. 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; } } A.value = 2 B.value = 4 C.value = 6 D.value = 8 Answer: D Because there are no break statements, once the desired result is found, the program continues though each of the remaining options. Enter details here Report type Wrong answer Wrong question Wrong answer description Wrong topic section / subject Showing : 6 
 Loading… 
 Topics Language Fundamentals - General Questions Language Fundamentals - Finding the output Operators and Assignments - Finding the output Operators and Assignments - Pointing out the correct statements Exceptions - Finding the output Exceptions - Pointing out the correct statements Inner Classes - General Questions Inner Classes - Finding the output Garbage Collections - General Questions Garbage Collections - Pointing out the correct statements Java.lang Class - General Questions Java.lang Class - Finding the output Java.lang Class - Pointing out the correct statements Declarations and Access Control - General Questions Declarations and Access Control - Finding the output Declarations and Access Control - Pointing out the correct statements Flow Control - General Questions Flow Control - Finding the output Objects and Collections - General Questions Objects and Collections - Finding the output Objects and Collections - Pointing out the correct statements Threads - General Questions Threads - Finding the output Threads - Pointing out the correct statements Assertions - Finding the output Assertions - Pointing out the correct statements Assertions - Pointing out the errors Subject Categories General Language Computer Knowledge Banking Engineering Medical / Science 
 
 Tags: Flow Control - Finding the output Questions and Answers || Flow Control - Finding the output MCQ Questions and Answers || Flow Control - Finding the output GK Questions and Answers || Flow Control - Finding the output GK MCQ Questions || Flow Control - Finding the output Multiple Choice Questions and Answers || Flow Control - Finding the output GK || GK on Flow Control - Finding the output || Java Programming Questions and Answers || Java Programming MCQ Questions and Answers || Java Programming GK Questions and Answers || GK on Java Programming .loading{display: none;} About Us Home About Us Features Contact Us Terms of use Policy ExamsHunt Stay informed about all important Dates & Notifications We'll write only best content for you Copyright © 2023 ExamsHunt . All Rights Reserved . With