Loops: for, while, do-while — C++ MCQs

55
Score: 0
Attempted: 0/55
1. What will be the output of the following code?
for(int i = 1; i <= 3; i++)
cout << i << " ";




2. What is the output of this code?
int i = 1;
while(i <= 3) {
cout << i;
i++;
}




3. What will the following code display?
int i = 3;
do {
cout << i;
i–;
} while(i > 0);




4. What will happen when this code runs?
int i = 1;
while(i <= 5);
{
cout << i;
i++;
}




5. What is the output?
for(int i = 1; i < 5; i += 2)
cout << i;




6. What will this code print?
int i = 0;
while(i < 3) {
cout << i;
i++;
}




7. What is the output?
int i = 1;
do {
cout << i;
i += 2;
} while(i < 6);




8. Find the output:
for(int i = 0; i < 5; i++)
if(i % 2 == 0)
cout << i;




9. What happens in this loop?
int i = 0;
while(i < 0)
cout << "Hello";




10. What will the following program print?
int i = 1;
do {
cout << i;
i–;
} while(i > 0);




11. What will be displayed?
for(int i = 5; i >= 1; i–)
cout << i;




12. What is the output?
int i = 1;
while(i <= 3) {
cout << i;
i = i + 2;
}




13. What will be printed?
for(int i = 0; i < 3; i++)
for(int j = 0; j < 2; j++)
cout << i << j << " ";




14. Output of the code below:
int i = 1;
do {
cout << "Hi";
i++;
} while(i < 1);




15. What is the output?
int i = 0;
while(i != 5) {
cout << i;
i++;
}




16. What happens?
for(int i = 1; i <= 3; i++);
cout << i;




17. Output of the loop:
int i = 1;
do {
cout << i;
i += 3;
} while(i < 10);




18. What is the result?
int i = 5;
while(–i > 0)
cout << i;




19. What will the following code do?
for(int i = 1; i <= 5; i++)
if(i == 3)
break;
cout << i;




20. What is the output?
int i = 0;
while(i < 5) {
if(i == 3) break;
cout << i;
i++;
}




21. What will print?
for(int i = 1; i <= 5; i++) {
if(i == 2) continue;
cout << i;
}




22. What will be printed?
int i = 0;
do {
i++;
if(i == 2) continue;
cout << i;
} while(i < 4);




23. What happens?
int i = 1;
while(true) {
cout << i;
if(i == 3) break;
i++;
}




24. What is the output?
for(int i = 2; i <= 6; i += 2)
cout << i;




25. What will this code print?
int i = 5;
while(i– > 0)
cout << i;




26. Output of the following:
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 2; j++)
cout << j;
}




27. What will print?
int i = 1;
while(i <= 5)
cout << i++;




28. Output of this code:
int i = 10;
do {
cout << i;
i -= 4;
} while(i > 0);




29. What happens in this loop?
for(int i = 1; i <= 3; i++)
for(int j = 1; j <= i; j++)
cout << "*";




30. What will be printed?
int i = 1;
do {
cout << i;
i++;
} while(i <= 0);




31. What is the result?
for(int i = 1; i <= 10; i++)
if(i % 2 == 0) cout << i;




32. What will this code output?
int i = 0;
while(i++ < 3)
cout << i;




33. What is the output?
int i = 3;
while(–i)
cout << i;




34. Find the output:
int i = 2;
do {
cout << i;
i = i + 2;
} while(i <= 8);




35. What is the result of this program?
int i = 1;
while(i <= 5) {
cout << i;
i += 2;
}




36. What will be the output of the following code?
int x = 2;
switch(x) {
case 1: cout << "One"; break;
case 2: cout << "Two"; break;
case 3: cout << "Three"; break;
}




37. What will the following program print?
int num = 5;
switch(num) {
case 1: cout << "A"; break;
case 5: cout << "B"; break;
default: cout << "C";
}




38. What will be printed?
int x = 10;
switch(x) {
case 5: cout << "Five"; break;
case 10: cout << "Ten"; break;
default: cout << "None";
}




39. What happens when the code runs?
int x = 3;
switch(x) {
case 1: cout << "A";
case 2: cout << "B";
case 3: cout << "C";
}




40. What will this code output?
int val = 2;
switch(val) {
case 1: cout << "X"; break;
case 2: cout << "Y";
case 3: cout << "Z"; break;
default: cout << "W";
}




41. Identify the output:
int x = 4;
switch(x) {
case 1: cout << "A"; break;
case 2: cout << "B"; break;
default: cout << "C";
}




42. Which statement is true about the following code?
int x = 5;
switch(x) {
case 5: cout << "Hello";
case 6: cout << "World";
}




43. What will be printed?
int a = 1;
switch(a + 1) {
case 1: cout << "One"; break;
case 2: cout << "Two"; break;
default: cout << "Other";
}




44. What is the output?
int n = 0;
switch(n) {
case 0: cout << "Zero"; break;
case 1: cout << "One"; break;
default: cout << "Other";
}




45. What will happen when this code executes?
char ch = 'B';
switch(ch) {
case 'A': cout << "Apple"; break;
case 'B': cout << "Banana"; break;
default: cout << "Other";
}




46. What will the following output?
int n = 2;
switch(n * 2) {
case 2: cout << "Two"; break;
case 4: cout << "Four"; break;
default: cout << "None";
}




47. What happens in this case?
int n = 1;
switch(n) {
case 1:
case 2: cout << "Match"; break;
default: cout << "No match";
}




48. Identify the error in this code:
float x = 1.5;
switch(x) {
case 1: cout << "One"; break;
case 2: cout << "Two"; break;
}




49. What will be printed?
int num = 7;
switch(num) {
case 5: cout << "Five"; break;
case 7: cout << "Seven"; break;
default: cout << "None";
}




50. What will this code produce?
int val = 9;
switch(val) {
case 10: cout << "Ten"; break;
case 11: cout << "Eleven";
default: cout << "Default";
}




51. What is the output of the following?
int n = 3;
switch(n) {
case 1: cout << "One";
case 2: cout << "Two";
case 3: cout << "Three";
default: cout << "Done";
}




52. What will happen here?
int x = 10;
switch(x) {
case 5: cout << "Five";
case 10: cout << "Ten"; break;
case 15: cout << "Fifteen";
}




53. What is the output?
int x = 1;
switch(x) {
case 0: cout << "Zero"; break;
case 1: cout << "One";
default: cout << "End";
}




54. What happens when this code runs?
int x = 2;
switch(x) {
case 1: cout << "Case1";
case 2: cout << "Case2";
case 3: cout << "Case3";
default: cout << "Default";
}




55. What will be printed?
int a = 4;
switch(a) {
case 2: cout << "Two"; break;
case 4: cout << "Four"; break;
case 6: cout << "Six"; break;
default: cout << "Other";
}




Contents Copyrights Reserved By T4Tutorials