WorksheetsQUiZ
Total questions: 10
Worksheet time: 15mins
What is essential in every recursive function?
A global variable
A base condition
At least two parameters
A loop
What does the following function return for fun(3)?
int fun(int n) {
if (n == 0) return 1;
return n * fun(n - 1);
}
6
3
0
1
In indirect recursion:
A function calls itself directly
A function never calls any other function
A function A calls B, and B calls A
Functions cannot have base cases
Which of the following is a disadvantage of recursion?
Less readability
More lines of code
Higher memory usage due to stack frames
Slower compilation
What will the following print for test(3)?
void test(int n) {
if(n == 0) return;
printf("%d ", n);
test(n - 1);
printf("%d ", n);
}
3 2 1
1 2 3
3 2 1 2 3
1 2 3 2 1
What is a main difference between malloc and calloc?
malloc initializes memory to zero
calloc initializes memory to zero
Both initialize memory to zero
Neither allocate memory
Which of the following correctly allocates memory for an array of 5 integers?
int *p = malloc(5);
int p = malloc(5 sizeof(int));
int *p = calloc(sizeof(int));
int p = malloc(5);
What happens if you call free() on a pointer allocated with malloc?
The memory is released back to the system
The pointer becomes NULL
The program terminates
Memory doubles automatically
Which is TRUE about calloc?
Allocates memory but does NOT set it to zero
Allocates contiguous memory blocks
Cannot allocate memory for arrays
Is faster than malloc
What is a dangling pointer?
A pointer that stores a negative address
A pointer that points to freed memory
A pointer that points to global memory
A pointer that has never been declared
