Java for and while loops questions for practice
- Categories Java, Iterative Statements, For & While
In this section we will cover some questions which are asked on Java for and while loops.
- What is the output of following lines of code?
int datacount = 1;
while(datacount <= 6) {
if(datacount % 2 == 2)
System.out.println ("First Set");
else if(datacount % 3 == 0)
System.out.println ("Second Set");
else
System.out.println ("Third Set");
datacount++; }
Ans.
Third Set
Third Set
Second Set
Third Set
Third Set
Second Set
- Give the output and show the dry run.
public static void abc()
{
int x=1, i=2;
do
{
x*=i;
}while(++i<=5);
System.out.println(x);
}
Ans. 120
- The following program is supposed to check the given number is prime or not. Some part of the program is replaced by ______, with the numbering 1 to 5, fill this part so that program works correctly.
class checkPrime {
public static void main(String args[]) {
int i, f=0;
int n = Integer.parseInt(args[0]);
for(i=__1__; __2__; i++)
{
if(__3__==0)
{
f=__4__;
break;
}
}
if(f==__5__)
System.out.print(“The given no. “+n+” is a prime number”);
else
System.out.print(“The given no. “+n+” is not a prime number”);
}
}
Ans:
(1) 2 (2) i < n (3) n%i (4) 1 (5) 0
- Analyse the given program segment and answer the following questions:
for(i = 3; i <=4; i++)
{
for(j=2; j<i; j++)
{
System.out.print("*"),
}
System. out.println("Simply");
- How many times in total will the inner loop execute?
- Write the output of the program segment.
Ans
(i) 3 Times
(ii) *Simply
**Simply
- Give the output of the following program.
int i,j;
for (i=0; i<4; i++) { for (j=i; j>=0; j--)
System.out.print(j);
System.out.println();
}
Ans
0
10
210
3210
- Convert following do-while loop into for loop.
int i=1; int d=5; do{ d=d*2 System.out.println(d); i++; }while(i<=5);
Ans.
for(int i=1, d=5; i<=5; i++)
{
d = d * 2;
System.out.println(d);
}
- Study the method and answer the given questions:
public void sampleMethod()
{
for(int i=0;i<3;i++) {
for(int j=0;j<2;j++) {
int number = (int)(Math.random() * 10);
System.out.println(number);
}}}
- How many times in total will the inner loop execute?
- What is the range of possible values stored in the variable number?
Ans.
a. The loop executes 6 times.
b. Range of the possible values stored in the variable number is from 0 to 9
- Convert the following while loop to the corresponding for loop
int m = 5, n = 10;
while (n>=1)
{
System.out.println(m*n);
n–-;
}
Ans.
for(int m=5, n=10; n >=1; n--) { System.out.println(m*n); }
- What is the final value of ctr when the iterative process given below executes?
int ctr = 0;
for(int i=1 i<5;i++);
for(int j=1 i<=5; j+=2);
System.out.println(++ctr);
Ans.
The value of ctr when the iteration process executes : 1
This is because the println statement is outside of the loop.
- Give the output of following code?
int y,p;
for (int x=1; x<=3; x++)
}
for (y=1; y<=2; y++)
{
p = x * y;
System.out.print(p);
}
System.out.printin( );
}
Ans.
12
24
36
- Write the output and show the dry run too.
public class test
{
public static void main()
{
int i=1,n=6,f=1;
while(i<=n)
{
f=f*i; i++;
System.out.print(f+” “);
}
}
}
Ans:
1 2 6 24 120 720
- Rewrite the program by using do-while loop;
Class Pattern
{
public static void main(string.args[]}
{
int i,j;
for(i=5;i>=1;i--)
{
for (j=1;j<=5;j++)
System.out.println(i);
System.out.println();
}}}
Ans
class Pattern{ public static void main(String args[]) { int i=5,j; do { j=1; do { System.outprint(i); j++; } while(j<=5);
System.out.println();
i--;
} while(i>=1); }}
- The following program is supposed to print the following pattern. Some part of the program is replaced by ___n____ where n is 1 to 5, fill this part so that program works correctly.
5
45
345
2345
12345
class ques
{
public static void main(String args[])
{
for(__1__; __2__; i--)
{
for(__3__; __4__; __5__)
System.out.print(j);
System.out.println();
}
}
}
Ans:
(1) int i=5 (2) i>=1 (3) int j=i (4) j<=5 (5) j++
- Write the output of the following code segment:
char ch;
int x = 97;
do {
ch = (char) x;
System.out.print(ch + " ");
if (x % 10 == 0)
break;
++x;
} while (x <= 100);
Ans.
The do-while loop runs for values of x from 97 to 100 and prints the corresponding char values.
97 is the ASCII value for a, 98 is the ASCII value for 99…
So, the output will be
a b c d
- Write an equivalent while() loop for the following for() loop.
int s=0;
for(int x=1; x<=25; x+=2)
s+=x;
Ans.
int x=1,s=0; while (x<=25){ s +=x; x+=2; }
- Write a small program code to print the sum of digits of a long number 8729 using for() loop.
Ans.
long num=8729, sum 0; for( long y= num; y> 0; y= y/10){ sum= sum + y % 10; } System.out.println("Sum of digits = "+ sum);
- Analyze the following program segment and determine how many times the body of loop will executed ?
int x= 5;
int y= 75;
while (x <= y) {
y =y/x;
System.out.println(y);
}
Ans.
- The above loop will execute two times
- Value of x value of y
5 75 ->It is initial values, check for the condition x<= y
(true), enters into loop
15 -> new value y= 15, again check for the condition
x<= y (true)
3 ->new value y= 3, again check for the condition
x<= y (false) Loop gets terminated.
- Output:
15
3
- Write an equivalent while() loop for the following for() loop.
for(int x=2, Sum=0;x<=30; x+=2)
Sum *=x;
Ans.
int x=2, Sum=0; while(x<=30){ Sum*=x; x +=2; }
- Predict the output
int a,b;
for (a=1; a<=2; a++)
{
for (b= (64+a); b<=70; b++)
System.out.print((char) b);
System.out.println( );
}
Ans
ABCDEF
BCDEF
- Analyse the following program segment and determine how many times the loop will be executed and what will be the output of the program segment.
int k=1,i=2;
while(++i<6)
k*=i;
System.out.println(k);
Ans.
The loop will execute 3 times and the output is 60
- Write the output of the program.
public class t200
{
public static void main()
{
int i,n=5,s=0;
double f=0;
for(i=n;i>0;i–)
{
s=i*i;
f=(Math.pow(s,2))-i;
System.out.println(f);
}
}
}
Ans
620.0
252.0
78.0
14.0
0.0
- What is the output of this code? Give explanation as well.
long num=729, sum 0;
for( long y= num; y> 0; y= y/10){
sum= sum + y % 10;
}
System.out.println("Sum of digits = "+ sum);
Ans.
Sum of digits = 18
Explanation: Initialization num = 729, sum = 0, y = 729 Iteration 1: sum = 0 + 729 % 10 = 9 Iteration 2: y = 729/10 = 72 sum = 9 + 72%10 = 11 Iteration 3 y = 72/10 = 7 sum = 11 + 7%10 = 18 y = 7/10 = 0 so the loop exits. Print: Sum of digits = 18
- What will be the output of the following code?
int m=2
int n=15;
for(int i=1;i<5;i++)
m++; - - n;
System.out.println("m="+m);
System.out.println("n="+n);
Ans
m = 6
n =14
- Give the general syntax of a while-loop. How do you create infinite loops using a while-loop structure?
Ans.
The syntax of a while loop is,
while (condition or test-expression) { bodyoftheloop; } One of the method of creating an infinite loop is to write ‘true’ in the test-expression of the loop. while(true) System.out.println(“Infinite”);
- Give the general syntax of a do-while loop. How do you create infinite loops using do-while loop structure?
Ans.
The syntax of the do while loop is:
do { statement; }while (condition); Infinite loop using do-while loop: do { System.out.println(“Infinite”); }while(true);
- Give the output and determine how many times the loop will execute:
x=1; y=1;
while(x<=y)
{
y = y/x;
System.out.println(y);
}
Ans.
x=1; y=1; x<=y -> the loop executes. y=y/x =1/1=1 x<=y -> the loop execution continues
The entire process will continue infinite number of times, with the output as 1 in different lines.
- Convert the following segment into an equivalent do loop.
int x,c;
for (x=10, c=20; c>=10; c = c – 2)
x++;
Ans.
int x=10,c=20; do { x++; c=c-2; }while(c>=10);
- 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);
Ans.
20
The loop does not execute, as the condition results to false.
- Convert the following while loop to the corresponding for loop:
int m = 5, n = 10;
while (n>=1)
{
System.out.println(m*n);
n--;
}
Ans.
int m=5,n; for(n=10;n>=1;n--) { System.out.println(m*n); }
- Convert following do-while loop into for loop.
int i = 1;
int d=5;
do {
d=d*2;
System.out.println(d;)
i++ ; } while (i<=5);
Ans.
int i,d=5; for(i=1;i<=5;i++) { d=d*2; System.out.println(d); }
- What will be the output of the following code?
int m=2;
int n=15;
for(int i = 1; i<5; i++);
m++; – –n;
System.out.println(“m=” +m);
System.out.println(“n=” +n);
Ans.
m=3
n=14
- Analyse the following program segment and determine how many times the loop will be executed and what will be the output of the program segment.
int k=1, i=2;
while (++i<6)
k*=i;
System.out.println(k);
Ans.
The loop executes 3 times.
Output: 60
- How many times will the following loop execute? What value will be returned?
int x = 2, y = 50;
do
{
++x;
y- = x++;
}
while(x <= 10);
return y;
Ans.
The loop will execute 5 times.
Value returned is 15.
- Analyse the following program segment and determine how many times the loop will be executed and what will be the output of the program segment?
int p=200;
while(true)
{
if(p<100)
break;
p=p-20;
}
System.out.println(p);
Ans.
The loop executes 7 times.
Output: 80
- What is the final value of ctr after the iteration process given below, executes?
int ctr = 0;
for (int i = 1 ; i < = 5 ; i++)
for (int j = 1 ; j < = 5 ; j+=2)
++ctr;
Ans.
Final value of ctr is 15.
- Study the method and answer the given questions.
public void sampleMethod()
{
for(int i=0;i<3;i++)
{
for(int j=0;j<2;j++)
{
int number = (int) (Math.random( ) * 10);
System.out.println(number);
}
}
}
(i) How many times does the loop execute?
(ii) What is the range of possible values stored in the variable number?
Ans.
(i) The loops executes for 6 times.
(ii) The possible range of values stored in ‘number’ is 0 to 9.
- Write the output of the following code segment:
char ch; int x = 97;
do
{
ch = (char)x;
System.out.print(ch+“ ”);
if(x%10==0)
break;
++x;
}
while(x<=100);
Ans.
a b c d
- Analyse the given program segment and answer the following questions:
(i) Write the output of the program segment.
(ii) How many times does the body of the loop gets executed?
for(int m=5; m<=20; m+=5)
{
if(m%3 == 0)
break;
else
if(m%5 == 0)
System.out.println(m);
continue;
}
Ans.
i. Output:
5
10
ii. The loop executes 3 times.
- 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);
Ans.
Loop initialization: a = 6, b = 4
First iteration:
Condition check: a <= 24 — 6 <= 24 —- true
Loop is executed for the first time
Loop execution: a % b = 6 % 4 = 2 != 0 Hence, break is not executed
Loop increment operator: a = 1 + 6 — a = 6 + 6 = 12
Second iteration:
Condition check: a <= 24 — 12 <= 24 —- true
Loop is executed for the second time
Loop execution: a % b = 12 % 4 = 0 = 0 Hence, break is executed
System.out.println(a); — 12
Output is 12 and loop is executed two times
- Analyze the given program segment and answer the following questions
(i) Write the output of the program segment
(ii) How many times does the body of the loop gets executed?
for(int m=5; m<=20; m+=5)
{
if(m%3 == 0)
break;
else
if(m%5 == 0)
System.out.println(m);
continue;
}
Ans.
For loop initialization: m = 5
Loop condition check: m <= 20 = 5 <=20 = true
Loop execution for first time
m%3 = = 0
= 5 % 3 = = 0
= 2 = = 0
= false
else is executed
m%5 = = 0
= 5 % 5 = = 0
= 0 = = 0
= true
5 is printed
Loop increment statement is executed: m+=5 = m = m + 5 = 5 + 5 = 10
Loop condition check: m <= 20 = 10 <=20 = true
Loop body is executed second time and 10 is printed
Loop increment statement is executed: m+=5 = m = m + 5 = 10 + 5 = 15
Loop condition check: m <= 20 = 15 <=20 = true
Loop body is executed third time
m%3 = = 0
= 15 % 3 = = 0
= 0 = = 0
= true
break statement is executed and loop terminates
(i) 5
10
(ii) 3 times
- What is the output of following code?
int x = 0;
while(x++ < 20) {
if(x%2 == 0)
System.out.println("Simply");
else if ( x==9)
break;
else
continue;
}
Ans.
Simply
Simply
Simply
Simply