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.

Press + to interact
int marks, noOfCourses = 3;
for(int i=1; i<=noOfCourses; i++)
{
cout << "Your marks " << endl;
cin >> marks; // where user inputs any number
if(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--;
}
}
1

For marks = 90, what grade would be displayed?

A)

A

B)

A-

Question 1 of 40 attempted

Question: for and while

Refer to the following code snippet to answer the quiz questions below.

Press + to interact
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;
1

What will be the output of the above code if we enter -5, 7, 2, and 10 as inputs?

A)

No output

B)

14

Question 1 of 20 attempted

Question: if and do-while

Refer to the following code snippet to answer the quiz questions below.

Press + to interact
int num, evenCount = 0;
cin >> num;
do
{
if(num % 2 == 0)
evenCount++;
cin >> num;
}
while(num != -1);
cout << evenCount << endl;
1

What will be the output of the following code if we enter -5, 1, 12, and 10 as inputs?

A)

No output

B)

18

C)

2

Question 1 of 40 attempted

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;
Q

Which of the code variants is more efficient?

A)

Variant 1

B)

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;
}
Code editor to test your code