Challenge - 5 Problems
Operator Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of arithmetic operations using operators
What is the output of the following C++ code?
C++
#include <iostream> int main() { int a = 5, b = 3; std::cout << a + b << std::endl; std::cout << a - b << std::endl; std::cout << a * b << std::endl; std::cout << a / b << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Remember integer division truncates the decimal part.
✗ Incorrect
The operators +, -, *, and / perform addition, subtraction, multiplication, and integer division respectively. Since a and b are integers, division truncates the decimal part.
🧠 Conceptual
intermediate1:30remaining
Why operators simplify code
Why do we use operators like +, -, *, and / in programming languages?
Attempts:
2 left
💡 Hint
Think about how operators help when writing math in code.
✗ Incorrect
Operators provide a simple and natural way to write common operations like addition or multiplication, making code easier to write and understand.
❓ Predict Output
advanced2:30remaining
Output of operator overloading in C++
What is the output of this C++ code that overloads the + operator?
C++
#include <iostream> class Number { public: int value; Number(int v) : value(v) {} Number operator+(const Number& other) { return Number(value * other.value); } }; int main() { Number a(2), b(3); Number c = a + b; std::cout << c.value << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Check what the overloaded + operator actually does.
✗ Incorrect
The + operator is overloaded to multiply the values instead of adding. So 2 * 3 = 6 is returned.
🔧 Debug
advanced2:00remaining
Identify the error in operator usage
What error will this C++ code produce?
C++
#include <iostream> int main() { int x = 10; int y = 0; int z = x / y; std::cout << z << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Think about what happens if you divide by zero in C++.
✗ Incorrect
Dividing by zero is undefined behavior and causes a runtime error or crash.
🚀 Application
expert2:30remaining
Using operators to simplify complex expressions
Given the code below, what is the value of result after execution?
C++
#include <iostream> int main() { int a = 4, b = 2, c = 3; int result = a + b * c - b / a; std::cout << result << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Remember operator precedence: * and / before + and -.
✗ Incorrect
Multiplication and division happen before addition and subtraction. So b * c = 6, b / a = 0 (integer division). Then 4 + 6 - 0 = 10.