New Year Sale 2026! 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 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:

0/2000 characters
Carylon
3 months ago
Nope, it works just fine as is!
upvoted 0 times
...
Shannon
4 months ago
I thought exceptions needed to be caught differently.
upvoted 0 times
...
Adelina
4 months ago
Wait, is there a chance of a compilation error?
upvoted 0 times
...
Kanisha
4 months ago
Totally agree, that's the expected output!
upvoted 0 times
...
Ricarda
4 months ago
It prints: My exception.
upvoted 0 times
...
Dannie
5 months ago
I’m a bit confused about the `throw` statement. Does it really throw the object correctly, or could that cause a compilation error?
upvoted 0 times
...
Levi
5 months ago
This kind of question seems familiar; I feel like it should print "My exception." based on how exceptions work.
upvoted 0 times
...
Franklyn
5 months ago
I'm not entirely sure, but I remember something about the `what()` function needing to be overridden properly.
upvoted 0 times
...
Shawna
5 months ago
I think the code should compile fine since it follows the correct syntax for exceptions.
upvoted 0 times
...
Louvenia
5 months ago
This looks straightforward. The code creates a custom exception class `myClass` that inherits from `exception`, and then throws an instance of that class. Since the `catch` block is catching `exception&`, it should print the message from the overridden `what()` function, which is "My exception.". I'll select option A.
upvoted 0 times
...
Precious
5 months ago
Hmm, I'm a bit confused by the inheritance from `exception` here. Does that mean the `myClass` exception will be caught by the `catch (exception& e)` block? I'll need to double-check the exception handling rules to be sure.
upvoted 0 times
...
Gabriele
5 months ago
This looks like a question on exception handling in C++. I think the key is to understand how the `exception` class and its derived classes work. Let me carefully read through the code and think it through.
upvoted 0 times
...
Nana
5 months ago
Okay, I think I've got it. The `myClass` exception is derived from `exception`, so it should be caught by the `catch (exception& e)` block. And since the `what()` function is overridden to return "My exception.", that's what should be printed. I'll go with option A.
upvoted 0 times
...
Maxima
5 months ago
I'm not totally sure about this one. The question mentions the user's role and permissions, but I'm not sure how that impacts their ability to access the data.
upvoted 0 times
...
Marget
6 months ago
Okay, I think I've got this. Since the end user needs to be able to modify the blueprints, they would need more permissions than the basic Consumer role. The Operator role seems like it would give them the necessary access to make the required changes.
upvoted 0 times
...
Mariann
10 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
9 months ago
User3: The compiler might not like all those custom exceptions.
upvoted 0 times
...
Soledad
10 months ago
User2: It prints: My exception.
upvoted 0 times
...
Isadora
10 months ago
User1: Yes, you can create your own exception classes.
upvoted 0 times
...
...
Lenny
10 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
11 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
Sharmaine
9 months ago
Yes, A seems to be the correct choice.
upvoted 0 times
...
Myra
9 months ago
So, we're all going with option A then?
upvoted 0 times
...
Fletcher
10 months ago
I agree, it should print the custom message.
upvoted 0 times
...
Merrilee
10 months ago
I think it prints: My exception.
upvoted 0 times
...
...
Alease
11 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
11 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
10 months ago
I agree, the answer is definitely A.
upvoted 0 times
...
Colby
10 months ago
A) It prints: My exception.
upvoted 0 times
...
...
Amber
11 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
11 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
11 months ago
I think the answer is A) It prints: My exception.
upvoted 0 times
...

Save Cancel