Hack the Planet!

Test your C++ programming skills by solving the given puzzle on the working of the stack.

Puzzle code

Carefully read the code given below:

Press + to interact
#include <iostream>
int getUserId() { return 1337; }
void restrictedTask1()
{
int id = getUserId();
if (id == 1337) { std::cout << "did task 1\n"; }
}
void restrictedTask2()
{
int id;
if (id == 1337) { std::cout << "did task 2\n"; }
}
int main()
{
restrictedTask1();
restrictedTask2();
}

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)
did task 1
did task 2
B)
did task 1
C)

Undefined behavior

D)

Compilation 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.