5.1 - Pointers.

5.5.1 - Overview of Pointers.


1. A pointer in C is used to store:
(A) The address of a variable
2. Which of the following is the correct way to declare a pointer in C?
(B) int *ptr;
3. What does the following statement do: int *ptr;?
(A) Declares a pointer
4. What is the output of the following code?
                int a = 10;
                int *p = &a;
                printf("%d", *p);
                
(B) 10
5. What is a null pointer?
(A) A pointer that does not point to any memory location
6. What is the correct syntax to access the value at the address stored by a pointer p?
(B) *p
7. Which operator is used to get the address of a variable?
(B) &
8. How much memory is consumed by a pointer on a 64-bit system?
(B) 8 bytes
9. If p is a pointer to an integer, what is p + 1?
(A) The address of the next integer
10. Which of the following is an invalid pointer arithmetic operation?
(C) Adding two pointers
11. A pointer in C is stored in:
(A) Stack memory
12. What happens if you try to dereference a null pointer?
(B) It causes undefined behavior
13. What does int **ptr; represent?
(A) Pointer to a pointer
14. Which operator is used to dereference a pointer?
(C) *
15. What is the output of the following code?
                int a = 20;
                int *p = &a;
                printf("%p", p);
                
(B) Address of a
16. What is a dangling pointer?
(B) A pointer pointing to memory that has been freed
17. Which of the following is the correct way to allocate memory dynamically?
(D) All of the above
18. Which of the following statements is true about pointer arithmetic?
(A) Only addition and subtraction are allowed
19. What is the output of the following code?
                int a = 5;
                int *p = &a;
                p++;
                printf("%d", *p);
                
(B) Random value
20. Which of the following is a valid use of a pointer?
(D) All of the above

5.5.2 - Pointers to Array and Functions.


21. Which of the following is used to refer to the base address of an array?
(D) Both A and B
22. How do you declare a pointer to an array of 5 integers?
(B) int (*p)[5];
23. What does p = &a; do if a is an array?
(A) Assigns the base address of the array a to p
24. What is the output of printf("%u", *p); if p is a pointer to an array a[]?
(B) Address of the first element
25. Which of the following is true about pointers to functions?
(A) They hold the address of a function
26. How do you declare a pointer to a function that returns an int and takes two int arguments?
(B) int (*p)(int, int);
27. What is the purpose of the clrscr() function in the example provided?
(A) Clear the console screen
28. Which of the following is valid syntax for calling a function using a function pointer?
(C) Both A and B
29. What will gets(a) do in the string example provided?
(A) Input a string from the user and store it in a
30. Which of the following is true about the pointer p in the string example?
(C) Both A and B
31. In the string example, what will the expression *p return?
(B) The value of the first character
32. What does *p += 32 do in the string conversion example?
(A) Converts an uppercase letter to lowercase
33. What is the output of the matrix example if the matrix contains only zeros?
(A) 0
34. What does the expression p = &a[0][0]; do in the matrix example?
(A) Assigns the base address of the matrix to p
35. In the matrix example, what will sum = sum + *p; do?
(A) Add the value pointed to by p to sum
36. What is the correct way to pass a 2D array to a function using pointers?
(A) int (*p)[N]
37. What is a NULL pointer in C?
(A) A pointer that doesn't point to any valid address
38. What does the dereference operator (*) do in C?
(B) It retrieves the value stored at the address pointed to by a pointer
39. What happens when you increment a pointer in C?
(A) The pointer moves to the next memory address according to the type it points to
40. What does *p++ mean in pointer arithmetic?
(A) Dereference p and then increment the pointer

5.5.3 - Array of Pointers.


41. What is the correct syntax for declaring a pointer to an array in C?
(B) int (*p)[5];
42. Which of the following is a valid way to initialize a pointer to the first element of an array?
(B) int *p = &array[0];
43. What does the expression *(p + 1) represent in pointer arithmetic when p points to the first element of an array?
(B) The second element
44. Which of the following statements about arrays and pointers is true?
(C) Array names are constants and cannot be changed
45. Which operator is used to get the address of the first element of an array?
(B) &
46. What is the output of the following code?
    int arr[] = {1, 2, 3};
    int *p = arr;
    printf("%d", *(p + 1));
    
(B) 2
47. Which of the following operations is invalid for array names?
(A) Incrementing the array name
48. Which of the following declares a pointer to a function?
(A) int (*p)(int, int);
49. If int arr[5] and int *p = arr;, which expression correctly accesses the third element of the array?
(A) *(p + 2)
50. What is the output of the following code?
    int arr[] = {10, 20, 30, 40};
    int *p = arr + 2;
    printf("%d", *p);
    
(C) 30
51. Which statement correctly passes an array to a function?
(C) func(array);
52. In pointer arithmetic, what is the result of subtracting two pointers?
(B) An integer representing the difference in elements
53. What is the size of a pointer on a 64-bit machine?
(C) 8 bytes
54. Which of the following is NOT true about pointers and arrays in C?
(C) Arrays can be resized using pointers
55. Which of the following correctly accesses the value at the second index of the array using pointer notation?
(A) *(array + 2)
56. Which of the following is a valid way to declare a pointer to an array of integers?
(B) int (*arr)[5];
57. Which of the following is true about the relationship between arrays and pointers?
(B) Array elements can be accessed using pointers
58. In C, how can you pass an entire array to a function?
(C) Both A and B
59. What is the difference between array and &array[0] in C?
(B) array points to the entire array, &array[0] points to the first element
60. What is the output of the following code?
    int arr[] = {5, 10, 15};
    int *p = arr;
    p += 2;
    printf("%d", *p);
    
(C) 15
61. What does the following code snippet do? *pArray1++ = 1;
(A) Assigns 1 to the current element pointed by pArray1 and increments the pointer.
62. What does the expression *(pArray++) do?
(B) Dereferences the pointer and then increments the pointer.
63. What happens if you remove parentheses from the expression *(pArray2 + i)?
(D) It will change the memory address calculation.
64. Which of the following is the correct way to declare an array in C?
(A) int array[5];
65. What is the result of the expression (*pArray)++?
(B) It increments the value stored at the memory location pointed by pArray.
66. What is the difference between *pArray++ and ++*pArray?
(B) *pArray++ increments the pointer, and ++*pArray increments the value at the pointer.
67. Which of the following statements is true about pointers in C?
(B) A pointer can store the address of another pointer.
68. What is the value of array[0] after executing the code int array[3] = {1, 2, 3};?
(B) 1
69. Which of the following is a valid use of a pointer?
(D) All of the above
70. What is the output of the following code? printf("%p", &array[0]);
(A) Prints the address of the first element of the array.
71. What does arrayName represent in C when dealing with arrays?
(B) The address of the first element
72. Which function is commonly used for dynamic memory allocation in C?
(A) malloc()
73. What is the purpose of free() in C?
(A) It deallocates dynamically allocated memory.
74. In C, how is a pointer incremented to traverse an array?
(A) By adding 1 to the pointer value.
75. Which operator is used to dereference a pointer?
(B) *
76. What will happen if you increment an array name directly in C?
(B) It will cause a compile-time error.
77. Which function is used to dynamically allocate an array?
(B) calloc()
78. What is the purpose of *(pArray++) in the context of pointer arithmetic?
(B) Dereference the pointer and increment it afterwards.
79. What is the result of the statement pArray1++?
(B) The pointer is moved to the next element.
80. In C, how can you initialize an array and pointer at the same time?
(A) int array[5] = {1, 2, 3, 4, 5}; int *pArray = array;

5.5.4 - Passing Arrays as Arguments.


81. What type of argument is passed when an array is supplied to a function in C?
Correct Answer: (B) By reference
82. What does passing an array to a function in C actually pass?
Correct Answer: (B) A pointer to the first element
83. When assigning an array to another variable, what is copied?
Correct Answer: (B) The memory address of the first element
84. Which of the following methods can be used to pass an array to a function in C?
Correct Answer: (C) Both A and B
85. In the function prototype void traverse1(int size, int arr[]), what does arr[] signify?
Correct Answer: (C) An array of integers
86. Which of the following is NOT a valid way to traverse an array in C?
Correct Answer: (D) Using an if statement
87. What is the output of *array if array is an integer array initialized as int array[5] = {10, 20, 30, 40, 50};?
Correct Answer: (B) 10
88. In the function traverse2(int size, int* pArr), how is the array accessed?
Correct Answer: (C) Using both A and B
89. Which of the following correctly increments a pointer inside a for loop?
Correct Answer: (C) Both A and B
90. When passing an array to a function, which notation can be used to access the elements?
Correct Answer: (C) Both A and B
91. What does &arr[i] return in the context of array access?
Correct Answer: (B) The address of the i-th element
92. What is the main advantage of passing arrays as pointers to functions?
Correct Answer: (B) Memory efficiency
93. Which of the following statements is true about arrays and pointers in C?
Correct Answer: (A) Arrays and pointers are interchangeable
94. In the function prototype void traverse3(int size, int arr[]), what does size represent?
Correct Answer: (B) The number of elements in the array
95. What is the purpose of using a pointer to an array as a function parameter?
Correct Answer: (B) To allow modification of the original array
96. Which loop structure is used to traverse the array in the provided examples?
Correct Answer: (A) For loop
97. What is the output of the following statement: printf("%p", &array);?
Correct Answer: (B) The address of the array
98. In the function traverse4(int size, int* pArr), what notation is used to access the array elements?
Correct Answer: (C) Both A and B
99. What happens if you try to assign one array to another in C?
Correct Answer: (B) The address of the first array is copied
100. Which statement is true about the memory allocation of arrays in C?
Correct Answer: (B) Arrays have a fixed size
101. When is it appropriate to use pointer notation instead of array notation?
Correct Answer: (B) When pointer arithmetic is needed
102. In the function prototype void traverse2(int size, int* pArr), what is the role of pArr?
Correct Answer: (B) It points to the first element of the array
103. Which of the following is a valid pointer declaration in C?
Correct Answer: (B) int* pArr;
104. What does the term "pointer arithmetic" refer to?
Correct Answer: (A) Adding or subtracting from the address of a pointer
105. In the context of arrays and pointers, which of the following statements is correct?
Correct Answer: (C) Both A and B
106. What will the expression sizeof(array) return for an integer array of size 5?
Correct Answer: (B) 20
107. In C, how are multi-dimensional arrays accessed?
Correct Answer: (A) Using nested for loops
108. What is the benefit of passing arrays to functions in C?
Correct Answer: (C) Both A and B
109. When using pointers in C, what must you always ensure?
Correct Answer: (C) Both A and B
110. What is the primary reason for using function prototypes in C?
Correct Answer: (C) To enable type checking

5.5.5 - Pointers and 2D Array.


111. Which of the following correctly declares a standard 2D array in C?
Correct Answer: (A) int array[3][5]
112. What does the statement int* arrayPtr[3] declare?
Correct Answer: (B) An array of three integer pointers
113. How are elements accessed in a standard 2D array?
Correct Answer: (D) Both A and C
114. Which of the following will correctly initialize a standard 2D array?
Correct Answer: (D) Both B and C
115. What is the purpose of the line pInteger = &(arrayStd[0][0]);?
Correct Answer: (C) To initialize the pointer to the first element
116. In the context of the array of pointers, how do you access the second element of the first sub-array?
Correct Answer: (B) arrayPtr[0][1]
117. Which statement is true about the memory layout of a standard 2D array?
Correct Answer: (A) It is stored in a contiguous block
118. What is the output of the statement printf("%2d", arrayStd[1][3]); given the standard 2D array is initialized as shown?
Correct Answer: (B) 24
119. In the provided code, which of the following will correctly traverse the arrayPtr using pointers?
Correct Answer: (D) Both A and C
120. When using pointer arithmetic with a standard 2D array, what is the correct way to increment the pointer?
Correct Answer: (D) Both A and B
121. Which of the following is a valid way to declare sub-arrays for the pointer array?
Correct Answer: (A) int* arrayPtr[3] = {array1, array2, array3};
122. Which of the following expressions correctly accesses the third element of the second row of arrayStd?
Correct Answer: (A) arrayStd[1][2]
123. What will happen if you attempt to access an element outside the bounds of the arrayPtr?
Correct Answer: (C) It will return a random value
124. Which of the following statements correctly initializes the pointer to the first element of the second sub-array?
Correct Answer: (D) Both A and B
125. In pointer arithmetic, how do you move to the next sub-array?
Correct Answer: (A) pInteger += 1
126. What will the output of the loop for (int i=0; i<3; i++) { printf("%d", arrayPtr[i][0]); } be given the initial values?
Correct Answer: (A) 11 21 31
127. How do you declare a pointer to a pointer in the context of an array of pointers?
Correct Answer: (D) Both A and C
128. Which of the following is true regarding the access of elements in the arrayStd vs. arrayPtr?
Correct Answer: (A) They can both be accessed using the same syntax
129. Which of the following statements is true regarding the initialization of arrayPtr?
Correct Answer: (D) All of the above
130. In the provided code, what will be the effect of setting #define EXPERIMENT 1?
Correct Answer: (B) It will incorrectly traverse the array as if it were contiguous

5.5.6 - Dangling Pointers.


131. What is a dangling pointer?
Correct Answer: (B) A pointer that points to de-allocated memory
132. Which of the following causes a dangling pointer?
Correct Answer: (C) Freeing memory without resetting the pointer
133. What happens if you dereference a dangling pointer?
Correct Answer: (B) The program may crash or produce undefined behavior
134. How can you prevent a dangling pointer after freeing memory?
Correct Answer: (B) Set the pointer to NULL
135. In the following code, what happens to ptr after free(ptr)?
            int *ptr = (int *)malloc(sizeof(int));
            free(ptr);
                
Correct Answer: (B) ptr is now a dangling pointer
136. Which of the following statements is true about a pointer that points to a local variable?
Correct Answer: (A) It becomes a dangling pointer when the function returns
137. In the following code, what will happen to str after the inner block ends?
            char *str;
            {
                char a = 'A';
                str = &a;
            }
                
Correct Answer: (C) str becomes a dangling pointer
138. What is the output of the following code if str becomes a dangling pointer?
            printf("%s", str);
                
Correct Answer: (B) It will cause a segmentation fault
139. Which of the following is a safe practice to avoid dangling pointers?
Correct Answer: (B) Always initializing pointers to NULL
140. What is the result of attempting to dereference a pointer after freeing its memory?
Correct Answer: (B) The program may behave unpredictably
141. In the context of memory management, which function is used to deallocate memory?
Correct Answer: (D) free()
142. What does it mean when a pointer is "wild"?
Correct Answer: (C) It points to a location that has been de-allocated
143. How can the issue of dangling pointers be detected in a program?
Correct Answer: (D) All of the above
144. Which statement is true regarding the lifetime of a pointer?
Correct Answer: (B) A pointer can outlive the variable it points to
145. In the provided example, what would be a better approach to handle pointers?
            int *ptr = (int *)malloc(sizeof(int));
            free(ptr);
            ptr = NULL; // Is this a good practice?
                
Correct Answer: (A) Yes, it prevents dangling pointers
146. What is the danger of not setting a pointer to NULL after freeing it?
Correct Answer: (B) It can cause the pointer to point to invalid memory
147. Which of the following can help manage memory effectively in C?
Correct Answer: (A) Using pointers carefully
148. How does the use of local variables influence dangling pointers?
Correct Answer: (C) They can cause pointers to become dangling when they go out of scope
149. What is the best practice after using free() on a pointer?
Correct Answer: (C) Set it to NULL
150. Why is it important to manage dangling pointers in C?
Correct Answer: (A) To prevent segmentation faults and memory corruption

5.5.7 - Function Returning Pointers.


151. What does a pointer in C hold?
Correct Answer: (C) The address of another variable in memory
152. Why is it not advisable to return a pointer to a local variable from a function?
Correct Answer: (B) The memory for local variables is released after the function exits
153. What will the following function return?
int* fun() { int A = 10; return &A; }
Correct Answer: (C) A pointer to an invalid memory location
154. In the context of returning pointers, what is the advantage of using static variables?
Correct Answer: (B) They preserve their value even after going out of scope
155. What does the following program print?
#include 
            int* fun() {
                static int A = 10;
                return &A;
            }
            int main() {
                int* p = fun();
                printf("%d\n", *p);
            }
Correct Answer: (B) 10
156. What is the purpose of the static keyword in the context of the fun function?
Correct Answer: (C) To keep the variable's value between function calls
157. What happens to the pointer returned by the fun function if A is not static?
Correct Answer: (C) It becomes a dangling pointer
158. Which of the following is true regarding the output of the program when returning a pointer to a static variable?
Correct Answer: (B) It will print a valid address and the value of the variable
159. What is the correct way to declare a function that returns a pointer to an integer?
Correct Answer: (C) int* fun()
160. How can you correctly return a pointer from a function?
Correct Answer: (B) By returning the address of a static variable
161. What will happen if you try to dereference a pointer to a local variable after the function exits?
Correct Answer: (B) It will lead to undefined behavior
162. What does the printf("%p\n", p); statement do in the provided code?
Correct Answer: (C) Prints the address pointed to by p
163. Which statement is correct about the lifetime of a static variable?
Correct Answer: (C) It exists for the entire duration of the program
164. In the context of the second program, what is the output of printf("%p\n", p);?
Correct Answer: (B) The address of the variable A
165. When is it safe to return a pointer from a function?
Correct Answer: (B) When pointing to a static variable
166. Which function cannot return a pointer?
Correct Answer: (A) void fun()
167. What will happen if the static variable in the fun function is removed?
Correct Answer: (C) It will cause a segmentation fault
168. What is the main takeaway when dealing with functions returning pointers in C?
Correct Answer: (B) Use static variables for returning pointers safely
169. Which of the following is NOT a valid way to manage memory with pointers?
Correct Answer: (B) Returning pointers to local variables
170. What is the output of the function fun() if called multiple times without changing its definition?
Correct Answer: (B) The same address every time