Java

Topic: Flowcontrol

Find the output of this program
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" ) ;}  
} 
}

A. 	If a is true and b is true then the output is "A && B"
B. 	If a is true and b is false then the output is "notB"
C. 	If a is false and b is true then the output is "ELSE"
D. 	If a is false and b is false then the output is "ELSE" 

Answer & Explanation
Answer: Option C

Explanation: 

Option C is correct. The output is "ELSE". Only when a is false do the output lines after 11 get some chance of executing.

Option A is wrong. The output is "A". When a is true, irrespective of the value of b, only the line 5 output will be executed. The condition at line 7 will never be evaluated (when a is true it will always be trapped by the line 12 condition) therefore the output will never be "A && B".

Option B is wrong. The output is "A". When a is true, irrespective of the value of b, only the line 5 output will be executed.

Option D is wrong. The output is "notB".

Browse random answers:

Find the output of this program
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" ) ;}  
} 
}
Which two are acceptable types for x?
switch(x) {  
default:  
System.out.println("Hello"); } 

 
byte 
long 
char 
float 
Short 
Long

A. 	1 and 3	
B. 	2 and 4
C. 	3 and 5	
D. 	4 and 6
Which of the below statement is true?
public void test(int x) {  
int odd = 1;  
if(odd) /* Line 4 */ {  System.out.println("odd");  }  
else  {  System.out.println("even");  } 
}

A. 	Compilation fails.
B. 	"odd" will always be output.
C. 	"even" will always be output.
D. 	"odd" will be output for odd values of x, and "even" for even values.
Which of the below statement is true?
public class While { 
public void loop(){  int x= 0;  while ( 1 ) /* Line 6 */  {  System.out.print("x plus one is " + (x + 1)); /* Line 8 */  
} 
}
}

A. There is a syntax error on line 1.
B. There are syntax errors on lines 1 and 6.
C.There are syntax errors on lines 1, 6, and 8.
D. There is a syntax error on line 6
How do you write an infinite loop using the for statement? 
How do you write an infinite loop using the while statement? 
Consider the following code snippet.if (aNumber >= 0)    if (aNumber == 0)        System.out.println("first string");else     System.out.println("second string");System.out.println("third string");    Exercise: What output do you think the code will produce if aNumber is 3? 
Write a test program containing the previous code snippet; make aNumber 3. What is the output of the program? Is it what you predicted? Explain why the output is what it is. In other words, what is the control flow for the code snippet?
Explain the If-else Statement ?
Explain Switch Case Statement?
Write switch case example?
Write syntax of while?
Write example of while loop?
Write syntax of do while?
Write example of do while?
Write explain Syntax of for loop?
Write example of for loop?