Loops Practice Questions (AP CSA)
Java MCQ with Answers
| <-- Back to Boolean & if-then-else | Next to Two-dimensional Arrays--> |
Loops in Java
The for Loop
A for loop repeats a block of code a controlled number of times. Its header has three parts separated by semicolons: for (initialization; condition; update) { ... }. The initialization runs once before the loop starts, the condition is evaluated before every iteration (the loop runs only while it is true), and the update runs at the end of every iteration. The update expression can increment, decrement, or step by any amount — for example, i++, i--, or i = i + 2.
Scope of the Loop Variable
Multiple Variables in the for Loop Header
Nested Loops
Infinite Loops
Tips for AP-CSA Exam
In the AP Computer Science A (AP CSA) exam, questions on loops test the ability to trace loop execution step by step, determine the exact number of iterations, identify the scope of loop variables, predict output for nested loops, and recognize when a loop is infinite. Carefully track the values of all loop variables after each iteration when tracing nested or multi-variable loops.
Q1. What will be the output of the following code:
for (int i = 0; i <= 5; i++)
{
System.out.print("*");
}
System.out.println("*");
System.out.println("Number of times star is printed: "+i);
(A) *******
Number of times star is printed: 6
(B) *******
Number of times star is printed: 7
(C) ******
Number of times star is printed: 7
(D) ******
Number of times star is printed: 6
(E) Compile time error
Answer:
View Output(E) Compile time error
Explanation:
The loop variable i is defined inside the loop and hence is NOT visible outside the loop. Therefore, the code snippet will result in compile-time error.
Q2. How many times will “Hello” be printed?
for ( int i = 1, j=10; (i <= 10) && (j>=1); i++, j--)
{
if (i==j) System.out.println("Hello");
}
(A) 0 times
(B) Exactly 1 time
(C) 6 times
(D) 10 times
(E) 11 times
Answer:
View Output(A) 0 times
Explanation:
The values of i will run from 1 to 10 and values of j run from 10 down to 1. However, at no point of time they will be equal. Hence, “Hello” will NOT be printed at all.
| i | j | (i == j) |
|---|---|---|
| 1 | 10 | FALSE |
| 2 | 9 | FALSE |
| 3 | 8 | FALSE |
| 4 | 7 | FALSE |
| 5 | 6 | FALSE |
| 6 | 5 | FALSE |
| 7 | 4 | FALSE |
| 8 | 3 | FALSE |
| 9 | 2 | FALSE |
| 10 | 1 | FALSE |
Q3. How many times will the code print “Good Morning”?
for ( int i = 0, j=10; (i <= 10) && (j>=1); i++, j--)
{
if ((i==j) && (i%2 == 1)) System.out.println("Good Morning");
}
(A) 0 times
(B) Exactly 1 time
(C) 2 times
(D) 10 times
(E) 11 times
Answer:
View Output(B) Exactly 1 time
Explanation:
The values of i will run from 0 to 9 (i = 10 will violate the loop condition) and values of j run from 10 down to 1. However, the value of i and j will be equal at 5. At this value, the second boolean condition ((i%2 == 1)) will also be satisfied. Hence “Good Morning” will be printed only once.
| i | j | ((i == j) && (i % 2 == 1)) |
|---|---|---|
| 0 | 10 | FALSE |
| 1 | 9 | FALSE |
| 2 | 8 | FALSE |
| 3 | 7 | FALSE |
| 4 | 6 | FALSE |
| 5 | 5 | TRUE |
| 6 | 4 | FALSE |
| 7 | 3 | FALSE |
| 8 | 2 | FALSE |
| 9 | 1 | FALSE |
Q4. What will be the output of the following code segment?
for ( int i = 1, j=0; (i <= 10); i=i+2)
{
if (i%2 == 0) System.out.println("Even");
if (j%2 == 1) System.out.println("Odd");
j = j + 2 + i;
}
(A) Even
Odd
(B) Odd
Even
(C) Even
Even
(D) Odd
Odd
(E) Nothing gets printed
Answer:
View Output(D) Odd
Odd
Explanation:
We need to run the values of i and j to get the result. However, we can easily make out that EVEN will never be printed because i will never take a even value (values of i start at 1 and are incremented by 2).
| i | j | (i%2 == 0) | (j%2 == 1) | Output |
|---|---|---|---|---|
| 1 | FALSE | |||
| 0 | FALSE | |||
| 3 | FALSE | |||
| 3 | TRUE | ODD | ||
| 5 | FALSE | |||
| 8 | FALSE | |||
| 7 | FALSE | |||
| 15 | TRUE | ODD | ||
| 9 | FALSE | |||
| 24 | FALSE |
Q5. How many times will the word “Eligible” be printed?
int i=2;
for ( int k = 23; (k >= 1); k=k-2)
{
if (k%i == 0) System.out.println("Eligible");
i++;
}
(A) 2 times
(B) 6 times
(C) 10 times
(D) 12 times
(E) Nothing gets printed
Answer:
View Output(A) 2 times
Explanation:
We need to run the values of k and i to get the result.
| k | i | (k % i == 0) | Output |
|---|---|---|---|
| 23 | 2 | FALSE | |
| 21 | 3 | TRUE | Eligible |
| 19 | 4 | FALSE | |
| 17 | 5 | FALSE | |
| 15 | 6 | FALSE | |
| 13 | 7 | FALSE | |
| 11 | 8 | FALSE | |
| 9 | 9 | TRUE | Eligible |
| 7 | 10 | FALSE | |
| 5 | 11 | FALSE | |
| 3 | 12 | FALSE | |
| 1 | 13 | FALSE |
Q6. What will be the output of the following code segment?
for ( int i= 1, j=2, k=3; (i <= 15); i++)
{
if (i%j == 0) System.out.print(i+" ");
if (i%k == 0) System.out.print(i+" ");
}
(A) 2 3 4 6 8 9 10 12 14 15
(B) 2 3 4 6 6 8 9 10 12 12 14 15
(C) 2 4 6 8 10 12 14
(D) 3 6 9 12 15
(E) Nothing gets printed
Answer:
View Output(B) 2 3 4 6 6 8 9 10 12 12 14 15
Explanation:
The code prints all multiples of 2 (value of j) and 3 (value of k) between 1 and 15. Moreover, numbers 6 and 12 get printed twice because they are multiples of both 2 and 3.
Q7. What will be the output of the following code segment?
int i=1;
int j=0;
for (i= 1; (i <= 12); i++)
{
if (i%2== 0) j=j+1;
else if (i%3== 0) j=j+2;
else j=j+3;
}
System.out.println("Value of i is: "+ i);
System.out.println("Value of j is: "+ j);
(A) Value of i is: 12
Value of j is: 20
(B) Value of i is: 13
Value of j is: 18
(C) Value of i is: 13
Value of j is: 22
(D) Value of i is: 12
Value of j is: 22
(E) Value of i is: 13
Value of j is: 20
Answer:
View Output(C) Value of i is: 13
Value of j is: 22
Explanation:
Value of i will be 13 after the loop execution completes.We need to calculate the value of j using the following table:
i |
j |
(i%2== 0) -> j++; |
(i%3== 0) -> j=j+2; |
j=j+3 |
Value of j |
|---|---|---|---|---|---|
| 1 | 0 | FALSE | FALSE | 3 | 3 |
| 2 | 3 | TRUE -> j=4 | 4 | ||
| 3 | 4 | FALSE | TRUE -> j=6 | 6 | |
| 4 | 6 | TRUE -> j=7 | 7 | ||
| 5 | 7 | FALSE | FALSE | 10 | 10 |
| 6 | 10 | TRUE -> j=11 | 11 | ||
| 7 | 11 | FALSE | FALSE | 14 | 14 |
| 8 | 14 | TRUE -> j=15 | 15 | ||
| 9 | 15 | FALSE | TRUE -> j=17 | 17 | |
| 10 | 17 | TRUE -> j=18 | 18 | ||
| 11 | 18 | FALSE | FALSE | 21 | 21 |
| 12 | 21 | TRUE -> j=22 | 22 |
Q8. What will be the output of the following code segment?
int value=1;
for (int times= 1; value <= 50; times++)
{
if (true)
System.out.println(value*times);
else value++;
}
(A) Prints the squares of numbers from 1 to 50
(B) Prints the squares of numbers from 2 to 50
(C) 1 4 9 16 25
(D) 4 9 16 25 36 49
(E) Infinite loop
Answer:
View Output(E) Infinite loop
Explanation:
The code segment results in an infinite loop since the variable value is not been incremented. The condition if (true) always evaluates to true and hence the else part never gets executed and value is not incremented.
Q9. What will be the output of the following code segment?
for (int i= 1; i <= 2; i++)
{
for (int j= 1; j <= 3; j++)
{
System.out.print((i+i) * (j+j) + " ");
}
System.out.println();
}
(A) 4 8 12
8 16 24
(B) 2 4 8
4 8 16
(C) 4 8 12 24
8 16 24 32
(D) 2 4 8 12
4 8 16 24
(E) 2 4 8 12 24
4 8 16 24 32
Answer:
View Output(A) 4 8 12
8 16 24
Explanation:
We can easily calculate the output by executing the two nested loops for different values of i and j, as shown below:
i |
j |
Output: (i+i) * (j+j)
|
|---|---|---|
| 1 | 1 | (1+1) * (1+1) = 2*2 = 4 |
| 1 | 2 | (1+1) * (2+2) = 2*4 = 8 |
| 1 | 3 | (1+1) * (3+3) = 2*6 = 12 |
| 2 | 1 | (2+2) * (1+1) = 4*2 = 8 |
| 2 | 2 | (2+2) * (2+2) = 4*4 = 16 |
| 2 | 3 | (2+2) * (3+3) = 4*6 = 24 |
Q10. What will be the output of the following code segment?
int i,j=1;
for (i= 1; i <= 6; i=i+2)
{
for (; j <= 6; j=j+2)
{
if (j == i) System.out.print(j+ " ");
}
}
(A) 1 3 5
(B) 3 5
(C) 1
(D) Compile time error; missing initialization in inner for-loop
(E) Infinite loop
Answer:
View Output(C) 1
Explanation:
The code segment does NOT result in a compile-time error. Although the initialization in the inner for-loop is empty; it is allowed. Additionally, the value of j does not get initialised to 1 every time the inner loop is entered. Hence the output will be 1. Same is shown in table below:
i |
j |
(j == i) |
Output |
|---|---|---|---|
| 1 | 1 | TRUE | 1 |
| 1 | 3 | FALSE | |
| 1 | 5 | FALSE | |
| 3 | 7 | Inner loop does not execute since j > 6 | |
| 5 | 7 | Inner loop does not execute since j > 6 | |
Q11. What will be the output of the following code segment?
int i,j;
for (i= 1; i <= 3; i++)
{
for (j=i; j <=2; j++)
{
System.out.print("*");
}
}
(A) ****** [6 times]
(B) *** [3 times]
(C) ** [2 times]
(D) * [1 time only]
(E) Infinite loop
Answer:
View Output(B) *** [3 times]
Explanation:
We need to trace the values of i and j across the two loops to get the output, as shown below:
i |
j |
Output |
|---|---|---|
| 1 | 1 | * |
| 1 | 2 | * |
| 2 | 2 | * |
| 3 | 3 | Inner loop does not execute since j > 2 |
Q12. What will be the output of the following code?
int k,j;
for (k= 50; k <= 5000; k=k*10)
{
j=k;
while (j>0)
{
System.out.print(j%10);
j= j/10;
}
System.out.println();
}
(A) 05
005
0005
(B) 050
00500
0005000
(C) 50
500
5000
(D) 5
5
5
(E) Infinite loop
Answer:
View Output(A) 05
005
0005
Explanation:
We need to trace the values of k and j across the two loops to get the output, as shown below:
k |
j |
Output (j % 10) |
|---|---|---|
| 50 | 50 | 50 % 10 = 0 |
| 5 | 5 % 10 = 5 | |
| line break | ||
| 500 | 500 | 500 % 10 = 0 |
| 50 | 50 % 10 = 0 | |
| 5 | 5 % 10 = 5 | |
| line break | ||
| 5000 | 5000 | 5000 % 10 = 0 |
| 500 | 500 % 10 = 0 | |
| 50 | 50 % 10 = 0 | |
| 5 | 5 % 10 = 5 | |
| line break |
Q13. What will be the output of the following code?
int basic= 1000;
int DA = 250;
int HRA = 100;
int salary=0;
// program to calculate total salary
for (int month=1 ; month <= 12; month++)
{
if (month <=6)
salary = basic + DA + HRA + salary;
else
salary = basic + (basic/2) + DA + HRA + salary;
month++;
}
System.out.println("Salary is: "+salary);
(A) 19200
(B) 19000
(C) 9200
(D) 9600
(E) Infinite loop
Answer:
View Output(D) 9600
Explanation:
We need to trace the values of the variables month and salary across the loop to get the output. However, it is important to note here that the code is NOT actually calculating the salary for all the 12 months because the loop variable month is getting incremented twice (once after the execution of the if-else statement and once in the loop execution).
month |
salary |
if (month <=6)
|
else
|
|---|---|---|---|
| 1 | 0 | salary = 1000 + 250 + 100 + 0 = 1350 | |
| 3 | 1350 | salary = 1000 + 250 + 100 + 1350 = 2700 | |
| 5 | 2700 | salary = 1000 + 250 + 100 + 2700 = 4050 | |
| 7 | 4050 | salary = 1000 + (1000/2) + 250 + 100 + 4050 = 5900 | |
| 9 | 5900 | salary = 1000 + (1000/2) + 250 + 100 + 5900 = 7750 | |
| 11 | 7750 | salary = 1000 + (1000/2) + 250 + 100 + 7750 = 9600 | |
| 13 | Loop terminates (month > 12) | ||
Q14. What will be the output of the following code?
for (int i=10 ; i <= 100; i=i+10)
{
for (int j=1; j <=10; j++)
{
System.out.print(j*i+ " ");
}
System.out.println();
}
(A) Prints table of 1 to 10 (all multiples from 1 to 10)
(B) Prints table of 10 (all multiples from 1 to 10)
(C) Prints table of 10 to 100 (all multiples from 1 to 10)
(D) Prints table of 1 to 10 (all multiples from 10 to 100)
(E) Prints table of 1 to 100 (all multiples from 1 to 10)
Answer:
View Output(C) Prints table of 10 to 100 (all multiples from 1 to 10)
Explanation:
We need to trace the values of the variables i and j across the loops to get the output. The output will be:
10 20 30 40 50 60 70 80 90 100
20 40 60 80 100 120 140 160 180 200
30 60 90 120 150 180 210 240 270 300
40 80 120 160 200 240 280 320 360 400
50 100 150 200 250 300 350 400 450 500
60 120 180 240 300 360 420 480 540 600
70 140 210 280 350 420 490 560 630 700
80 160 240 320 400 480 560 640 720 800
90 180 270 360 450 540 630 720 810 900
100 200 300 400 500 600 700 800 900 1000
Q15. Which one of the following codes can be used to print all the even numbers between 1 and 100 (both numbers inclusive)?
(A) for (int i=1, j=2 ; i <= 100; i++)
{
if (i%j == 2) System.out.print(i + " ");
}
(B) for (int i=1, j=2 ; i <= 100; i++)
{
if (i%j == 0) System.out.print(i + " ");
}
(C) for (int i=1, j=2 ; i <= 100; i++)
{
if (i%2 != 0) System.out.print(i + " ");
}
(D) for (int i=1; i <= 100; i=i+2)
{
System.out.print(i + " ");
}
(E) for (int i=1, j=0; i <= 100; i++)
{
System.out.print(i + " ");
j++;
i++;
}
Answer:
View Output(B) for (int i=1, j=2 ; i <= 100; i++)
{
if (i%j == 0) System.out.print(i + " ");
}
Explanation:
Options (C), (D) and (E) will result in printing all odd numbers between 1 and 100. We are now left with option (A) and (B). Option (A) is incorrect because it checks for numbers which when mod with 2 give 2. There are no such numbers. All even numbers when mod with 2 give 0 and all odd numbers give 1. Hence for option (A) nothing gets prints. Option (B) is the correct option, since it checks for numbers which when mod with j (=2) result in a 0.
| <-- Back to Boolean & if-then-else | Next to Two-dimensional Arrays--> |