C Pointers Quiz - MCQ Questions and Answers

1. What is a pointer in C?

a) A variable that stores the address of another variable
b) A function that points to another function
c) A special type of array
d) A keyword in C programming

Answer:

a) A variable that stores the address of another variable

Explanation:

In C, a pointer is a variable that stores the memory address of another variable.

2. How do you declare a pointer in C?

a) dataType *pointerName;
b) *dataType pointerName;
c) dataType pointerName*;
d) pointer dataType* pointerName;

Answer:

a) dataType *pointerName;

Explanation:

A pointer is declared by specifying the data type of the variable it points to, followed by an asterisk (*) and the pointer name.

3. Which of the following operators is used to access the value pointed to by a pointer?

a) &
b) *
c) ->
d) ::

Answer:

b) *

Explanation:

The dereference operator (*) is used to access the value at the memory address stored in a pointer.

4. What does the '&' operator do in C?

a) It adds two values
b) It performs a logical AND operation
c) It gives the address of a variable
d) It is used to declare a pointer

Answer:

c) It gives the address of a variable

Explanation:

The address-of operator (&) is used to obtain the memory address of a variable.

5. What is the output of the following C code?

a) 5
b) A memory address
c) Error
d) Undefined behavior

Answer:

a) 5

Explanation:

The pointer ptr is assigned the address of var. Using the dereference operator (*ptr), the value at that address, which is 5, is accessed and printed.

6. How can you increment the value pointed to by a pointer in C?

a) (*ptr)++;
b) *ptr++;
c) ++*ptr;
d) Both a) and c)

Answer:

d) Both a) and c)

Explanation:

Both (*ptr)++ and ++*ptr increment the value pointed to by ptr. The parentheses ensure that the increment operation is applied to the value pointed to by ptr, not to the address stored in ptr.

7. What is the correct way to declare a pointer to a pointer in C?

a) int **ptr;
b) int *ptr*;
c) int &ptr;
d) int ptr**;

Answer:

a) int **ptr;

Explanation:

A pointer to a pointer is declared by using two asterisks (**). It means that 'ptr' is a pointer to another pointer.

8. What is a NULL pointer in C?

a) A pointer that points to the number 0
b) A pointer that has an invalid address
c) A special pointer value representing an empty or uninitialized pointer
d) A pointer to a void type

Answer:

c) A special pointer value representing an empty or uninitialized pointer

Explanation:

A NULL pointer in C is a pointer that does not point to any valid memory location. It is used to indicate that the pointer is not in use.

9. How do you dynamically allocate memory in C?

a) Using the alloc() function
b) Using the new operator
c) Using the malloc() function
d) Using the create() function

Answer:

c) Using the malloc() function

Explanation:

Memory in C can be dynamically allocated using the malloc() function from the stdlib.h library.

10. What is pointer arithmetic in C?

a) Adding, subtracting, and comparing pointers
b) Multiplying and dividing pointers
c) Changing the address a pointer points to
d) Performing arithmetic operations on the values pointed by pointers

Answer:

a) Adding, subtracting, and comparing pointers

Explanation:

Pointer arithmetic involves adding or subtracting integers to or from pointers, or comparing pointers.

11. What is the result of comparing two pointers in C?

a) The difference in their memory addresses
b) True or false, depending on whether they point to the same memory location
c) The sum of their memory addresses
d) The result is undefined

Answer:

b) True or false, depending on whether they point to the same memory location

Explanation:

Comparing two pointers results in a boolean value indicating whether they point to the same memory location.

12. What is the output of the following C code?

a) 10
b) 20
c) 30
d) Error

Answer:

b) 20

Explanation:

The pointer ptr initially points to the first element of arr. The expression *++ptr increments the pointer, making it point to the second element, and then dereferences it, printing 20.

13. Which of the following is a correct way to pass a pointer to a function in C?

a) Passing the address of the pointer
b) Passing the pointer directly
c) Both a) and b)
d) Neither a) nor b)

Answer:

c) Both a) and b)

Explanation:

A pointer can be passed to a function either by passing the pointer itself or by passing the address of the pointer.

14. Can a function return a pointer in C?

a) Yes
b) No
c) Only if it is a void pointer
d) Only if it points to a static variable

Answer:

a) Yes

Explanation:

A function in C can return a pointer. However, care must be taken to ensure that the pointer does not point to a variable that goes out of scope once the function returns.

15. What is the purpose of the const keyword with pointers in C?

a) To make the pointer itself constant
b) To make the value pointed to by the pointer constant
c) Both a) and b)
d) Neither a) nor b)

Answer:

c) Both a) and b)

Explanation:

The const keyword can be used with pointers to either make the pointer itself constant, the value it points to constant, or both.

16. How do you declare an array of pointers in C?

a) dataType *arrayName[size];
b) dataType arrayName*[size];
c) dataType[size] *arrayName;
d) *dataType arrayName[size];

Answer:

a) dataType *arrayName[size];

Explanation:

An array of pointers in C is declared by placing an asterisk (*) before the array name, indicating that each element of the array is a pointer.

17. How do you access a member of a structure using a pointer to that structure in C?

a) ptr.member
b) ptr->member
c) (*ptr).member
d) Both b) and c)

Answer:

d) Both b) and c)

Explanation:

You can access a member of a structure using a pointer either with the arrow operator (->) or by dereferencing the pointer and using the dot operator (.)

18. What happens if you try to dereference a NULL pointer in C?

a) It returns 0
b) It results in undefined behavior, typically a segmentation fault
c) The program exits
d) The pointer gets initialized to a default value

Answer:

b) It results in undefined behavior, typically a segmentation fault

Explanation:

Dereferencing a NULL pointer is dangerous as it leads to undefined behavior, often causing a runtime error like a segmentation fault.

19. How do you free dynamically allocated memory in C?

a) Using the free() function
b) Using the dealloc() function
c) The memory is automatically freed
d) Using the delete operator

Answer:

a) Using the free() function

Explanation:

Dynamically allocated memory in C must be manually freed using the free() function.

20. What is the output of the following C code?

a) The size of an integer
b) The size of a pointer
c) 5
d) Error

Answer:

b) The size of a pointer

Explanation:

The sizeof operator when used with a pointer variable returns the size of the pointer itself, not the size of the data it points to.

Comments