Ad Code

Responsive Advertisement

Pointers in C MCQ (Multiple Choise Questions)

Pointers in C MCQ ?


1. What is a pointer in C?
 a. A variable that stores an integer value 
 b. A variable that stores a string 
 c. A variable that stores the address of another variable 
 d. A variable that stores a floating-point number

Answer: c. A variable that stores the address of another variable


2. How do you declare a pointer in C?
 a. int *ptr;
 b. ptr int; 
 c. pointer = int;
 d. int ptr;

Answer: a. int *ptr;


3. What is the value of a pointer variable if it is uninitialized?
 a. 0 
 b. -1 
 c. Undefined
 d. 1

Answer: c. Undefined


4. What is the purpose of the dereference operator (*) in C?
 a. It declares a pointer variable.
 b. It multiplies two numbers.
 c. It accesses the value stored at the address pointed to by a pointer.
 d. It finds the address of a variable.

Answer: c. It accesses the value stored at the address pointed to by a pointer.


5. What is the output of the following code?

c
int x = 10; int *ptr = &x; printf("%d", *ptr);

 a. 10
 b. &x
 c. 0
 d. Error

Answer: a. 10


6. How can you allocate memory for a pointer in C?
 a. Use the new keyword. 
 b. Use the malloc function.
 c. Use the sizeof operator. 
 d. Pointers do not require memory allocation.

 Answer: b. Use the malloc function.


7. What does the NULL pointer represent in C? 
 a. An invalid pointer
 b. A pointer with a value of 0 
 c. A pointer with a value of 1 
 d. A pointer that points to the address of the current program.

Answer: b. A pointer with a value of 0


8. What is pointer arithmetic in C? 
 a. Performing arithmetic operations on pointer variables
 b. Arithmetic operations that involve floating-point numbers
 c. Arithmetic operations that involve complex numbers 
 d. Arithmetic operations that involve string manipulation

Answer: a. Performing arithmetic operations on pointer variables


9. What is a dangling pointer in C? 
 a. A pointer that points to a valid memory location.
 b. A pointer that has been deallocated, but its value remains unchanged. 
 c. A pointer with an invalid data type.
 d. A pointer that cannot be dereferenced.

Answer: b. A pointer that has been deallocated, but its value remains unchanged.


10. How do you free memory allocated to a pointer in C? 
 a. Use the free function. 
 b. Set the pointer to NULL. 
 c. Use the delete keyword.
 d. Memory is automatically released when the program exits.

Answer: a. Use the free function.


11. Which operator is used to get the address of a variable in C? 
 a. & (ampersand) 
 b. * (asterisk)
 c. ! (exclamation mark) 
 d. # (hash)

Answer: a. & (ampersand)


12. What is the size of a pointer variable in C (assuming a 32-bit system)? 
 a. 2 bytes  
 b. 4 bytes
 c. 8 bytes 
 d. It varies depending on the data type.

Answer: b. 4 bytes


13. What is the purpose of the sizeof operator in C?
  a. It calculates the sum of two numbers. 
  b. It determines the size of a variable or data type in bytes.
  c. It is used to declare pointers.
  d. It is used to free memory.

Answer: b. It determines the size of a variable or data type in bytes.


14. How do you declare a pointer to an integer array in C?
 a. int ptr[]; 
 b. int *ptr; 
 c. int[] ptr;
 d. int (*ptr)[];

Answer: b. int *ptr;

15. What is the significance of the -> operator in C? 
 a. It performs logical AND operations.
 b. It accesses the member of a structure through a pointer to the structure.
 c. It is used for bitwise shift operations. 
 d. It is used for division.

Answer: b. It accesses the member of a structure through a pointer to the structure.


16. What is the purpose of the void pointer in C? 
 a. It points to a function. 
 b. It points to a specific memory address. 
 c. It is used to represent an empty or unknown data type. 
 d. It is used for text processing.

Answer: c. It is used to represent an empty or unknown data type.


17. What does the & operator do when used with an array name in C? 
 a. It finds the sum of all elements in the array.
 b. It returns the size of the array.
 c. It gives the address of the first element in the array.
 d. It performs a bitwise AND operation on all elements.

Answer: c. It gives the address of the first element in the array.


18. What is the relationship between pointers and arrays in C? 
 a. Pointers and arrays are entirely unrelated in C. 
 b. Pointers and arrays are the same concept in C. 
 c. Pointers can be used to access elements of an array, but they are not the same. 
 d. Arrays are pointers in C.

Answer: c. Pointers can be used to access elements of an array, but they are not the same.


19. What is a double pointer in C?
 a. A pointer with a value twice as large as a regular pointer. 
 b. A pointer to a pointer (a pointer that stores the address of another pointer).
 c. A pointer that can point to both integers and floats. 
 d. A pointer with two asterisks.

Answer: b. A pointer to a pointer (a pointer that stores the address of another pointer).


20. Which function is used to copy a string to another string in C? 
 a. strcat()
 b. strcpy() 
 c. strcopy() 
 d. stringcopy()

Answer: b. strcpy()


21. What is the purpose of the const keyword when declaring a pointer in C?
 a. It prevents the pointer from being used. 
 b. It makes the pointer point to a constant value.
 c. It makes the pointer itself constant, so it cannot be modified. 
 d. It is used to declare a pointer to an array.

Answer: c. It makes the pointer itself constant, so it cannot be modified.


22. In C, which function is used to compare two strings?
 a. strcomp() 
 b. strcmp() 
 c. strcompare() 
 d. comparestr()

Answer: b. strcmp()


23. What is the purpose of the -> operator when used with a structure pointer in C?
 a. It performs a logical OR operation. 
 b. It accesses the elements of the structure through the pointer. 
 c. It calculates the square root of a number. 
 d. It is used for integer division.

Answer: b. It accesses the elements of the structure through the pointer.


24. What is a null-terminated string in C?
 a. A string that contains only null characters.
 b. A string that is empty.
 c. A string that ends with a null character ('\0').
 d. A string that cannot be modified.

Answer: c. A string that ends with a null character ('\0').


25. What is the size of a pointer to a function in C?
 a. 1 byte 
 b. 2 bytes
 c. 4 bytes 
 d. It varies depending on the function.

Answer: c. 4 bytes


26. What is the purpose of the void * pointer in C?
  a. It is not a valid pointer type in C. 
  b. It is used for arithmetic operations. 
  c. It is a pointer to a generic data type that can point to any data. 
  d. It is used for type casting.

Answer: c. It is a pointer to a generic data type that can point to any data.


27. Which of the following is true about pointers and memory management in C? 
 a. Pointers automatically manage memory.
 b. Pointers cannot be used to access memory. 
 c. Pointers are essential for dynamic memory management. 
 d. Pointers are used only for printing values.

Answer: c. Pointers are essential for dynamic memory management.


28. What is the purpose of the free function in C?
 a. It deallocates memory previously allocated by malloc.
 b. It allocates memory for a new pointer. 
 c. It accesses the value of a pointer. 
 d. It initializes a pointer.

Answer: a. It deallocates memory previously allocated by malloc.


29. What does the volatile keyword signify when used with a pointer in C? 
 a. It makes the pointer constant and unchangeable.
 b. It indicates that the pointer is volatile and can change unexpectedly.
 c. It has no effect on the pointer. 
 d. It specifies that the pointer is used for arithmetic operations.

Answer: b. It indicates that the pointer is volatile and can change unexpectedly.


30. What is the purpose of the offsetof macro in C? 
 a. It calculates the size of a data type. 
 b. It calculates the size of an array. 
 c. It calculates the offset of a member within a structure.
 d. It calculates the difference between two pointers.

Answer: c. It calculates the offset of a member within a structure.


31. Which operator is used to get the value stored at a pointer's address in C? 
 a. & (ampersand)
 b. * (asterisk) 
 c. ! (exclamation mark) 
 d. # (hash)

Answer: b. * (asterisk)


32. What is the purpose of the volatile keyword when used with a variable in C?
 a. It makes the variable constant and unchangeable.
 b. It indicates that the variable is volatile and can change unexpectedly. 
 c. It specifies that the variable is used for arithmetic operations.
 d. It has no effect on the variable.

Answer: b. It indicates that the variable is volatile and can change unexpectedly.


33. What is a function pointer in C?
 a. A pointer to a function that can be used to call the function indirectly. 
 b. A pointer to an integer.
 c. A pointer to a floating-point number.
 d. A pointer to a string.

Answer: a. A pointer to a function that can be used to call the function indirectly.


34. What is a "wild pointer" in C?
 a. A pointer that points to a valid memory location. 
 b. A pointer that has been initialized with a valid value. 
 c. A pointer that has not been initialized or is pointing to an invalid memory location. 
 d. A pointer that is used for arithmetic operations.

Answer: c. A pointer that has not been initialized or is pointing to an invalid memory location.


35. What is the purpose of the const keyword when used with a function pointer in C?
 a. It makes the function pointer constant and unchangeable.
 b. It specifies that the function pointer is used for arithmetic operations. 
 c. It has no effect on the function pointer.
 d. It indicates that the function pointer points to a constant function.

Answer: a. It makes the function pointer constant and unchangeable.


36. What is a "segfault" in C? 
 a. A type of data type in C.
 b. A division by zero error. 
 c. A segmentation fault, which occurs when a program tries to access a restricted memory area.
 d. A type of loop in C.

Answer: c. A segmentation fault, which occurs when a program tries to access a restricted memory area.


37. What is the purpose of the restrict keyword in C?
 a. It restricts the use of pointers. 
 b. It restricts the use of the malloc function. 
 c. It provides optimization hints to the compiler to improve performance. 
 d. It restricts the use of the free function.

Answer: c. It provides optimization hints to the compiler to improve performance.


38. What is the purpose of the near, far, and huge keywords in C? 
 a. They are used to specify the size of data types.
 b. They are used to specify the type of data a pointer can point to.
 c. They are used to specify the memory model for pointer access. 
 d. They are used to declare arrays.

Answer: c. They are used to specify the memory model for pointer access.


39.. What is a "memory leak" in C? 
 a. A type of error that occurs when a program uses too much memory. 
 b. A type of data structure.
 c. A condition in which a program does not release memory it has allocated, causing a gradual loss of     available memory.
 d. A type of pointer.

Answer: c. A condition in which a program does not release memory it has allocated, causing a gradual loss of available memory.


40. In C, what is the difference between char *ptr and const char *ptr? 
 a. There is no difference. 
 b. char *ptr is a constant pointer, and const char *ptr is a pointer to a constant character.
 c. char *ptr is a pointer to a constant character, and const char *ptr is a constant pointer.
 d. char *ptr is a pointer to an integer, and const char *ptr is a pointer to a character.

Answer: c. char *ptr is a pointer to a constant character, and const char *ptr is a constant pointer.

Post a Comment

0 Comments