Unit 5: Pointers, Dynamic memory allocation and Strings - Practice Quiz

CSE101 — Computer Programming 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. -> (arrow)
B. & (ampersand)
C. . (dot)
D. * (asterisk)

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 deletes the pointer
C. It returns the memory address of the pointer
D. It declares a new 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 a void type
C. A special pointer that intentionally points to nothing
D. A pointer that points to the first memory address (0x0000)

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 last element, &arr[9]
B. The address of the first element, &arr[0]
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. realloc()
B. free()
C. calloc()
D. malloc()

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. clear()
B. remove()
C. free()
D. delete()

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

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

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

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

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. strcpy()
C. strcat()
D. strcmp()

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 memory address of the next integer
C. The value of the integer plus one
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 makes the code run faster.
C. It allows the function to modify the original variable's value.
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. Wild pointer
C. Void pointer
D. Dangling 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() is for integers, calloc() is for floats.
D. malloc() allocates memory, while calloc() deallocates it.

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

Reading and writing a string Easy
A. %d
B. %c
C. %s
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. Null pointer
B. Generic pointer
C. Wild pointer
D. Dangling pointer

18 What is a void pointer?

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

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

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

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. Division of one pointer by another
B. Multiplication of the two pointers
C. Subtraction of one pointer from 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. 12
B. 3
C. An error will occur
D. Depends on the size of an integer

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 = 10;
B. ptr = arr + 2;
C. arr++;
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. malloc can allocate more memory than calloc.
B. calloc initializes the allocated memory to zero, while malloc leaves it uninitialized.
C. calloc is faster than malloc.
D. malloc returns a void* but calloc returns an int*.

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. 10
B. 15
C. 5
D. Compiler Error

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 dangling pointer.
C. p becomes a void pointer.
D. p becomes a NULL pointer automatically.

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 = &num;
    int **q = &p;
    **q = 200;
    printf("%d %d\n", num, *p);
    return 0;
}

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

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[12]; strcpy(str, "Hello World");
B. char str[] = "Hello World"; str[0] = 'J';
C. char str[] = {'H', 'e', 'l', 'l', 'o', '\0'};
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", *(int*)vptr);
C. printf("%d", (int*)vptr);
D. printf("%d", *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 8
B. 8 8
C. 4 8
D. 40 40

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. An integer value
B. h
C. H
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. Segmentation fault
B. Memory leak
C. Stack overflow
D. Null pointer assignment

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. A garbage value
B. 10
C. 20
D. Compiler Error

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. The program will crash due to buffer overflow.
B. San
C. San Francisco
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\0xxxxxxxx
B. Hi\0
C. Hi\0\0\0xxxxx
D. Hixxxxxxxxx

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. An infinite loop occurs
B. COMPUTER
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. They are identical.
C. Both are invalid syntax.
D. 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?

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 might allocate a new block of memory, copy the old data, free the old block, and return the address of the new block.
C. It will always extend the memory block at its current location.
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 6, *(p-2) is 2
D. *p is 8, *(p-2) is 4

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. 16
B. 1
C. Undefined Behavior
D. 4

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. realloc fails, new_ptr is NULL, but the memory at ptr remains allocated and valid.
C. The program prints "Memory freed and new_ptr is NULL", and ptr becomes a dangling pointer.
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. 10 10
B. 20 20
C. 10 20
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 11 integers (int (*)[11])
B. The expression is invalid and will not compile.
C. A pointer to the second integer in the array (int *)
D. A pointer to an array of 10 integers (int (*)[10])

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-beta-gamma
C. beta-gamma
D. alpha\0beta\0gamma

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 causes a stack overflow due to returning a large array from the stack.
B. It correctly returns a pointer to the string "Hello, World!".
C. It returns a dangling pointer, leading to undefined behavior when dereferenced.
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. 2 2 4 4
B. 3 3 5 5
C. 2 3 5 5
D. 2 3 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 = &num;
    char *c_ptr = (char*)v_ptr;
    printf("%c%c", *c_ptr, *(c_ptr + 1));
    return 0;
}

Generic (void) pointers Hard
A. DC
B. DA
C. BA
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 < m1
B. All of the above are well-defined.
C. p1 < p2
D. m1 < m2

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 and s2 are both modifiable strings, and uncommenting Line X would be valid.
B. s1 is a read-only string, while s2 is a modifiable string.
C. s1 is a modifiable array on the stack; attempting to modify s2 (uncommenting Line X) results in undefined behavior.
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. 10 30
B. A compile-time error occurs.
C. 10 20
D. 10 10

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

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. Both functions declare the same type: a constant pointer to a constant integer.
C. 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).
D. 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;
}

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

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. 10 2 20
B. 20 3 30
C. 10 1 10
D. 20 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. Gode
B. Eode
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. The program has a type mismatch error.
B. 10
C. 154
D. 49