Home Subjects C Programming

C Programming

C language from basics to advanced placement prep

499 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 221–230 of 499
Topics in C Programming
Q.221 Medium Pointers
Consider the code:
int *p = NULL;
int *q = malloc(sizeof(int));
free(q);
free(q);
What is the issue here?
A Double free error
B Memory leak
C No issue, free() checks for NULL
D Compilation error
Correct Answer:  A. Double free error
EXPLANATION

Freeing the same pointer twice causes double free error and undefined behavior. Should set q=NULL after first free.

Take Test
Q.222 Medium Pointers
What happens when you try to modify a string literal through a pointer?
char *str = "Hello";
str[0] = 'J';
A String changes to Jello
B Undefined behavior/Runtime error
C Compilation error
D No change, str remains Hello
Correct Answer:  B. Undefined behavior/Runtime error
EXPLANATION

String literals are stored in read-only memory. Attempting to modify causes undefined behavior or segmentation fault.

Take Test
Q.223 Medium Pointers
Which of the following correctly declares a pointer to a function that takes two integers and returns an integer?
A int (*ptr)(int, int);
B int *ptr(int, int);
C int (*ptr[2])(int, int);
D int *ptr[2];
Correct Answer:  A. int (*ptr)(int, int);
EXPLANATION

int (*ptr)(int, int) declares a pointer to a function. int *ptr(int, int) declares a function returning pointer to int.

Take Test
Q.224 Medium Pointers
Which of the following is valid pointer arithmetic in C?
A p + 5 (where p is a pointer)
B p * 2 (where p is a pointer)
C p / 2 (where p is a pointer)
D p1 + p2 (where p1 and p2 are pointers)
Correct Answer:  A. p + 5 (where p is a pointer)
EXPLANATION

Valid pointer arithmetic includes addition/subtraction with integers and subtraction between pointers. Multiplication, division, and pointer addition are invalid.

Take Test
Q.225 Medium Pointers
What is the output?
void func(int *arr) { arr[0] = 100; }
int main() {
int a[5] = {1,2,3,4,5};
func(a);
printf("%d", a[0]);
}
A 1
B 100
C Garbage value
D Compilation error
Correct Answer:  B. 100
EXPLANATION

Arrays are passed by reference (as pointers). Changes made in func() affect the original array. a[0] becomes 100.

Take Test
Q.226 Medium Pointers
What will be the output?
char *str = "Hello";
printf("%c", *(str+1));
A H
B e
C l
D Compilation error
Correct Answer:  B. e
EXPLANATION

str points to 'H', str+1 points to 'e'. Dereferencing gives 'e'.

Take Test
Q.227 Medium Pointers
What is the primary disadvantage of using free() without proper checks?
A Memory leak
B Double free error or use-after-free vulnerability
C Slower program execution
D Compilation warning
Correct Answer:  B. Double free error or use-after-free vulnerability
EXPLANATION

Calling free() on already freed memory or using a pointer after freeing it causes double-free errors and use-after-free bugs, leading to undefined behavior.

Take Test
Q.228 Medium Pointers
What will be printed?
int x = 100, y = 200;
int *p = &x;
int *q = &y;
p = q;
printf("%d", *p);
A 100
B 200
C Address of y
D Garbage value
Correct Answer:  B. 200
EXPLANATION

p is reassigned to point to y (p = q). Dereferencing p now gives the value of y, which is 200.

Take Test
Q.229 Medium Pointers
Which statement correctly declares a pointer to a pointer?
A int *p;
B int **p;
C int *&p;
D int &*p;
Correct Answer:  B. int **p;
EXPLANATION

int **p declares a pointer to a pointer (double pointer). Each * adds one level of indirection.

Take Test
Q.230 Medium Pointers
What is the output of this code?
int *p, *q;
int x = 5;
p = &x;
q = p;
printf("%d %d", *p, *q);
A 5 5
B Address Address
C 5 Garbage
D Compilation error
Correct Answer:  A. 5 5
EXPLANATION

Both p and q point to the same variable x. Dereferencing both gives the value 5.

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