Unit 5 - Practice Quiz

CSE101 60 Questions
0 Correct 0 Wrong 60 Left
0/60

1 Which of the following is the correct syntax to declare a pointer to an integer?

Pointer declaration and initialization Easy
A. int &ptr;
B. int ptr;
C. int *ptr;
D. *int ptr;

2 Which operator is used to get the memory address of a variable?

Pointer operators Easy
A. * (asterisk)
B. -> (arrow)
C. & (ampersand)
D. . (dot)

3 What does the * operator do when used with a pointer variable (e.g., *p)?

Pointer operators Easy
A. It accesses the value stored at the address the pointer is pointing to
B. It declares a new pointer
C. It deletes the pointer
D. It returns the memory address of the pointer

4 What is a NULL pointer?

Types of pointers - null Easy
A. A pointer that has not been initialized
B. A pointer that points to the first memory address (0x0000)
C. A special pointer that intentionally points to nothing
D. A pointer that points to a void type

5 If an integer array is declared as int arr[10];, what does the expression arr itself represent?

Pointer and one dimensional array Easy
A. The address of the first element, &arr[0]
B. The address of the last element, &arr[9]
C. The total number of elements in the array
D. The value of the first element, arr[0]

6 Which function is used to allocate a single block of memory of a specified size in bytes?

Dynamic memory management functions (malloc, calloc, realloc and free) Easy
A. calloc()
B. realloc()
C. malloc()
D. free()

7 Which function must be called to release dynamically allocated memory and prevent memory leaks?

Dynamic memory management functions (malloc, calloc, realloc and free) Easy
A. delete()
B. clear()
C. remove()
D. free()

8 In C, a string is fundamentally a...

Defining and initializing strings Easy
A. linked list of characters
B. collection of pointers
C. special data type called string
D. character array terminated by a null character (\0)

9 Which standard library function is used to find the length of a string?

String manipulation functions and library functions of string Easy
A. strlength()
B. length()
C. strlen()
D. sizeof()

10 Which function is used to copy the content of one string to another?

String manipulation functions and library functions of string Easy
A. strcopy()
B. strcat()
C. strcmp()
D. strcpy()

11 Given int x = 10; and int *p;, which statement correctly initializes the pointer p with the address of x?

Pointer declaration and initialization Easy
A. p = x;
B. *p = &x;
C. p = &x;
D. p = *x;

12 If p is a pointer to an integer (int *p;), what does the expression p + 1 point to?

Pointer expressions and arithmetic Easy
A. This operation is invalid
B. The value of the integer plus one
C. The memory address of the next integer
D. The next memory byte after the address in p

13 What is the primary benefit of passing a pointer to a variable as an argument to a function?

Passing pointer to a function Easy
A. It uses less memory than passing the variable itself.
B. It allows the function to modify the original variable's value.
C. It makes the code run faster.
D. It is the only way to pass arguments to a function.

14 A pointer that has been declared but not yet initialized with a specific memory address is known as a:

Types of pointers - wild Easy
A. Null pointer
B. Void pointer
C. Dangling pointer
D. Wild pointer

15 What is the key difference between malloc() and calloc()?

Dynamic memory management functions (malloc, calloc, realloc and free) Easy
A. calloc() allocates memory and initializes it to zero, while malloc() does not.
B. calloc() is faster than malloc().
C. malloc() allocates memory, while calloc() deallocates it.
D. malloc() is for integers, calloc() is for floats.

16 Which format specifier is used with printf() and scanf() to handle strings?

Reading and writing a string Easy
A. %c
B. %s
C. %d
D. %f

17 A pointer that continues to point to a memory location after the memory has been freed is called a:

Types of pointers - dangling Easy
A. Wild pointer
B. Generic pointer
C. Null pointer
D. Dangling pointer

18 What is a void pointer?

Types of pointers - generic (void) Easy
A. An invalid pointer that causes a compilation error.
B. A pointer that points to a function with no return value.
C. A pointer that must point to NULL.
D. A special pointer that can point to any data type.

19 Assuming ASCII encoding, what is the integer result of the expression 'C' - 'A'?

Character arithmetic Easy
A. 0
B. 1
C. 3
D. 2

20 Which of the following is a valid operation on two pointers that point to elements of the same array?

Operations on pointers Easy
A. Subtraction of one pointer from another
B. Multiplication of the two pointers
C. Division of one pointer by another
D. Addition of the two pointers

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;
}

Pointer expressions and arithmetic Medium
A. 3
B. Depends on the size of an integer
C. 12
D. An error will occur

22 Given the declarations int arr[5]; and int *ptr = arr;, which of the following operations is invalid in C?

Pointer and one dimensional array Medium
A. ptr = arr + 2;
B. arr++;
C. *ptr = 10;
D. ptr++;

23 What is a key difference between malloc(10 * sizeof(int)) and calloc(10, sizeof(int))?

Dynamic memory management functions (malloc, calloc, realloc and free) Medium
A. calloc initializes the allocated memory to zero, while malloc leaves it uninitialized.
B. malloc returns a void* but calloc returns an int*.
C. calloc is faster than malloc.
D. 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;
}

Passing pointer to a function Medium
A. Compiler Error
B. 10
C. 5
D. 15

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?

Types of pointers - dangling , wild, null, generic (void) Medium
A. The memory block pointed to by p is locked.
B. p becomes a NULL pointer automatically.
C. p becomes a dangling pointer.
D. 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");

String manipulation functions and library functions of string Medium
A. result is a positive value.
B. The code will not compile.
C. result is a negative value.
D. 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;
}

Pointer operators Medium
A. 200 100
B. 200 200
C. 100 100
D. 100 200

28 Given int arr[] = {10, 20, 30, 40, 50};, which of the following expressions does not evaluate to the value 30?

Pointer and one dimensional array Medium
A. arr[2]
B. *(2 + arr)
C. *arr + 2
D. *(arr + 2)

29 Which of the following statements about string initialization is true and can lead to undefined behavior if the string is modified?

Defining and initializing strings Medium
A. char str[] = "Hello World"; str[0] = 'J';
B. char str[] = {'H', 'e', 'l', 'l', 'o', '\0'};
C. char str[12]; strcpy(str, "Hello World");
D. 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?

Types of pointers - dangling , wild, null, generic (void) Medium
A. printf("%d", (int*)vptr);
B. printf("%d", *vptr);
C. printf("%d", *(int*)vptr);
D. 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;
}

Pointer and one dimensional array Medium
A. 40 40
B. 8 8
C. 4 8
D. 40 8

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;
}

Character arithmetic Medium
A. H
B. h
C. An integer value
D. Compiler Error

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;
}

Dynamic memory management functions (malloc, calloc, realloc and free) Medium
A. Null pointer assignment
B. Stack overflow
C. Segmentation fault
D. Memory leak

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;
}

Passing pointer to a function Medium
A. Compiler Error
B. A garbage value
C. 20
D. 10

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;
}

Reading and writing a string Medium
A. San Francisco
B. San
C. The program will crash due to buffer overflow.
D. 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;
}

String manipulation functions and library functions of string Medium
A. Hi\0
B. Hi\0\0\0xxxxx
C. Hixxxxxxxxx
D. 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;
}

Processing of string Medium
A. COMPUTER
B. An infinite loop occurs
C. Computer
D. cOMPUTER

38 What is the difference between const int *p and int * const p?

Pointer declaration and initialization Medium
A. const int *p is a constant pointer to an integer; int * const p is a pointer to a constant integer.
B. const int *p is a pointer to a constant integer; int * const p is a constant pointer to an integer.
C. They are identical.
D. Both are invalid syntax.

39 What is a potential outcome of calling realloc(ptr, new_size) where new_size is larger than the original size?

Dynamic memory management functions (malloc, calloc, realloc and free) Medium
A. It will always fail and return NULL if contiguous memory is not available.
B. It will always extend the memory block at its current location.
C. It might allocate a new block of memory, copy the old data, free the old block, and return the address of the new block.
D. It returns a pointer to the new memory but leaves the old memory block allocated, causing a leak.

40 Given int arr[] = {2, 4, 6, 8}; int *p = arr + 3;, what are the values of *p and *(p-2)?

Pointer expressions and arithmetic Medium
A. *p is a garbage value, *(p-2) is 6
B. *p is 8, *(p-2) is 6
C. *p is 8, *(p-2) is 4
D. *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;
}

Pointer Expressions and Arithmetic Hard
A. 1
B. 4
C. 16
D. Undefined Behavior

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;
}

Dynamic memory management functions (malloc, calloc, realloc and free) Hard
A. The program has a memory leak because realloc with size 0 does not free memory.
B. The program prints "Memory freed and new_ptr is NULL", and ptr becomes a dangling pointer.
C. realloc fails, new_ptr is NULL, but the memory at ptr remains allocated and valid.
D. This is undefined behavior from the start because 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;
}

Passing pointer to a function Hard
A. 20 20
B. 10 20
C. 10 10
D. A compile-time error occurs.

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;

Pointer and one dimensional array Hard
A. A pointer to an array of 10 integers (int (*)[10])
B. The expression is invalid and will not compile.
C. A pointer to an array of 11 integers (int (*)[11])
D. A pointer to the second integer in the array (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;
}

String manipulation functions and library functions of string Hard
A. alpha\0beta-gamma
B. alpha\0beta\0gamma
C. alpha-beta-gamma
D. 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?
}

Types of pointers - dangling , wild, null, generic (void) Hard
A. It returns a dangling pointer, leading to undefined behavior when dereferenced.
B. It correctly returns a pointer to the string "Hello, World!".
C. It causes a stack overflow due to returning a large array from the stack.
D. It returns a NULL pointer because local arrays cannot be returned.

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;
}

Pointer operators Hard
A. 3 3 5 5
B. 2 3 5 5
C. 2 3 4 4
D. 2 2 4 4

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;
}

Generic (void) pointers Hard
A. DC
B. BA
C. DA
D. AB

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?
}

Operations on pointers Hard
A. p1 < p2
B. m1 < m2
C. All of the above are well-defined.
D. 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;
}

Defining and initializing strings Hard
A. s1 is a modifiable array on the stack; attempting to modify s2 (uncommenting Line X) results in undefined behavior.
B. s1 and s2 are both modifiable strings, and uncommenting Line X would be valid.
C. s1 is a read-only string, while s2 is a modifiable string.
D. Both 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;
}

Pointer and one dimensional array Hard
A. A compile-time error occurs.
B. 10 30
C. 10 10
D. 10 20

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);

Dynamic memory management functions (malloc, calloc, realloc and free) Hard
A. Option B: char *sB = calloc(10, sizeof(char));
B. Option A: char *sA = malloc(10); *sA = '\0';
C. Option C: char *sC = malloc(10); strcpy(sC, "");
D. Option D is sufficient because 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);

Passing pointer to a function Hard
A. There is no functional difference; const applies to the pointer p in both cases.
B. 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).
C. funcA takes a pointer to a constant integer, while funcB takes a constant pointer to an integer.
D. Both functions declare the same type: a constant pointer to a constant 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;
}

String manipulation functions and library functions of string Hard
A. xyz followed by a null character and some portion of the original 'A's, though printf will stop at the null.
B. xyz
C. A compile-time error because the sizes do not match.
D. 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;
}

Types of pointers - dangling , wild, null, generic (void) Hard
A. Line 2
B. Line 3
C. Line 4
D. Line 1

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;
}

Pointer Expressions and Arithmetic Hard
A. The behavior is undefined.
B. 40
C. 20
D. 30

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;
}

Pointer operators Hard
A. 20 3 30
B. 10 1 10
C. 20 2 20
D. 10 2 20

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;
}

Character arithmetic Hard
A. Eode
B. Gode
C. Gocd
D. Godc

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;
}

Pointer and one dimensional array Hard
A. Statement C, because &arr has type int (*)[5] which is different from the void* expected by %p.
B. Statement A, because arr is an array, not a pointer.
C. Statement B, because taking the address of an array element is not standard.
D. None of them; all are valid and will print the same address without warnings.

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;
}

Processing of string Hard
A. 154
B. 10
C. 49
D. The program has a type mismatch error.