Home Subjects C Programming

C Programming

C language from basics to advanced placement prep

198 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 91–100 of 198
Topics in C Programming
Q.91 Hard Pointers
What will be printed?
int *p = (int*)malloc(5 * sizeof(int));
int *q = p;
p = NULL;
free(q);
A Compilation error
B Memory leak
C Program runs successfully
D Double free error
Correct Answer:  C. Program runs successfully
EXPLANATION

q still holds the address even though p is NULL. free(q) properly deallocates memory.

Take Test
Q.92 Hard Pointers
What is malloc(0) likely to return?
A NULL pointer
B Valid pointer to 0 bytes
C Garbage value
D Compilation error
Correct Answer:  B. Valid pointer to 0 bytes
EXPLANATION

malloc(0) behavior is implementation-defined but typically returns a valid pointer. Freeing it is safe.

Take Test
Q.93 Hard Pointers
Consider: int arr[] = {1,2,3,4,5}; int *p = arr + 2; What is arr - p?
A -2
B 2
C Address difference
D Compilation error
Correct Answer:  A. -2
EXPLANATION

arr points to arr[0], p points to arr[2]. arr - p = -2 (p is 2 positions ahead).

Take Test
Q.94 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.

Take Test
Q.95 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.

Take Test
Q.96 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.

Take Test
Q.97 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).

Take Test
Q.98 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.

Take Test
Q.99 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.

Take Test
Q.100 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.

Take Test
IGET
iget AI
Online · Ask anything about exams
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