# Flow Of Control – Programming Questions.

Q1. Given the value of a variable, write a statement, without using if construct, which will produce the absolute value of the variable.

Ans. x = ( x < 0 ) ? -x : x ;

Q2. What is wrong with the following code?

switch(x)         {

case 1 :

n1=10;

n2=20;

case 2 :

n3=30;

break;

n4=40;

}

Ans. n=40; is unreachable because of break statement above it.

Q3. What is the problem with the following snippet?

:

int i = 5 , j = 10 ;

if ( ( i < j ) | | ( i = 10 ) )

System.out.println(“OK”);

System.out.println(“NOT OK”);

Ans. (i=10) used with if is the problem.

Q4.Show the output of the following code:

int a = 5 , b = 10 ;

if ( a > 5 )

if ( b > 5 ) {

System.out.println(“b is “+b);

}

else

System.out.println(“a is “+a);

Ans. a is 5.

Q5. Show the output of the following code:

int a = 10, b = 5;

if ( a > b){

if ( b > 5 )

System.out.println(“b is “+b);

}

else

System.out.println(“a is “+a);

Ans. No output.

Q6. What will the output of following two code fragments:

//version1

i = 10 ;

j = 10 ;

if ( a < 100 )

if ( b > 50 )

+ + i ;

else

+ + j ;

System.out.println( ” i = ” + i ) ;

System.out.println( ” j = ” + j ) ;

//version2

i = 10 ;

j = 10 ;

if ( a < 100 ) {

if ( b > 50 )

+ + i ;

}

else

+ + j ;

System.out.println( ” i = ” + i ) ;

System.out.println( ” j = ” + j ) ;

if the input given is shown below:

(i) a = 30 , b = 30

(ii) a = 60 , b = 70

Ans. case (i) when a = 30 , b = 30

version 1’s output will be as follows:

i=10

j=11

version 2’s output will be as follows:

i=10

j=10

case (ii) when a = 60 , b = 70

version 1’s output will be as follows:

i=11

j=10

version 2’s output will be as follows:

i=11

j=10

Q7. Identify the error(s) in the following code fragment:

switch(ch){

case ‘a’:

case ‘A’:

case ‘e’:

case ‘E’:

case ‘i’:

case ‘i’:

case ‘o’:

case ‘o’:

case ‘u’:

case ‘U’: ++vowels;

break;

default:++others;

}

:

Ans. The errors in the above code fragment are:

1. An uninitialized variable ch is being used in while’s test expression. The variable ch must have a value before it is used in an expression.
2. The switch statement’s two case constants are identical: case ‘i’ and case ‘i’. The case constants of same switch have different values.

Q8. What will be the output of following code fragment if the value of ch is

(i) a

(ii) c

(iii) d

(iv) h

(v) b?

:

switch (ch){

case ‘a’ : System.out.println(“It is a.”);

case ‘b’ : System.out.println(“It is b.”);

case ‘c’ : System.out.println(“It is c.”);

break;

case ‘d’ : System.out.println(“It is d.”);

break;

default : System.out.println(“Not a b c d.”);

}

:

Ans.  (i) When input is a, the output will be as follows:

It is a.

It is b.

It is c.

(ii) When input is c, the output will be as follows:

It is c.

(iii) When input is d, the output will be as follows:

It is d.

(iv) When input is h, the output will be as follows:

Not a b c d.

(v) When input is b, the output will be as follows:

It is b.

It is c.

Q9. Give the output of the following code:

int m =100;

while(m>0){   //true is boolean value

if (m<10)

break;

m=m-10;

}

System.out.println(“m is “+m);

Ans. m is 0.

Q10. Give the output of the following code:

int m=100;

while(true){

if (m<10)

continue

m=m-10;

}

System.out.println(“m is “+m);

Ans. No output; Infinite loop.

754 Views