Page 188 - CA_Blue( J )_Class10
P. 188
System.out.print(v);
} [2022]
a. 43210 b. 54321
c. 543210 d. 4321
Ans. a
5. Give the output of the following code and mention how many times the loop will execute:
int i;
for(i = 5; i >= 1; i--){
if(i % 2 == 1)
continue;
System.out.print(i + " ");
} [2019]
Ans. The output of the following code is:
4 2
The loop executes 5 times.
6. State the difference between while and do-while loop. [2018]
Ans.
while Loop do-while Loop
It is an entry-controlled loop. It is an exit-controlled loop.
It executes the body if the condition is true. It executes the body at least once whether the
condition is true or false.
Example: Example:
int a=10; int a=10;
while(a<=100) do
{ {
System.out.println(a); System.out.println(a);
a=a+10; a=a+10;
} } while(a<=100);
7. Give the output of the following program segment and also mention how many times the loop is executed:
int i;
for(i = 5; i > 10; i++)
System.out.println(i);
System.out.println(i * 4); [2018]
Ans. 20
The loop executes zero times.
8. Give the output of the following program segment and also mention the number of times the loop is executed:
int a, b;
for(a = 6, b = 4; a <= 24; a = a + 6)
{
if(a % b == 0)
break;
}
System.out.println(a); [2017]
Ans. Output: 12
Loop executes for 2 times.
9. Convert the following do-while loop into for loop.
int i = 1;
int d = 5;
do{
d = d * 2;
System.out.println(d);
186186 Touchpad Computer Applications-X

