Skip to main content

control-flow

1).Which of the following is the correct syntax for an `if` statement in C++?

1) if (condition) { // code }
2) if condition { // code }
3) if {condition} { // code }
4) if: condition { // code }

2).Which of the following is the correct syntax to declare a `while` loop in C++?

1) while condition { // code }
2) while (condition) { // code }
3) while (condition) : { // code }
4) while {condition} { // code }

3).What is the output of the following C++ code?

#include <iostream>
using namespace std;

int main() {
  for (int i = 0; i < 3; i++) {
      cout << i << " ";
  }
  return 0;
}
1) 0 1 2
2) 1 2 3
3) 0 1 2 3
4) 1 2 3 4

4).What is the purpose of the `break` statement in C++?

1) To stop the execution of the program
2) To exit a loop or switch statement
3) To pause the program execution
4) To terminate the current function

5).Which loop is guaranteed to execute at least once in C++?

1) for loop
2) while loop
3) do-while loop
4) None of the above

6).What is the correct syntax for a `switch` statement in C++?

1) switch (expression) { case x: // code; break; }
2) switch {expression} { case x: // code; break; }
3) switch: expression { case x: // code; break; }
4) switch(expression) { case x: // code }

7).In which scenario would you use a `continue` statement in C++?

1) To stop the execution of the current loop iteration and move to the next iteration
2) To exit the loop entirely
3) To break out of a function
4) To jump to the end of the function

8) What is the output of the following C++ code?

#include <iostream>
using namespace std;

int main() {
  for (int i = 0; i < 3; i++) {
      if (i == 0) {
          cout << i << " ";
      } else if (i == 1) {
          cout << i << " ";
      } else {
          cout << i << " ";
      }
  }
  return 0;
}
1) 0 1 2
2) 1 2 3
3) 0 1 2 3
4) 0 1 2 3 4

9).What will be printed if the following C++ code is executed?

1) Less than 5
2) Equal to 10
3) Greater than 5
4) Nothing will be printed

10).Which of the following statements is true about a `do-while` loop in C++?

1) It is guaranteed to execute at least once
2) It may not execute at all
3) It requires the condition to be checked first
4) None of the above