Sample questions
Question 1Easy
What is the output?
#include <stdio.h>
int main(void) {
int a[3] = {1, 2, 3}, b[3] = {4, 5, 6};
a = b;
printf("%d\n", a[0]);
return 0;
}
- Runtime error
- 4
- Compilation error
- 1
Answer: C. Compilation error
Explanation
An array name is not a modifiable lvalue, so arrays cannot be assigned. Copy elements in a loop or with memcpy.
Step-by-step solution
- Arrays are not assignable in C.
- a = b is a compile-time error.
Why the other options are wrong
- A. It never runs.
- B. It would not compile.
- D. The program does not compile.
Tip: Arrays cannot be assigned.
Question 2Easy
What is the output?
#include <stdio.h>
#include <string.h>
int main(void) {
char buf[20] = "Hi";
strcat(buf, ", ");
strcat(buf, "C");
printf("%s|%zu\n", buf, strlen(buf));
return 0;
}
- Hi, C|5
- Hi,C|4
- Hi, C|20
- Hi, C|6
Answer: A. Hi, C|5
Explanation
strcat appends after the existing text, giving "Hi, C" with 5 characters; strlen ignores the unused part of the 20-byte buffer.
Step-by-step solution
- buf becomes Hi, C.
- Length is 5.
Why the other options are wrong
- B. Comma and space present.
- C. 20 is capacity.
- D. Length is 5.
Tip: Length != capacity.
Question 3Easy
What is the output?
#include <stdio.h>
int main(void) {
const char *s = "a1b22c333";
int digits = 0, sum = 0;
for (; *s; s++)
if (*s >= '0' && *s <= '9') {
digits++;
sum += *s - '0';
}
printf("%d %d\n", digits, sum);
return 0;
}
- 6 14
- 3 14
- 9 14
- 6 356
Answer: A. 6 14
Explanation
The digit characters are 1, 2, 2, 3, 3, 3: six of them, summing to 14.
Step-by-step solution
- The loop goes through each character of the string a1b22c333.
- The digit characters are 1, 2, 2, 3, 3 and 3, which is 6 digits.
- Their sum is 1 + 2 + 2 + 3 + 3 + 3 = 14. The output is 6 14, the marked option A.
Why the other options are wrong
- B. There are 6 digit characters, not 3; each digit is counted.
- C. 9 is the total number of characters, not the number of digits.
- D. 356 is the digits joined together, not their sum of 14.
Tip: Subtract '0' from a digit character to get its numeric value.
Question 4Easy
What is the output?
#include <stdio.h>
int main(void) {
int a[5] = {1, 2};
int s = 0;
for (int i = 0; i < 5; i++)
s += a[i] * i;
printf("%d\n", s);
return 0;
}
- 3
- 2
- Garbage value
- 8
Answer: B. 2
Explanation
Elements without an initialiser are set to 0, so only a[1] * 1 = 2 contributes (a[0] is multiplied by 0).
Step-by-step solution
- The array a is {1, 2, 0, 0, 0}, because missing entries are zero.
- The sum is a[0] × 0 + a[1] × 1 + a[2] × 2 + a[3] × 3 + a[4] × 4.
- Only a[1] × 1 = 2 is non-zero, so s = 2.
- Output is 2: option B.
Why the other options are wrong
- A. 3 would need an extra non-zero term; the only non-zero term is 2.
- C. Missing array entries are zero, not garbage.
- D. 8 would need a[2] to be 4, but a[2] is 0.
Tip: A partly initialised array in C is zero-filled for the entries that are not given values.
Question 5Medium
What is the output?
#include <stdio.h>
int main(void) {
int a[][3] = {{1, 2}, {3}, {4, 5, 6}};
printf("%zu %d\n", sizeof a / sizeof a[0], a[0][2] + a[1][1] + a[2][2]);
return 0;
}
- 2 6
- 3 9
- 9 6
- 3 6
Answer: D. 3 6
Explanation
The three brace groups make 3 rows. Missing elements are zero, so 0 + 0 + 6 = 6.
Step-by-step solution
- Sizeof a is 36 bytes and sizeof a[0] is 12 bytes, so there are 36 / 12 = 3 rows.
- a[0][2] is 0, a[1][1] is 0 because it is not given, and a[2][2] is 6.
- The sum is 0 + 0 + 6 = 6. The output is 3 6, the marked option D.
Why the other options are wrong
- A. There are 3 rows, not 2; the third brace group still makes a row.
- B. The sum is 6, not 9, because missing elements are 0.
- C. There are 3 rows, not 9; 9 is the number of elements, not rows.
Tip: Missing elements in a C initialiser are zero, and the first dimension can be left out.
Question 6Medium
What is the output?
#include <stdio.h>
#include <string.h>
int main(void) {
char s[] = "a,b,,c";
int n = 0;
for (char *t = strtok(s, ","); t; t = strtok(NULL, ","))
n++;
printf("%d\n", n);
return 0;
}
- 5
- 3
- 4
- 2
Answer: B. 3
Explanation
strtok treats consecutive delimiters as one, so the empty field between the two commas is skipped: tokens are a, b and c.
Step-by-step solution
- Strtok splits the string on commas and skips empty fields between consecutive delimiters.
- The tokens are a, then b, then the empty field between the two commas is skipped, then c.
- That gives 3 tokens, so n is 3. The output is 3, the marked option B.
Why the other options are wrong
- A. 5 would count the empty field, but strtok never returns an empty token here.
- C. 4 would count the empty field, but strtok merges consecutive delimiters into one.
- D. 2 misses the last token c.
Tip: Strtok merges consecutive delimiters; use strchr or a manual scan if empty fields matter.
Question 7Medium
What is the output?
#include <stdio.h>
#include <string.h>
int main(void) {
printf("%d %d %d\n",
strcmp("Zoo", "apple") < 0,
strcmp("abc", "abcd") < 0,
strcmp("B", "a") > 0);
return 0;
}
- 0 1 1
- 1 1 0
- 1 0 0
- 0 1 0
Answer: B. 1 1 0
Explanation
strcmp compares character codes: 'Z' (90) < 'a' (97); "abc" is a prefix of "abcd" so it is smaller; 'B' (66) < 'a' (97) so the last test is false.
Step-by-step solution
- Strcmp returns a negative value when the first string is smaller by character code.
- Z (90) is less than a (97), so the first test is true: 1.
- Abc is a prefix of abcd, so it is smaller: 1.
- B (66) is less than a (97), so the last test is false: 0.
- Output is 1 1 0: option B.
Why the other options are wrong
- A. The first test is true because Z is less than a, so it should be 1; B is less than a, so the last should be 0.
- C. The second test is true because abc is a prefix of abcd, so it should be 1.
- D. Z is less than a, so the first test is 1, not 0.
Tip: Strcmp compares character codes: uppercase letters come before lowercase, and a prefix is smaller than the longer string.
Question 8Medium
After
char d[3];
strncpy(d, "hello", 3);
what is true about d?
- d holds "hel" with no terminating '\0'
- d holds "he" followed by '\0'
- strncpy overflows d by writing 6 bytes
- d holds "hello" because strncpy grows the array
Answer: A. d holds "hel" with no terminating '\0'
Explanation
strncpy copies at most n bytes and adds a terminator only if the source is shorter than n, so d is not a valid string.
Step-by-step solution
- strncpy(d, "hello", 3) copies at most 3 characters: h, e and l.
- The source is longer than 3, so no terminator is added after the third character.
- D holds hel with no terminator, so it is not a valid string. The answer is option A.
Why the other options are wrong
- B. D holds three characters h, e and l, not he followed by a terminator.
- C. Strncpy writes at most 3 bytes into the 3-byte array, so there is no overflow.
- D. Arrays never grow; d stays 3 bytes and holds only hel.
Tip: Strncpy does not always add a terminator; leave room and set the last byte to \0 yourself.
Question 9Hard
Assume a 64-bit system: int is 4 bytes, pointers are 8 bytes, usual alignment. What is the output?
#include <stdio.h>
int main(void) {
char a[][6] = {"one", "three", "five"};
const char *b[] = {"one", "three", "five"};
printf("%zu %zu\n", sizeof a, sizeof b);
return 0;
}
- 15 24
- 24 18
- 18 24
- 18 12
Answer: C. 18 24
Explanation
a is a 3 x 6 char array (18 bytes). b is an array of 3 pointers (3 x 8 = 24 bytes); the strings live elsewhere.
Step-by-step solution
- A is char a[][6] with 3 rows of 6 bytes each: 3 × 6 = 18 bytes.
- B is an array of 3 pointers; each pointer is 8 bytes on this 64-bit system: 3 × 8 = 24 bytes.
- The output is 18 24, the marked option C.
Why the other options are wrong
- A. A has 3 rows of 6 bytes, so it is 18, not 15.
- B. The two numbers are swapped: a is 18 bytes and b is 24 bytes.
- D. B holds 3 pointers of 8 bytes each, so it is 24 bytes, not 12.
Tip: Sizeof on an array gives the total bytes; sizeof on a pointer array counts 8 bytes per pointer here.
Question 10Hard
What is the output?
#include <stdio.h>
#include <string.h>
int main(void) {
char s[] = "123456";
memmove(s + 2, s, 3);
puts(s);
return 0;
}
- 121256
- 121212
- 123123
- 121236
Answer: D. 121236
Explanation
The three bytes "123" are copied to positions 2..4 as if through a temporary buffer, replacing "345" and leaving the final 6.
Step-by-step solution
- S starts as 123456; memmove copies the 3 bytes 1, 2, 3 from index 0 to index 2.
- Memmove is safe when the areas overlap, as if it copied through a temporary buffer.
- The result is 1 2 1 2 3 6, so the output is 121236, the marked option D.
Why the other options are wrong
- A. Index 4 should be 3 from the original text, so 121256 with a 5 there is wrong.
- B. Index 5 stays 6, so the result is not 121212.
- C. Only 3 bytes move, so the result is not a full copy of 123123.
Tip: Use memmove when source and destination may overlap; memcpy does not promise that.
Practise all 27 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →