C++ Decision-making Statements
Predict Output Questions For Beginners
(if, if-else, nested if, else if ladder, switch, etc.)
Answers are given at the bottom of this page.
Q1
#include <iostream>
using namespace std;
int main() {
int x = 10;
if (x > 5)
cout << "A";
if (x > 8)
cout << "B";
return 0;
}
Q2
#include <iostream>
using namespace std;
int main() {
int x = 4;
if (x % 2 == 0)
cout << "Even";
else
cout << "Odd";
return 0;
}
Q3
#include <iostream>
using namespace std;
int main() {
int a = 5, b = 8;
if (a > b)
cout << "A";
else if (a == b)
cout << "Equal";
else
cout << "B";
return 0;
}
Q4
#include <iostream>
using namespace std;
int main() {
int x = 0;
if (x)
cout << "True";
else
cout << "False";
return 0;
}
Q5
#include <iostream>
using namespace std;
int main() {
int x = 7;
if (x > 5) {
if (x < 10)
cout << "Yes";
else
cout << "No";
}
return 0;
}
Q6
#include <iostream>
using namespace std;
int main() {
int day = 2;
switch(day) {
case 1: cout << "Mon";
break;
case 2: cout << "Tue";
break;
case 3: cout << "Wed";
break;
default: cout << "Invalid";
}
return 0;
}
Q7
#include <iostream>
using namespace std;
int main() {
int x = 3;
switch(x) {
case 1:
case 2:
case 3: cout << "Low";
break;
case 4:
case 5: cout << "High";
break;
}
return 0;
}
Q8
#include <iostream>
using namespace std;
int main() {
int x = 10;
if (x > 5)
if (x > 8)
cout << "A";
else
cout << "B";
return 0;
}
Q9
#include <iostream>
using namespace std;
int main() {
int a = 5;
cout << (a > 3 ? "Yes" : "No");
return 0;
}
Q10
#include <iostream>
using namespace std;
int main() {
int x = 5;
if (x = 0)
cout << "True";
else
cout << "False";
return 0;
}
Q11
#include <iostream>
using namespace std;
int main() {
int x = 5;
if (x = 1)
cout << "True";
else
cout << "False";
return 0;
}
* * * * *
Answer Key
Question Output
Q1 ABQ2 EvenQ3 BQ4 FalseQ5 YesQ6 TueQ7 LowQ8 AQ9 YesQ10 False
Q11 True
* * * * *
No comments:
Post a Comment