C Programming MCQs - Pointers and Dynamic Memory (Set 1)

C Programming MCQs - Pointers and Dynamic Memory (Set 1) - CodingHumans

Pointers are where most C programs - and most C interview questions - go wrong. This set covers pointer arithmetic, arrays and pointers, pointers to pointers, malloc/free and common memory bugs. Assume a 64-bit system where int is 4 bytes unless a question says otherwise.

How to practise: read each question and tap the option you think is correct. You will see right away whether you got it, along with a short explanation, and your score is tracked at the bottom of the screen. This set has 15 questions.

Q1. What is printed?

int x = 10;
int *p = &x;
*p = 20;
printf("%d", x);

a) 10

b) 20

c) Address of x

d) Compilation error

Q2. If int *p points to address 1000 and sizeof(int) is 4, what address does p + 3 point to?

a) 1003

b) 1012

c) 1004

d) 3000

Q3. What is printed?

int a[] = {5, 10, 15, 20};
int *p = a;
printf("%d", *(p + 2));

a) 5

b) 10

c) 15

d) 20

Q4. Which declaration declares a pointer to a pointer to an int?

a) int *p;

b) int **p;

c) int &p;

d) int *p[];

Q5. What does malloc() return when it cannot allocate the requested memory?

a) 0 bytes of memory

b) NULL

c) -1

d) It terminates the program

Q6. What is the key difference between calloc() and malloc()?

a) calloc allocates on the stack

b) calloc initialises the allocated memory to zero

c) malloc can only allocate arrays

d) calloc does not need free()

Q7. A pointer that still holds the address of memory that has already been freed is called a:

a) Null pointer

b) Void pointer

c) Dangling pointer

d) Wild pointer

Q8. Forgetting to call free() on memory allocated with malloc() in a long-running program causes:

a) A segmentation fault immediately

b) A memory leak

c) A compilation error

d) Stack overflow

Q9. What does a void * pointer represent?

a) A pointer that points to nothing

b) A generic pointer that can hold the address of any data type

c) A pointer to a function returning void

d) An invalid pointer

Q10. What is printed?

char s[] = "hello";
char *p = s;
p++;
printf("%c", *p);

a) h

b) e

c) l

d) Compilation error

Q11. What is printed?

void change(int *n) { *n = *n * 2; }
int main() {
    int v = 7;
    change(&v);
    printf("%d", v);
}

a) 7

b) 14

c) Address of v

d) 0

Q12. Which of these correctly declares p as a pointer to a function that takes an int and returns an int?

a) int *p(int);

b) int (*p)(int);

c) int p*(int);

d) (int *)p(int);

Q13. For the declaration int arr[10]; what does sizeof(arr) evaluate to when int is 4 bytes?

a) 4

b) 8

c) 10

d) 40

Q14. What does const int *p declare?

a) A constant pointer to an int

b) A pointer to a constant int - the value cannot be changed through p

c) A pointer that cannot be incremented

d) A constant int, not a pointer

Q15. Which function changes the size of previously allocated dynamic memory?

a) resize()

b) realloc()

c) malloc_resize()

d) extend()

Recommended MCQs

More C and C++ posts on CodingHumans

All MCQ sets

MCQ Practice hub - every subject in one place

Comments

Answered 0/0Correct 0