Unit 4: Functions - Practice Quiz

CAP1008 — C Programming 60 Questions
0 Correct 0 Wrong 60 Left
0/60

1 Which of the following is the correct syntax for a function definition in C?

function definition Easy
A. int sum(int a, int b) { return a + b; }
B. int sum = (int a, int b) { return a + b; }
C. function sum(int a, int b) { return a + b; }
D. int sum(int a, int b);

2 What are the two main parts of a function definition in C?

function definition Easy
A. Prototype and pointer
B. Header file and library
C. Argument and macro
D. Function header and function body

3 Functions like printf() and scanf() are examples of which type of functions?

predefined functions and user defined functions Easy
A. Recursive functions
B. Predefined (library) functions
C. Pointer functions
D. User defined functions

4 A function written by the programmer to perform a specific task is called a:

predefined functions and user defined functions Easy
A. Library function
B. System function
C. Predefined function
D. User defined function

5 Which header file must be included to use the sqrt() function in C?

predefined functions and user defined functions Easy
A. stdlib.h
B. stdio.h
C. string.h
D. math.h

6 A variable declared inside a function is known as a:

scope rules - local and global scope Easy
A. External variable
B. Static library variable
C. Global variable
D. Local variable

7 A global variable in C is accessible:

scope rules - local and global scope Easy
A. Only in the function where it is declared
B. Only inside loops
C. Only inside the main function
D. Throughout the entire program

8 Where should a global variable be declared in a C program?

scope rules - local and global scope Easy
A. Inside a loop body
B. Inside the main function
C. Inside the function header
D. Outside all functions

9 Which of the following correctly declares a pointer to an integer?

basics of pointers: pointer declaration Easy
A. int p*;
B. pointer int p;
C. int &p;
D. int *p;

10 What does a pointer variable store?

basics of pointers: pointer declaration Easy
A. A copy of a function
B. A constant value only
C. The memory address of another variable
D. The value of another variable

11 Which operator is used to get the address of a variable when initializing a pointer?

initialization Easy
A. # (hash operator)
B. * (dereference operator)
C. % (modulus operator)
D. & (address-of operator)

12 Given int x = 5; int *p = &x;, what does p hold?

initialization Easy
A. The address of x
B. The value 5
C. The value 0
D. A copy of x

13 Which operator is used to access the value stored at the address a pointer points to?

accessing values using pointers Easy
A. * (dereference operator)
B. . (dot operator)
C. & (address-of operator)
D. -> (arrow operator)

14 If int x = 10; int *p = &x;, what is the value of *p?

accessing values using pointers Easy
A. 0
B. The address of p
C. The address of x
D. 10

15 In call by value, changes made to the parameters inside the function:

passing arguments by value and passing arguments by address Easy
A. Delete the original arguments
B. Affect the original arguments
C. Do not affect the original arguments
D. Convert arguments to pointers

16 Which method allows a function to modify the caller's original variables?

passing arguments by value and passing arguments by address Easy
A. Passing arguments by value
B. Passing arguments by copy
C. Passing arguments by address
D. Passing arguments by name

17 When passing arguments by address, what is actually passed to the function?

passing arguments by value and passing arguments by address Easy
A. The memory address of the variable
B. A copy of the variable's value
C. The size of the variable
D. The name of the variable

18 Recursion is a process in which a function:

recursion Easy
A. Calls another function only
B. Never returns a value
C. Runs without parameters
D. Calls itself

19 What is essential in every recursive function to prevent infinite calls?

recursion Easy
A. A pointer declaration
B. A library function
C. A global variable
D. A base (terminating) condition

20 Which of the following is a standard string library function in C?

library functions Easy
A. printf()
B. scanf()
C. sqrt()
D. strlen()

21 Consider the following function definition:

C
int compute(int a, int b) {
    return a * b + a;
}



What value does compute(3, 4) return?

function definition Medium
A.
B.
C.
D.

22 Which of the following correctly describes the components of a C function definition?

function definition Medium
A. Return type, function name, parameter list, and function body
B. Function name, arguments, and semicolon only
C. Only the function body enclosed in braces
D. Return type and function name only

23 Which statement best distinguishes a user-defined function from a predefined (library) function in C?

predefined functions and user defined functions Medium
A. A predefined function must be written in the main() function
B. A user-defined function cannot return a value while a predefined function always returns a value
C. A user-defined function does not need a return type
D. A user-defined function is written by the programmer, while a predefined function is supplied by C libraries and accessed by including the relevant header file such as stdio.h or math.h

24 To use the pow() function in a C program, which header file must be included?

predefined functions and user defined functions Medium
A. math.h
B. stdlib.h
C. string.h
D. stdio.h

25 What is the output of the following program?

C
#include <stdio.h>
int x = 10;
void show() {
    int x = 20;
    printf("%d", x);
}
int main() {
    show();
    return 0;
}

scope rules - local and global scope Medium
A.
B.
C.
D. Compilation error

26 Which statement about local and global scope in C is correct?

scope rules - local and global scope Medium
A. A global variable can only be accessed inside the function where it is defined
B. A local variable retains its value across different function calls by default
C. A local variable exists only within the block where it is declared, while a global variable is accessible throughout the entire program after its declaration
D. Global variables are automatically destroyed when a function returns

27 What is the output of this program?

C
#include <stdio.h>
int count = 5;
void update() {
    count = count + 10;
}
int main() {
    update();
    printf("%d", count);
    return 0;
}

scope rules - local and global scope Medium
A.
B.
C.
D.

28 Given the declarations int a = 25; int *p = &a;, what does *p evaluate to?

basics of pointers: pointer declaration, initialization, accessing values using pointers Medium
A. A garbage value
B. The address of a
C.
D. The address of p

29 What is the output of the following code?

C
#include <stdio.h>
int main() {
    int x = 7;
    int *p = &x;
    *p = *p + 3;
    printf("%d", x);
    return 0;
}

basics of pointers: pointer declaration, initialization, accessing values using pointers Medium
A.
B.
C.
D. Address of x

30 Which of the following correctly declares a pointer to an integer and initializes it with the address of variable n?

basics of pointers: pointer declaration, initialization, accessing values using pointers Medium
A. int *p = &n;
B. int *p = n;
C. int &p = *n;
D. int p = *n;

31 What is the output of the following program?

C
#include <stdio.h>
void change(int a) {
    a = a + 5;
}
int main() {
    int x = 10;
    change(x);
    printf("%d", x);
    return 0;
}

passing arguments by value and passing arguments by address Medium
A.
B.
C.
D.

32 What is the output of the following program?

C
#include <stdio.h>
void change(int *a) {
    *a = *a + 5;
}
int main() {
    int x = 10;
    change(&x);
    printf("%d", x);
    return 0;
}

passing arguments by value and passing arguments by address Medium
A.
B.
C.
D. Compilation error

33 Which technique should be used to write a swap function that successfully exchanges the values of two variables from the caller?

passing arguments by value and passing arguments by address Medium
A. Returning both values using a single return statement
B. Passing the variables by value into the function
C. Passing the addresses of the variables so the function can dereference the pointers and modify the original memory locations directly
D. Declaring the variables as local inside the swap function

34 What does the following recursive function return for fun(4)?

C
int fun(int n) {
    if (n == 0) return 0;
    return n + fun(n - 1);
}

recursion Medium
A.
B.
C.
D.

35 What is the output of fact(5) for the following function?

C
int fact(int n) {
    if (n <= 1) return 1;
    return n * fact(n - 1);
}

recursion Medium
A.
B.
C.
D.

36 What happens if a recursive function does not have a proper base case?

recursion Medium
A. The compiler automatically adds a base case
B. It leads to infinite recursion, eventually causing a stack overflow because function calls keep getting pushed onto the call stack without ever returning
C. It executes exactly once and stops
D. It returns zero by default

37 For the recursive function below, how many times is display called (including the initial call) when invoked as display(3)?

C
void display(int n) {
    if (n == 0) return;
    printf("%d", n);
    display(n - 1);
}

recursion Medium
A.
B.
C.
D.

38 Which library function is used to determine the length of a string (excluding the null terminator) in C?

library functions Medium
A. sizeof()
B. strcpy()
C. strcat()
D. strlen()

39 What is the return value of sqrt(16.0) from the math library?

library functions Medium
A.
B.
C.
D.

40 Which library function converts a string such as "123" into its integer equivalent 123?

library functions Medium
A. strcmp()
B. gets()
C. itoa()
D. atoi()

41 Consider the following function:

C
int f(int n) {
    if (n == 0) return 0;
    return n + f(n - 1);
}



What is the maximum recursion depth (number of active stack frames including the initial call) when f(5) is invoked?

recursion Hard
A. 6
B. 15
C. 5
D. 7

42 What does the following program print?

C
#include <stdio.h>
void swap(int *a, int b) {
    int t = *a; *a = b; b = t;
}
int main() {
    int x = 3, y = 7;
    swap(&x, y);
    printf("%d %d", x, y);
}

passing arguments by value and passing arguments by address Hard
A. 3 3
B. 7 3
C. 3 7
D. 7 7

43 What is the output of this program?

C
#include <stdio.h>
int x = 10;
void f() { x += 5; }
int main() {
    int x = 20;
    f();
    { int x = 30; printf("%d ", x); }
    printf("%d", x);
}

scope rules - local and global scope Hard
A. 30 25
B. 30 20
C. 35 20
D. 15 20

44 Given int a[] = {10, 20, 30, 40}; int *p = a + 1;, what is the value of *(p + 1) + *(a + 2)?

basics of pointers: pointer declaration, initialization, accessing values using pointers Hard
A. 60
B. 40
C. 50
D. 70

45 What does mystery(4) return?

C
int mystery(int n) {
    if (n <= 1) return 1;
    return mystery(n - 1) + mystery(n - 2);
}

recursion Hard
A. 4
B. 8
C. 3
D. 5

46 What is printed by the following code?

C
#include <stdio.h>
int main() {
    int x = 5;
    int *p = &x;
    int **q = &p;
    **q = **q + 10;
    printf("%d", x);
}

basics of pointers: pointer declaration, initialization, accessing values using pointers Hard
A. 15
B. Undefined
C. 5
D. 10

47 What is the output of this program using a static local variable?

C
#include <stdio.h>
int counter() {
    static int c = 0;
    c++;
    return c;
}
int main() {
    printf("%d%d%d", counter(), counter(), counter());
}

scope rules - local and global scope Hard
A. 111
B. Implementation-defined ordering of 1, 2, 3
C. 123
D. 333

48 What is the output?

C
#include <stdio.h>
void change(int *p) {
    p = p + 1;
    *p = 99;
}
int main() {
    int a[3] = {1, 2, 3};
    change(a);
    printf("%d %d %d", a[0], a[1], a[2]);
}

passing arguments by value and passing arguments by address Hard
A. 1 99 3
B. 1 2 3
C. 1 2 99
D. 99 2 3

49 What does the following code print?

C
#include <stdio.h>
#include <string.h>
int main() {
    char s[20] = "Hello";
    strcat(s, "World");
    printf("%zu", strlen(s));
}

library functions Hard
A. 11
B. 10
C. 9
D. 5

50 For the function below, what is f(6)?

C
int f(int n) {
    if (n == 0) return 1;
    if (n % 2 == 0) return f(n - 1);
    return 2 * f(n - 1);
}

recursion Hard
A. 16
B. 6
C. 8
D. 12

51 In C, what happens if a function is called before any declaration or definition is visible, and it returns a double, but is implicitly assumed to return int?

function definition Hard
A. The program always crashes at compile time
B. The return value is misinterpreted, causing incorrect results or undefined behavior
C. The return value is silently truncated to a valid int
D. The compiler automatically infers the double return type

52 Which statement best distinguishes a predefined (library) function from a user-defined function in C?

predefined functions and user defined functions Hard
A. A library function runs faster because it is written in the same source file
B. A library function cannot be called recursively, while a user-defined function can
C. A library function needs no header, while a user-defined function always needs a header
D. A library function's definition resides in precompiled libraries linked at build time, while a user-defined function is compiled from source in the program

53 Which declaration correctly defines p as a pointer to a function that takes an int and returns an int?

basics of pointers: pointer declaration, initialization, accessing values using pointers Hard
A. int (*p(int));
B. int *(p)(int);
C. int (*p)(int);
D. int *p(int);

54 How many times is f called in total (including the initial call) when f(3) runs?

C
void f(int n) {
    if (n <= 0) return;
    f(n - 1);
    f(n - 1);
}

recursion Hard
A. 16
B. 8
C. 15
D. 7

55 What is the output?

C
#include <stdio.h>
void modify(int *pp) {
    static int val = 100;
    pp = &val;
    *pp = 200;
}
int main() {
    int a = 5;
    int *p = &a;
    modify(p);
    printf("%d", *p);
}

passing arguments by value and passing arguments by address Hard
A. Undefined
B. 5
C. 200
D. 100

56 What does this program output?

C
#include <stdio.h>
int x = 1;
int f() { return x; }
int main() {
    int x = 2;
    { extern int x; printf("%d ", x); }
    printf("%d", f());
}

scope rules - local and global scope Hard
A. 1 1
B. 1 2
C. 2 1
D. 2 2

57 What is printed?

C
#include <stdio.h>
#include <stdlib.h>
int main() {
    printf("%d %d", abs(-7), (int)strtol("1010", NULL, 2));
}

library functions Hard
A. -7 10
B. 7 10
C. 7 2
D. 7 1010

58 Given char *s = "ABCD";, what does *(s + 2) - *s evaluate to?

basics of pointers: pointer declaration, initialization, accessing values using pointers Hard
A. 67
B. 'C'
C. 2
D. 'C' - 'A'

59 What is the effect of defining int f(void) versus int f() in C?

function definition Hard
A. f() takes no parameters, while f(void) accepts any arguments
B. f(void) specifies the function takes no parameters, while f() leaves the parameter list unspecified
C. Both forms are identical in every respect
D. f(void) is invalid C syntax

60 What does g(1234) print?

C
void g(int n) {
    if (n == 0) return;
    g(n / 10);
    printf("%d", n % 10);
}

recursion Hard
A. 1234
B. 4321
C. 10
D. 1