Home Subjects C Programming

C Programming

C language from basics to advanced placement prep

198 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 101–110 of 198
Topics in C Programming
Q.101 Hard Pointers
What is the output?
int x = 50;
int *p = &x;
int *q = p;
q = NULL;
printf("%d", *p);
A 50
B NULL
C Garbage value
D Segmentation fault
Correct Answer:  A. 50
EXPLANATION

Setting q to NULL doesn't affect p. p still points to x, so *p is 50.

Take Test
Q.102 Hard Pointers
Which statement is true about void pointers?
A Cannot be dereferenced without casting
B Can only point to void type
C Are always NULL
D Cannot be incremented
Correct Answer:  A. Cannot be dereferenced without casting
EXPLANATION

void pointers are generic pointers that must be cast to appropriate type before dereferencing.

Take Test
Q.103 Hard Pointers
What does the following code do?
int *p = (int*)malloc(sizeof(int));
*p = 5;
free(p);
p = NULL;
A Allocates, assigns, deallocates and nullifies pointer
B Creates a memory leak
C Causes segmentation fault
D Compilation error
Correct Answer:  A. Allocates, assigns, deallocates and nullifies pointer
EXPLANATION

This is proper memory management: allocate, use, free, and nullify to prevent dangling pointer.

Take Test
Q.104 Hard Pointers
What is the difference between *p++ and (*p)++?
A No difference
B *p++ increments pointer, (*p)++ increments value
C (*p)++ increments pointer, *p++ increments value
D Syntax error
Correct Answer:  B. *p++ increments pointer, (*p)++ increments value
EXPLANATION

Due to operator precedence, *p++ is equivalent to *(p++), incrementing the pointer. (*p)++ explicitly increments the value.

Take Test
Q.105 Hard Arrays & Strings
What does the strspn() function do?
A Finds substring in a string
B Spans the initial segment containing only characters from specified set
C Compares two strings
D Reverses a string
Correct Answer:  B. Spans the initial segment containing only characters from specified set
EXPLANATION

strspn(str, charset) returns length of initial segment of str containing only characters from charset. Useful for token parsing.

Take Test
Q.106 Hard Arrays & Strings
In what scenario would you use memcpy() instead of strcpy()?
A memcpy() is always faster
B When copying binary data or strings without null terminators
C When the destination is smaller
D memcpy() is safer
Correct Answer:  B. When copying binary data or strings without null terminators
EXPLANATION

strcpy() assumes null-terminated strings. memcpy() copies exact number of bytes, suitable for binary data or embedded nulls.

Take Test
Q.107 Hard Arrays & Strings
Which of the following correctly declares a pointer to an array (not array of pointers)?
A int *arr[5];
B int (*ptr)[5];
C int [5]*ptr;
D int &arr[5];
Correct Answer:  B. int (*ptr)[5];
EXPLANATION

Parentheses change precedence. int (*ptr)[5] is pointer to array of 5 ints. int *arr[5] is array of 5 pointers.

Take Test
Q.108 Hard Arrays & Strings
In string handling, what is the difference between fgets() and gets()?
A fgets() is slower
B fgets() includes newline character, gets() doesn't
C fgets() takes size parameter and is safer; gets() is unsafe (removed from C11)
D gets() works only with integers
Correct Answer:  C. fgets() takes size parameter and is safer; gets() is unsafe (removed from C11)
EXPLANATION

gets() has no buffer overflow protection and was removed in C11. fgets(str, size, stdin) is the safer alternative with size limiting.

Take Test
Q.109 Hard Arrays & Strings
For a 3D array int arr[2][3][4], what is arr[1][2][3] equivalent to?
A arr + 2*3*4 + 2*4 + 3
B arr + 1*3*4 + 2*4 + 3
C arr + 3 + 2 + 1
D arr + 1 + 2 + 3
Correct Answer:  B. arr + 1*3*4 + 2*4 + 3
EXPLANATION

In row-major order: address = base + (i*m*n + j*n + k) where m=3, n=4. So arr[1][2][3] = base + 1*12 + 2*4 + 3 = base + 23.

Take Test
Q.110 Hard Arrays & Strings
What potential issue exists with this code: char *ptr = "Hello"; ptr[0] = 'h';?
A Compilation error
B Attempting to modify a string literal (undefined behavior)
C Memory leak
D Syntax error in character assignment
Correct Answer:  B. Attempting to modify a string literal (undefined behavior)
EXPLANATION

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

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