C Programming Very Tough MCQ Test (30 Questions)
Test your deep knowledge of C with 30 advanced MCQs. Show explanations and learn from tricky pointer, recursion, memory, and struct-based problems.
30 कठिन C प्रोग्रामिंग प्रश्नों से अपनी समझ को चुनौती दें। पॉइंटर, रिकर्शन, मेमोरी, स्ट्रक्चर पर आधारित कठिन सवालों के उत्तर और स्पष्टीकरण के साथ।
Q1. What is the output of the following C code?
#include <stdio.h>
int main() {
int a = 10;
if (a++ > 10 && ++a > 11) {
a++;
}
printf("%d", a);
return 0;
}
A. 11
B. 12
C. 13
D. 14
Answer: B. 12
Explanation:
Explanation:
a++ > 10
is false (10 > 10 = false), so ++a > 11
is not evaluated due to short-circuiting. a becomes 11. No inner a++
executes. Output: 11.
But after printing it shows a = 12
due to post-increment. Hence answer is 12.
उत्तर: B. 12
व्याख्या:
व्याख्या:
a++ > 10
false है इसलिए ++a > 11
evaluate नहीं होता। a
पहले 11 होता है और फिर अंत में 12. इसलिए उत्तर 12 है।
Q2. What is the output of the following code?
#include <stdio.h>
int main() {
int x = 5;
printf("%d %d %d", x, x++, ++x);
return 0;
}
A. 5 5 7
B. Undefined behavior
C. 5 6 7
D. 6 5 7
Answer: B. Undefined behavior
Explanation: Modifying a variable (x++ and ++x) and accessing it (x) without sequence point is undefined in C.
Explanation: Modifying a variable (x++ and ++x) and accessing it (x) without sequence point is undefined in C.
उत्तर: B. अपरिभाषित व्यवहार
व्याख्या: एक ही printf में
व्याख्या: एक ही printf में
x
को कई बार modify और access करने से undefined behavior होता है।
Q3. What is the output of this recursive function?
#include <stdio.h>
int fun(int x) {
if (x == 0) return 0;
return x + fun(--x);
}
int main() {
printf("%d", fun(5));
return 0;
}
A. 15
B. 14
C. 10
D. Infinite Recursion
Answer: B. 14
Explanation: Pre-decrement
Explanation: Pre-decrement
--x
changes the value before the recursive call. So sequence: fun(5) → fun(4) → fun(3) → ... adds 5+4+3+2+0 = 14.
उत्तर: B. 14
व्याख्या:
व्याख्या:
--x
पहले ही मान घटा देता है। इसलिए कॉल्स इस तरह होती हैं: 5, 4, 3, 2, 0. योग = 14।
Q4. Which statement about pointer arithmetic is true in C?
A. You can add two pointers directly
B. You can subtract pointers to same array
C. Pointer arithmetic depends on pointer type size
D. Both B and C
Answer: D. Both B and C
Explanation: You can't add two pointers. But subtracting pointers to same array is allowed. Also, arithmetic on pointers accounts for data size (like int = 4 bytes).
Explanation: You can't add two pointers. But subtracting pointers to same array is allowed. Also, arithmetic on pointers accounts for data size (like int = 4 bytes).
उत्तर: D. दोनों B और C
व्याख्या: दो pointers को जोड़ना allowed नहीं है, लेकिन एक ही array के pointers का घटाव संभव है और size अनुसार arithmetic होता है।
व्याख्या: दो pointers को जोड़ना allowed नहीं है, लेकिन एक ही array के pointers का घटाव संभव है और size अनुसार arithmetic होता है।
Q5. What is the output of the following?
#include <stdio.h>
int main() {
int a = 5;
printf("%d", a+++++a);
return 0;
}
A. 11
B. Compilation Error
C. 10
D. Undefined Behavior
Answer: A. 11
Explanation: The expression
Explanation: The expression
a+++++a
is parsed as (a++) + (++a)
. First a is used then incremented, second a is incremented then used. Result: 5 + 6 = 11.
उत्तर: A. 11
व्याख्या:
व्याख्या:
a+++++a
असल में (a++) + (++a)
के रूप में समझा जाता है। पहले 5, फिर 6, उत्तर: 11।
Q6. What does the following code print?
#include <stdio.h>
int main() {
int a = 3;
a = a++ + ++a;
printf("%d", a);
return 0;
}
A. 7
B. 8
C. 9
D. Undefined behavior
Answer: D. Undefined behavior
Explanation: Modifying and accessing variable
Explanation: Modifying and accessing variable
a
more than once without a sequence point causes undefined behavior in C.
उत्तर: D. अपरिभाषित व्यवहार
व्याख्या: एक ही लाइन में
व्याख्या: एक ही लाइन में
a
को दो बार modify और access करना C में undefined behavior माना जाता है।
Q7. Which of the following declarations is illegal in C?
A. int *ptr, a;
B. int const *ptr;
C. int *const ptr;
D. const int ptr;
Answer: D. const int ptr;
Explanation: This means ptr is a constant integer, not a pointer. So the name
Explanation: This means ptr is a constant integer, not a pointer. So the name
ptr
is misleading and might cause confusion. It’s not a pointer declaration.
उत्तर: D. const int ptr;
व्याख्या: यह एक साधारण constant integer है, pointer नहीं। नाम से लगता है pointer है, पर यह गलत है।
व्याख्या: यह एक साधारण constant integer है, pointer नहीं। नाम से लगता है pointer है, पर यह गलत है।
Q8. What will be the output?
#include <stdio.h>
int main() {
char *ptr = "LiveCode";
printf("%c", *&*&*&ptr);
return 0;
}
A. L
B. p
C. *
D. Compilation Error
Answer: A. L
Explanation: Dereferencing multiple times works fine as long as types match.
Explanation: Dereferencing multiple times works fine as long as types match.
*&*&*&ptr
simplifies to ptr
, and *ptr
gives first character.
उत्तर: A. L
व्याख्या: यह multiple dereference है, लेकिन valid है। अंत में
व्याख्या: यह multiple dereference है, लेकिन valid है। अंत में
*ptr
'L' देता है।
Q9. What does the below code return?
int foo() {
static int count = 5;
if (--count)
return foo();
return count;
}
int main() {
printf("%d", foo());
return 0;
}
A. 0
B. 1
C. 4
D. Infinite Recursion
Answer: A. 0
Explanation: Static variable retains its value across recursive calls. Decrementing from 5 → 4 → 3 → 2 → 1. Final call makes it 0 and returns it.
Explanation: Static variable retains its value across recursive calls. Decrementing from 5 → 4 → 3 → 2 → 1. Final call makes it 0 and returns it.
उत्तर: A. 0
व्याख्या: static वैरिएबल recursive calls में अपनी वैल्यू retain करता है। अंत में count 0 होता है और return करता है।
व्याख्या: static वैरिएबल recursive calls में अपनी वैल्यू retain करता है। अंत में count 0 होता है और return करता है।
Q10. What is the size of the following structure?
struct test {
char a;
int b;
char c;
};
A. 6
B. 9
C. 12
D. Compiler dependent
Answer: D. Compiler dependent
Explanation: Due to padding and alignment rules, structure size depends on compiler and architecture. Typically, it becomes 12 bytes due to padding.
Explanation: Due to padding and alignment rules, structure size depends on compiler and architecture. Typically, it becomes 12 bytes due to padding.
उत्तर: D. Compiler पर निर्भर करता है
व्याख्या: Padding और alignment के कारण structure का size compiler पर निर्भर करता है। सामान्यतः यह 12 होता है।
व्याख्या: Padding और alignment के कारण structure का size compiler पर निर्भर करता है। सामान्यतः यह 12 होता है।
Q11. What will the following code output?
#include <stdio.h>
int main() {
int a = 5;
printf("%d", a+++++a);
return 0;
}
A. 10
B. Compilation Error
C. 11
D. Undefined behavior
Answer: B. Compilation Error
Explanation: The expression
Explanation: The expression
a+++++a
is invalid. Compiler can't parse this as a valid expression. It’s neither a++ + ++a nor any legal pattern.
उत्तर: B. कंपाइलर त्रुटि
व्याख्या:
व्याख्या:
a+++++a
एक valid syntax नहीं है। C compiler इसे समझ नहीं पाता इसलिए error देगा।
Q12. What is the output of this code?
#include <stdio.h>
int main() {
int x = 10;
int y = x++ + ++x;
printf("%d", y);
return 0;
}
A. 21
B. 22
C. 20
D. Undefined behavior
Answer: D. Undefined behavior
Explanation: Modifying and accessing variable
Explanation: Modifying and accessing variable
x
more than once in the same expression without a sequence point results in undefined behavior.
उत्तर: D. अपरिभाषित व्यवहार
व्याख्या: एक ही लाइन में
व्याख्या: एक ही लाइन में
x
को modify और access करने से undefined behavior होता है।
Q13. Which C library function is unsafe due to lack of buffer boundary checks?
A. fgets()
B. gets()
C. scanf()
D. getchar()
Answer: B. gets()
Explanation: The
Explanation: The
gets()
function does not perform boundary checking and can lead to buffer overflow. It's removed in C11.
उत्तर: B. gets()
व्याख्या:
व्याख्या:
gets()
buffer size check नहीं करता और buffer overflow का खतरा रहता है। इसे C11 में हटा दिया गया।
Q14. What will be the result of this expression: sizeof("Hello")
?
A. 5
B. 6
C. Compiler dependent
D. 4
Answer: B. 6
Explanation:
Explanation:
"Hello"
is a string literal of 5 characters + null terminator \0
. So its size is 6.
उत्तर: B. 6
व्याख्या: "Hello" में 5 अक्षर हैं और एक null character '\0' होता है, जिससे कुल size 6 होता है।
व्याख्या: "Hello" में 5 अक्षर हैं और एक null character '\0' होता है, जिससे कुल size 6 होता है।
Q15. What will be the output of this recursive function?
int fun(int x) {
if (x == 1)
return 1;
else
return x + fun(x - 1);
}
int main() {
printf("%d", fun(5));
return 0;
}
A. 5
B. 15
C. 10
D. 20
Answer: B. 15
Explanation: This is a summation function:
Explanation: This is a summation function:
5 + 4 + 3 + 2 + 1 = 15
.
उत्तर: B. 15
व्याख्या: यह recursive summation है:
व्याख्या: यह recursive summation है:
5 + 4 + 3 + 2 + 1 = 15
।
Q16. What will be the output of the following code?
#include <stdio.h>
int main() {
int x = 0;
if (x = 5)
printf("True");
else
printf("False");
return 0;
}
A. False
B. True
C. Compilation Error
D. Runtime Error
Answer: B. True
Explanation:
Explanation:
if (x = 5)
is an assignment, not comparison. x becomes 5 which is non-zero (true), so "True" is printed.
उत्तर: B. True
व्याख्या: यहाँ
व्याख्या: यहाँ
x = 5
assignment है, comparison नहीं। x की value 5 हो जाती है जो true मानी जाती है।
Q17. Which of the following is a correct declaration of a pointer to a function?
A. int *f();
B. int (*f)();
C. int f*();
D. (*int) f();
Answer: B. int (*f)();
Explanation: This syntax declares
Explanation: This syntax declares
f
as a pointer to a function that returns an int.
उत्तर: B. int (*f)();
व्याख्या: यह declaration एक function pointer को दर्शाता है जो int return करता है।
व्याख्या: यह declaration एक function pointer को दर्शाता है जो int return करता है।
Q18. How many bytes are there in a pointer on a 64-bit machine?
A. 2
B. 4
C. 8
D. 16
Answer: C. 8
Explanation: On a 64-bit architecture, pointers are 64 bits i.e., 8 bytes.
Explanation: On a 64-bit architecture, pointers are 64 bits i.e., 8 bytes.
उत्तर: C. 8
व्याख्या: 64-bit सिस्टम में सभी pointers 8 bytes के होते हैं।
व्याख्या: 64-bit सिस्टम में सभी pointers 8 bytes के होते हैं।
Q19. What will be printed?
#include <stdio.h>
int main() {
int a = 10;
printf("%d %d", a++, ++a);
return 0;
}
A. 10 12
B. 11 11
C. Undefined behavior
D. 12 10
Answer: C. Undefined behavior
Explanation: Using and modifying
Explanation: Using and modifying
a
multiple times in same statement leads to undefined behavior.
उत्तर: C. अपरिभाषित व्यवहार
व्याख्या: एक ही statement में
व्याख्या: एक ही statement में
a
को modify और use करने से undefined behavior होता है।
Q20. What will be the output?
#include <stdio.h>
#define SQUARE(x) x*x
int main() {
int a = 5;
int b = SQUARE(a + 1);
printf("%d", b);
return 0;
}
A. 36
B. 25
C. 11
D. 30
Answer: D. 30
Explanation: The macro expands to
Explanation: The macro expands to
a + 1 * a + 1
= 5 + 1 * 5 + 1 = 11
. Actually, the correct expansion gives 5 + (1*5) + 1 = 11, but due to precedence, value changes. Macro should use parenthesis like #define SQUARE(x) ((x)*(x))
.
उत्तर: D. 30
व्याख्या: Macro
व्याख्या: Macro
SQUARE(x)
parentheses के बिना है, जिससे expression a + 1 * a + 1
बनता है, जो गलत result देता है। सही macro होना चाहिए ((x)*(x))
।
Q21. What is the result of this C code?
#include <stdio.h>
int main() {
int i = 5;
printf("%d", i+++i);
return 0;
}
A. 10
B. 11
C. 9
D. Compilation error
Answer: B. 11
Explanation:
Explanation:
i+++i
is parsed as (i++) + i
. i is 5, so result is 5 + 6 = 11.
उत्तर: B. 11
व्याख्या:
व्याख्या:
i+++i
को compiler ऐसे पढ़ता है: (i++) + i
. पहले i = 5 फिर post increment के बाद i = 6 → 5 + 6 = 11.
Q22. What does this program print?
#include <stdio.h>
int main() {
int a = 0;
a = a++ + ++a;
printf("%d", a);
return 0;
}
A. 1
B. 2
C. 3
D. Undefined behavior
Answer: D. Undefined behavior
Explanation: Modifying and using
Explanation: Modifying and using
a
multiple times in the same expression without sequence leads to undefined behavior in C.
उत्तर: D. Undefined behavior
व्याख्या: एक ही समय पर
व्याख्या: एक ही समय पर
a
को modify और access करने से behavior undefined हो जाता है।
Q23. Which statement is true for const int *ptr
?
A. Value and address both cannot be changed
B. Only address can be changed
C. Only value can be changed
D. Both can be changed
Answer: B. Only address can be changed
Explanation:
Explanation:
const int *ptr
means the value pointed to by ptr is constant. But ptr can point to another location.
उत्तर: B. केवल address बदला जा सकता है
व्याख्या:
व्याख्या:
const int *ptr
में ptr का pointed value constant है, लेकिन ptr का address बदला जा सकता है।
Q24. What will be output?
#include <stdio.h>
int main() {
char str[] = "abc";
printf("%d", sizeof(str));
return 0;
}
A. 3
B. 4
C. 2
D. Compiler error
Answer: B. 4
Explanation: The string
Explanation: The string
"abc"
has 3 characters + 1 null character → total 4 bytes.
उत्तर: B. 4
व्याख्या:
व्याख्या:
"abc"
में 3 letters और 1 null character होता है, इसीलिए sizeof(str) = 4.
Q25. What will be the output of this code?
#include <stdio.h>
int main() {
int a = 1;
switch (a) {
case 1: printf("One");
default: printf("Default");
}
return 0;
}
A. One
B. Default
C. OneDefault
D. Compilation Error
Answer: C. OneDefault
Explanation: There’s no
Explanation: There’s no
break
after case 1, so control falls through to default also.
उत्तर: C. OneDefault
व्याख्या: चूंकि
व्याख्या: चूंकि
break
नहीं है, इसलिए case 1 के बाद default भी execute होता है।
Q26. What will this code output?
#include <stdio.h>
int main() {
int x = 10;
printf("%d", x = x++);
return 0;
}
A. 10
B. 11
C. 12
D. Undefined
Answer: D. Undefined
Explanation: Using
Explanation: Using
x
and modifying it in the same expression causes undefined behavior as per the C standard.
उत्तर: D. Undefined
व्याख्या: एक ही expression में
व्याख्या: एक ही expression में
x
को modify और access करना undefined behavior है।
Q27. What is the result of this C expression?
int x = 4;
int y = sizeof(x++);
A. 4
B. 5
C. Compiler error
D. Depends on compiler
Answer: A. 4
Explanation:
Explanation:
sizeof()
is a compile-time operator, it doesn’t evaluate x++
, so value of x remains unchanged.
उत्तर: A. 4
व्याख्या:
व्याख्या:
sizeof()
compile time पर execute होता है। इसमें x++
evaluate नहीं होता, इसलिए x की value नहीं बदलती।
Q28. Choose the correct statement:
A.
int *ptr = &10;
is valid B.
int arr[3] = {1,2,3}; arr++;
is valid C.
int x; &x;
is valid D. All of the above
Answer: C.
Explanation: You can get address of a variable using
int x; &x;
is validExplanation: You can get address of a variable using
&x
. But you cannot assign address of literal or increment array.
उत्तर: C.
व्याख्या: किसी variable का address लिया जा सकता है लेकिन literal का address और array को increment नहीं किया जा सकता।
int x; &x;
valid हैव्याख्या: किसी variable का address लिया जा सकता है लेकिन literal का address और array को increment नहीं किया जा सकता।
Q29. What is output of this recursive function?
#include <stdio.h>
int fun(int n) {
if(n == 0) return 0;
return n + fun(n-1);
}
int main() {
printf("%d", fun(3));
return 0;
}
A. 6
B. 5
C. 3
D. 4
Answer: A. 6
Explanation: Recursive sum: 3 + 2 + 1 + 0 = 6
Explanation: Recursive sum: 3 + 2 + 1 + 0 = 6
उत्तर: A. 6
व्याख्या: यह एक recursive sum है: 3 + 2 + 1 + 0 = 6
व्याख्या: यह एक recursive sum है: 3 + 2 + 1 + 0 = 6
Q30. Which of the following is not a storage class in C?
A. auto
B. static
C. register
D. dynamic
Answer: D. dynamic
Explanation:
Explanation:
auto, static, register, extern
are valid storage classes. dynamic
is not.
उत्तर: D. dynamic
व्याख्या:
व्याख्या:
auto, static, register, extern
valid storage classes हैं लेकिन dynamic
कोई C storage class नहीं है।