Flow Control Questions and Answers updated daily – Java



10 Flow Control Questions and answers section with explanation for various online exam preparation, various interviews, Java Flow Control online test. Flow Control Questions with detailed description, explanation will help you to master the topic.

Flow Control Questions

1.
What will be the output of the program?

public class Test 
{
    public static void main(String [] args) 
    {
        int I = 1;
        do while ( I < 1 )
        System.out.print("I is " + I);
        while ( I > 1 ) ;
    }
}



SHOW ANSWER
Correct Ans:no output is produced
Explanation:
There are two different looping constructs in this problem. The first is a do-while loop and the second is a while loop, nested inside the do-while. The body of the do-while is only a single statement-brackets are not needed. You are assured that the while expression will be evaluated at least once, followed by an evaluation of the do-while expression. Both expressions are false and no output is produced.


2.
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);
    }
}



SHOW ANSWER
Correct Ans:101
Explanation:
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.


3.
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;  
    } 
}



Fresherslive Current Affairs

SHOW ANSWER
Correct Ans:value = 8
Explanation:
Because there are no break statements, once the desired result is found, the program continues though each of the remaining options.


4.
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; z++) 
        {
            switch (z) 
            {
                case x: System.out.print("0 ");
                case x-1: System.out.print("1 ");
                case x-2: System.out.print("2 ");
            }
        }
    }
}



SHOW ANSWER
Correct Ans:2 1 2 0 1 2
Explanation:
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.


5.
What will be the output of the program?

int i = 1, j = 10; 
do 
{
    if(i > j) 
    {
        break; 
    } 
    j--; 
} while (++i < 5); 
System.out.println("i = " + i + " and j = " + j);



Fresherslive Current Affairs

SHOW ANSWER
Correct Ans:i = 5 and j = 6
Explanation:
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.


6.
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); 



SHOW ANSWER
Correct Ans:compilation fails
Explanation:
The case statement takes only a single argument. The case statement on line 4 is given two arguments so the compiler complains.


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



SHOW ANSWER
Correct Ans:There is a syntax error on line 6.
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.


8.
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?



SHOW ANSWER
Correct Ans:compilation fails
Explanation:
The compiler will complain because of incompatible types (line 4), the if expects a boolean but it gets an integer.


9.
switch(x) 
    default:  
        System.out.println("Hello"); 
}
Which two are acceptable types for x?
byte
long
char
float
Short
Long



SHOW ANSWER
Correct Ans:1 and 3
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.


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



Fresherslive Current Affairs

SHOW ANSWER
Correct Ans:If a is false and b is true then the output is "ELSE"
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".


Are you seeking for good platform for practicing Flow Control questions in online. This is the right place. The time you spent in Fresherslive will be the most beneficial one for you.

Online Test on Flow Control @ Fresherslive

This page provides important questions on Flow Control along with correct answers and clear explanation, which will be very useful for various Interviews, Competitive examinations and Entrance tests. Here, Most of the Flow Control questions are framed with Latest concepts, so that you may get updated through these Flow Control Online tests. Flow Control Online Test questions are granted from basic level to complex level.

Why To Practice Flow Control Test questions Online @ Fresherslive?

Flow Control questions are delivered with accurate answer. For solving each and every question, very lucid explanations are provided with diagrams wherever necessary.

Practice in advance of similar questions on Flow Control may improve your performance in the real Exams and Interview.

Time Management for answering the Flow Control questions quickly is foremost important for success in Competitive Exams and Placement Interviews.

Through Fresherslive Flow Control questions and answers, you can acquire all the essential idea to solve any difficult questions on Flow Control in short time and also in short cut method.

Winners are those who can use the simplest method for solving a question. So that they have enough time for solving all the questions in examination, correctly without any tense. Fresherslive provides most simplest methods to answer any tough questions. Practise through Fresherslive test series to ensure success in all competitive exams, entrance exams and placement tests.

Why Fresherslive For Flow Control Online Test Preparation?

Most of the job seekers finding it hard to clear Flow Control test or get stuck on any particular question, our Flow Control test sections will help you to success in Exams as well as Interviews. To acquire clear understanding of Flow Control, exercise these advanced Flow Control questions with answers.

You're Welcome to use the Fresherslive Online Test at any time you want. Start your beginning, of anything you want by using our sample Flow Control Online Test and create yourself a successful one. Fresherslive provides you a new opportunity to improve yourself. Take it and make use of it to the fullest. GOODLUCK for Your Bright Future.




SSC CHSL Tier-I - Advanced
Latest pattern
Clear explanation
Access via
+
Rs. 49
Rs.800
Buy Now


Premium Package
200+ Mock Test Series
Validity 12 months
Rs. 399
2200
Buy Now


SBI Clerk 2019
Latest pattern
Clear explanation
Access via
+
Rs. 49
Rs.800
Buy Now


RRB NTPC 2019
Latest pattern
Clear explanation
Access via
+
Rs. 49
Rs.800
Buy Now


LIC ADO PRELIMS 2019
Latest pattern
Clear explanation
Access via
+
Rs. 49
Rs.800
Buy Now


DRDO- TECHNICIAN A - TIER I
Latest pattern
Clear explanation
Access via
+
Rs. 49
Rs.800
Buy Now


ONGC Executive E1 Level
Latest pattern
Clear explanation
Access via
+
Rs. 49
Rs.400
Buy Now


IBPS RRB Officer Scale I PRELIMS
Latest pattern
Clear explanation
Access via
+
Rs. 49
Rs.800
Buy Now


SOUTH INDIAN BANK CLERK 2019
Latest pattern
Clear explanation
Access via
+
Rs. 49
Rs.800
Buy Now


IBPS RRB Office Assistant Prelims 2019
Latest pattern
Clear explanation
Access via
+
Rs. 49
Rs.800
Buy Now


IBPS PO PRELIMS 2019
Latest pattern
Clear explanation
Access via
+
Rs. 49
Rs.800
Buy Now


One Month Pass
200+ Test Series
20,000+ Questions
Six Months Unlimited Access
Rs. 99
500
Buy Now


SSC MTS 2019
Latest pattern
Clear explanation
Access via
+
Rs. 49
Rs.800
Buy Now


Six Month Pass
200+ Test Series
20,000+ Questions
Six Months Unlimited Access
Rs. 199
1200
Buy Now


SSC Selection Posts Phase VII Matriculation Level
Latest pattern
Clear explanation
Access via
+
Rs. 49
Rs.800
Buy Now


RBI Assistant Prelims
Latest pattern
Clear explanation
Access via
+
Rs. 49
Rs.800
Buy Now


NABARD Development Assistant Prelims
Latest pattern
Clear explanation
Access via
+
Rs. 49
Rs.800
Buy Now


LIC Assistant Prelims
Latest pattern
Clear explanation
Access via
+
Rs. 49
Rs.800
Buy Now


SBI Apprentice
Latest pattern
Clear explanation
Access via
+
Rs. 49
Rs.800
Buy Now


FCI Manager Phase 1
Latest pattern
Clear explanation
Access via
+
Rs. 49
Rs.800
Buy Now


RBI Grade B Phase I
Latest pattern
Clear explanation
Access via
+
Rs. 49
Rs.800
Buy Now


SSC CPO Phase I
Latest pattern
Clear explanation
Access via
+
Rs. 49
Rs.800
Buy Now


IBPS SO Prelims 2019
Latest pattern
Clear explanation
Access via
+
Rs. 49
Rs.800
Buy Now


RRB ALP Stage II
Latest pattern
Clear explanation
Access via
+
Rs. 49
Rs.800
Buy Now


NIACL AO Prelims 2019
Latest pattern
Clear explanation
Access via
+
Rs. 49
Rs.800
Buy Now


IBPS Clerk Mains
Latest pattern
Clear explanation
Access via
+
Rs. 49
Rs.800
Buy Now



×

Get Unlimited Mock Tests


Unlock all 200+ Mock Tests series


12 Month Pass
Rs. 399
for 365 days

6 Month Pass
Rs. 199
for 180 days

One Month Pass
Rs. 99
for 30 Days



***Crack All Banking | Railways | SSC | Insurance | More Govt Exams***


FreshersLive - No.1 Job site in India. Here you can find latest 2020 government as well as private job recruitment notifications for different posts vacancies in India. Get top company jobs for both fresher and experienced. Job Seekers can get useful interview tips, resume services & interview Question and answer. Practice online test free which is helpful for interview preparation. Register with us to get latest employment news/rojgar samachar notifications. Also get latest free govt and other sarkari naukri job alerts daily through E-mail.