LearnFrenzy
Sign in Take a free test

Functions and Default Arguments · Exercise 1 of 4

Functions and Default Arguments Functions MCQ

  • 20 questions
  • Page 1 of 2
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

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.

Question 1 Easy

Which line is a function declaration but not a definition?

A int max(int a, int b) { return a > b ? a : b; }
B int max(int a, int b);
C max(3, 4);
D auto m = max;
Question page & discussion
Question 2 Easy

A function void f(int x) { x = 0; } is called as f(n);. What happens to n?

A n becomes 0
B n is unchanged – x is a copy
C It does not compile
D n becomes undefined
Question page & discussion
Question 3 Easy

Why is void print(const std::string& s) preferred to void print(std::string s)?

A It allows modifying s
B It avoids copying the string while preventing modification, and still accepts temporaries
C It is required for strings
D It makes the function inline
Question page & discussion
Question 4 Medium

Given void inc(int& x);, which call is valid?

A inc(5);
B int n = 5; inc(n);
C inc(n + 1);
D inc(5.0);
Question page & discussion
Question 5 Easy

Which declaration is valid?

A void f(int a = 1, int b);
B void f(int a, int b = 2, int c);
C void f(int a, int b = 2, int c = 3);
D void f(int a = 1, int b, int c = 3);
Question page & discussion
Question 6 Easy

Can int read(); and double read(); coexist as overloads?

A Yes
B No – overloads must differ in their parameter lists
C Only in classes
D Only if one is inline
Question page & discussion
Question 7 Easy

Function overloading is an example of…

A Run-time polymorphism
B Compile-time (static) polymorphism
C Inheritance
D Encapsulation
Question page & discussion
Question 8 Medium

With void f(int); and void f(double);, which one does f('A') call?

A f(double)
B f(int) – char to int is a promotion, better than a conversion to double
C Ambiguous – compile error
D Neither
Question page & discussion
Question 9 Medium

Why does f(2.5) fail with void f(int); and void f(long);?

A double cannot be converted to integers
B double → int and double → long are both standard conversions of the same rank, so neither overload is better
C f must be inline
D long is not a valid parameter type
Question page & discussion
Question 10 Easy

What happens when a recursive function never reaches a base case?

A It returns 0
B It keeps calling itself until the call stack overflows and the program crashes
C The compiler adds a base case
D It loops quietly forever without using memory
Question page & discussion

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links