Machine-translated by Google. Questions are authored in English.
Top

Flow Control - General Questions Questions and Answers

1.

switch(x) 
{ 
    default:  
        System.out.println("Hello"); 
}

Which two are acceptable types for x?

1.byte

2.long

3.char

4.float

5.Short

6.Long

Answer: A

Switch statements are based on integer expressions and since both bytes and chars can implicitly be widened to an integer, these can also be used. Also shorts can be used. Short and Long are wrapper classes and reference types can not be used as variables.

Please log in or register to report this question.

2.

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?

Answer: D

Using the integer 1 in the while statement, or any other looping or conditional construct for that matter, will result in a compiler error. This is old C Program syntax, not valid Java.

A, B and C are incorrect because line 1 is valid (Java is case sensitive so While is a valid class name). Line 8 is also valid because an equation may be placed in a String operation as shown.

Please log in or register to report this question.

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?

Answer: A

Correct answer: Compilation fails.

Java requires the condition of an if to be a boolean, and odd is an int. There is no implicit conversion from a number to a boolean, so the C idiom of testing an integer for non-zero does not compile - it must be written if (odd != 0). The parameter x is never used at all, which is a hint that the fragment was never going to behave as its option about odd and even values suggests.

Please log in or register to report this question.

4.

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" ) ; 
        } 
    } 
}

Answer: C

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".

Please log in or register to report this question.

Loading…

Topics

    No topic!
Tags: Flow Control - General Questions Questions and Answers || Flow Control - General Questions MCQ Questions and Answers || Flow Control - General Questions GK Questions and Answers || Flow Control - General Questions GK MCQ Questions || Flow Control - General Questions Multiple Choice Questions and Answers || Flow Control - General Questions GK || GK on Flow Control - General Questions || Java Programming Questions and Answers || Java Programming MCQ Questions and Answers || Java Programming GK Questions and Answers || GK on Java Programming