C Programming questions
Pointers, arrays, strings, storage classes, plus output questions checked by running the code.
1. Which header file declares printf()?
- <stdlib.h>
- <conio.h>
- <stdio.h>
- <string.h>
Answer
C. <stdio.h> printf and scanf are declared in stdio.h.
2. What is the size of char in C?
- 2 bytes
- 4 bytes
- Depends on the compiler
- 1 byte
Answer
D. 1 byte sizeof(char) is 1 by definition.
3. Which format specifier prints an unsigned int?
- %c
- %d
- %u
- %f
Answer
C. %u %u is for unsigned int; %d is for signed int.
4. Which of these is NOT a valid C keyword?
- extern
- register
- boolean
- volatile
Answer
C. boolean C89 has no "boolean" keyword (C99 adds _Bool).
5. Which storage class keeps a local variable alive between function calls?
- static
- extern
- auto
- register
Answer
A. static A static local is initialised once and keeps its value.
6. What is the default storage class of a local variable?
- extern
- auto
- static
- register
Answer
B. auto Local variables are auto by default.
7. Which function allocates memory and initialises it to zero?
- alloc()
- realloc()
- calloc()
- malloc()
Answer
C. calloc() calloc zeroes the memory; malloc leaves it uninitialised.
8. Which function releases memory allocated with malloc()?
- release()
- delete
- free()
- dealloc()
Answer
C. free() free() returns heap memory in C.
9. What does the & operator return when applied to a variable?
- Its address
- Its type
- Its value
- Its size
Answer
A. Its address &x gives the address of x.
10. A pointer that points to a memory location that has been freed is called a:
- Dangling pointer
- Null pointer
- Void pointer
- Wild card pointer
Answer
A. Dangling pointer After free(), the pointer still holds the old address: dangling.
11. Which pointer can hold the address of any data type?
- void *
- null *
- int *
- char **
Answer
A. void * A void pointer is a generic pointer.
12. What does the "break" statement do inside a loop?
- Exits the program
- Exits the loop immediately
- Restarts the loop
- Skips to the next iteration
Answer
B. Exits the loop immediately break leaves the innermost loop or switch.
13. Which loop always runs its body at least once?
- while
- do-while
- for
- None of these
Answer
B. do-while do-while checks the condition after the body.
14. What is the index of the first element of an array in C?
- 1
- -1
- Depends on declaration
- 0
Answer
D. 0 C arrays are zero-indexed.
15. Which function returns the length of a string (excluding the null character)?
- strlen()
- length()
- sizeof()
- strsize()
Answer
A. strlen() strlen counts characters before the terminating \0.
16. What character marks the end of a C string?
- '\0'
- ' '
- '\n'
- '.'
Answer
A. '\0' C strings end with the null character.
17. A structure member is accessed through a pointer using:
- ->
- .
- ::
- &
Answer
A. -> p->member is shorthand for (*p).member.
18. In a union, all members share:
- Separate memory locations
- The same memory location
- The same name
- Nothing
Answer
B. The same memory location A union is as large as its largest member; members overlap.
19. Which preprocessor directive defines a macro?
- #include
- #macro
- #ifdef
- #define
Answer
D. #define #define creates a macro.
20. Recursion requires:
- A base case
- A loop
- A pointer
- A global variable
Answer
A. A base case Without a base case recursion never ends.
21. What is the time complexity of binary search on a sorted array of n elements?
- O(n)
- O(log n)
- O(1)
- O(n log n)
Answer
B. O(log n) Each step halves the search range.
22. Which operator has the highest precedence?
- () function call / parentheses
- &&
- +
- =
Answer
A. () function call / parentheses Parentheses and postfix operators bind tightest.
23. What does "sizeof" return?
- The size in bytes of a type or object
- The number of elements always
- The address of a variable
- The value of a variable
Answer
A. The size in bytes of a type or object sizeof yields a size in bytes.
24. What is the output?
#include <stdio.h>
int main() {
int a = 10, b = 3;
printf("%d %d", a / b, a % b);
return 0;
}- 3.33 1
- 1 3
- 3 0
- 3 1
Answer
D. 3 1 Integer division gives 3; remainder is 1.
25. What is the output?
#include <stdio.h>
int main() {
int x = 5;
int y = x++ + 2;
printf("%d %d", x, y);
return 0;
}- 5 7
- 5 8
- 6 8
- 6 7
Answer
D. 6 7 x++ uses 5 then increments: y = 7, x = 6.
26. What is the output?
#include <stdio.h>
int main() {
int i;
for (i = 0; i < 5; i++);
printf("%d", i);
return 0;
}- 5
- 0
- 01234
- 4
Answer
A. 5 The semicolon ends the loop body; printf runs once after the loop with i = 5.
27. What is the output?
#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40};
int *p = arr;
p += 2;
printf("%d", *p);
return 0;
}- 12
- 20
- 30
- 40
Answer
C. 30 p moves two elements forward to arr[2] = 30.
28. What is the output?
#include <stdio.h>
int main() {
char s[] = "Placement";
printf("%c%c", s[0], s[3]);
return 0;
}- Pe
- Pa
- Pl
- Pc
Answer
D. Pc s[0] = P, s[3] = c.
29. What is the output?
#include <stdio.h>
int f(int n) {
if (n == 0) return 0;
return n + f(n - 1);
}
int main() {
printf("%d", f(5));
return 0;
}- 10
- 15
- 120
- 5
Answer
B. 15 f(5) = 5 + 4 + 3 + 2 + 1 + 0 = 15.
30. What is the output?
#include <stdio.h>
int main() {
int x = 7;
if (x > 5)
if (x > 10)
printf("A");
else
printf("B");
return 0;
}- AB
- B
- A
- Nothing is printed
Answer
B. B The else belongs to the nearest if (x > 10), which is false, so B.
31. What is the output?
#include <stdio.h>
int main() {
int a = 5;
switch (a) {
case 4: printf("four ");
case 5: printf("five ");
case 6: printf("six");
}
return 0;
}- five six
- four five six
- five
- six
Answer
A. five six Without break, execution falls through from case 5 to case 6.
32. What is the output?
#include <stdio.h>
int main() {
int count = 0;
for (int i = 1; i <= 20; i++)
if (i % 3 == 0) count++;
printf("%d", count);
return 0;
}- 6
- 5
- 20
- 7
Answer
A. 6 Multiples of 3 up to 20: 3, 6, 9, 12, 15, 18.
33. What is the output?
#include <stdio.h>
int main() {
int a = 3, b = 4;
a = a ^ b;
b = a ^ b;
a = a ^ b;
printf("%d %d", a, b);
return 0;
}- 4 3
- 7 4
- 3 4
- 0 0
Answer
A. 4 3 The XOR trick swaps the two values.
34. What is the output?
#include <stdio.h>
int main() {
int x = 1;
x = x << 3;
printf("%d", x);
return 0;
}- 4
- 16
- 3
- 8
Answer
D. 8 Shifting 1 left by 3 bits gives 2³ = 8.
35. What is the output?
#include <stdio.h>
void inc(int *p) { (*p)++; }
int main() {
int n = 9;
inc(&n);
printf("%d", n);
return 0;
}- Address of n
- 10
- Compile error
- 9
Answer
B. 10 Passing the address lets the function change n.
36. What is the output?
#include <stdio.h>
int main() {
static int s;
printf("%d", s);
return 0;
}- Garbage value
- 1
- 0
- Compile error
Answer
C. 0 Static variables are zero-initialised.
37. What is the output?
#include <stdio.h>
#define SQ(x) x * x
int main() {
printf("%d", SQ(2 + 3));
return 0;
}- 11
- 13
- 25
- 10
Answer
A. 11 The macro expands to 2 + 3 * 2 + 3 = 11 (no brackets around x).
38. What is the output?
#include <stdio.h>
int main() {
int a = 10;
printf("%d", a > 5 ? a * 2 : a / 2);
return 0;
}- 5
- 20
- 1
- 10
Answer
B. 20 The condition is true, so a * 2 = 20.
39. What is the output?
#include <stdio.h>
int main() {
int i = 0, sum = 0;
while (i < 10) {
i++;
if (i % 2) continue;
sum += i;
}
printf("%d", sum);
return 0;
}- 30
- 55
- 25
- 20
Answer
A. 30 Only even i are added: 2 + 4 + 6 + 8 + 10 = 30.
40. What is the output?
#include <stdio.h>
#include <string.h>
int main() {
char s[20] = "Hello";
strcat(s, "OU");
printf("%zu", strlen(s));
return 0;
}- 20
- 7
- 8
- 5
Answer
B. 7 "HelloOU" has 7 characters.
41. What is the output?
#include <stdio.h>
struct P { int x; int y; };
int main() {
struct P p = {3, 4};
struct P *q = &p;
printf("%d", q->x * q->y);
return 0;
}- 0
- 7
- 34
- 12
Answer
D. 12 3 × 4 = 12.
42. What is the output?
#include <stdio.h>
int main() {
int m[2][3] = {{1, 2, 3}, {4, 5, 6}};
printf("%d", m[1][2]);
return 0;
}- 5
- 4
- 6
- 3
Answer
C. 6 Row 1, column 2 is 6.
43. What is the output?
#include <stdio.h>
int main() {
float f = 7 / 2;
printf("%.1f", f);
return 0;
}- 4.0
- 3.5
- 3.0
- 3
Answer
C. 3.0 7 / 2 is integer division (3) before it is stored in the float.
44. What is the output?
#include <stdio.h>
int main() {
int n = 1234, rev = 0;
while (n) {
rev = rev * 10 + n % 10;
n /= 10;
}
printf("%d", rev);
return 0;
}- 1234
- 10
- 4321
- 4
Answer
C. 4321 The loop reverses the digits.
45. What is the output?
#include <stdio.h>
int main() {
int a = 5, b = 0;
if (b != 0 && a / b > 1) printf("yes");
else printf("no");
return 0;
}- yes
- no
- Runtime error
- Compile error
Answer
B. no && short-circuits: b != 0 is false, so a / b never runs.
46. What is the output?
#include <stdio.h>
int main() {
char c = 'A' + 2;
printf("%c", c);
return 0;
}- 67
- A2
- C
- B
Answer
C. C 'A' is 65; 67 is 'C'.
47. What is the output?
#include <stdio.h>
int main() {
int x = 10;
int *p = &x, **pp = &p;
**pp = 20;
printf("%d", x);
return 0;
}- 20
- Compile error
- Address of x
- 10
Answer
A. 20 **pp refers to x through two levels of indirection.
48. What is the output?
#include <stdio.h>
int main() {
for (int i = 1; i <= 3; i++)
for (int j = 1; j <= i; j++)
printf("*");
return 0;
}- ***
- **
- *********
- ******
Answer
D. ****** 1 + 2 + 3 = 6 stars.
49. In C, which description best fits “printf”?
- skip to next loop iteration
- group of named fields
- static storage duration for a local variable
- formatted output
Answer
D. formatted output printf is used to formatted output.
50. In C, which description best fits “scanf”?
- formatted input
- exit the nearest loop or switch
- memory address reference
- dynamic memory allocation
Answer
A. formatted input scanf is used to formatted input.
51. In C, which description best fits “malloc”?
- static storage duration for a local variable
- dynamic memory allocation
- exit the nearest loop or switch
- formatted output
Answer
B. dynamic memory allocation malloc is used to dynamic memory allocation.
52. In C, which description best fits “sizeof”?
- skip to next loop iteration
- dynamic memory allocation
- contiguous elements of one type
- size in bytes of a type or object
Answer
D. size in bytes of a type or object sizeof is used to size in bytes of a type or object.
53. In C, which description best fits “pointer”?
- memory address reference
- formatted input
- static storage duration for a local variable
- contiguous elements of one type
Answer
A. memory address reference pointer is used to memory address reference.
54. In C, which description best fits “array”?
- dynamic memory allocation
- static storage duration for a local variable
- contiguous elements of one type
- formatted input
Answer
C. contiguous elements of one type array is used to contiguous elements of one type.
55. In C, which description best fits “struct”?
- skip to next loop iteration
- group of named fields
- size in bytes of a type or object
- exit the nearest loop or switch
Answer
B. group of named fields struct is used to group of named fields.
56. In C, which description best fits “break”?
- exit the nearest loop or switch
- formatted output
- static storage duration for a local variable
- skip to next loop iteration
Answer
A. exit the nearest loop or switch break is used to exit the nearest loop or switch.
57. In C, which description best fits “continue”?
- static storage duration for a local variable
- formatted input
- memory address reference
- skip to next loop iteration
Answer
D. skip to next loop iteration continue is used to skip to next loop iteration.
58. In C, which description best fits “static”?
- group of named fields
- formatted output
- exit the nearest loop or switch
- static storage duration for a local variable
Answer
D. static storage duration for a local variable static is used to static storage duration for a local variable.
59. Which C concept matches this description: “formatted output”?
- break
- continue
- sizeof
- printf
Answer
D. printf The definition describes printf.
60. Which C concept matches this description: “formatted input”?
- pointer
- break
- sizeof
- scanf
Answer
D. scanf The definition describes scanf.
61. Which C concept matches this description: “dynamic memory allocation”?
- struct
- malloc
- sizeof
- printf
Answer
B. malloc The definition describes malloc.
62. Which C concept matches this description: “size in bytes of a type or object”?
- static
- printf
- sizeof
- array
Answer
C. sizeof The definition describes sizeof.
63. Which C concept matches this description: “memory address reference”?
- pointer
- array
- struct
- static
Answer
A. pointer The definition describes pointer.
64. Which C concept matches this description: “contiguous elements of one type”?
- array
- sizeof
- scanf
- continue
Answer
A. array The definition describes array.
65. Which C concept matches this description: “group of named fields”?
- break
- continue
- scanf
- struct
Answer
D. struct The definition describes struct.
66. Which C concept matches this description: “exit the nearest loop or switch”?
- struct
- break
- array
- static
Answer
B. break The definition describes break.
67. Which C concept matches this description: “skip to next loop iteration”?
- continue
- break
- pointer
- array
Answer
A. continue The definition describes continue.
68. Which C concept matches this description: “static storage duration for a local variable”?
- pointer
- sizeof
- static
- struct
Answer
C. static The definition describes static.
Mock test
- C Programming Mock Test 15 questions · 15 min