You are on page 1of 7

Q1. What will be the output of the following code snippet?

[Show the dry run]

(a) int m=2,n=15,i;


for(i=1;i<5;i++);
m++;
--n;
m+=++m;
System.out.println(“i=”+i);
System.out.println(“m=”+m);
System.out.println(“n=”+n);
Ans.
i i<5
1 true
2 true
3 true
4 true
5 false

m++ = 3
--n =14
m+= ++m
m=m + ++m
m= 3+4
m=7
Output
i=5
m=7
n=14

(b) int sum=0;


for(int i=0, j=0; i<5 && j<5; ++i, j=i+1)
sum=sum+i+j;
System.out.println(“Sum=”+sum);
Ans.
i j i<5 && j<5 sum=sum+i+j
0 0 true sum=0+0+0=0
1 2 true sum=0+1+2=3
2 3 true sum=3+2+3=8
3 4 true sum=8+3+4=15
4 5 false
Output:- Sum=15
(c) for(int x=0, n=3, z=10; n>=0; x++,n--, z-=x)
{
if(n==0)
System.out.println(“Z=”+z);
System.out.println(“X=”+x);
}
Ans.
x n z n>=0 if(n==0) Output
0 3 10 true false X=0
1 2 9 true false X=1
2 1 7 true false X=2
3 0 4 true true Z=4
X=3
5 -1 -1 false
Output:-
X=0
X=1
X=2
Z=4
X=3

Q2. What will be the output of the following code snippet? How many times the
loop gets executed? [Show the dry run]

(a) int x=5, y=50;


while(x<=y)
{
y=y/x;
System.out.println(y);
}
Ans.
x y x<=y y=y/x Output
5 50 true 10 10
5 10 true 2 2
5 2 false
Output:-
10
2
The loop will execute two times
(b) int a=10, x=7;
while(a%x>0)
{
System.out.println(a%x);
a++;
x+=2;

}
Ans.
a x a%x>0 Output
10 7 3>0 true 3
11 9 2>0 true 2
12 11 1>0 true 1
13 13 0>0 false
Output:-
3
2
1
The loop will execute three times

(c) int g=1, x=1, n=3;


while(x<=n)
{
g=g*x;
++x;
System.out.println(g);
}
Ans.
g x x<=n g=g*x Output
1 1 1<=3 true g=1*1=1 1
1 2 2<=3 true g=1*2=2 2
2 3 3<=3 true g=2*3=6 6
6 4 4<=3 false
Output:-
1
2
6
The loop will execute three times
Q3. What will be the output of the following code snippet? How many times the
loop gets executed?

(a) for(int i=10; i>1; i--)


{
if(i%2==1)
continue;
System.out.print(i+” “);
}
Ans.
i i>1 i%2==1 continue Output
10 10>1 true 10%2==1 false - 10
9 9>1 true 9%2==1 true
8 8>1 true 8%2==1 false - 8
7 7>1 true 7%2==1 true
6 6>1 true 6%2==1 false - 6
5 5>1 true 5%2==1 true
4 4>1 true 4%2==1 false - 4
3 3>1 true 3%2==1 true
2 2>1 true 2%2==1 false - 2
1 1>1 false
Output:-
10 8 6 4 2
Loop will execute 9 times

(b) for(int i=1; i< 100; i*=3)


{
System.out.println(i);
if(i*3==81)
break;
}
Ans.
i i<100 Output i*3==81 break
1 1<100 true 1 3==81 false -
3 3<100 true 3 9==81 false -
9 9<100 true 9 27==81 false -
27 27<81 true 27 81==81 true
Output:-
1
3
9
27
Loop will execute 4 times

(c) for(int y=41; y>=0; y/=10)


{
System.out.println(y);
if(y%10==0)
break;
}
Ans.
y y>0 Output y%10==0 break
41 41>=0 true 41 1==0 false -
4 4>=0 true 4 4==0 false -
0 0>=0 true 0 0==0 true
Output:-
41
4
0
Loop will execute 3 times

Q4. What will be the output of the following code snippet? How many times the
loop gets executed?
(a) int x=5, y=500;
do
{
y=y/x;
if(y==20)
continue;
System.out.println(y);
}
while(x<=y);
Ans.
x y y=y/x y==20 continue Output x<=y
5 500 100 100==20 false - 100 5<=100 true
5 100 20 20==20 true - 5<=20 true
5 20 4 4==20 false - 4 5<=4 false
Output:-
100
4
The loop will execute three times
(b) int a=10, x=7;
do
{
System.out.println(“value= ”+(a%x));
if(x==9)
break;
a++;
x+=2;

}
while(a%x>0);
Ans.
a x Output (a%x) x==9 break a++ x=x+2 a%x>0
10 7 3 7==9 false - 11 9 2>0 true
11 9 2 9==9 true - - -
Output:-
3
2
The loop will execute two times

(c) int g=1, x=1, n=3;


do
{
g=g*x;
++x;
if(x==2)
continue;
System.out.println(g);
}
while(x<=n);
Ans.
g x g=g*x ++x x==2 continue Output x<=n
1 1 g=1*1=1 2 2==2 true - 2<=3 true
1 2 g=1*2=2 3 3==2 false - 2 3<=3 true
2 3 g=2*3=6 4 4==2 false - 6 4<=3 false
Output:-
2
6
The loop will execute three times
Q5. Convert the following for loop to corresponding while loop:

(a) for(int m=7, p=1 ; m<=25 ; m++)


{
p*=m ;
}
Ans. int m=7, p=1;
while(m<=25)
{
p*=m;
m++;
}

(b) for(int x=1,s=0 ; x<=25 ; x+=2)


s+=x;
Ans. int x=1, s=0;
while(x<=25)
{
s+=x;
x+=2;
}
(c) int x,y,result=1;
for(int q=1 ; q<=y ; q++)
{
result*=x ;
}
Ans. int x,y,result=1,q=1;
while(q<=y)
{
result*=x ;
q++;
}

You might also like