wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

C++ Functions Quiz

Total questions: 23

Worksheet time: 18mins

Name
Class
Date
1.

What is the main purpose of functions in C++?

a)

To execute loops faster

b)

To reduce code redundancy and improve reusability

c)

To replace the main() function

d)

To speed up compilation

2.

Which keyword defines a function that does not return a value?

a)

int

b)

return

c)

void

d)

float

3.

How do you call a function in C++?

a)

functionName;

b)

functionName()

c)

call functionName();

d)

define functionName();

4.

What is required in a function declaration?

a)

Function name and parameters

b)

Only the function body

c)

Function name, return type, and parameters (if any)

d)

Only the return type

5.

What will happen if a function is declared but not called?

a)

It will cause a compilation error

b)

It will execute automatically

c)

Nothing will happen

d)

The program will crash

6.

What is the default return type of a function in C++ if not specified?

a)

void

b)

int

c)

float

d)

char

7.

Which of the following statements about function parameters is true?

a)

A function can only have one parameter

b)

Parameters are mandatory in all functions

c)

Parameters act as variables inside the function

d)

Parameters must be initialized in the function declaration

8.

What is the correct way to define a function with two integer parameters?

a)

void function(int a, int b);

b)

function(int a, int b) {}

c)

int function(int a, int b) { return a + b; }

d)

void function() { int a, b; }

9.

What is "pass by reference" in functions?

a)

Passing values by copying them

b)

Allowing a function to modify the original variable

c)

Passing only integer values

d)

Calling a function multiple times

10.

Which function type returns a value?

a)

void function

b)

Function with no parameters

c)

Function with a return type

d)

Function with default parameters

11.

Which of the following is an example of function overloading?

a)

Two functions with the same name but different parameters

b)

A function calling another function

c)

A function using multiple loops

d)

A function with no return type

12.

Which statement about the return keyword is correct?

a)

It must always be used in functions

b)

It ends function execution and sends a value back

c)

It only works with int functions

d)

It can only be used once per function

13.

Which function prototype is correct?

a)

int sum(int x, y);

b)

void sum(int, int);

c)

function sum(int a, int b);

d)

int sum(int a) return a + 1;

14.

What happens if a function with void return type has a return statement with a value?

a)

Compilation error

b)

Function returns the value

c)

Undefined behavior

d)

The value is ignored

15.

What is required to pass an array as a function parameter?

a)

Specify the array size in the function definition

b)

Use the return keyword

c)

Declare the array inside the function

d)

Pass the array by reference or pointer

16.

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);

a)

1 1

b)

1 0

c)

0 1

d)

0 0

17.

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);

a)

9

b)

27

c)

81

d)

243

18.

What is the output of the following code: int check(int num) { if (num > 10) return 1; return 0; } cout << check(15) << " " << check(5);

a)

0 0

b)

1 1

c)

1 0

d)

0 1

19.

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);

a)

10 9 11

b)

13 11 14

c)

20 18 15

d)

12 10 13

20.

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));

a)

8

b)

16

c)

64

d)

256

21.

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);

a)

5

b)

7

c)

10

d)

12

22.

What is the output of the following code: void update(int &x) { x += 10; } int num = 5; update(num); cout << num;

a)

5

b)

10

c)

15

d)

Compilation error

23.

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!

4 lines