Challenge - 5 Problems
Function Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple function call
What is the output of this C++ program?
C++
#include <iostream> int add(int a, int b); int main() { std::cout << add(3, 4) << std::endl; return 0; } int add(int a, int b) { return a + b; }
Attempts:
2 left
💡 Hint
Look at what the function 'add' returns when called with 3 and 4.
✗ Incorrect
The function 'add' takes two integers and returns their sum. So add(3,4) returns 7, which is printed.
❓ Predict Output
intermediate2:00remaining
Function declaration without definition
What error will this C++ code produce when compiled?
C++
#include <iostream> int multiply(int x, int y); int main() { std::cout << multiply(2, 3) << std::endl; return 0; }
Attempts:
2 left
💡 Hint
The function is declared but not defined anywhere.
✗ Incorrect
The compiler knows the function signature but cannot find its body during linking, causing a linker error.
❓ Predict Output
advanced2:00remaining
Function overloading and output
What is the output of this C++ program?
C++
#include <iostream> int compute(int x) { return x * 2; } double compute(double x) { return x / 2.0; } int main() { std::cout << compute(10) << " " << compute(10.0) << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Look at which function is called for int and double arguments.
✗ Incorrect
compute(10) calls the int version returning 20; compute(10.0) calls the double version returning 5.0, printed as 5.
🔧 Debug
advanced2:00remaining
Identify the error in function definition
What error does this code produce?
C++
#include <iostream> void greet(); int main() { greet(); return 0; } void greet() { std::cout << "Hello, world!" << std::endl }
Attempts:
2 left
💡 Hint
Check the last line of the function definition.
✗ Incorrect
The function greet() is missing a semicolon after std::endl, causing a syntax error.
🧠 Conceptual
expert2:00remaining
Function declaration and definition order
Which statement is true about this code snippet?
C++
#include <iostream> int square(int x) { return x * x; } int main() { std::cout << square(5) << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Think about whether a function definition counts as a declaration.
✗ Incorrect
Defining a function before main acts as its declaration, so main can call it without a separate declaration.