Select the correct output of the following C++ program segment, assuming all…
2022
Select the correct output of the following C++ program segment, assuming all necessary header files are correctly included.
- A.
itence
- B.
tance
- C.
Tence
- D.
Inheri
Attempted by 231 students.
Show answer & explanation
Correct answer: B
Final output: tance
Write the string with indices to locate characters: I n h e r i t a n c e
Indices: 0 1 2 3 4 5 6 7 8 9 10
S initially points to index 0 (character 'I').
S += 6 advances the pointer to index 6, which is the character 't'.
cout << S prints the string starting at that pointer until the null terminator, producing "tance".
Note: In modern C++ string literals have type const char[]. Assigning them to a non-const char* is deprecated or ill-formed in some compilers, but assuming the code compiles, the printed output is "tance".
A video solution is available for this question — log in and enroll to watch it.