Home Subjects C Programming Pointers

C Programming
Pointers

C language from basics to advanced placement prep

100 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 31–40 of 100
Topics in C Programming
Q.31 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.32 Medium Pointers
What is the output?
int x = 5;
int *const p = &x;
x = 10;
printf("%d", *p);
A 5
B 10
C Garbage value
D Compilation error
Correct Answer:  B. 10
EXPLANATION

const pointer p cannot be changed, but x can be modified. *p prints current value of x which is 10.

Take Test
Q.33 Medium Pointers
Which of the following is correct about pointer assignment?
int *p, *q;
p = q; // What happens?
A Copies the value p points to into q
B Makes p point to the same address as q
C Causes compilation error
D Allocates new memory for p
Correct Answer:  B. Makes p point to the same address as q
EXPLANATION

p = q copies the address stored in q to p. Now both point to same location.

Take Test
Q.34 Easy Pointers
What is the output?
char str[] = "ABC";
char *p = str;
printf("%c", *(p+2));
A A
B B
C C
D Null character
Correct Answer:  C. C
EXPLANATION

p+2 points to str[2] which is 'C'. Note: str[3] is null terminator.

Take Test
Q.35 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.36 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.37 Easy Pointers
What is the output?
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int x = 5, y = 10;
swap(&x, &y);
printf("%d %d", x, y);
A 5 10
B 10 5
C 10 10
D 5 5
Correct Answer:  B. 10 5
EXPLANATION

swap() exchanges values through pointers. x becomes 10, y becomes 5 after function call.

Take Test
Q.38 Easy Pointers
What does void *ptr represent?
A Generic pointer that can point to any data type
B Pointer that points to nothing
C Null pointer
D Invalid pointer declaration
Correct Answer:  A. Generic pointer that can point to any data type
EXPLANATION

void* is a generic pointer. It can be cast to any other pointer type. No arithmetic can be done directly.

Take Test
Q.39 Medium Pointers
What is the output?
int arr[2][3] = {{1,2,3}, {4,5,6}};
int *p = (int*)arr;
printf("%d", *(p+4));
A 5
B 4
C 6
D 1
Correct Answer:  A. 5
EXPLANATION

Array flattened: 1,2,3,4,5,6. p+4 points to 5th element which is 5.

Take Test
Q.40 Medium Pointers
Which pointer operation is NOT valid in C?
A int *p, *q; p - q (pointer subtraction)
B int *p; p++ (pointer increment)
C int *p, *q; p * q (pointer multiplication)
D int *p; *p = 5 (pointer dereference assignment)
Correct Answer:  C. int *p, *q; p * q (pointer multiplication)
EXPLANATION

Pointer multiplication is not allowed. Valid operations are addition, subtraction, increment, decrement, and comparison.

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