Site icon T4Tutorials.com

if, if-else, nested if — C++ MCQs

1. What will be the output of the following code?
int x = 10;
if (x > 5)
cout << "Hello";
else
cout << "World";

(A) Hello


(B) World


(C) Error


(D) Nothing



2. What will the following code print?
int x = 5;
if (x == 5)
cout << "Yes";

(A) Yes


(B) No


(C) Error


(D) Nothing



3. What will be the output of this code?
int a = 3;
if (a > 5)
cout << "Greater";
else
cout << "Smaller";

(A) Greater


(B) Smaller


(C) 0


(D) Error



4. What will this code output?
int x = 7;
if (x < 5)
cout << "A";
else if (x == 7)
cout << "B";
else
cout << "C";

(A) A


(B) Error


(C) C


(D) B



5. Identify the error in the following code:
int x = 5
if (x > 0)
cout << "Positive";

(A) Missing semicolon after if


(B) Missing semicolon after variable declaration


(C) Wrong if syntax


(D) No error



6. What is the output?
int num = 10;
if (num % 2 == 0)
cout << "Even";
else
cout << "Odd";

(A) Odd


(B) Error


(C) Even


(D) 0



7. What will the following code display?
int x = 2, y = 3;
if (x > y)
cout << "X is greater";
else
cout << "Y is greater";

(A) X is greater


(B) Y is greater


(C) Error


(D) Nothing



8. What will be the output of this nested if code?
int a = 4, b = 8;
if (a > 5)
if (b > 5)
cout << "Both";
else
cout << "Only a";
else
cout << "None";

(A) Both


(B) Only a


(C) None


(D) Error



9. What will the code print?
int x = 0;
if (x)
cout << "True";
else
cout << "False";

(A) True


(B) 0


(C) Error


(D) False



10. Identify the output:
int x = 10;
if (x = 5)
cout << "Yes";
else
cout << "No";

(A) Yes


(B) No


(C) Error


(D) Nothing



11. What is the output of this code?
int x = 3;
if (x > 0 && x < 5)
cout << "Inside";
else
cout << "Outside";

(A) Inside


(B) Outside


(C) Error


(D) Nothing



12. What happens when this code runs?
int x = 4;
if (x > 5)
cout << "A";
else if (x > 3)
cout << "B";
else
cout << "C";

(A) A


(B) B


(C) C


(D) Error



13. Which statement is true about this code?
int x = 7;
if (x > 5)
if (x < 10)
cout << "Yes";

(A) Yes


(B) No


(C) Error


(D) Nothing



14. What will this code print?
int x = 2;
if (x == 1)
cout << "One";
else if (x == 2)
cout << "Two";
else
cout << "Other";

(A) One


(B) Other


(C) Two


(D) Error



15. Identify the error:
if (x > 5);
cout << "Hello";

(A) Extra semicolon after if


(B) Missing braces


(C) Syntax is correct


(D) Nothing



16. Output of the following code:
int x = 5;
if (x != 5)
cout << "No";
else
cout << "Yes";

(A) Yes


(B) No


(C) Error


(D) Nothing



17. What will the following code output?
int a = 10, b = 20;
if (a > b)
cout << "A";
else if (a < b)
cout << "B";
else
cout << "C";

(A) A


(B) B


(C) C


(D) Error



18. What will be printed?
int x = -1;
if (x > 0)
cout << "Positive";
else if (x < 0)
cout << "Negative";
else
cout << "Zero";

(A) Positive


(B) Error


(C) Zero


(D) Negative



19. What is the output?
int x = 5;
if (x > 2)
if (x < 10)
cout << "Inside";
else
cout << "Outside";

(A) Inside


(B) Outside


(C) Error


(D) Nothing



20. What will this code print?
int x = 0;
if (x == 0)
cout << "Zero";
else
cout << "Non-zero";

(A) Zero


(B) Non-zero


(C) Error


(D) Nothing



Exit mobile version