XII-IP-QUESTION BANK ::FLOW OF CONTROL(for , while , do-while , if-else , nested if , switch-case)
XII IP
:: QUESTION BANK 2014-15
JAVA IMPLEMENTAION OF
for , while , do-while , if-else ,
nested if , switch-case statements
1.Rewrite the following code fragment using
do-while loop and also give output :
for(int i=1;i<=10;i++)
{
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 = 3;
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:
int
d=3;
String day;
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 than i ’)
j--;
++i;
}
JOptionPane.showMessageDialog("Hello");
7. What will be the contents of jTextField1 and
jTextField2 after executing the following code:
String
s = "ABC Micro Systems";
jTextField1.setText(s.length()+" ");
jTextField2.setText(s.toLowerCase());
8. Rewrite the
following code using While LOOP
i=2;
do
{
System.out.println(i);
i+=2;
}
while(i<50);
9. Find the output of the following code for the
following value of character
variable ch :
(i) ch=’B’
(ii) ch=’A’
(iii) ch=’D’
(iv)
ch=’F’
Char ch;
switch(ch)
{
case
‘A’ : System.out.println( “APPLE”);
case
‘B’ :
System.out.println(“BANANA”);
case ‘C’ :
System.out.println(“CARROT”);
break;
case ‘D’ : System.out.println(“DATES”);
default : System.out.println(“GOOD BYE …”);
}
10. 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));
11. Rewrite the following code using for loop.
int i=0;
while(i<20)
{
if( i == 8)
break;
System.out.println(i++);
}
12.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;
}
13.What will
be the output of the following code snippet?
int x=
10;
int y =
20;
if ((x<y)||(x=5) > 10)
System.out.println(x);
else
System.out.println(y)
14. 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);
15.Rewrite
the following switch statement using if-else statement and display the result
using appropriate IDE.
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;
}
16. State the output of the following code:
int a=10,b=15;
if(a>b) {
if(b>5)
System.out.println(“b Is” +b);
}
else
System.out.println(“a Is” +a);
17. Find the
syntax errors if any in the code given below : 1) int principal = prinTF.getText();
2) int time=timeTF.getText();
3) float int = principal * 0.08 * time;
4) JOptionpane.showMessageDialog(“
Interest is” +int);
18.Rewrite the
following switch statement using if-else statement and display the result using
appropriate IDE.
switch(day){
case
1:
jLable2.setText(“sunday”);
break;
case 2:
jLable2.setText(“monday”);
break;
case 3:
jLable2.setText(“tuesday”);
break;
case 4:
jLable2.setText(“wednesday”);
break;
case 5:
jLable2.setText(“thursday”);
break;
case 6:
jLable2.setText(“friday”);
break;
case 7:
jLable2.setText(“saturday”);
break;
default:
jLable2.setText(“error”);
break;
}
19. What will be the output of following segment?
public static void main(String[] args)
{
int
i, j, k, x=0;
for
(i=0; i<5; ++i)
for (j=0; j<i; ++j)
{
k=(i+j-1);
if (k%2==0)
x+=k;
else
if (k%3==0)
x+=k-2;
System.out.print(x+” ”);
}
System.out.println(“\n”+x);
}
20.Convert the following segment into an equivalent do while loop.
int x, c;
for (x=10, c=20; c>=10; c=c-2)
x++;
21. What will be the
output of the following code
int I = 3 , n =0 ;
while ( I < 4)
{
n++ ; I -- ;
}
System.out.println( n );
22. What will be the
content of jTextField after executing the following code?
int num= 4;
num= num + 1;
if (num > 5)
jTextfiled1.setText(Integer.toString(num));
else
jTextField1.setText(Integer.toString(num*4));
23. What will be the value of jTextField1 after execution of following
code:
jTextField1.setText(“Information”.reverse());
24. Rewrite the following program code using for
loop.
int i=1, sum=0;
while
(i<15){
sum +=i;
i +=2;}
25.Rewrite the code after making correction.
Underline the corrections.
int
sum; value ; inct;
int i;
for
(i==0,i<=10; i++)
sum+= I;
inct ++;
26. 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;
}
27. Rewrite the
folowing code to for loop
int f=1,I=2 ;
while(++I <5)
f*=I
;
System.out.println(f);
28.
Write the output of the following
code
|
||
int j = 10,x=0,i=0;
for (i = 1;i<=4;i++)
{
if(i%2==0)
x = x+ (i * j);
j = j – 2 ;
}
System.out.println(x);
|
||
|
||
|
|
|
29.Write
the following code segment using for… loop without effecting the output of
the code:
|
||
int
Num=6;
int Temp=Num;
while (Num>=1)
{
Temp=Temp-1;
if (Temp% 2== 0)
System.out.println(" is Even");
else
System.out.println(" is Odd");
Num=Num-2;
}
|
||
30. What will be the output of the following code
fragment if the value of ch is “a”
switch(ch)
{
default : System.out.println(“not a b c d.”);
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.”);
}
31.The following code has some error(s). Rewrite the
correct code underlining all the corrections made:
int a, b=20;
DO
{ a=b*2;
b=a/b;
}While(b>40)
URGENT: Refer similar question papers asked in XII
IP BOARD EXAM 2011-2014