The editorial team at ProProfs Quizzes consists of a select group of subject experts, trivia writers, and quiz masters who have authored over 10,000 quizzes taken by more than 100 million users. This team includes our in-house seasoned quiz moderators and subject matter experts. Our editorial experts, spread across the world, are rigorously trained using our comprehensive guidelines to ensure that you receive the highest quality quizzes.
C is an amazing language that is easy to grasp at the same time. Take this C Loops: For, While, Do While Quiz! Test yourself today with the help of these carefully designed questions and find out just how much you know about creating loops in C. Don't forget to share this quiz with your programmer friends so they can try and answer these questions and see how well they did too! All the best!
Questions and Answers
1.
What is the output?
for(int i=1; i<15; i=i+3)
{
out.print(i);
}
A.
15
B.
1471013
C.
47101316
D.
147101516
Correct Answer B. 1471013
Explanation The loop starts with the value of i initialized as 1. Then, in each iteration, i is incremented by 3 (i=i+3). Therefore, the loop prints out the values of i which are 1, 4, 7, 10, and 13.
Rate this question:
2.
What represents the condition?for(int j=11; j>-2; j=j-2)
Correct Answer j>-2
Explanation The condition "j>-2" represents the condition for the given for loop. It specifies that the loop will continue as long as the value of "j" is greater than -2.
Rate this question:
3.
What is the value of m when you exit the loop?for(int m=30; m>0; m=m-4)
{
out.println(m);
}
Correct Answer -2
Explanation The loop starts with m=30 and continues as long as m is greater than 0. In each iteration, m is decremented by 4. Therefore, the loop will continue until m becomes less than or equal to 0. The last value of m that will be printed before exiting the loop is -2.
Rate this question:
4.
What is the output?int total=0;
for(int s=1; s<15; s++)
{
total=total+s;
}
out.println(total);
Correct Answer 105
Explanation The given code initializes a variable "total" to 0. It then enters a for loop that iterates from 1 to 14. In each iteration, the value of "s" is added to "total". After the loop ends, the value of "total" is printed, which is 105.
Rate this question:
5.
What is the output?for(int m=30; m>0; m=m-4)
{
out.print(m);
}
Correct Answer 30262218141062
Explanation The given code snippet is a for loop that starts with the value of m as 30 and decrements it by 4 in each iteration until it becomes less than or equal to 0. The loop body consists of a print statement that prints the value of m.
Therefore, the output of the code will be the concatenation of all the values of m that are printed in each iteration: 30, 26, 22, 18, 14, 10, and 6, resulting in the output: 30262218141062.
Rate this question:
6.
What is the output"for(int x=20; x<40; x=x+3)
{
out.print(x);
}
Correct Answer 20232629323538
Explanation The given code snippet is a for loop that starts with the variable x initialized to 20. The loop will continue as long as x is less than 40. In each iteration of the loop, the value of x is incremented by 3. The output of the code is the values of x printed on the same line without any spaces in between. The loop will execute 7 times, resulting in the output: 20 23 26 29 32 35 38.
Rate this question:
7.
What is the start point of the loop?(just the number)for(int x=20; x<40; x=x+3)
{
out.println(x);
}
Correct Answer 20
Explanation The start point of the loop is 20 because the variable x is initialized to 20 in the for loop declaration.
Rate this question:
8.
What is the output?int x=1;
while(x<5)
{
x++;
System.out.print(x);
}
Correct Answer 2345
Explanation The given code initializes the variable x to 1. It then enters a while loop that continues as long as x is less than 5. Inside the loop, x is incremented by 1 and then the value of x is printed. This process is repeated until x becomes 5, at which point the loop terminates. Therefore, the output of the code is 2345.
Rate this question:
9.
What is the output?int m=2, total=0;
while(m<6)
{
total=total+m;
m++;
}
System.out.print(total);
Correct Answer 14
Explanation The given code initializes two variables, 'm' and 'total', with values 2 and 0 respectively. It then enters a while loop that will continue as long as 'm' is less than 6. Inside the loop, the value of 'm' is added to 'total' and then 'm' is incremented by 1. This process continues until 'm' becomes 6. Finally, the value of 'total' is printed, which is 14.
Rate this question:
10.
What is the output?int z=2, sum=0;
while(z<9)
{
z++;
sum=sum+z;
}
System.out.print(sum);
Correct Answer 42
Explanation The code initializes two variables, z with a value of 2 and sum with a value of 0. It then enters a while loop that continues as long as z is less than 9. Inside the loop, z is incremented by 1 and added to the sum. This process repeats until z is equal to 9. Finally, the value of sum, which is the sum of all the numbers from 3 to 9, is printed, resulting in the output of 42.
Rate this question:
11.
What is the output?int k=3;
String s="";
while(k>-1){
s=k+" "+s;
k--;
}
System.out.print(s);
Correct Answer 0 1 2 3
Explanation The given code initializes an integer variable 'k' with a value of 3 and an empty string variable 's'. It then enters a while loop that continues as long as 'k' is greater than -1. In each iteration of the loop, it concatenates the value of 'k' with a space and the current value of 's', and assigns the result back to 's'. It also decrements the value of 'k' by 1. Therefore, the loop will execute 4 times, with 'k' taking the values 3, 2, 1, and 0. The final value of 's' will be "0 1 2 3".
Rate this question:
12.
What is the output?int b=5;
String list="";
while(b<11)
{
b=b+2;
if(b%2==1)
list=b+" "+list;
}
System.out.print(list);
Correct Answer 11 9 7
Explanation The code initializes the variable "b" to 5. Then, it enters a while loop that will continue as long as "b" is less than 11. Inside the loop, "b" is incremented by 2. If "b" is odd (when divided by 2 leaves a remainder of 1), it is appended to the string "list" followed by a space. Finally, the string "list" is printed.
In this case, the loop will iterate three times: when "b" is 7, 9, and 11. However, since the condition for appending to "list" is that "b" must be odd, only the values 7, 9, and 11 will be added to "list". Therefore, the output will be "11 9 7".
Rate this question:
13.
What is the output?int x=1;
do{
x++;
System.out.print(x);
}while(x<5);
Correct Answer 2345
Explanation The code initializes the variable x to 1. It then enters a do-while loop, where x is incremented by 1 and printed. This process continues until x is no longer less than 5. Therefore, the output is 2345.
Rate this question:
14.
What is the output?int k=3, tot=0;
do{
tot=tot+k;
k++;
}while(k<11);
System.out.print(tot);
Correct Answer 52
Explanation The code initializes two variables, k and tot, with k being equal to 3 and tot being equal to 0. Then, it enters a do-while loop that adds the value of k to tot and increments k by 1. This loop continues until the value of k is no longer less than 11. Finally, the code prints the value of tot, which is 52.
Rate this question:
Quiz Review Timeline +
Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.