MCQs on Pointers | C Programming

Chapter 7: Pointers

Introduction to Pointers

  1. What is a pointer in C?
    • a) A variable that holds the address of another variable
    • b) A variable that holds the value of another variable
    • c) A data type
    • d) None of the above
  2. Which of the following operator is used to access the value of a variable via a pointer?
    • a) &
    • b) *
    • c) #
    • d) @
  3. What does the ‘address-of’ operator (&) do in C?
    • a) Returns the value of a variable
    • b) Returns the address of a variable
    • c) Dereferences a pointer
    • d) Initializes a pointer
  4. How do you declare a pointer to an integer in C?
    • a) int *ptr;
    • b) int &ptr;
    • c) pointer int;
    • d) ptr int;
  5. What is the output of the following code?cCopy codeint num = 10; int *ptr = # printf("%d", *ptr);
    • a) 10
    • b) Address of num
    • c) Garbage value
    • d) Error

Pointer Declaration, Initialization, and Dereferencing

  1. What does dereferencing a pointer mean?
    • a) Changing the pointer’s address
    • b) Accessing the value stored at the memory address the pointer points to
    • c) Initializing the pointer
    • d) Declaring the pointer
  2. How can a pointer be initialized?
    • a) By using the address-of operator
    • b) By assigning a constant value
    • c) By assigning the value of a variable
    • d) Both a and c
  3. Which of the following is a valid pointer declaration?
    • a) int* ptr;
    • b) int ptr*;
    • c) *int ptr;
    • d) ptr int*;
  4. In C, what will the following code output?cCopy codeint x = 5; int *ptr = &x; *ptr = 10; printf("%d", x);
    • a) 5
    • b) 10
    • c) 0
    • d) Error
  5. What is the role of a pointer in dynamic memory allocation?
  • a) Points to an already allocated memory block
  • b) Helps in allocating memory at runtime
  • c) Deallocates memory
  • d) Both a and b

Pointer Arithmetic

  1. What does pointer arithmetic allow you to do in C?
  • a) Add or subtract integers to/from pointers
  • b) Add or subtract pointers
  • c) Assign values to a pointer
  • d) Initialize pointers
  1. What happens when you increment a pointer (e.g., ptr++)?
  • a) It points to the next variable of the same type
  • b) It increments the value stored at the memory address
  • c) It points to a random address
  • d) It points to the previous variable
  1. If ptr is a pointer to an integer, what does ptr + 1 do?
  • a) Increments the value of ptr
  • b) Moves the pointer to the next integer location
  • c) Subtracts one from the pointer
  • d) Dereferences the pointer
  1. Which of the following is the correct way to perform pointer arithmetic on a pointer to an array?
  • a) ptr = ptr + 3;
  • b) ptr = ptr - 3;
  • c) ptr++;
  • d) All of the above
  1. How do you calculate the distance between two pointers in C?
  • a) By subtracting the address of the first pointer from the second pointer
  • b) By adding the two pointer values
  • c) By finding the length of the array
  • d) By dereferencing both pointers

Pointers and Arrays

  1. How are pointers and arrays related in C?
  • a) An array is a pointer to its first element
  • b) A pointer is an array of values
  • c) Pointers cannot be used with arrays
  • d) An array stores pointer values
  1. What is the result of this code snippet?
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;
printf("%d", *(ptr + 2));
  • a) 1
  • b) 3
  • c) 5
  • d) Error
  1. What is the output of the following code?
int arr[] = {10, 20, 30};
int *ptr = arr;
printf("%d", *ptr);
  • a) 10
  • b) 20
  • c) 30
  • d) Garbage value
  1. When using pointers with arrays, which operation can be used to access an element of the array?
  • a) *ptr + index
  • b) ptr[index]
  • c) *ptr[index]
  • d) Both b and c
  1. Which of the following is an example of pointer access to an array element?
  • a) *arr[0]
  • b) ptr[0]
  • c) &arr[0]
  • d) arr[ptr]

Pointers and Strings

  1. How are strings represented in C?
  • a) As arrays of characters
  • b) As arrays of integers
  • c) As arrays of pointers
  • d) As integers
  1. Which function is used to find the length of a string in C?
  • a) strlen()
  • b) strsize()
  • c) strlen()
  • d) stringlen()
  1. Which of the following will dereference a pointer pointing to a string?
  • a) *ptr
  • b) ptr[]
  • c) ptr()
  • d) ptr + 1
  1. In the statement char *str = "Hello";, what type of variable is str?
  • a) Integer pointer
  • b) Character array
  • c) Character pointer
  • d) String literal
  1. Which of the following functions copies a string in C?
  • a) strcpy()
  • b) strcat()
  • c) strlen()
  • d) strdup()

Pointers to Functions

  1. What does a function pointer store?
  • a) The address of a function
  • b) The return type of the function
  • c) The parameters of the function
  • d) The function’s name
  1. Which syntax is used to declare a pointer to a function?
  • a) return_type (*pointer_name)(parameter_types);
  • b) pointer_type *function_name(parameter_types);
  • c) function_pointer return_type(parameter_types);
  • d) pointer_type function_name(parameter_types);
  1. How do you call a function through a function pointer?
  • a) pointer_name()
  • b) *pointer_name()
  • c) pointer_name;
  • d) (*pointer_name)();
  1. Which of the following correctly declares a pointer to a function that takes two integers and returns an integer?
  • a) int (*ptr)(int, int);
  • b) int ptr(int, int);
  • c) int *ptr(int, int);
  • d) ptr(int, int) int;
  1. How would you use a function pointer in C?
  • a) To change the logic of a function dynamically
  • b) To point to the data members of a structure
  • c) To call a function in place of a loop
  • d) None of the above

Dynamic Memory Allocation

  1. What function is used to allocate memory dynamically in C?
  • a) malloc()
  • b) calloc()
  • c) free()
  • d) realloc()
  1. How do you allocate memory for an array of 10 integers dynamically?
  • a) int *arr = malloc(10 * sizeof(int));
  • b) int arr = malloc(10 * sizeof(int));
  • c) int arr[10] = malloc();
  • d) int *arr = calloc(10, sizeof(int));
  1. What does calloc() do in dynamic memory allocation?
  • a) Allocates memory and initializes it to 0
  • b) Allocates memory without initialization
  • c) Frees allocated memory
  • d) Reallocates the size of memory block
  1. Which function is used to free dynamically allocated memory in C?
  • a) free()
  • b) realloc()
  • c) malloc()
  • d) calloc()
  1. What happens if you don’t free dynamically allocated memory in C?
  • a) Memory leak occurs
  • b) Memory is automatically freed
  • c) Program will crash
  • d) Memory will get corrupted
  1. How do you resize a previously allocated memory block in C?
  • a) realloc()
  • b) malloc()
  • c) calloc()
  • d) resize()
  1. Which of the following is correct for reallocating memory?
  • a) ptr = realloc(ptr, new_size);
  • b) ptr = malloc(ptr, new_size);
  • c) ptr = realloc(new_size, ptr);
  • d) ptr = resize(ptr, new_size);
  1. How do you check if memory allocation was successful using malloc()?
  • a) By checking if the pointer is NULL
  • b) By checking if the pointer is not NULL
  • c) By checking the memory address
  • d) By checking the array size
  1. What value is returned by malloc() if memory allocation fails?
  • a) NULL
  • b) 0
  • c) -1
  • d) Error
  1. What is the purpose of sizeof() in dynamic memory allocation?
  • a) It calculates the amount of memory to allocate
  • b) It frees allocated memory
  • c) It checks if memory allocation is successful
  • d) It allocates the required memory

41. Which of the following code snippet is correct to pass an array to a function using pointers?

  • a) void function(int *arr)
  • b) void function(int arr[])
  • c) void function(int arr[10])
  • d) All of the above

42. What does the following code do?

int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;
printf("%d", *(ptr + 3));
  • a) Prints the address of the fourth element of the array
  • b) Prints the value of the fourth element of the array
  • c) Prints the value of the first element of the array
  • d) Prints a garbage value

43. What is the result of the following code?

int arr[3] = {5, 10, 15};
int *ptr = arr;
ptr++;
printf("%d", *ptr);
  • a) 10
  • b) 5
  • c) 15
  • d) Garbage value

44. In the statement int arr[] = {5, 10, 15};, what type is arr?

  • a) Integer
  • b) Integer pointer
  • c) Array of integers
  • d) Array of pointers

Pointers and Strings


45. What is the correct way to assign a string to a pointer?

  • a) char *ptr = "Hello";
  • b) ptr = "Hello";
  • c) char ptr = "Hello";
  • d) ptr = char "Hello";

46. Which function is used to compare two strings in C?

  • a) strcmp()
  • b) strcompare()
  • c) strcmpi()
  • d) strcomp()

47. What will this code output?

char str[] = "Hello";
char *ptr = str;
ptr++;
printf("%c", *ptr);
  • a) H
  • b) e
  • c) l
  • d) o

48. Which of the following functions concatenates two strings?

  • a) strcat()
  • b) strconcat()
  • c) strcopy()
  • d) strjoin()

49. How would you initialize a pointer to the first character of a string?

  • a) char *ptr = "Hello";
  • b) char *ptr = &"Hello";
  • c) char *ptr = "H";
  • d) char *ptr = &'H';

50. What is the result of this code?

char *str = "Programming";
printf("%s", str);
  • a) “Programming”
  • b) P
  • c) Error
  • d) g

QnoAnswer
1a) A variable that holds the address of another variable
2b) *
3b) Returns the address of a variable
4a) int *ptr;
5a) 10
6b) Accessing the value stored at the memory address the pointer points to
7d) Both a and c
8a) int* ptr;
9b) 10
10b) Helps in allocating memory at runtime
11a) Add or subtract integers to/from pointers
12a) It points to the next variable of the same type
13b) Moves the pointer to the next integer location
14d) All of the above
15a) By subtracting the address of the first pointer from the second pointer
16a) An array is a pointer to its first element
17b) 3
18a) 10
19d) Both b and c
20b) ptr[index]
21a) As arrays of characters
22a) strlen()
23a) *ptr
24c) Character pointer
25a) strcpy()
26a) The address of a function
27a) return_type (*pointer_name)(parameter_types);
28d) (*pointer_name)();
29a) int (*ptr)(int, int);
30a) To change the logic of a function dynamically
31a) malloc()
32a) int *arr = malloc(10 * sizeof(int));
33a) Allocates memory and initializes it to 0
34a) free()
35a) Memory leak occurs
36a) realloc()
37a) ptr = realloc(ptr, new_size);
38a) By checking if the pointer is NULL
39a) NULL
40a) It calculates the amount of memory to allocate
41d) All of the above
42b) Prints the value of the fourth element of the array
43a) 10
44c) Array of integers
45a) char *ptr = “Hello”;
46a) strcmp()
47b) e
48a) strcat()
49a) char *ptr = “Hello”;
50a) “Programming”

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top