Challenge - 5 Problems
Assignment Operator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of compound assignment with multiplication
What is the output of this C++ code snippet?
C++
#include <iostream> int main() { int a = 5; a *= 3 + 2; std::cout << a << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Remember operator precedence: multiplication assignment and addition.
✗ Incorrect
The expression 'a *= 3 + 2;' is evaluated as 'a *= (3 + 2);' because '+' has higher precedence than '*='. So, a = 5 * 5 = 25.
❓ Predict Output
intermediate2:00remaining
Result of chained assignment
What is the value of variable
c after running this code?C++
#include <iostream> int main() { int a, b, c; a = b = c = 7; c += 3; std::cout << c << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Look at the chained assignment and then the addition assignment.
✗ Incorrect
All variables a, b, and c are assigned 7. Then c is increased by 3, so c becomes 10.
❓ Predict Output
advanced2:00remaining
Effect of bitwise assignment operator
What is the output of this C++ program?
C++
#include <iostream> int main() { unsigned int x = 12; // binary 1100 x >>= 2; std::cout << x << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Right shift operator divides by powers of two.
✗ Incorrect
Right shifting 12 (1100 binary) by 2 bits results in 3 (0011 binary).
❓ Predict Output
advanced2:00remaining
Output of combined assignment with logical operators
What is the output of this code snippet?
C++
#include <iostream> int main() { int a = 5; a &= 3; std::cout << a << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Use bitwise AND on the binary forms of 5 and 3.
✗ Incorrect
5 in binary is 0101, 3 is 0011. Bitwise AND is 0001 which is 1.
🧠 Conceptual
expert2:00remaining
Understanding behavior of assignment in expressions
Consider this code snippet. What is the value of
y after execution?C++
#include <iostream> int main() { int x = 4; int y = (x += 3) * 2; std::cout << y << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Remember that assignment expressions return the assigned value.
✗ Incorrect
x += 3 changes x to 7 and returns 7. Then y = 7 * 2 = 14.