Java Programming - Flow Control

Why should I learn to solve Java Programming questions and answers section on "Flow Control"?

Learn and practise solving Java Programming questions and answers section on "Flow Control" to enhance your skills so that you can clear interviews, competitive examinations, and various entrance tests (CAT, GATE, GRE, MAT, bank exams, railway exams, etc.) with full confidence.

Where can I get the Java Programming questions and answers section on "Flow Control"?

IndiaBIX provides you with numerous Java Programming questions and answers based on "Flow Control" along with fully solved examples and detailed explanations that will be easy to understand.

Where can I get the Java Programming section on "Flow Control" MCQ-type interview questions and answers (objective type, multiple choice)?

Here you can find multiple-choice Java Programming questions and answers based on "Flow Control" for your placement interviews and competitive exams. Objective-type and true-or-false-type questions are given too.

How do I download the Java Programming questions and answers section on "Flow Control" in PDF format?

You can download the Java Programming quiz questions and answers section on "Flow Control" as PDF files or eBooks.

How do I solve Java Programming quiz problems based on "Flow Control"?

You can easily solve Java Programming quiz problems based on "Flow Control" by practising the given exercises, including shortcuts and tricks.

Exercise : Flow Control - General Questions
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 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"
Answer: Option
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".


2.
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
1 and 3
2 and 4
3 and 5
4 and 6
Answer: Option
Explanation:

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.


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?
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.
Answer: Option
Explanation:

The compiler will complain because of incompatible types (line 4), the if expects a boolean but it gets an integer.


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?
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.
Answer: Option
Explanation:

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.