Home Subjects C Programming

C Programming

C language from basics to advanced placement prep

490 Q 10 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 221–230 of 490
Topics in C Programming
Q.221 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.

Test
Q.222 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.

Test
Q.223 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.

Test
Q.224 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.

Test
Q.225 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'.

Test
Q.226 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.

Test
Q.227 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.

Test
Q.228 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.

Test
Q.229 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.

Test
Q.230 Medium Pointers
What does calloc() do that malloc() doesn't?
A Allocates more memory
B Initializes allocated memory to zero
C Allocates memory faster
D Returns a void pointer
Correct Answer:  B. Initializes allocated memory to zero
EXPLANATION

calloc() takes two parameters (number of elements, size) and initializes all bytes to zero, while malloc() leaves memory uninitialized.

Test
IGET
IGET AI
Online · Exam prep assistant
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