5.1 - Pointers.


Back Button Home

5.5.1 - Overview of Pointers.


1. A pointer in C is used to store:
  • (A) The address of a variable
  • (B) The value of a variable
  • (C) The size of a variable
  • (D) None of the above
(A) The address of a variable
2. Which of the following is the correct way to declare a pointer in C?
  • (A) int ptr;
  • (B) int *ptr;
  • (C) ptr int;
  • (D) *ptr int;
(B) int *ptr;
3. What does the following statement do: int *ptr;?
  • (A) Declares a pointer
  • (B) Declares a normal variable
  • (C) Declares a pointer and assigns it to a value
  • (D) None of the above
(A) Declares a pointer
4. What is the output of the following code?
                int a = 10;
                int *p = &a;
                printf("%d", *p);
                
  • (A) Memory address of a
  • (B) 10
  • (C) Error
  • (D) None of the above
(B) 10
5. What is a null pointer?
  • (A) A pointer that does not point to any memory location
  • (B) A pointer that points to a random memory location
  • (C) A pointer that points to the first memory address
  • (D) None of the above
(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?
  • (A) p
  • (B) *p
  • (C) &p
  • (D) p*
(B) *p
7. Which operator is used to get the address of a variable?
  • (A) *
  • (B) &
  • (C) %
  • (D) $
(B) &
8. How much memory is consumed by a pointer on a 64-bit system?
  • (A) 4 bytes
  • (B) 8 bytes
  • (C) 16 bytes
  • (D) Depends on the pointer type
(B) 8 bytes
9. If p is a pointer to an integer, what is p + 1?
  • (A) The address of the next integer
  • (B) The address of the next byte
  • (C) The value of the next integer
  • (D) None of the above
(A) The address of the next integer
10. Which of the following is an invalid pointer arithmetic operation?
  • (A) Addition of a number to a pointer
  • (B) Subtraction of a number from a pointer
  • (C) Adding two pointers
  • (D) Subtracting two pointers
(C) Adding two pointers
11. A pointer in C is stored in:
  • (A) Stack memory
  • (B) Heap memory
  • (C) RAM
  • (D) Register
(A) Stack memory
12. What happens if you try to dereference a null pointer?
  • (A) Nothing
  • (B) It causes undefined behavior
  • (C) It returns zero
  • (D) The program continues normally
(B) It causes undefined behavior
13. What does int **ptr; represent?
  • (A) Pointer to a pointer
  • (B) Pointer to an integer
  • (C) Pointer to a double
  • (D) Double pointer
(A) Pointer to a pointer
14. Which operator is used to dereference a pointer?
  • (A) &
  • (B) %
  • (C) *
  • (D) @
(C) *
15. What is the output of the following code?
                int a = 20;
                int *p = &a;
                printf("%p", p);
                
  • (A) Value of a
  • (B) Address of a
  • (C) Error
  • (D) None of the above
(B) Address of a
16. What is a dangling pointer?
  • (A) A pointer pointing to null
  • (B) A pointer pointing to memory that has been freed
  • (C) A pointer not initialized
  • (D) A pointer pointing to the wrong address
(B) A pointer pointing to memory that has been freed
17. Which of the following is the correct way to allocate memory dynamically?
  • (A) malloc()
  • (B) calloc()
  • (C) realloc()
  • (D) All of the above
(D) All of the above
18. Which of the following statements is true about pointer arithmetic?
  • (A) Only addition and subtraction are allowed
  • (B) Multiplication and division are allowed
  • (C) No arithmetic operations are allowed
  • (D) All arithmetic operations are allowed
(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);
                
  • (A) 5
  • (B) Random value
  • (C) Error
  • (D) None of the above
(B) Random value
20. Which of the following is a valid use of a pointer?
  • (A) Accessing array elements
  • (B) Dynamic memory allocation
  • (C) Function parameters
  • (D) All of the above
(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?
  • (A) The array name
  • (B) A pointer variable
  • (C) A reference variable
  • (D) Both A and B
(D) Both A and B
22. How do you declare a pointer to an array of 5 integers?
  • (A) int *p[5];
  • (B) int (*p)[5];
  • (C) int p[5];
  • (D) int *p;
(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
  • (B) Assigns the value of a to p
  • (C) Assigns the first element of a to p
  • (D) None of the above
(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[]?
  • (A) Address of the array
  • (B) Address of the first element
  • (C) Value of the first element
  • (D) Value of the last element
(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
  • (B) They hold the return type of a function
  • (C) They store the arguments of a function
  • (D) They are used to declare 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?
  • (A) int *p(int, int);
  • (B) int (*p)(int, int);
  • (C) int *p[];
  • (D) int p(int, int);
(B) int (*p)(int, int);
27. What is the purpose of the clrscr() function in the example provided?
  • (A) Clear the console screen
  • (B) Assign values to variables
  • (C) Initialize the pointer
  • (D) Display the result
(A) Clear the console screen
28. Which of the following is valid syntax for calling a function using a function pointer?
  • (A) z = (*p)(x, y);
  • (B) z = p(x, y);
  • (C) Both A and B
  • (D) None of the above
(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
  • (B) Convert the string to uppercase
  • (C) Clear the string
  • (D) Print the string
(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?
  • (A) p points to the first character of the string
  • (B) p holds the address of the string
  • (C) Both A and B
  • (D) None of the above
(C) Both A and B
31. In the string example, what will the expression *p return?
  • (A) The address of the first character
  • (B) The value of the first character
  • (C) The last character of the string
  • (D) The length of the string
(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
  • (B) Converts a lowercase letter to uppercase
  • (C) Adds 32 to the ASCII value
  • (D) Converts a number to a character
(A) Converts an uppercase letter to lowercase
33. What is the output of the matrix example if the matrix contains only zeros?
  • (A) 0
  • (B) Sum of elements
  • (C) 9
  • (D) 1
(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
  • (B) Assigns the value of the first element to p
  • (C) Initializes the matrix
  • (D) Assigns the last element of the matrix to p
(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
  • (B) Add the address of p to sum
  • (C) Multiply sum by the value of p
  • (D) Increment sum by 1
(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]
  • (B) int p[][N]
  • (C) int p[][]
  • (D) int *p[N]
(A) int (*p)[N]
37. What is a NULL pointer in C?
  • (A) A pointer that doesn't point to any valid address
  • (B) A pointer that points to a valid address
  • (C) A pointer that points to zero
  • (D) A pointer that holds a function's address
(A) A pointer that doesn't point to any valid address
38. What does the dereference operator (*) do in C?
  • (A) It retrieves the address of a variable
  • (B) It retrieves the value stored at the address pointed to by a pointer
  • (C) It increments the pointer
  • (D) It assigns a value to the pointer
(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
  • (B) The pointer decreases
  • (C) The pointer remains the same
  • (D) The pointer moves to the previous address
(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
  • (B) Increment p and then dereference it
  • (C) Increment the value pointed to by p
  • (D) Decrement the pointer
(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?
  • (A) int *p[5];
  • (B) int (*p)[5];
  • (C) int p[5];
  • (D) int p[5][5];
(B) int (*p)[5];
42. Which of the following is a valid way to initialize a pointer to the first element of an array?
  • (A) int *p = array[0];
  • (B) int *p = &array[0];
  • (C) int p = array[0];
  • (D) int *p = &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?
  • (A) The first element
  • (B) The second element
  • (C) The third element
  • (D) The last element
(B) The second element
44. Which of the following statements about arrays and pointers is true?
  • (A) Arrays are pointers
  • (B) Arrays and pointers are the same
  • (C) Array names are constants and cannot be changed
  • (D) Pointers can be incremented, but arrays cannot
(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?
  • (A) *
  • (B) &
  • (C) []
  • (D) +
(B) &
46. What is the output of the following code?
    int arr[] = {1, 2, 3};
    int *p = arr;
    printf("%d", *(p + 1));
    
  • (A) 1
  • (B) 2
  • (C) 3
  • (D) Error
(B) 2
47. Which of the following operations is invalid for array names?
  • (A) Incrementing the array name
  • (B) Getting the address of the array name
  • (C) Passing the array to a function
  • (D) Accessing array elements using a pointer
(A) Incrementing the array name
48. Which of the following declares a pointer to a function?
  • (A) int (*p)(int, int);
  • (B) int p(int, int);
  • (C) int *p(int, int);
  • (D) int p*(int, int);
(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)
  • (B) p[3]
  • (C) *(p + 3)
  • (D) p[2]
(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);
    
  • (A) 10
  • (B) 20
  • (C) 30
  • (D) 40
(C) 30
51. Which statement correctly passes an array to a function?
  • (A) func(&array);
  • (B) func(*array);
  • (C) func(array);
  • (D) func(array[0]);
(C) func(array);
52. In pointer arithmetic, what is the result of subtracting two pointers?
  • (A) A pointer
  • (B) An integer representing the difference in elements
  • (C) The memory address difference
  • (D) An error
(B) An integer representing the difference in elements
53. What is the size of a pointer on a 64-bit machine?
  • (A) 2 bytes
  • (B) 4 bytes
  • (C) 8 bytes
  • (D) 16 bytes
(C) 8 bytes
54. Which of the following is NOT true about pointers and arrays in C?
  • (A) A pointer can be used to traverse an array
  • (B) An array name can be assigned to a pointer
  • (C) Arrays can be resized using pointers
  • (D) Pointer arithmetic can be performed on an array
(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)
  • (B) *(array + 1)
  • (C) array[2]
  • (D) array[1]
(A) *(array + 2)
56. Which of the following is a valid way to declare a pointer to an array of integers?
  • (A) int *arr[5];
  • (B) int (*arr)[5];
  • (C) int arr*;
  • (D) int *arr[];
(B) int (*arr)[5];
57. Which of the following is true about the relationship between arrays and pointers?
  • (A) Arrays can be resized using pointers
  • (B) Array elements can be accessed using pointers
  • (C) Array names are mutable
  • (D) Arrays are dynamically allocated by default
(B) Array elements can be accessed using pointers
58. In C, how can you pass an entire array to a function?
  • (A) By passing the array name
  • (B) By passing a pointer to the first element
  • (C) Both A and B
  • (D) None of the above
(C) Both A and B
59. What is the difference between array and &array[0] in C?
  • (A) They are the same
  • (B) array points to the entire array, &array[0] points to the first element
  • (C) array gives the address of the array, &array[0] gives the address of the first element
  • (D) &array[0] points to the entire array
(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);
    
  • (A) 5
  • (B) 10
  • (C) 15
  • (D) 20
(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.
  • (B) Assigns 1 to the current element without incrementing the pointer.
  • (C) Increments the pointer and then assigns 1 to the element.
  • (D) Assigns 1 to the next element pointed by pArray1.
(A) Assigns 1 to the current element pointed by pArray1 and increments the pointer.
62. What does the expression *(pArray++) do?
  • (A) Increments the value stored at the pointer.
  • (B) Dereferences the pointer and then increments the pointer.
  • (C) Increments the pointer first, then dereferences it.
  • (D) Both increments and dereferences the pointer simultaneously.
(B) Dereferences the pointer and then increments the pointer.
63. What happens if you remove parentheses from the expression *(pArray2 + i)?
  • (A) The code will produce an error.
  • (B) It will behave the same as before.
  • (C) It will cause incorrect pointer arithmetic.
  • (D) It will change the memory address calculation.
(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];
  • (B) int array{5};
  • (C) array int[5];
  • (D) int [5]array;
(A) int array[5];
65. What is the result of the expression (*pArray)++?
  • (A) It increments the pointer value.
  • (B) It increments the value stored at the memory location pointed by pArray.
  • (C) It increments both the value and the pointer.
  • (D) It increments the value before dereferencing the pointer.
(B) It increments the value stored at the memory location pointed by pArray.
66. What is the difference between *pArray++ and ++*pArray?
  • (A) Both expressions increment the value at pArray.
  • (B) *pArray++ increments the pointer, and ++*pArray increments the value at the pointer.
  • (C) ++*pArray increments the pointer, and *pArray++ increments the value at the pointer.
  • (D) Both expressions increment the pointer.
(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?
  • (A) Pointers can only point to integer values.
  • (B) A pointer can store the address of another pointer.
  • (C) Pointers cannot be dereferenced.
  • (D) Pointers can point to different data types, but not arrays.
(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};?
  • (A) 0
  • (B) 1
  • (C) 2
  • (D) Undefined
(B) 1
69. Which of the following is a valid use of a pointer?
  • (A) Accessing array elements
  • (B) Dynamic memory allocation
  • (C) Function parameters
  • (D) All of the above
(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.
  • (B) Prints the value of the first element of the array.
  • (C) Prints the address of the entire array.
  • (D) Prints the memory size of the array.
(A) Prints the address of the first element of the array.
71. What does arrayName represent in C when dealing with arrays?
  • (A) The number of elements in the array
  • (B) The address of the first element
  • (C) The address of the last element
  • (D) A constant pointer to the entire array
(B) The address of the first element
72. Which function is commonly used for dynamic memory allocation in C?
  • (A) malloc()
  • (B) scanf()
  • (C) printf()
  • (D) free()
(A) malloc()
73. What is the purpose of free() in C?
  • (A) It deallocates dynamically allocated memory.
  • (B) It creates a new dynamic array.
  • (C) It prints a pointer value.
  • (D) It reallocates the memory block.
(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.
  • (B) By multiplying the pointer value by the array size.
  • (C) By adding the array size to the pointer.
  • (D) By dividing the pointer by the array length.
(A) By adding 1 to the pointer value.
75. Which operator is used to dereference a pointer?
  • (A) &
  • (B) *
  • (C) ++
  • (D) --
(B) *
76. What will happen if you increment an array name directly in C?
  • (A) The pointer will move to the next element.
  • (B) It will cause a compile-time error.
  • (C) The array size will change.
  • (D) The value of the first element will increase.
(B) It will cause a compile-time error.
77. Which function is used to dynamically allocate an array?
  • (A) malloc()
  • (B) calloc()
  • (C) realloc()
  • (D) All of the above
(B) calloc()
78. What is the purpose of *(pArray++) in the context of pointer arithmetic?
  • (A) Increment the pointer and dereference it simultaneously.
  • (B) Dereference the pointer and increment it afterwards.
  • (C) Increment the value at the pointer location.
  • (D) Move to the next element of the array.
(B) Dereference the pointer and increment it afterwards.
79. What is the result of the statement pArray1++?
  • (A) The value at the current pointer is incremented.
  • (B) The pointer is moved to the next element.
  • (C) The pointer is reset to the first element.
  • (D) The array size is incremented.
(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;
  • (B) int array[5] = {1, 2, 3, 4, 5}; int pArray = array[0];
  • (C) int array[5] = {1, 2, 3, 4, 5}; array[0] = pArray;
  • (D) int array[5] = {1, 2, 3, 4, 5}; int *pArray = &array[5];
(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?
  • (A) By value
  • (B) By reference
  • (C) By pointer
  • (D) None of the above
Correct Answer: (B) By reference
82. What does passing an array to a function in C actually pass?
  • (A) The entire array
  • (B) A pointer to the first element
  • (C) The size of the array
  • (D) None of the above
Correct Answer: (B) A pointer to the first element
83. When assigning an array to another variable, what is copied?
  • (A) The entire array
  • (B) The memory address of the first element
  • (C) The size of the array
  • (D) None of the above
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?
  • (A) Using array notation
  • (B) Using pointer notation
  • (C) Both A and B
  • (D) None of the above
Correct Answer: (C) Both A and B
85. In the function prototype void traverse1(int size, int arr[]), what does arr[] signify?
  • (A) An integer
  • (B) A pointer to an integer
  • (C) An array of integers
  • (D) A reference to an integer
Correct Answer: (C) An array of integers
86. Which of the following is NOT a valid way to traverse an array in C?
  • (A) Using a for loop
  • (B) Using a while loop
  • (C) Using a do-while loop
  • (D) Using an if statement
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};?
  • (A) 20
  • (B) 10
  • (C) 30
  • (D) 50
Correct Answer: (B) 10
88. In the function traverse2(int size, int* pArr), how is the array accessed?
  • (A) Using array notation
  • (B) Using pointer arithmetic
  • (C) Using both A and B
  • (D) None of the above
Correct Answer: (C) Using both A and B
89. Which of the following correctly increments a pointer inside a for loop?
  • (A) pArr++
  • (B) ++pArr
  • (C) Both A and B
  • (D) None of the above
Correct Answer: (C) Both A and B
90. When passing an array to a function, which notation can be used to access the elements?
  • (A) Array notation
  • (B) Pointer notation
  • (C) Both A and B
  • (D) None of the above
Correct Answer: (C) Both A and B
91. What does &arr[i] return in the context of array access?
  • (A) The value at index i
  • (B) The address of the i-th element
  • (C) The size of the array
  • (D) None of the above
Correct Answer: (B) The address of the i-th element
92. What is the main advantage of passing arrays as pointers to functions?
  • (A) Improved readability
  • (B) Memory efficiency
  • (C) Code simplicity
  • (D) None of the above
Correct Answer: (B) Memory efficiency
93. Which of the following statements is true about arrays and pointers in C?
  • (A) Arrays and pointers are interchangeable
  • (B) Arrays can be resized dynamically
  • (C) Pointers cannot point to arrays
  • (D) None of the above
Correct Answer: (A) Arrays and pointers are interchangeable
94. In the function prototype void traverse3(int size, int arr[]), what does size represent?
  • (A) The address of the array
  • (B) The number of elements in the array
  • (C) The size of the data type
  • (D) None of the above
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?
  • (A) To pass the array by value
  • (B) To allow modification of the original array
  • (C) To avoid pointer arithmetic
  • (D) None of the above
Correct Answer: (B) To allow modification of the original array
96. Which loop structure is used to traverse the array in the provided examples?
  • (A) For loop
  • (B) While loop
  • (C) Do-while loop
  • (D) None of the above
Correct Answer: (A) For loop
97. What is the output of the following statement: printf("%p", &array);?
  • (A) The value of the first element
  • (B) The address of the array
  • (C) The size of the array
  • (D) None of the above
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?
  • (A) Pointer notation
  • (B) Array notation
  • (C) Both A and B
  • (D) None of the above
Correct Answer: (C) Both A and B
99. What happens if you try to assign one array to another in C?
  • (A) Both arrays are identical
  • (B) The address of the first array is copied
  • (C) The entire array is copied
  • (D) None of the above
Correct Answer: (B) The address of the first array is copied
100. Which statement is true about the memory allocation of arrays in C?
  • (A) Arrays are dynamically allocated
  • (B) Arrays have a fixed size
  • (C) Both A and B
  • (D) None of the above
Correct Answer: (B) Arrays have a fixed size
101. When is it appropriate to use pointer notation instead of array notation?
  • (A) When the array is small
  • (B) When pointer arithmetic is needed
  • (C) When passing arrays to functions
  • (D) None of the above
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?
  • (A) It holds the size of the array
  • (B) It points to the first element of the array
  • (C) It stores the values of the array
  • (D) None of the above
Correct Answer: (B) It points to the first element of the array
103. Which of the following is a valid pointer declaration in C?
  • (A) int pArr[];
  • (B) int* pArr;
  • (C) int pArr*;
  • (D) None of the above
Correct Answer: (B) int* pArr;
104. What does the term "pointer arithmetic" refer to?
  • (A) Adding or subtracting from the address of a pointer
  • (B) Changing the value of a pointer
  • (C) Declaring multiple pointers
  • (D) None of the above
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?
  • (A) An array name is a constant pointer
  • (B) A pointer can be assigned an array name
  • (C) Both A and B
  • (D) None of the above
Correct Answer: (C) Both A and B
106. What will the expression sizeof(array) return for an integer array of size 5?
  • (A) 5
  • (B) 20
  • (C) 10
  • (D) 0
Correct Answer: (B) 20
107. In C, how are multi-dimensional arrays accessed?
  • (A) Using nested for loops
  • (B) Using single for loops
  • (C) Using pointers only
  • (D) None of the above
Correct Answer: (A) Using nested for loops
108. What is the benefit of passing arrays to functions in C?
  • (A) Faster execution
  • (B) Reduced memory usage
  • (C) Both A and B
  • (D) None of the above
Correct Answer: (C) Both A and B
109. When using pointers in C, what must you always ensure?
  • (A) The pointer is initialized
  • (B) The pointer points to valid memory
  • (C) Both A and B
  • (D) None of the above
Correct Answer: (C) Both A and B
110. What is the primary reason for using function prototypes in C?
  • (A) To increase execution speed
  • (B) To improve code readability
  • (C) To enable type checking
  • (D) None of the above
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?
  • (A) int array[3][5]
  • (B) int array[5][3]
  • (C) int* array[3]
  • (D) int array[3]
Correct Answer: (A) int array[3][5]
112. What does the statement int* arrayPtr[3] declare?
  • (A) An array of three integers
  • (B) An array of three integer pointers
  • (C) A pointer to an array of three integers
  • (D) A pointer to three arrays
Correct Answer: (B) An array of three integer pointers
113. How are elements accessed in a standard 2D array?
  • (A) array[i][j]
  • (B) array[j][i]
  • (C) arrayPtr[i][j]
  • (D) Both A and C
Correct Answer: (D) Both A and C
114. Which of the following will correctly initialize a standard 2D array?
  • (A) int array[3][5] = {1, 2, 3, 4, 5};
  • (B) int array[3][5] = {{1, 2, 3, 4, 5}};
  • (C) int array[3][5] = { {1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15} };
  • (D) Both B and C
Correct Answer: (D) Both B and C
115. What is the purpose of the line pInteger = &(arrayStd[0][0]);?
  • (A) To access the first element of the array
  • (B) To access the entire array
  • (C) To initialize the pointer to the first element
  • (D) To declare the pointer
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?
  • (A) arrayPtr[1][2]
  • (B) arrayPtr[0][1]
  • (C) arrayPtr[0][2]
  • (D) arrayPtr[1][1]
Correct Answer: (B) arrayPtr[0][1]
117. Which statement is true about the memory layout of a standard 2D array?
  • (A) It is stored in a contiguous block
  • (B) It is stored in non-contiguous blocks
  • (C) It cannot be accessed using pointer arithmetic
  • (D) It requires more memory than an array of pointers
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?
  • (A) 23
  • (B) 24
  • (C) 25
  • (D) 21
Correct Answer: (B) 24
119. In the provided code, which of the following will correctly traverse the arrayPtr using pointers?
  • (A) pInteger = arrayPtr[i];
  • (B) pInteger = &arrayPtr[i][j];
  • (C) pInteger = *(arrayPtr + i);
  • (D) Both A and C
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?
  • (A) pInteger++
  • (B) ++pInteger
  • (C) pInteger += 5
  • (D) Both A and B
Correct Answer: (D) Both A and B
121. Which of the following is a valid way to declare sub-arrays for the pointer array?
  • (A) int* arrayPtr[3] = {array1, array2, array3};
  • (B) int arrayPtr[3][5] = {array1, array2, array3};
  • (C) int arrayPtr[3] = {array1, array2, array3};
  • (D) int* arrayPtr[3] = {NULL};
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?
  • (A) arrayStd[1][2]
  • (B) arrayStd[2][1]
  • (C) arrayStd[1][3]
  • (D) arrayStd[0][2]
Correct Answer: (A) arrayStd[1][2]
123. What will happen if you attempt to access an element outside the bounds of the arrayPtr?
  • (A) It will return zero
  • (B) It will cause a segmentation fault
  • (C) It will return a random value
  • (D) It will be ignored
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?
  • (A) pInteger = arrayPtr[1];
  • (B) pInteger = &arrayPtr[1][0];
  • (C) pInteger = arrayPtr[1][0];
  • (D) Both A and B
Correct Answer: (D) Both A and B
125. In pointer arithmetic, how do you move to the next sub-array?
  • (A) pInteger += 1
  • (B) pInteger++
  • (C) pInteger += 5
  • (D) None of the above
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?
  • (A) 11 21 31
  • (B) 12 22 32
  • (C) 15 25 35
  • (D) 11 22 33
Correct Answer: (A) 11 21 31
127. How do you declare a pointer to a pointer in the context of an array of pointers?
  • (A) int** ptrPtr;
  • (B) int* ptr;
  • (C) int** arrayPtr;
  • (D) Both A and C
Correct Answer: (D) Both A and C
128. Which of the following is true regarding the access of elements in the arrayStd vs. arrayPtr?
  • (A) They can both be accessed using the same syntax
  • (B) Only arrayStd can be accessed using pointers
  • (C) They require different access methods
  • (D) None of the above
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?
  • (A) It must be done before assigning sub-arrays
  • (B) It can be done after the sub-arrays are declared
  • (C) It can be NULL initially
  • (D) All of the above
Correct Answer: (D) All of the above
130. In the provided code, what will be the effect of setting #define EXPERIMENT 1?
  • (A) It will correctly traverse the array
  • (B) It will incorrectly traverse the array as if it were contiguous
  • (C) It will not affect the output
  • (D) It will cause a compilation error
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?
  • (A) A pointer that points to a valid memory location
  • (B) A pointer that points to de-allocated memory
  • (C) A pointer that is uninitialized
  • (D) A pointer that points to an array
Correct Answer: (B) A pointer that points to de-allocated memory
132. Which of the following causes a dangling pointer?
  • (A) Using malloc() without checking for null
  • (B) Dereferencing a null pointer
  • (C) Freeing memory without resetting the pointer
  • (D) Initializing a pointer with a valid address
Correct Answer: (C) Freeing memory without resetting the pointer
133. What happens if you dereference a dangling pointer?
  • (A) The program will terminate immediately
  • (B) The program may crash or produce undefined behavior
  • (C) The pointer will become null
  • (D) The program will run normally
Correct Answer: (B) The program may crash or produce undefined behavior
134. How can you prevent a dangling pointer after freeing memory?
  • (A) Set the pointer to zero
  • (B) Set the pointer to NULL
  • (C) Leave the pointer unchanged
  • (D) Reallocate memory for the pointer
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);
                
  • (A) ptr points to the allocated memory
  • (B) ptr is now a dangling pointer
  • (C) ptr is reset to zero
  • (D) ptr becomes a valid pointer again
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?
  • (A) It becomes a dangling pointer when the function returns
  • (B) It remains valid after the function returns
  • (C) It cannot be dereferenced
  • (D) It points to static memory
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;
            }
                
  • (A) str will point to a valid memory address
  • (B) str will be NULL
  • (C) str becomes a dangling pointer
  • (D) str will contain the value '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);
                
  • (A) It will print the value of str
  • (B) It will cause a segmentation fault
  • (C) It will print "undefined"
  • (D) It will print an empty string
Correct Answer: (B) It will cause a segmentation fault
139. Which of the following is a safe practice to avoid dangling pointers?
  • (A) Using global variables
  • (B) Always initializing pointers to NULL
  • (C) Allocating memory without using free()
  • (D) Storing pointers in arrays
Correct Answer: (B) Always initializing pointers to NULL
140. What is the result of attempting to dereference a pointer after freeing its memory?
  • (A) The memory will be reallocated
  • (B) The program may behave unpredictably
  • (C) It will return the value that was previously stored
  • (D) The pointer will become valid again
Correct Answer: (B) The program may behave unpredictably
141. In the context of memory management, which function is used to deallocate memory?
  • (A) malloc()
  • (B) calloc()
  • (C) realloc()
  • (D) free()
Correct Answer: (D) free()
142. What does it mean when a pointer is "wild"?
  • (A) It points to a valid memory location
  • (B) It points to a memory location that has not been allocated
  • (C) It points to a location that has been de-allocated
  • (D) It is a null pointer
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?
  • (A) By checking if the pointer is NULL
  • (B) By using memory analysis tools
  • (C) By setting the pointer to a specific value
  • (D) All of the above
Correct Answer: (D) All of the above
144. Which statement is true regarding the lifetime of a pointer?
  • (A) A pointer's lifetime is independent of the variable it points to
  • (B) A pointer can outlive the variable it points to
  • (C) A pointer will always point to valid memory
  • (D) A pointer must be freed before it can go out of scope
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?
                
  • (A) Yes, it prevents dangling pointers
  • (B) No, it's unnecessary
  • (C) Yes, but only for global variables
  • (D) No, because it causes memory leaks
Correct Answer: (A) Yes, it prevents dangling pointers
146. What is the danger of not setting a pointer to NULL after freeing it?
  • (A) It can lead to memory leaks
  • (B) It can cause the pointer to point to invalid memory
  • (C) It can reset the pointer
  • (D) It has no effect
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?
  • (A) Using pointers carefully
  • (B) Avoiding the use of malloc()
  • (C) Using global variables for all data
  • (D) Allocating memory without freeing it
Correct Answer: (A) Using pointers carefully
148. How does the use of local variables influence dangling pointers?
  • (A) They cannot create dangling pointers
  • (B) They always lead to dangling pointers
  • (C) They can cause pointers to become dangling when they go out of scope
  • (D) They are not related to 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?
  • (A) Leave it unchanged
  • (B) Reassign it to another valid memory location
  • (C) Set it to NULL
  • (D) Dereference it to access the value
Correct Answer: (C) Set it to NULL
150. Why is it important to manage dangling pointers in C?
  • (A) To prevent segmentation faults and memory corruption
  • (B) To ensure all pointers are initialized
  • (C) To avoid using the free() function
  • (D) To make the code easier to read
Correct Answer: (A) To prevent segmentation faults and memory corruption

5.5.7 - Function Returning Pointers.


151. What does a pointer in C hold?
  • (A) A string value
  • (B) An integer value
  • (C) The address of another variable in memory
  • (D) The size of a variable
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?
  • (A) Local variables are stored in heap memory
  • (B) The memory for local variables is released after the function exits
  • (C) Pointers cannot be returned from functions
  • (D) Local variables do not exist
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; }
  • (A) A pointer to a static variable
  • (B) A pointer to an integer
  • (C) A pointer to an invalid memory location
  • (D) A pointer to a character
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?
  • (A) They are faster than local variables
  • (B) They preserve their value even after going out of scope
  • (C) They use less memory
  • (D) They cannot be changed
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);
            }
  • (A) 0
  • (B) 10
  • (C) Garbage value
  • (D) Compilation error
Correct Answer: (B) 10
156. What is the purpose of the static keyword in the context of the fun function?
  • (A) To make the variable global
  • (B) To allocate memory on the heap
  • (C) To keep the variable's value between function calls
  • (D) To declare a constant variable
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?
  • (A) It remains valid
  • (B) It points to a valid memory address
  • (C) It becomes a dangling pointer
  • (D) It causes a segmentation fault
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?
  • (A) It will print a random memory address
  • (B) It will print a valid address and the value of the variable
  • (C) It will print only the address
  • (D) It will cause a runtime error
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?
  • (A) void fun()
  • (B) int fun()
  • (C) int* fun()
  • (D) int fun()
Correct Answer: (C) int* fun()
160. How can you correctly return a pointer from a function?
  • (A) By using return statement only
  • (B) By returning the address of a static variable
  • (C) By returning the address of a local variable
  • (D) By using global variables
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?
  • (A) The program will work correctly
  • (B) It will lead to undefined behavior
  • (C) It will print a valid value
  • (D) It will cause a compile-time error
Correct Answer: (B) It will lead to undefined behavior
162. What does the printf("%p\n", p); statement do in the provided code?
  • (A) Prints the value of A
  • (B) Prints the address of the pointer p
  • (C) Prints the address pointed to by p
  • (D) Prints the size of p
Correct Answer: (C) Prints the address pointed to by p
163. Which statement is correct about the lifetime of a static variable?
  • (A) It lasts only for the duration of the function call
  • (B) It is automatically deallocated
  • (C) It exists for the entire duration of the program
  • (D) It exists only in the main function
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);?
  • (A) 0x0
  • (B) The address of the variable A
  • (C) A random address
  • (D) It will cause an error
Correct Answer: (B) The address of the variable A
165. When is it safe to return a pointer from a function?
  • (A) When pointing to a local variable
  • (B) When pointing to a static variable
  • (C) When the function has completed
  • (D) When using global variables
Correct Answer: (B) When pointing to a static variable
166. Which function cannot return a pointer?
  • (A) void fun()
  • (B) int* fun()
  • (C) char* fun()
  • (D) float* fun()
Correct Answer: (A) void fun()
167. What will happen if the static variable in the fun function is removed?
  • (A) The program will compile successfully
  • (B) The program will work without issues
  • (C) It will cause a segmentation fault
  • (D) The output will always be 0
Correct Answer: (C) It will cause a segmentation fault
168. What is the main takeaway when dealing with functions returning pointers in C?
  • (A) Always return pointers to local variables
  • (B) Use static variables for returning pointers safely
  • (C) Dereference pointers immediately after returning
  • (D) Avoid using pointers altogether
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?
  • (A) Using malloc() and free()
  • (B) Returning pointers to local variables
  • (C) Using static variables
  • (D) Using global variables
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?
  • (A) Different addresses each time
  • (B) The same address every time
  • (C) Random values
  • (D) A compile-time error
Correct Answer: (B) The same address every time