A Strange Assignment

Test your C++ programming skills by solving the given puzzle about temporary materilization.

Puzzle code

Carefully read the code given below:

Press + to interact
#include <iostream>
#include <string>
std::string getName()
{
return "Alice";
}
int main()
{
std::string name{"Bob"};
getName() = name;
std::cout << "Assigned to a function!\n";
}

Your task: Guess the output

Try to guess the output of the above code. Attempt the following quiz to assess your understanding.

Q

What is the expected output of the above code?

A)

Assigned to a function!

B)

Undefined behavior

C)

Compilation error

D)

Runtime error

Let’s discuss this code and output together in the next lesson.

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.