In C, which memory segment stores local variables?
A
Heap B
Stack C
Data segment D
Code segment
Analysis & Theory
Local variables are stored in the stack segment.
Which memory segment is used for dynamically allocated memory (malloc, calloc)?
A
Stack B
Heap C
Data D
Text
Analysis & Theory
The heap segment is used for dynamic memory allocation in C.
What is the purpose of the `malloc()` function?
A
To create a variable B
To allocate memory at compile time C
To allocate memory at runtime D
To copy memory
Analysis & Theory
`malloc()` allocates memory dynamically at runtime.
Which header file is required for `malloc()`?
A
stdio.h B
stdlib.h C
string.h D
malloc.h
Analysis & Theory
`stdlib.h` includes the declaration for `malloc()`.
What should always be done after using `malloc()`?
A
Nothing B
Close the file C
Free the memory with `free()` D
Use `scanf()`
Analysis & Theory
Always free dynamically allocated memory to prevent memory leaks.
What will this code output?
```c
int a = 10;
int *p = &a;
printf("%d", *p);
```
A
10 B
Address of a C
Garbage value D
Error
Analysis & Theory
`*p` gives the value at the address stored in `p`, which is `10`.
What is the size of an `int*` pointer (in a typical 64-bit system)?
A
2 bytes B
4 bytes C
8 bytes D
Depends on compiler only
Analysis & Theory
On a 64-bit system, pointers typically occupy 8 bytes.
Which function is used to free dynamically allocated memory in C?
A
release() B
free() C
delete() D
dispose()
Analysis & Theory
`free()` is used to release memory allocated by `malloc()`, `calloc()`, or `realloc()`.
Which function initializes allocated memory to zero?
A
malloc() B
calloc() C
realloc() D
memset()
Analysis & Theory
`calloc()` allocates and initializes memory blocks to zero.
What will happen if `free()` is called on a NULL pointer?
A
Crash B
Segmentation fault C
Nothing D
Memory leak
Analysis & Theory
`free(NULL)` is safe and does nothing.