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 Exam CPA-21-02 Topic 1 Question 11 Discussion

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

What happens when you attempt to compile and run the following code?

#include

#include

using namespace std;

class myClass : public exception

{

virtual const char* what() const throw()

{

return "My exception.";

}

} obj;

int main () {

try

{

throw obj;

}

catch (exception& e)

{

cout << e.what() << endl;

}

return 0;

}

Show Suggested Answer Hide Answer
Suggested Answer: B

Contribute your Thoughts:

Mariann
1 months ago
Wait, does this mean I can create my own exception classes and throw them around like candy? That's kind of awesome, if you ask me. Though I bet the compiler's gonna have a field day with all those custom exceptions flying around.
upvoted 0 times
Annamae
1 days ago
User3: The compiler might not like all those custom exceptions.
upvoted 0 times
...
Soledad
13 days ago
User2: It prints: My exception.
upvoted 0 times
...
Isadora
23 days ago
User1: Yes, you can create your own exception classes.
upvoted 0 times
...
...
Lenny
1 months ago
Haha, this one's a classic tricky C++ question. I bet a lot of people are going to overthink it and pick the wrong answer. But not me! I'm sticking with A - it's gotta be the custom exception message, no doubt about it.
upvoted 0 times
...
Angella
1 months ago
Okay, let's see. We're throwing a custom exception, and the catch block is set up to handle exceptions of type exception&. I'm pretty sure that means it'll print the custom message, so I'm going with A as well.
upvoted 0 times
Fletcher
9 days ago
I agree, it should print the custom message.
upvoted 0 times
...
Merrilee
16 days ago
I think it prints: My exception.
upvoted 0 times
...
...
Alease
1 months ago
Hmm, let me think this through. The code is throwing a custom exception, so it should print the message we defined in the what() method. I'm going to go with A on this one.
upvoted 0 times
...
Robt
2 months ago
Ah, the classic C++ exception handling question! This one's a breeze - the answer is clearly A. The code creates a custom exception class and throws an instance of it, which the catch block then properly handles and prints the expected message.
upvoted 0 times
Melvin
21 days ago
I agree, the answer is definitely A.
upvoted 0 times
...
Colby
1 months ago
A) It prints: My exception.
upvoted 0 times
...
...
Amber
2 months ago
I'm not sure, but I think D) Compilation error could also be a possibility if there is an issue with the code.
upvoted 0 times
...
Rima
2 months ago
I agree with Cordelia, because the code defines a custom exception class and throws an object of that class.
upvoted 0 times
...
Cordelia
2 months ago
I think the answer is A) It prints: My exception.
upvoted 0 times
...

Save Cancel