UINT 3 LESSON 11 Functions Unit Test

1 answer

1. What is a function in programming?
a) A variable that stores data
b) A piece of code that performs a specific task
c) A loop that repeats a set of instructions
d) A condition that controls the flow of a program

2. Which keyword is used to define a function in C++?
a) def
b) function
c) define
d) void

3. What does the return statement do in a function?
a) Stops the function from executing
b) Returns a value from the function
c) Prints a message to the console
d) Skips a specific line of code

4. What is the output of the following function?
```cpp
int add(int a, int b) {
return a + b;
}

int result = add(5, 3);
cout << result;
```
a) 8
b) 15
c) 10
d) 3

5. Which of the following is a valid way to declare a function in C++?
a) int functionName {}
b) void functionName ()
c) functionName ()
d) double functionName {}

6. What is the purpose of function parameters?
a) To store local variables in the function
b) To define the return type of a function
c) To hold input values that can be used in the function
d) To control the flow of the program

7. What is the syntax for calling a function in C++?
a) functionName;
b) functionName();
c) functionName();
d) functionName;

8. What is the output of the following code snippet?
```cpp
void printMessage() {
cout << "Hello, World!";
}

int main() {
printMessage();
return 0;
}
```
a) "Hello, World!"
b) 0
c) No output
d) Compilation error

9. Which of the following is a valid function declaration in C++?
a) int add(int a, int b) {}
b) add(int a, int b) {}
c) void add(int a, int b);
d) int add(int a, int b);

10. What is the purpose of a function prototype?
a) To define the return type of a function
b) To declare a function before it is defined
c) To print a message to the console
d) To store local variables in the function

Answers:
1. b) A piece of code that performs a specific task
2. d) void
3. b) Returns a value from the function
4. a) 8
5. b) void functionName ()
6. c) To hold input values that can be used in the function
7. b) functionName();
8. a) "Hello, World!"
9. d) int add(int a, int b);
10. b) To declare a function before it is defined