Test Your Knowledge (Ch 11)

The following questions are based on what we have learned so far in Chapter 11 – Functions in C. To test your understanding, try answering the following questions. The explanations are also provided to each question so you can always check the correctness of your answers. All the best 👍


Question 1

Which of the following best describes a function in programming?

A piece of code that performs a specific task and can be reused.
A set of variables stored in memory.
A command to start the execution of a program.
A way to print output on the screen.
explanation

A function is a block of code that is designed to perform a specific task.

Question 2

Which of the following is the correct way to define a function in C?

int functionName() {}
void functionName;
functionName()
void functionName {}
explanation

A function in C is defined using a return type, a function name, parentheses for parameters (even if empty), and a function body enclosed in curly braces.

Question 3

What is the purpose of a function prototype in C?

To define the body of the function.
To provide the function’s signature to the compiler.
To call the function.
To initialize the function.
explanation

A function prototype declares the function’s name, return type, and parameters to the compiler before its actual definition. This allows the function to be called before it is defined in the code.

Question 4

What will be the output of the following C code?

#include <stdio.h>

int fun(int x, int y)
{
    int i, res = 1;
    for (i = 1; i <= y; i++) {
        res *= x;
    }
    return res;
}

int main()
{
    int result = fun(8, 3);
    printf("%d", result);
    return 0;
}
6561
512
Error
24
explanation

The function returns 8 to the power 3 which is equal to 512.

Question 5

Which header file must be included to use the printf function in C?

math.h
stdlib.h
ioscreen.h
stdio.h
explanation

The printf function, which is used for output in C, is defined in the stdio.h (Standard Input/Output) header file.

Question 6

What is the key difference between call by value and call by reference?

Call by value modifies the actual arguments, while call by reference does not.
Call by reference passes a copy of the arguments to the function.
Call by value passes a copy of the arguments, while call by reference passes the actual arguments.
There is no difference between them.
explanation

In call by value, the function operates on a copy of the actual arguments, so changes do not affect the original value. In call by reference, the function operates on the actual arguments, so changes affect the original value.

Question 7

What is the effect of the static keyword on a variable inside a function?

The variable retains its value between function calls.
The variable can be accessed globally.
The variable is initialized only once.
First and third options are true.
explanation

A static variable inside a function retains its value between function calls and is initialized only once during the program’s execution.

Question 8

What is the difference between formal and actual parameters in C?

Formal parameters are used during the function definition, while actual parameters are used during the function call.
Actual parameters are used during the function definition, while formal parameters are used during the function call.
Both are used interchangeably.
None of the above.
explanation

Formal parameters are the variables defined in the function’s definition, whereas actual parameters are the values passed to the function during the call.

Question 9

What will be the output of the following C program?

void fun()
{
    static int a = 20;
    printf("%d ", a);
    a++;
}

int main()
{
    fun();
    fun();
    return 0;
}
10 11
20 20
20 21
21 22
explanation

The static variable ‘a’ inside the function fun() retains its value between function calls. The first call prints 20, and the second call prints 21 after incrementing a by 1.

Question 10

Determine the output of the following code:

void fun(int *n)
{
    *n = *n + 10;
}

int main()
{
    int a = 5;
    fun(&a);
    printf("%d", a);
    return 0;
}
5
10
15
Compilation error
explanation

The function fun() is working on the actual argument as the call to this function is by reference. Therefore, variable ‘a’ itself within the main() function is updated to 15 (as 5 + 10 = 15). Therefore, the output of the program is 15.

Your score is out of



Leave a comment

Leave a comment

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

Thank you for choosing to leave a comment. Please be aware that all comments are moderated in accordance with our policy. For more information, please review our comments policy. Rest assured that your email address will not be shared with anyone. Kindly refrain from using keywords in your comment. Let’s maintain a respectful atmosphere and engage in meaningful conversations.