Java - Flow Control Question and Answer
Java - Flow Control Question and Answer
1. 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" ) ;
}
}
}
{
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" ) ;
}
}
}
- If a is true and b is true then the output is "A && B"
- If a is true and b is false then the output is "notB"
- If a is false and b is true then the output is "ELSE"
- If a is false and b is false then the output is "ELSE"
2. switch(x)
{
default:
System.out.println("Hello");
}
Which two are acceptable types for x?
byte
long
char
float
Short
Long
{
default:
System.out.println("Hello");
}
Which two are acceptable types for x?
byte
long
char
float
Short
Long
- 1 and 3
- 2 and 4
- 3 and 5
- 4 and 6
3. 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?
{
int odd = 1;
if(odd) /* Line 4 */
{
System.out.println("odd");
}
else
{
System.out.println("even");
}
}
Which statement is true?
- Compilation fails.
- "odd" will always be output.
- "even" will always be output.
- "odd" will be output for odd values of x, and "even" for even values.
4. 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?
{
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?
- There is a syntax error on line 1.
- There are syntax errors on lines 1 and 6.
- There are syntax errors on lines 1, 6, and 8.
- There is a syntax error on line 6.