Pointers and Memory Management # MCQs Practice set

Q.1 What does a pointer in C store?

An integer value
A memory address
A character
A function
Explanation - Pointers are variables that store the memory address of another variable.
Correct answer is: A memory address

Q.2 Which operator is used to access the value stored at the address a pointer points to?

&
*
->
%
Explanation - The * operator is the dereference operator, used to access the value stored at the pointer’s address.
Correct answer is: *

Q.3 What does the '&' operator do in C?

Gives size of variable
Returns memory address
Dereferences pointer
Initializes pointer
Explanation - The & operator gives the address of a variable.
Correct answer is: Returns memory address

Q.4 What is NULL pointer?

Pointer to first element
Pointer to 0 address
Pointer not pointing to any valid memory
Pointer to garbage value
Explanation - A NULL pointer is a pointer that does not point to any valid memory location.
Correct answer is: Pointer not pointing to any valid memory

Q.5 Which of the following is the correct way to declare a pointer to an integer?

int p;
int *p;
pointer int p;
int &p;
Explanation - The correct syntax for declaring a pointer to an integer is int *p.
Correct answer is: int *p;

Q.6 Which memory allocation function in C initializes memory to zero?

malloc()
calloc()
realloc()
free()
Explanation - calloc() allocates and initializes the memory to zero.
Correct answer is: calloc()

Q.7 What happens if you dereference an uninitialized pointer?

Program runs normally
Compilation error
Undefined behavior
Pointer becomes NULL
Explanation - Dereferencing an uninitialized pointer leads to undefined behavior since it points to an unknown location.
Correct answer is: Undefined behavior

Q.8 What is the size of a pointer in a 64-bit system?

2 bytes
4 bytes
8 bytes
Depends on compiler
Explanation - On a 64-bit system, pointers typically take 8 bytes regardless of the data type they point to.
Correct answer is: 8 bytes

Q.9 Which function is used to free dynamically allocated memory in C?

malloc()
delete()
dealloc()
free()
Explanation - The free() function deallocates memory that was previously allocated with malloc(), calloc(), or realloc().
Correct answer is: free()

Q.10 Pointer arithmetic is not valid with which type?

int*
char*
void*
float*
Explanation - Pointer arithmetic is not valid on void* since it has no defined size.
Correct answer is: void*

Q.11 What will happen if malloc() fails to allocate memory?

Returns -1
Returns NULL
Program exits
Throws exception
Explanation - malloc() returns NULL if memory allocation fails.
Correct answer is: Returns NULL

Q.12 Which operator is used to access members of a structure through a pointer?

.
->
&
*
Explanation - The arrow operator (->) is used to access structure members via a pointer.
Correct answer is: ->

Q.13 What is dangling pointer?

Pointer initialized to NULL
Pointer pointing to deallocated memory
Uninitialized pointer
Pointer with garbage value
Explanation - Dangling pointers occur when a memory location is freed, but the pointer still references it.
Correct answer is: Pointer pointing to deallocated memory

Q.14 Which of the following dynamically resizes allocated memory?

malloc()
calloc()
realloc()
alloc()
Explanation - The realloc() function resizes previously allocated memory.
Correct answer is: realloc()

Q.15 Which keyword is used in C++ but not in C for memory deallocation?

free
delete
remove
clear
Explanation - C++ uses delete for memory deallocation, whereas C uses free().
Correct answer is: delete

Q.16 What is the output of sizeof(int*) on a 32-bit system?

2
4
8
Depends on int size
Explanation - On 32-bit systems, all pointers typically take 4 bytes regardless of the data type.
Correct answer is: 4

Q.17 Which type of memory allocation is handled by the operating system during program execution?

Static
Automatic
Dynamic
Manual
Explanation - Dynamic memory allocation is managed at runtime by the OS.
Correct answer is: Dynamic

Q.18 What is memory leak?

Using too much memory
Forgetting to free allocated memory
Pointer initialized to NULL
Dereferencing a NULL pointer
Explanation - A memory leak occurs when dynamically allocated memory is not freed after use.
Correct answer is: Forgetting to free allocated memory

Q.19 Which C function returns memory that is contiguous and initialized to zero?

malloc()
calloc()
alloc()
memset()
Explanation - calloc() allocates contiguous memory and initializes it to zero.
Correct answer is: calloc()

Q.20 What is the default value of an uninitialized pointer?

NULL
0
Random garbage value
Depends on compiler
Explanation - Uninitialized pointers contain random garbage values until explicitly assigned.
Correct answer is: Random garbage value

Q.21 What is the relationship between an array and a pointer in C?

Array name is a pointer
Pointer and array are completely unrelated
Array is larger than pointer
Pointer is always NULL
Explanation - The array name can be treated as a pointer to its first element.
Correct answer is: Array name is a pointer

Q.22 Which of the following is true about pointer to pointer?

Holds address of variable
Holds address of function
Holds address of another pointer
Holds garbage value
Explanation - A pointer to pointer stores the address of another pointer.
Correct answer is: Holds address of another pointer

Q.23 What is wild pointer?

Pointer initialized to NULL
Pointer pointing to invalid memory
Pointer to pointer
Pointer to a structure
Explanation - Wild pointers are uninitialized pointers pointing to an invalid location.
Correct answer is: Pointer pointing to invalid memory

Q.24 Which of the following cannot be incremented using pointer arithmetic?

int*
float*
struct*
void*
Explanation - Since void* has no type size, arithmetic cannot be done directly on it.
Correct answer is: void*

Q.25 Which of the following is NOT a type of memory allocation in C?

Static
Automatic
Dynamic
Permanent
Explanation - C supports static, automatic, and dynamic allocation, but not permanent allocation.
Correct answer is: Permanent