XII-IP 2014-15 QUESTION BANK (FLOW OF CONTROL)
Topics Covered:
1. Transformation from if-else to switch –case
statement and vice versa
2. Transformation from do-while loop to while
loop and vice versa
3. Transformation from for loop to while loop and
vice versa
4. Transformation from for loop to do-while and
vice versa
1. Rewrite the following code fragment using
do-while loop and also give output:
for(int i=1;i<=20;i+=5) {
System.out.println(“”+i);
}
2.
How many times will each of the following loops execute? Which one of these is
an entry control loop and which one is an exit control loop?
Loop1:
int sum = 0, i = 5;
while (i<5)
{sum += i;i++;}
Loop2:
int sum = 0, i = 5;
do
{ sum += i;i++;} while (i<5);
3. What will be the content of jTextField1 after
executing the following code:
int Num = 6;
Num = Num + 1;
if ( Num > 5)
jTextField1.setText(Integer.toString(Num));
else
jTextField1.setText(Integer.toString(Num+5));
4. What will be the contents of jTextArea1 after
executing the following statement:
jTextArea1.setText("Object\nOriented\tProgramming");
5. Rewrite the following program code using
switch statement:
if (d == 1)
day = "Monday";
else if (d == 2)
day = "Tuesday";
else if (d == 3)
day = "Wednesday";
else
day = "-";
6. The following code has
some error(s). Rewrite the correct code underlining all the corrections made:
INT i=2; j=5;
While j>i
{jTextField1.gettext("j is
greater");
j--;++i;}
jOptionPane.showmessageDialog("Hello");
7.
Rewrite the following code using While LOOP
i=2;
do
{
System.out.println(i);
i+=2;
}
while(i<50);
8. Find the output of the following code if
ch=’B’
switch(ch)
{
case
‘A’ : System.out.println( ‘A’);
case
‘B’ :
System.out.println(‘B’);
case ‘C’ : System.out.println(‘c’);
case ‘D’ : System.out.println(‘D’); break;
default :
System.out.println(‘F’);
}
9. What will be the content of jTextField1
after executing the following code:
int Num = 6;
Num = Num + 1;
if ( Num > 5)
jTextField1.setText(Integer.toString(Num));
else
jTextField1.setText(Integer.toString(Num+5));
10. Rewrite the following code using for
loop.
int i=0;
while(i<20)
{ if( i == 8)
break;
System.out.println(i++);
}
11.The following
code has some error(s). Rewrite the correct code underlining all the
corrections made.
int y=3;
switch(y);
{
case= 1: System.out.print(“Yes its One”);
break;
case>=2: System.out.println(“Yes its more than Two”);
break;
case else: System.out.print(“Invalid
Number);
break;
}
12. Find the syntax errors if any in the following
programme:
int i ; sum=0;
i=1;
while(i=<10)
{
sum=sum+i;
i=i+3 }
System.println(sum);
13.Rewrite the following switch statement using if-else statement.
switch(number){
case 1:
jLable2.setText(“digits”);
break;
case 10:
jLable2.setText(“Tens”);
break;
case 100:
jLable2.setText(“Hundreds”);
break;
case 1000:
jLable2.setText(“Thousands”);
break;
default:
jLable2.setText(“error”);
break; }
14. State 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);
15.Find the syntax errors if any in the
following programme:
int i ; sum=0;
i=1;
While
i=<10 { sum=sum+i; i=i+3
}
System.println(sum);
16. What will be the output of the following code
int
i = 3 , n =0 ;
while
( i < 4) {
n++ ; i -- ; }
System.out.println( n );
17. Rewrite the following program code using
for loop.
int i=1, sum=0;
while (i<15){ sum +=i;
i +=2;}
18.Rewrite the
code after making correction. Underline the corrections.
int sum; value ; inct; int i;
FOR (i==0,i<=10; i++)
sum+= I;
inct ++;
19. What will be the value of j and k after execution
of the following code?
int j =10, k =12;
if (k>=j) {
k = j; j = k; }
20. Rewrite the folowing code to for loop
int f=1,i=2 ;
while(++i <5)
f*=i ;
System.out.println(f);
*************************************************************