C++ Functions MCQ

Functions in C++ are a fundamental building block of programs. They allow developers to compartmentalize their code, making it modular, reusable, and efficient. As you embark on your journey to master C++, understanding functions is essential.

In this quiz, we’ll be putting your knowledge of C++ functions to the test! Whether you’re a complete beginner or just looking for a refresher, give it a try. After each question, you’ll find the correct answer along with a concise explanation.

Let’s get started!

1. Which of the following defines a function in C++?

a) int funcName()
b) funcName: int
c) int funcName[]
d) int: funcName()

Answer:

a) int funcName()

Explanation:

Functions in C++ are defined by specifying a return type followed by the function name and parentheses.

2. What does the void keyword signify in a function declaration?

a) The function takes no arguments.
b) The function does not return a value.
c) The function can be overridden.
d) The function is virtual.

Answer:

b) The function does not return a value.

Explanation:

When void is used as the return type of a function, it means the function doesn’t return any value.

3. Which keyword is used to pass arguments by reference?

a) ref
b) byref
c) &
d) *

Answer:

c) &

Explanation:

In C++, the & symbol before a parameter in a function declaration indicates that the argument is passed by reference.

4. If a function does not include a return statement, which of the following return types is assumed?

a) int
b) char
c) void
d) float

Answer:

a) int

Explanation:

In older C++ standards, if a function did not have a return type specified, it was assumed to be int. However, modern C++ standards require specifying the return type, and not doing so will result in a compilation error.

5. What does the term “function overloading” mean?

a) A function that is too long.
b) Multiple functions with the same name but different parameters.
c) Calling a function too many times.
d) A function that performs too many operations.

Answer:

b) Multiple functions with the same name but different parameters.

Explanation:

Function overloading allows multiple functions with the same name but with different parameters, enabling the same function name to have different implementations based on its arguments.

6. What is the output of a recursive function that doesn’t have a base case?

a) It runs once.
b) It runs a fixed number of times.
c) It runs indefinitely or until a stack overflow occurs.
d) It results in a compilation error.

Answer:

c) It runs indefinitely or until a stack overflow occurs.

Explanation:

Without a base case, a recursive function will keep calling itself indefinitely. This will eventually lead to a stack overflow as the system runs out of memory for the function call stack.

7. Which keyword is used to specify a default value for a function parameter?

a) default
b) const
c) =
d) def

Answer:

c) =

Explanation:

In C++, the = symbol is used to specify a default value for a function parameter, allowing the function to be called without providing that specific argument.

8. What is the correct way to declare a function prototype?

a) int funcName();
b) funcName: int
c) int funcName[]
d) int: funcName()

Answer:

a) int funcName();

Explanation:

A function prototype in C++ is a declaration of a function that tells the compiler about the function name, return type, and parameters. It ends with a semicolon.

9. What is the purpose of the inline keyword before a function?

a) To make the function run faster by avoiding function-call overhead.
b) To force all calls to the function to be inline.
c) To make the function available across multiple files.
d) To indicate that the function should not be compiled.

Answer:

a) To make the function run faster by avoiding function-call overhead.

Explanation:

The inline keyword is a request to the compiler to inline-expand the function at the point of call, which can make it faster by eliminating the function-call overhead. However, it’s only a request, and the compiler can ignore it.

10. Which of the following is true regarding function arguments in C++?

a) Arguments are passed by value by default.
b) Arguments are passed by reference by default.
c) Arguments are passed by pointer by default.
d) None of the above.

Answer:

a) Arguments are passed by value by default.

Explanation:

By default, function arguments in C++ are passed by value, meaning that a copy of the data is passed into the function.

Great job making it through the quiz! Functions are a pivotal component of C++, and understanding them deeply will serve you well as you continue your programming journey. Don’t forget to revisit these concepts and practice frequently to make them second nature!


Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top