CPA-21-02 practice question 10 of 35

What happens when you attempt to compile and run the following code? #include #include using namespace std; class A { public: string s; A(string s) { this?>s = s; } }; class B { public: string s; B (A a) { this?>s = a.s; } void print() { cout

Full set: 257 questions
Answer options
A: It prints: Hello world
B: It prints: Hello
C: Compilation error Correct answer
D: None of these
Correct Answer:
C. Compilation error
Explanation

The provided code contains a syntax error. The character sequence ?> in the expressions this?>s = s; and this?>s = a.s; is not a valid C++ operator. The correct member access operator through a pointer is ->. Because of this invalid syntax, the compiler will fail to parse the code, resulting in a compilation error. Even if this was intended to be a typographical error for ->, the code must be evaluated exactly as written, which makes it uncompilable.

Why Incorrect

A: The code will not compile to print "Hello world" due to the invalid ?> syntax. B: The code will not compile, and there is no logic that would truncate the string to "Hello". D: Option C is the correct choice because a compilation error definitively occurs.

References

- ISO/IEC 14882:2017 (C++17 Standard), Section 5.4 [lex.operators], Table 12.

- Stroustrup, B. (2013). The C++ Programming Language (4th Ed.). Addison-Wesley. Chapter 10.

Scroll to Top