Which line is a function declaration but not a definition?
A declaration ends with a semicolon instead of a body. It tells the compiler how to call the function; the definition can come later or in another file.
Functions and Default Arguments · Exercise 1 of 4
Twenty multiple-choice questions on C++ functions: prototypes, parameter passing, default arguments, overloading, recursion, static locals, inline, constexpr, noexcept and std::function. Pick an option to check your answer instantly.
Which line is a function declaration but not a definition?
A declaration ends with a semicolon instead of a body. It tells the compiler how to call the function; the definition can come later or in another file.
A function void f(int x) { x = 0; } is called as f(n);. What happens to n?
Pass by value copies the argument into the parameter. Declare int& x to work on the caller's variable.
Why is void print(const std::string& s) preferred to void print(std::string s)?
Copying a long string allocates memory. A const reference reads the caller's string directly and safely.
Given void inc(int& x);, which call is valid?
A non-const lvalue reference binds only to a modifiable variable. Literals and expression results are temporaries; they bind to const int& but not int&.
Which declaration is valid?
Once a parameter has a default, all parameters to its right need defaults too, because arguments are matched from the left.
Can int read(); and double read(); coexist as overloads?
A call such as read() gives the compiler no information about which return type is wanted, so the return type is not part of overload resolution.
Function overloading is an example of…
The compiler chooses the overload while compiling. Virtual functions provide run-time polymorphism.
With void f(int); and void f(double);, which one does f('A') call?
Promotions (char/short/bool → int, float → double) rank above standard conversions such as char → double.
Why does f(2.5) fail with void f(int); and void f(long);?
When the best candidates tie, the call is ambiguous. Add an f(double) overload or cast the argument.
What happens when a recursive function never reaches a base case?
Every call uses a new stack frame, so unbounded recursion exhausts the stack (stack overflow).
Timed online tests with instant scores and explanations.