Unit 5 - Practice Quiz
1 Which of the following is the correct syntax to declare a pointer to an integer?
2 Which operator is used to get the memory address of a variable?
3
What does the * operator do when used with a pointer variable (e.g., *p)?
4 What is a NULL pointer?
void type
5
If an integer array is declared as int arr[10];, what does the expression arr itself represent?
&arr[0]
&arr[9]
arr[0]
6 Which function is used to allocate a single block of memory of a specified size in bytes?
7 Which function must be called to release dynamically allocated memory and prevent memory leaks?
8 In C, a string is fundamentally a...
string
\0)
9 Which standard library function is used to find the length of a string?
10 Which function is used to copy the content of one string to another?
11
Given int x = 10; and int *p;, which statement correctly initializes the pointer p with the address of x?
12
If p is a pointer to an integer (int *p;), what does the expression p + 1 point to?
p
13 What is the primary benefit of passing a pointer to a variable as an argument to a function?
14 A pointer that has been declared but not yet initialized with a specific memory address is known as a:
15
What is the key difference between malloc() and calloc()?
calloc() allocates memory and initializes it to zero, while malloc() does not.
calloc() is faster than malloc().
malloc() allocates memory, while calloc() deallocates it.
malloc() is for integers, calloc() is for floats.
16
Which format specifier is used with printf() and scanf() to handle strings?
17 A pointer that continues to point to a memory location after the memory has been freed is called a:
18
What is a void pointer?
19
Assuming ASCII encoding, what is the integer result of the expression 'C' - 'A'?
20 Which of the following is a valid operation on two pointers that point to elements of the same array?
21
Consider the following C code snippet. What will be the output?
c
#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40, 50};
int p1 = &arr[1];
int p2 = &arr[4];
printf("%ld\n", p2 - p1);
return 0;
}
22
Given the declarations int arr[5]; and int *ptr = arr;, which of the following operations is invalid in C?
ptr = arr + 2;
arr++;
*ptr = 10;
ptr++;
23
What is a key difference between malloc(10 * sizeof(int)) and calloc(10, sizeof(int))?
calloc initializes the allocated memory to zero, while malloc leaves it uninitialized.
malloc returns a void* but calloc returns an int*.
calloc is faster than malloc.
malloc can allocate more memory than calloc.
24
What is the output of the following C program?
c
#include <stdio.h>
void modify(int p) {
p = *p + 10;
}
int main() {
int x = 5;
modify(&x);
printf("%d\n", x);
return 0;
}
25
What is the status of the pointer p after the free(p); call in the code below?
c
int p = (int)malloc(sizeof(int));
*p = 100;
// ... some operations ...
free(p);
// What is p now?
p is locked.
p becomes a NULL pointer automatically.
p becomes a dangling pointer.
p becomes a void pointer.
26
What can be inferred about the integer result after this C code is executed?
c
#include <string.h>
int result = strcmp("Banana", "Bandana");
result is a positive value.
result is a negative value.
result is 0.
27
Predict the output of the following C code:
c
#include <stdio.h>
int main() {
int num = 100;
int p = #
int q = &p;
q = 200;
printf("%d %d\n", num, p);
return 0;
}
28
Given int arr[] = {10, 20, 30, 40, 50};, which of the following expressions does not evaluate to the value 30?
arr[2]
*(2 + arr)
*arr + 2
*(arr + 2)
29 Which of the following statements about string initialization is true and can lead to undefined behavior if the string is modified?
char str[] = "Hello World"; str[0] = 'J';
char str[] = {'H', 'e', 'l', 'l', 'o', '\0'};
char str[12]; strcpy(str, "Hello World");
char *str = "Hello World"; str[0] = 'J';
30
Given int val = 50; void *vptr = &val;, how would you correctly retrieve and print the integer value using vptr?
printf("%d", (int*)vptr);
printf("%d", *vptr);
printf("%d", *(int*)vptr);
printf("%d", *(int)vptr);
31
On a 64-bit system where sizeof(int) is 4 and sizeof(int*) is 8, what will be the output of the following code?
c
#include <stdio.h>
int main() {
int arr[10];
int *p = arr;
printf("%zu %zu\n", sizeof(arr), sizeof(p));
return 0;
}
32
What is the output of the following C code snippet, assuming ASCII character encoding?
c
#include <stdio.h>
int main() {
char c = 'h';
printf("%c\n", c - 'a' + 'A');
return 0;
}
H
h
33
What is the primary risk associated with the following function if it's called repeatedly?
c
void process() {
int ptr = (int)malloc(100 * sizeof(int));
// Code to work with ptr
// ...
return;
}
34
Analyze the following code. What will it print?
c
#include <stdio.h>
void func(int ptr) {
int y = 20;
ptr = &y;
}
int main() {
int x = 10;
int p = &x;
func(p);
printf("%d\n", *p);
return 0;
}
35
If a user enters San Francisco when prompted, what will be stored in the city array after this code executes?
c
#include <stdio.h>
int main() {
char city[20];
printf("Enter city: ");
scanf("%s", city);
// ...
return 0;
}
San Francisco
San
San followed by garbage data
36
Consider the following code using strncpy. What is the final content of the dest array?
c
#include <string.h>
#include <stdio.h>
int main() {
char dest[10] = "xxxxxxxxx";
char src[] = "Hi"; // length is 2
strncpy(dest, src, 5);
// What is in dest?
return 0;
}
Hi\0
Hi\0\0\0xxxxx
Hixxxxxxxxx
Hi\0xxxxxxxx
37
What is the output of the following C program that processes a string using a pointer?
c
#include <stdio.h>
int main() {
char str[] = "Computer";
char p = str;
while (p != '\0') {
if (p >= 'a' && p <= 'z') {
p = p - 32;
}
p++;
}
printf("%s\n", str);
return 0;
}
COMPUTER
Computer
cOMPUTER
38
What is the difference between const int *p and int * const p?
const int *p is a constant pointer to an integer; int * const p is a pointer to a constant integer.
const int *p is a pointer to a constant integer; int * const p is a constant pointer to an integer.
39
What is a potential outcome of calling realloc(ptr, new_size) where new_size is larger than the original size?
NULL if contiguous memory is not available.
40
Given int arr[] = {2, 4, 6, 8}; int *p = arr + 3;, what are the values of *p and *(p-2)?
*p is a garbage value, *(p-2) is 6
*p is 8, *(p-2) is 6
*p is 8, *(p-2) is 4
*p is 6, *(p-2) is 2
41
Consider the following C code snippet. Assuming an int is 4 bytes and pointers are 8 bytes, what will be printed to the console?
c
#include <stdio.h>
int main() {
int arr[] = {100, 200, 300, 400, 500};
int p = arr;
int q = &arr[4];
printf("%ld\n", (char)q - (char)p);
return 0;
}
42
What is the behavior of the following C code snippet?
c
#include <stdlib.h>
#include <stdio.h>
int main() {
int ptr = (int) malloc(5 sizeof(int));
if (ptr == NULL) return 1;
int new_ptr = (int) realloc(ptr, 0);
if (new_ptr == NULL) {
printf("Memory freed and new_ptr is NULL\n");
// Can we still use ptr?
// ptr = 10; // Line A
}
return 0;
}
realloc with size 0 does not free memory.
ptr becomes a dangling pointer.
realloc fails, new_ptr is NULL, but the memory at ptr remains allocated and valid.
realloc cannot be called with size 0.
43
Analyze the following C code. What will be its output?
c
#include <stdio.h>
void reassign(int *p) {
static int y = 20;
p = &y;
}
int main() {
int x = 10;
int ptr = &x;
printf("%d ", ptr);
reassign(&ptr);
printf("%d\n", *ptr);
return 0;
}
44
Given int arr[10];, what is the data type of the expression &arr + 1?
c
// What is the type of the expression on the right?
// typeof(&arr + 1) result_ptr = &arr + 1;
int (*)[10])
int (*)[11])
int *)
45
What is the final state of str after this code executes?
c
#include <stdio.h>
#include <string.h>
int main() {
char str[20] = "alpha-beta-gamma";
char *token = strtok(str, "-");
token = strtok(NULL, "-");
printf("%s\n", str);
return 0;
}
alpha\0beta-gamma
alpha\0beta\0gamma
alpha-beta-gamma
beta-gamma
46
What is the primary risk associated with the following C function?
c
char create_message() {
char message[] = "Hello, World!";
return message;
}
int main() {
char msg = create_message();
// What is the status of msg here?
}
47
Predict the output of the following C program, paying close attention to operator precedence and side effects.
c
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5};
int ptr = arr + 1;
int x = (ptr)++;
int y = ptr++;
int z = ++ptr;
printf("%d %d %d %d\n", x, y, z, *ptr);
return 0;
}
48
What is the output of this C code, assuming a little-endian architecture where sizeof(int) is 4?
c
#include <stdio.h>
int main() {
int num = 0x41424344; // Represents 'DCBA' in memory on little-endian
void v_ptr = #
char c_ptr = (char)v_ptr;
printf("%c%c", c_ptr, *(c_ptr + 1));
return 0;
}
49
According to the C standard, which of the following pointer comparisons results in specified, well-defined behavior?
c
#include <stdlib.h>
int main() {
int arr[10];
int p1 = &arr[2];
int p2 = &arr[5];
int m1 = malloc(sizeof(int));
int m2 = malloc(sizeof(int));
// Which comparison is guaranteed to be well-defined?
}
p1 < p2
m1 < m2
p1 < m1
50
Which of the following statements is true regarding the two string definitions below, and what is the outcome of the code?
c
#include <stdio.h>
int main() {
char s1[] = "hello";
char *s2 = "world";
s1[0] = 'H';
// s2[0] = 'W'; // Line X
printf("%s %s\n", s1, s2);
return 0;
}
s1 is a modifiable array on the stack; attempting to modify s2 (uncommenting Line X) results in undefined behavior.
s1 and s2 are both modifiable strings, and uncommenting Line X would be valid.
s1 is a read-only string, while s2 is a modifiable string.
s1 and s2 are pointers to string literals in read-only memory.
51
What is the output of the following C program?
c
#include <stdio.h>
int main() {
int a[] = {10, 20, 30};
int p = a;
int pp = &p;
printf("%d ", pp);
(pp)++;
printf("%d\n", **pp);
return 0;
}
52
A programmer intends to create a dynamically allocated string and initialize it to empty. Which of the following is the most robust way to do this, relying on guaranteed behavior?
c
// Goal: Create a dynamic string of size 10,
// ensuring it is a valid, empty C-string.
// Option A
char sA = malloc(10);
sA = '\0';
// Option B
char sB = calloc(10, sizeof(char));
// Option C
char sC = malloc(10);
strcpy(sC, "");
// Option D
char *sD = malloc(10);
char *sB = calloc(10, sizeof(char));
char *sA = malloc(10); *sA = '\0';
char *sC = malloc(10); strcpy(sC, "");
malloc zero-initializes memory.
53
What is the key difference in how const is used in the declarations of funcA and funcB?
c
void funcA(int const p);
void funcB(const int p);
const applies to the pointer p in both cases.
funcA takes a constant pointer to an integer (the pointer itself cannot be changed), while funcB takes a pointer to a constant integer (the integer value cannot be changed via the pointer).
funcA takes a pointer to a constant integer, while funcB takes a constant pointer to an integer.
54
Consider the following code that uses strncpy. What will be printed to standard output?
c
#include <stdio.h>
#include <string.h>
int main() {
char dest[5] = "AAAA";
const char *src = "xyz";
strncpy(dest, src, 4);
printf("%s", dest);
return 0;
}
xyz followed by a null character and some portion of the original 'A's, though printf will stop at the null.
xyz
xyz followed by an 'A', without a null terminator.
55
In the following C code, after which line does p1 become a dangling pointer for the first time?
c
#include <stdlib.h>
int main() {
int p1, p2;
p1 = (int) malloc(sizeof(int)); // Line 1
p1 = 10;
p2 = p1; // Line 2
free(p1); // Line 3
p1 = NULL; // Line 4
// ...
return 0;
}
56
What is the output of this C code snippet?
c
#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40, 50, 60};
int p = &arr[1];
int q = &arr[4];
int result = *(p + (q - p) / 2);
printf("%d\n", result);
return 0;
}
57
What is the output of the following C code that involves pointer to a struct?
c
#include <stdio.h>
struct Point {
int x, y;
};
int main() {
struct Point arr[] = {{1, 10}, {2, 20}, {3, 30}};
struct Point *p = arr;
int val = p++->y;
printf("%d %d %d\n", val, p->x, p->y);
return 0;
}
58
Predict the output of the following C program. Assume standard ASCII character encoding.
c
#include <stdio.h>
int main() {
char str[] = "Code";
char p = str;
p = p + 4;
(p + 2) = *(p + 2) - 1;
printf("%s\n", str);
return 0;
}
59
Which of the following printf statements will likely cause a compiler warning about incompatible pointer types and why?
c
#include <stdio.h>
int main() {
int arr[5];
// Statement A
printf("%p\n", arr);
// Statement B
printf("%p\n", &arr[0]);
// Statement C
printf("%p\n", &arr);
return 0;
}
&arr has type int (*)[5] which is different from the void* expected by %p.
arr is an array, not a pointer.
60
What is the final value of the integer sum after the execution of the following C code? The code is intended to sum the digits in a string.
c
#include <stdio.h>
#include <ctype.h>
int main() {
const char str = "a1;b23c_4";
int sum = 0;
while (str) {
if (isdigit(str)) {
sum += str;
}
str++;
}
printf("%d\n", sum);
return 0;
}