Deal of The Day! Hurry Up, Grab the Special Discount - Save 25% - Ends In 00:00:00 Coupon code: SAVE25
Welcome to Pass4Success

- Free Preparation Discussions

C++ Institute CPA-21-02 Exam - Topic 3 Question 55 Discussion

What is the output of the program if character 4 is supplied as input?#include using namespace std;int main () {int c;cin >> c;try{switch (c){case 1:throw 20;case 2:throw 5.2f;case 3:throw 'a';default:cout
D) It prints: No exception
A) It prints: float exception. Exception Nr.
B) It prints: int exception. Exception Nr.
C) It prints: An exception occurred

C++ Institute CPA-21-02 Exam - Topic 3 Question 55 Discussion

Actual exam question for C++ Institute's CPA-21-02 exam
Question #: 55
Topic #: 3
[All CPA-21-02 Questions]

What is the output of the program if character 4 is supplied as input?

#include

using namespace std;

int main () {

int c;

cin >> c;

try

{

switch (c)

{

case 1:

throw 20;

case 2:

throw 5.2f;

case 3:

throw 'a';

default:

cout<<"No exception";

}

}

catch (int e)

{ cout << "int exception. Exception Nr. " << e; }

catch (float e)

{ cout << "float exception. Exception Nr. " << e; }

catch (...)

{ cout << "An exception occurred."; }

return 0;

}

Show Suggested Answer Hide Answer
Suggested Answer: D

Contribute your Thoughts:

0/2000 characters
I’m a bit confused about the catch blocks. If it hits the default, does it really just print "No exception" without throwing anything? That seems too straightforward!
upvoted 0 times
...
Teresita
5 days ago
I practiced a similar question where the default case was also involved. I feel like it should just output "No exception" since 4 isn't one of the cases.
upvoted 0 times
...
Carry
10 days ago
I'm not entirely sure, but I remember something about exceptions being thrown for specific cases. I guess 4 doesn't match any case, so maybe it just skips to the default?
upvoted 0 times
...
Carmela
15 days ago
I think if character 4 is input, it goes to the default case, right? So it should print "No exception".
upvoted 0 times
...

Save Cancel