Notice: Due to an ongoing migration of our storage and publishing system, material requested since last month will not be uploaded for the time being, and new requests are paused. All existing notes remain available.
Which of the following is the correct syntax for a function definition in C?
Not quite. Try another option.
Answer: int sum(int a, int b) { return a + b; }
A function definition includes the return type, name, parameter list, and a body enclosed in braces. The second option is only a declaration (prototype).
Question 2 of 60 · function definition · Easy
What are the two main parts of a function definition in C?
Not quite. Try another option.
Answer: Function header and function body
A function definition consists of a header (return type, name, parameters) and a body (the block of statements inside braces).
Question 3 of 60 · predefined functions and user defined functions · Easy
Functions like printf() and scanf() are examples of which type of functions?
Not quite. Try another option.
Answer: Predefined (library) functions
printf() and scanf() are predefined library functions provided by the C standard library, declared in stdio.h.
Question 4 of 60 · predefined functions and user defined functions · Easy
A function written by the programmer to perform a specific task is called a:
Not quite. Try another option.
Answer: User defined function
Functions created by the programmer according to their own requirements are called user defined functions.
Question 5 of 60 · predefined functions and user defined functions · Easy
Which header file must be included to use the sqrt() function in C?
Not quite. Try another option.
Answer: math.h
The sqrt() function is a math library function and requires the math.h header file to be included.
Question 6 of 60 · scope rules - local and global scope · Easy
A variable declared inside a function is known as a:
Not quite. Try another option.
Answer: Local variable
A variable declared inside a function has local scope and can only be accessed within that function.
Question 7 of 60 · scope rules - local and global scope · Easy
A global variable in C is accessible:
Not quite. Try another option.
Answer: Throughout the entire program
A global variable is declared outside all functions and is accessible from any function in the program.
Question 8 of 60 · scope rules - local and global scope · Easy
Where should a global variable be declared in a C program?
Not quite. Try another option.
Answer: Outside all functions
Global variables are declared outside of every function, typically at the top of the program, giving them global scope.
Question 9 of 60 · basics of pointers: pointer declaration · Easy
Which of the following correctly declares a pointer to an integer?
Not quite. Try another option.
Answer: int *p;
A pointer is declared by placing an asterisk (*) before the variable name, as in int *p;.
Question 10 of 60 · basics of pointers: pointer declaration · Easy
What does a pointer variable store?
Not quite. Try another option.
Answer: The memory address of another variable
A pointer is a variable that stores the memory address of another variable rather than a direct data value.
Question 11 of 60 · initialization · Easy
Which operator is used to get the address of a variable when initializing a pointer?
Not quite. Try another option.
Answer: & (address-of operator)
The & operator returns the memory address of a variable, for example p = &x;.
Question 12 of 60 · initialization · Easy
Given int x = 5; int *p = &x;, what does p hold?
Not quite. Try another option.
Answer: The address of x
The statement p = &x; stores the address of x in the pointer p, not the value of x.
Question 13 of 60 · accessing values using pointers · Easy
Which operator is used to access the value stored at the address a pointer points to?
Not quite. Try another option.
Answer: * (dereference operator)
The * (dereference) operator accesses the value stored at the memory address held by a pointer, for example *p.
Question 14 of 60 · accessing values using pointers · Easy
If int x = 10; int *p = &x;, what is the value of *p?
Not quite. Try another option.
Answer: 10
*p dereferences the pointer to give the value stored at the address it points to, which is the value of x, i.e. 10.
Question 15 of 60 · passing arguments by value and passing arguments by address · Easy
In call by value, changes made to the parameters inside the function:
Not quite. Try another option.
Answer: Do not affect the original arguments
In call by value, a copy of the argument is passed, so modifications inside the function do not change the original variable.
Question 16 of 60 · passing arguments by value and passing arguments by address · Easy
Which method allows a function to modify the caller's original variables?
Not quite. Try another option.
Answer: Passing arguments by address
Passing arguments by address (using pointers) lets the function access and modify the original variables directly.
Question 17 of 60 · passing arguments by value and passing arguments by address · Easy
When passing arguments by address, what is actually passed to the function?
Not quite. Try another option.
Answer: The memory address of the variable
In call by address, the memory address of the argument is passed, allowing the function to work with the original data.
Question 18 of 60 · recursion · Easy
Recursion is a process in which a function:
Not quite. Try another option.
Answer: Calls itself
Recursion occurs when a function calls itself directly or indirectly to solve a problem.
Question 19 of 60 · recursion · Easy
What is essential in every recursive function to prevent infinite calls?
Not quite. Try another option.
Answer: A base (terminating) condition
A base condition stops the recursion; without it, the function would call itself endlessly and cause a stack overflow.
Question 20 of 60 · library functions · Easy
Which of the following is a standard string library function in C?
Not quite. Try another option.
Answer: strlen()
strlen() is a string handling library function declared in string.h that returns the length of a string.
Question 21 of 60 · function definition · Medium
Consider the following function definition:
C
int compute(int a, int b) {
return a * b + a;
}
What value does compute(3, 4) return?
Not quite. Try another option.
Answer:
The expression evaluates as a * b + a = 3 * 4 + 3 = 12 + 3 = 15.
Question 22 of 60 · function definition · Medium
Which of the following correctly describes the components of a C function definition?
Not quite. Try another option.
Answer: Return type, function name, parameter list, and function body
A function definition consists of the return type, the function name, a parameter list in parentheses, and the body enclosed in braces.
Question 23 of 60 · predefined functions and user defined functions · Medium
Which statement best distinguishes a user-defined function from a predefined (library) function in C?
Not quite. Try another option.
Answer: 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
User-defined functions are created by the programmer for specific tasks, whereas predefined functions like printf and sqrt come from standard libraries via header files.
Question 24 of 60 · predefined functions and user defined functions · Medium
To use the pow() function in a C program, which header file must be included?
Not quite. Try another option.
Answer: math.h
The pow() function is declared in math.h, which contains mathematical library functions.
Question 25 of 60 · scope rules - local and global scope · Medium
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;
}
Not quite. Try another option.
Answer:
The local variable x inside show() shadows the global x, so the local value 20 is printed.
Question 26 of 60 · scope rules - local and global scope · Medium
Which statement about local and global scope in C is correct?
Not quite. Try another option.
Answer: 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
Local variables have block scope and are limited to their block, while global variables declared outside all functions are visible program-wide.
Question 27 of 60 · scope rules - local and global scope · Medium
count is global, so update() modifies the same variable, changing it from 5 to 15, which main() prints.
Question 28 of 60 · basics of pointers: pointer declaration, initialization, accessing values using pointers · Medium
Given the declarations int a = 25; int *p = &a;, what does *p evaluate to?
Not quite. Try another option.
Answer:
p holds the address of a, and the dereference operator *p accesses the value stored at that address, which is 25.
Question 29 of 60 · basics of pointers: pointer declaration, initialization, accessing values using pointers · Medium
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;
}
Not quite. Try another option.
Answer:
p points to x, so *p = *p + 3 updates the value at x from 7 to 10.
Question 30 of 60 · basics of pointers: pointer declaration, initialization, accessing values using pointers · Medium
Which of the following correctly declares a pointer to an integer and initializes it with the address of variable n?
Not quite. Try another option.
Answer: int *p = &n;
int *p declares an integer pointer, and &n provides the address of n for initialization.
Question 31 of 60 · passing arguments by value and passing arguments by address · Medium
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;
}
Not quite. Try another option.
Answer:
In call by value, a copy of x is passed. Changes to a inside the function do not affect the original x, which stays 10.
Question 32 of 60 · passing arguments by value and passing arguments by address · Medium
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;
}
Not quite. Try another option.
Answer:
In call by address, the function receives the address of x and modifies it directly, so x becomes 15.
Question 33 of 60 · passing arguments by value and passing arguments by address · Medium
Which technique should be used to write a swap function that successfully exchanges the values of two variables from the caller?
Not quite. Try another option.
Answer: Passing the addresses of the variables so the function can dereference the pointers and modify the original memory locations directly
Passing by value only copies the data, so a working swap requires call by address (pointers) to alter the caller's original variables.
Question 34 of 60 · recursion · Medium
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);
}
Not quite. Try another option.
Answer:
The function sums 4 + 3 + 2 + 1 + 0 = 10.
Question 35 of 60 · recursion · Medium
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);
}
Not quite. Try another option.
Answer:
This computes the factorial: .
Question 36 of 60 · recursion · Medium
What happens if a recursive function does not have a proper base case?
Not quite. Try another option.
Answer: It leads to infinite recursion, eventually causing a stack overflow because function calls keep getting pushed onto the call stack without ever returning
Without a base case, recursion never terminates, and repeated stack frame allocation exhausts the stack, causing a stack overflow.
Question 37 of 60 · recursion · Medium
For the recursive function below, how many times is display called (including the initial call) when invoked as display(3)?
Calls occur for n = 3, 2, 1, 0. The n = 0 call returns immediately, giving 4 total calls.
Question 38 of 60 · library functions · Medium
Which library function is used to determine the length of a string (excluding the null terminator) in C?
Not quite. Try another option.
Answer: strlen()
strlen() from string.h returns the number of characters before the null terminator \0.
Question 39 of 60 · library functions · Medium
What is the return value of sqrt(16.0) from the math library?
Not quite. Try another option.
Answer:
sqrt() returns the square root of its argument, and .
Question 40 of 60 · library functions · Medium
Which library function converts a string such as "123" into its integer equivalent 123?
Not quite. Try another option.
Answer: atoi()
atoi() from stdlib.h converts a numeric string into its equivalent integer value.
Question 41 of 60 · recursion · Hard
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?
Not quite. Try another option.
Answer: 6
Calls are made for . That is 6 distinct frames alive simultaneously at the deepest point (before any returns), since the base case f(0) is also a frame.
Question 42 of 60 · passing arguments by value and passing arguments by address · Hard
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);
}
Not quite. Try another option.
Answer: 7 7
a receives the address of x, so *a = b sets x to 7. b is passed by value, so modifying b does not affect y. Thus x becomes 7 and y stays 7, printing 7 7.
Question 43 of 60 · scope rules - local and global scope · Hard
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);
}
Not quite. Try another option.
Answer: 30 20
f() modifies the global x (from 10 to 15), not either local x. The inner block's x is 30, and after the block the main-scope local x is still 20. So the output is 30 20.
Question 44 of 60 · basics of pointers: pointer declaration, initialization, accessing values using pointers · Hard
Given int a[] = {10, 20, 30, 40}; int *p = a + 1;, what is the value of *(p + 1) + *(a + 2)?
Not quite. Try another option.
Answer: 60
p points to a[1], so *(p + 1) is a[2] = 30. Also *(a + 2) is a[2] = 30. The sum is .
Question 45 of 60 · recursion · Hard
What does mystery(4) return?
C
int mystery(int n) {
if (n <= 1) return 1;
return mystery(n - 1) + mystery(n - 2);
}
Not quite. Try another option.
Answer: 5
This is a Fibonacci-like recurrence with base cases returning 1. mystery(2)=2, mystery(3)=3, mystery(4)=mystery(3)+mystery(2)=3+2=5.
Question 46 of 60 · basics of pointers: pointer declaration, initialization, accessing values using pointers · Hard
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);
}
Not quite. Try another option.
Answer: 15
q is a pointer to pointer p, and *q is p, so **q dereferences to x. **q = **q + 10 sets x to .
Question 47 of 60 · scope rules - local and global scope · Hard
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());
}
Not quite. Try another option.
Answer: Implementation-defined ordering of 1, 2, 3
The static variable retains its value across calls, so the three calls yield 1, 2, and 3. However, the evaluation order of function arguments is unspecified in C, so the printed order of those values (e.g. 123 vs 321) is not guaranteed.
Question 48 of 60 · passing arguments by value and passing arguments by address · Hard
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]);
}
Not quite. Try another option.
Answer: 1 99 3
p initially points to a[0]. p = p + 1 advances the local copy to point at a[1], then *p = 99 sets a[1] to 99. The pointer change is local, but the array write persists, giving 1 99 3.
strcat appends "World" (5 chars) to "Hello" (5 chars), producing "HelloWorld". strlen counts characters excluding the null terminator, giving .
Question 50 of 60 · recursion · Hard
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);
}
Not quite. Try another option.
Answer: 8
Doubling happens only at odd n. For n from 1 to 6, the odd values are 1, 3, 5 (three of them), each contributing a factor of 2. So .
Question 51 of 60 · function definition · Hard
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?
Not quite. Try another option.
Answer: The return value is misinterpreted, causing incorrect results or undefined behavior
With an implicit int return assumption (removed in C99, warned in C89), the caller interprets the returned bits as an int while the function actually returned a double. This type mismatch produces garbage values or undefined behavior.
Question 52 of 60 · predefined functions and user defined functions · Hard
Which statement best distinguishes a predefined (library) function from a user-defined function in C?
Not quite. Try another option.
Answer: 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
Library functions are declared in headers but their compiled definitions live in standard libraries linked during the build. User-defined functions are written and compiled directly from the program's own source. Recursion and header use are not distinguishing factors.
Question 53 of 60 · basics of pointers: pointer declaration, initialization, accessing values using pointers · Hard
Which declaration correctly defines p as a pointer to a function that takes an int and returns an int?
Not quite. Try another option.
Answer: int (*p)(int);
int (*p)(int); groups *p with parentheses so p is a pointer, and (int) returning int makes it a pointer to a function. Without the parentheses, int *p(int); declares a function returning int *.
Question 54 of 60 · recursion · Hard
How many times is f called in total (including the initial call) when f(3) runs?
Total calls follow with . So , , . This equals for .
Question 55 of 60 · passing arguments by value and passing arguments by address · Hard
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);
}
Not quite. Try another option.
Answer: 5
Inside modify, pp is a local copy that is reassigned to point at val; changing *pp alters val, not a. Back in main, p still points to a, whose value remains 5.
Question 56 of 60 · scope rules - local and global scope · Hard
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());
}
Not quite. Try another option.
Answer: 1 1
The extern int x; inside the block refers to the global x (value 1), so the first print is 1. f() also reads the global x, which is 1. The local x = 2 in main is shadowed within that block and unused by f.
abs(-7) returns 7. strtol("1010", NULL, 2) interprets "1010" in base 2, which equals in decimal. So the output is 7 10.
Question 58 of 60 · basics of pointers: pointer declaration, initialization, accessing values using pointers · Hard
Given char *s = "ABCD";, what does *(s + 2) - *s evaluate to?
Not quite. Try another option.
Answer: 2
*(s + 2) is 'C' and *s is 'A'. Subtracting their character codes gives . The result is an int value 2, not a character.
Question 59 of 60 · function definition · Hard
What is the effect of defining int f(void) versus int f() in C?
Not quite. Try another option.
Answer: f(void) specifies the function takes no parameters, while f() leaves the parameter list unspecified
In C, f(void) explicitly declares that the function accepts no arguments, so calling it with arguments is an error. The empty f() form (in older prototypes) leaves the parameters unspecified, disabling argument checking.
Question 60 of 60 · recursion · Hard
What does g(1234) print?
C
void g(int n) {
if (n == 0) return;
g(n / 10);
printf("%d", n % 10);
}
Not quite. Try another option.
Answer: 1234
The recursive call g(n / 10) happens before printing n % 10, so digits are printed from most significant to least significant. This prints the number in its original order: 1234.
That's unit 4
Answer a few questions and your score shows up here.
Nearly through. Did unit 4 of CAP1008 save you a night before the exam?
LPU Notes is free. A student pays for the servers.
Did unit 4 of CAP1008 save you a night before the exam?
Then you can help keep it free for the next batch. 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.