Unit 4: Functions - Practice Quiz
1 Which of the following is the correct syntax for a function definition in C?
int sum(int a, int b) { return a + b; }
int sum = (int a, int b) { return a + b; }
function sum(int a, int b) { return a + b; }
int sum(int a, int b);
2 What are the two main parts of a function definition in C?
3
Functions like printf() and scanf() are examples of which type of functions?
4 A function written by the programmer to perform a specific task is called a:
5
Which header file must be included to use the sqrt() function in C?
stdlib.h
stdio.h
string.h
math.h
6 A variable declared inside a function is known as a:
7 A global variable in C is accessible:
8 Where should a global variable be declared in a C program?
9 Which of the following correctly declares a pointer to an integer?
int p*;
pointer int p;
int &p;
int *p;
10 What does a pointer variable store?
11 Which operator is used to get the address of a variable when initializing a pointer?
# (hash operator)
* (dereference operator)
% (modulus operator)
& (address-of operator)
12
Given int x = 5; int *p = &x;, what does p hold?
x
5
0
x
13 Which operator is used to access the value stored at the address a pointer points to?
* (dereference operator)
. (dot operator)
& (address-of operator)
-> (arrow operator)
14
If int x = 10; int *p = &x;, what is the value of *p?
p
x
15 In call by value, changes made to the parameters inside the function:
16 Which method allows a function to modify the caller's original variables?
17 When passing arguments by address, what is actually passed to the function?
18 Recursion is a process in which a function:
19 What is essential in every recursive function to prevent infinite calls?
20 Which of the following is a standard string library function in C?
printf()
scanf()
sqrt()
strlen()
21
Consider the following function definition:
int compute(int a, int b) {
return a * b + a;
}
What value does compute(3, 4) return?
22 Which of the following correctly describes the components of a C function definition?
23 Which statement best distinguishes a user-defined function from a predefined (library) function in C?
main() function
stdio.h or math.h
24
To use the pow() function in a C program, which header file must be included?
math.h
stdlib.h
string.h
stdio.h
25
What is the output of the following program?
#include <stdio.h>
int x = 10;
void show() {
int x = 20;
printf("%d", x);
}
int main() {
show();
return 0;
}
26 Which statement about local and global scope in C is correct?
27
What is the output of this program?
#include <stdio.h>
int count = 5;
void update() {
count = count + 10;
}
int main() {
update();
printf("%d", count);
return 0;
}
28
Given the declarations int a = 25; int *p = &a;, what does *p evaluate to?
a
p
29
What is the output of the following code?
#include <stdio.h>
int main() {
int x = 7;
int *p = &x;
*p = *p + 3;
printf("%d", x);
return 0;
}
x
30
Which of the following correctly declares a pointer to an integer and initializes it with the address of variable n?
int *p = &n;
int *p = n;
int &p = *n;
int p = *n;
31
What is the output of the following program?
#include <stdio.h>
void change(int a) {
a = a + 5;
}
int main() {
int x = 10;
change(x);
printf("%d", x);
return 0;
}
32
What is the output of the following program?
#include <stdio.h>
void change(int *a) {
*a = *a + 5;
}
int main() {
int x = 10;
change(&x);
printf("%d", x);
return 0;
}
33
Which technique should be used to write a swap function that successfully exchanges the values of two variables from the caller?
34
What does the following recursive function return for fun(4)?
int fun(int n) {
if (n == 0) return 0;
return n + fun(n - 1);
}
35
What is the output of fact(5) for the following function?
int fact(int n) {
if (n <= 1) return 1;
return n * fact(n - 1);
}
36 What happens if a recursive function does not have a proper base case?
37
For the recursive function below, how many times is display called (including the initial call) when invoked as display(3)?
void display(int n) {
if (n == 0) return;
printf("%d", n);
display(n - 1);
}
38 Which library function is used to determine the length of a string (excluding the null terminator) in C?
sizeof()
strcpy()
strcat()
strlen()
39
What is the return value of sqrt(16.0) from the math library?
40
Which library function converts a string such as "123" into its integer equivalent 123?
strcmp()
gets()
itoa()
atoi()
41
Consider the following function:
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?
42
What does the following program print?
#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);
}
43
What is the output of this program?
#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);
}
44
Given int a[] = {10, 20, 30, 40}; int *p = a + 1;, what is the value of *(p + 1) + *(a + 2)?
45
What does mystery(4) return?
int mystery(int n) {
if (n <= 1) return 1;
return mystery(n - 1) + mystery(n - 2);
}
46
What is printed by the following code?
#include <stdio.h>
int main() {
int x = 5;
int *p = &x;
int **q = &p;
**q = **q + 10;
printf("%d", x);
}
47
What is the output of this program using a static local variable?
#include <stdio.h>
int counter() {
static int c = 0;
c++;
return c;
}
int main() {
printf("%d%d%d", counter(), counter(), counter());
}
48
What is the output?
#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]);
}
49
What does the following code print?
#include <stdio.h>
#include <string.h>
int main() {
char s[20] = "Hello";
strcat(s, "World");
printf("%zu", strlen(s));
}
50
For the function below, what is f(6)?
int f(int n) {
if (n == 0) return 1;
if (n % 2 == 0) return f(n - 1);
return 2 * f(n - 1);
}
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?
52 Which statement best distinguishes a predefined (library) function from a user-defined function in C?
53
Which declaration correctly defines p as a pointer to a function that takes an int and returns an int?
54
How many times is f called in total (including the initial call) when f(3) runs?
void f(int n) {
if (n <= 0) return;
f(n - 1);
f(n - 1);
}
55
What is the output?
#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);
}
56
What does this program output?
#include <stdio.h>
int x = 1;
int f() { return x; }
int main() {
int x = 2;
{ extern int x; printf("%d ", x); }
printf("%d", f());
}
57
What is printed?
#include <stdio.h>
#include <stdlib.h>
int main() {
printf("%d %d", abs(-7), (int)strtol("1010", NULL, 2));
}
58
Given char *s = "ABCD";, what does *(s + 2) - *s evaluate to?
59
What is the effect of defining int f(void) versus int f() in C?
f() takes no parameters, while f(void) accepts any arguments
f(void) specifies the function takes no parameters, while f() leaves the parameter list unspecified
f(void) is invalid C syntax
60
What does g(1234) print?
void g(int n) {
if (n == 0) return;
g(n / 10);
printf("%d", n % 10);
}
Did this save you a night before the exam?
LPU Notes is free, and it stays free. Ads cover part of the server bill. The rest comes out of a student's own pocket: the domain, the storage, and keeping the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason. to try again.
Nothing here is ever locked, and nothing unlocks. Chip in only if it was worth it. What it pays for →