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

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




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




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




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




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




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




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




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




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




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




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




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




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




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




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




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




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




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




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




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




Contents Copyrights Reserved By T4Tutorials