Home Subjects C Programming Pointers

C Programming
Pointers

C language from basics to advanced placement prep

21 Q 10 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 11–20 of 21
Topics in C Programming
Q.11 Hard Pointers
What is the primary use of const pointer (int * const p)?
A Prevent modification of the value pointed to
B Prevent modification of the pointer itself
C Prevent allocation of memory
D Prevent deallocation of memory
Correct Answer:  B. Prevent modification of the pointer itself
EXPLANATION

int * const p is a constant pointer - the pointer address cannot be changed, but the value it points to can be modified.

Test
Q.12 Hard Pointers
What does realloc() do?
A Allocates new memory block
B Resizes an existing allocated memory block and returns new address
C Frees memory and allocates new
D Initializes memory to zero
Correct Answer:  B. Resizes an existing allocated memory block and returns new address
EXPLANATION

realloc() changes the size of previously allocated memory. It may return the same address or a new one depending on availability.

Test
Q.13 Hard Pointers
What will be the output?
int arr[3][3] = {{1,2,3}, {4,5,6}, {7,8,9}};
int *p = (int *)arr;
printf("%d", *(p+5));
A 5
B 6
C 8
D Garbage value
Correct Answer:  B. 6
EXPLANATION

Converting 2D array to int pointer treats it as 1D. p+5 points to the 6th element (0-indexed), which is 6.

Test
Q.14 Hard Pointers
What is the relationship between arrays and pointers in C?
A Arrays are pointers
B Pointers are arrays
C Array names decay to pointers to their first element in most contexts
D No relationship
Correct Answer:  C. Array names decay to pointers to their first element in most contexts
EXPLANATION

In most contexts, array names automatically decay to pointers to their first element. This is why array[i] is equivalent to *(array+i).

Test
Q.15 Hard Pointers
A programmer uses a pointer variable but forgets to initialize it before dereferencing. Which type of error will occur?
A Compile-time error
B Syntax error
C Runtime error or undefined behavior
D Logical error
Correct Answer:  C. Runtime error or undefined behavior
EXPLANATION

Uninitialized pointers contain garbage values. Dereferencing them accesses arbitrary memory locations, causing runtime errors or undefined behavior.

Test
Q.16 Hard Pointers
Consider: int *p = (int *)malloc(5 * sizeof(int)); for(int i=0; i
A delete p;
B free(p);
C free(&p);
D p = NULL;
Correct Answer:  B. free(p);
EXPLANATION

malloc() allocated memory must be freed using free(p), not free(&p). After freeing, it's good practice to set p = NULL.

Test
Q.17 Hard Pointers
In a function that returns a pointer, which of the following is UNSAFE?
A Returning a pointer to dynamically allocated memory (malloc)
B Returning a pointer to a local variable
C Returning a pointer to a global variable
D Returning a pointer to a static variable
Correct Answer:  B. Returning a pointer to a local variable
EXPLANATION

Returning a pointer to a local variable is unsafe because the variable ceases to exist after the function returns, creating a dangling pointer.

Test
Q.18 Hard Pointers
What is the output?
int x = 50;
int *p = &x;
int *q = p;
q = NULL;
printf("%d", *p);
A 50
B NULL
C Garbage value
D Segmentation fault
Correct Answer:  A. 50
EXPLANATION

Setting q to NULL doesn't affect p. p still points to x, so *p is 50.

Test
Q.19 Hard Pointers
Which statement is true about void pointers?
A Cannot be dereferenced without casting
B Can only point to void type
C Are always NULL
D Cannot be incremented
Correct Answer:  A. Cannot be dereferenced without casting
EXPLANATION

void pointers are generic pointers that must be cast to appropriate type before dereferencing.

Test
Q.20 Hard Pointers
What does the following code do?
int *p = (int*)malloc(sizeof(int));
*p = 5;
free(p);
p = NULL;
A Allocates, assigns, deallocates and nullifies pointer
B Creates a memory leak
C Causes segmentation fault
D Compilation error
Correct Answer:  A. Allocates, assigns, deallocates and nullifies pointer
EXPLANATION

This is proper memory management: allocate, use, free, and nullify to prevent dangling pointer.

Test
IGET
IGET AI
Online · Exam prep assistant
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips