Question 7
Back to Exercise
What is the output of the following C++ program on a 64-bit system?
#include <iostream>
#include <string>
int main() {
std::string s = "hello";
s[0] = 'j';
s.back() = 'y';
for (char& c : s)
if (c == 'l') c = 'L';
std::cout << s << '\n';
return 0;
}
- A jeLLy Correct
- B jelly
- C hello
- D JeLLy
Correct Answer: Option A
[] and back() return references to the characters, and the loop uses char&, so every change modifies s.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!