Quiz: Control Structures
Take this quiz to test your understanding of the different combinations of control structures.
We have covered the selection control structures (if
, if-else
, and switch
) and repetition or iteration control structures (while
, for
, and do-while
).
Let’s solve some quiz questions below to revise the working of different combinations of nested control structures. In the quiz, we may have multiple correct options. So let’s start!
Question: if
, switch
, and for
Refer to the following code snippet to answer the quiz questions below.
int marks, noOfCourses = 3;for(int i=1; i<=noOfCourses; i++){cout << "Your marks " << endl;cin >> marks; // where user inputs any numberif(marks >= 0 && marks <= 100){switch((marks-1)/10){case 9:cout << "A" << endl;break;case 8:cout << "A-" << endl;break;case 7:cout << "B" << endl;break;case 6:cout << "B-" << endl;break;case 5:cout << "C" << endl;break;case 4:case 3:case 2:case 1:case 0:cout << "F" << endl;break;}}else{cout << "Invalid input";i--;}}
For marks = 90
, what grade would be displayed?
A
A-
Question: for
and while
Refer to the following code snippet to answer the quiz questions below.
int sum = 0, size = 5, even;for(int i=1; i<size; i++){cin >> even;while(even % 2 != 0){cin >> even;}sum += even;}cout << sum;
What will be the output of the above code if we enter -5
, 7
, 2
, and 10
as inputs?
No output
14
Question: if
and do-while
Refer to the following code snippet to answer the quiz questions below.
int num, evenCount = 0;cin >> num;do{if(num % 2 == 0)evenCount++;cin >> num;}while(num != -1);cout << evenCount << endl;
What will be the output of the following code if we enter -5
, 1
, 12
, and 10
as inputs?
No output
18
2
Question: for
, if
and, if-else
Refer to the following code snippet to answer the quiz questions below.
Variant 1
int max,
value;
for(int i=1; i<=5; i++)
{
cin >> value;
if(i==1)
max = value;
else if(value > max)
max = value;
}
cout << max;
Variant 2
int max, value;
cin >> value;
max=value;
for(int i=2; i<=5; i++)
{
cin >> value;
if(value > max)
max = value;
}
cout << max;
Which of the code variants is more efficient?
Variant 1
Variant 2
Coding playground
If you chose a wrong answer to any of the questions above, use the editor below to run that question’s code and observe its output.
#include<iostream> using namespace std; int main() { // Test your code here. return 0; }