0% found this document useful (0 votes)
36 views5 pages

IfElseIf and SwitchCase Answers

Uploaded by

Ahmed Tariq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views5 pages

IfElseIf and SwitchCase Answers

Uploaded by

Ahmed Tariq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

If-Else If and Switch Case Questions

with Answers
1. 1. Write a program that checks if a number is positive, negative, or zero using if–else if
statements.

Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter a number: ";
cin >> n;
if (n > 0) cout << "Positive";
else if (n < 0) cout << "Negative";
else cout << "Zero";
return 0;
}
Explanation: The program checks conditions in sequence: greater than 0, less than 0,
otherwise zero.

2. 2. Which condition is correct for checking if x is between 10 and 20 (inclusive)?

Answer: (b) if (x >= 10 && x <= 20)


Explanation: Both conditions must be true simultaneously, hence '&&' is correct.

3. 3. What will happen if none of the if–else if conditions are true and there is no else?

Answer: (b) Program will skip the block


Explanation: If no condition is true, the program simply skips that code block.

4. 4. Write a program that takes marks and displays grades using if–else if.

Answer:
#include <iostream>
using namespace std;
int main() {
int marks;
cin >> marks;
if (marks >= 80) cout << "Grade A";
else if (marks >= 70) cout << "Grade B";
else if (marks >= 60) cout << "Grade C";
else if (marks >= 50) cout << "Grade D";
else cout << "Grade F";
return 0;
}
Explanation: Conditions are checked in descending order to avoid overlap.

5. 5. Which is the correct if–else if structure?

Answer: (b) if() { } else if() { } else { }


Explanation: The proper structure always starts with 'if', followed by optional 'else if', and
optional 'else'.

6. Predict the output for n = 5.

Answer: Output → Medium


Explanation: Since n = 5 is not > 10, but it is > 3, the 'Medium' branch executes.

7. 6. Write a program to check leap year using if–else if.

Answer:
#include <iostream>
using namespace std;
int main() {
int year;
cin >> year;
if (year % 400 == 0) cout << "Leap Year";
else if (year % 100 == 0) cout << "Not Leap Year";
else if (year % 4 == 0) cout << "Leap Year";
else cout << "Not Leap Year";
return 0;
}
Explanation: Leap years follow rules divisible by 4, 100, and 400.

8. 7. Why is else if used instead of multiple if statements?

Answer: (c) To avoid unnecessary checking after a true condition (also d: all of the above)
Explanation: Once a condition is true, else-if prevents further checks, making it efficient.

9. What will be the output if x = 15 in the given code?

Answer: Output → Medium


Explanation: x = 15 is not < 10, but it is < 20, so 'Medium' is printed.

10. 8. Write a program using if–else if to check whether a character is vowel, consonant,
digit, or special symbol.

Answer:
#include <iostream>
#include <cctype>
using namespace std;
int main() {
char ch;
cin >> ch;
if (isalpha(ch)) {
if (ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||
ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
cout << "Vowel";
else cout << "Consonant";
} else if (isdigit(ch)) cout << "Digit";
else cout << "Special Symbol";
return 0;
}
Explanation: First check alphabet, then vowel/consonant, else digit, else symbol.

11. 1. Write a program that displays the day name using switch–case.

Answer:
#include <iostream>
using namespace std;
int main() {
int day;
cin >> day;
switch(day) {
case 1: cout << "Monday"; break;
case 2: cout << "Tuesday"; break;
case 3: cout << "Wednesday"; break;
case 4: cout << "Thursday"; break;
case 5: cout << "Friday"; break;
case 6: cout << "Saturday"; break;
case 7: cout << "Sunday"; break;
default: cout << "Invalid";
}
return 0;
}
Explanation: Each case matches a day number with a name.

12. 2. Which of the following is not allowed in a switch expression?

Answer: (c) String


Explanation: Switch only supports integer, char, and enum types in C++.

13. 3. What is the purpose of break in switch?


Answer: (b) Exits from the switch block
Explanation: Break prevents fall-through to other cases.

14. Predict the output of given switch code when x = 2.

Answer: Output → Two


Explanation: Case 2 executes, then break exits.

15. 4. Write a program using switch to perform basic arithmetic based on user choice.

Answer:
#include <iostream>
using namespace std;
int main() {
int a, b;
char op;
cin >> a >> b >> op;
switch(op) {
case '+': cout << a+b; break;
case '-': cout << a-b; break;
case '*': cout << a*b; break;
case '/': cout << a/b; break;
default: cout << "Invalid";
}
return 0;
}
Explanation: Operator determines which arithmetic operation runs.

16. 5. What happens if break is missing in a switch case?

Answer: (c) All following cases also execute


Explanation: Without break, execution continues until end or a break is found.

17. 6. Which statement is optional in switch–case block?

Answer: (c) default


Explanation: Default is not required, but recommended.

18. 7. Can a switch statement work with floating-point numbers?

Answer: (b) No, only integers and characters are allowed


Explanation: Floating values are not supported in switch expressions.

19. Predict output when ch = 'B'.

Answer: Output → Ball


Explanation: 'B' matches case 'B', printing 'Ball'.
20. 8. Write a program using switch–case that takes a grade and displays a remark.

Answer:
#include <iostream>
using namespace std;
int main() {
char grade;
cin >> grade;
switch(grade) {
case 'A': cout << "Excellent"; break;
case 'B': cout << "Good"; break;
case 'C': cout << "Average"; break;
case 'D': cout << "Poor"; break;
case 'F': cout << "Fail"; break;
default: cout << "Invalid Grade";
}
return 0;
}
Explanation: Switch maps grade letters to remarks.

You might also like