Font size
WorksheetsC++ Functions Quiz
Total questions: 23
Worksheet time: 18mins
What is the main purpose of functions in C++?
To execute loops faster
To reduce code redundancy and improve reusability
To replace the main() function
To speed up compilation
Which keyword defines a function that does not return a value?
int
return
void
float
How do you call a function in C++?
functionName;
functionName()
call functionName();
define functionName();
What is required in a function declaration?
Function name and parameters
Only the function body
Function name, return type, and parameters (if any)
Only the return type
What will happen if a function is declared but not called?
It will cause a compilation error
It will execute automatically
Nothing will happen
The program will crash
What is the default return type of a function in C++ if not specified?
void
int
float
char
Which of the following statements about function parameters is true?
A function can only have one parameter
Parameters are mandatory in all functions
Parameters act as variables inside the function
Parameters must be initialized in the function declaration
What is the correct way to define a function with two integer parameters?
void function(int a, int b);
function(int a, int b) {}
int function(int a, int b) { return a + b; }
void function() { int a, b; }
What is "pass by reference" in functions?
Passing values by copying them
Allowing a function to modify the original variable
Passing only integer values
Calling a function multiple times
Which function type returns a value?
void function
Function with no parameters
Function with a return type
Function with default parameters
Which of the following is an example of function overloading?
Two functions with the same name but different parameters
A function calling another function
A function using multiple loops
A function with no return type
Which statement about the return keyword is correct?
It must always be used in functions
It ends function execution and sends a value back
It only works with int functions
It can only be used once per function
Which function prototype is correct?
int sum(int x, y);
void sum(int, int);
function sum(int a, int b);
int sum(int a) return a + 1;
What happens if a function with void return type has a return statement with a value?
Compilation error
Function returns the value
Undefined behavior
The value is ignored
What is required to pass an array as a function parameter?
Specify the array size in the function definition
Use the return keyword
Declare the array inside the function
Pass the array by reference or pointer
What is the output of the following code: bool isDivisible(int x, int y) { return (x % y == 0); } cout << isDivisible(10, 5) << " " << isDivisible(10, 3);
1 1
1 0
0 1
0 0
What is the output of the following code: int power(int base, int exp) { int result = 1; for (int i = 0; i < exp; i++) { result *= base; } return result; } cout << power(3, 3);
9
27
81
243
What is the output of the following code: int check(int num) { if (num > 10) return 1; return 0; } cout << check(15) << " " << check(5);
0 0
1 1
1 0
0 1
What is the output of the following code: int compute(int a, int b = 4, int c = 2) { return a + b * c; } cout << compute(5) << " " << compute(5, 3) << " " << compute(5, 3, 3);
10 9 11
13 11 14
20 18 15
12 10 13
What is the output of the following code: int square(int n) { return n * n; } int cube(int n) { return n * n * n; } cout << square(cube(2));
8
16
64
256
What is the output of the following code: int multiply(int a, int b) { return a * b; } int add(int a, int b) { return a + b; } cout << multiply(add(2, 3), 2);
5
7
10
12
What is the output of the following code: void update(int &x) { x += 10; } int num = 5; update(num); cout << num;
5
10
15
Compilation error
Develop a C++ program that checks if a person is eligible to vote. Implement a function that takes an integer representing a person's age as input and prints whether they are eligible to vote (18 years or older). Requirements: Implement a function void checkEligibility(int age). If the age is 18 or above, print "You are eligible to vote!" Otherwise, print "You are NOT eligible to vote!" Example Input/Output: Enter your age: 16 You are NOT eligible to vote!
