Test your understanding of C arrays and strings with these 18 multiple-choice questions — array decay, sizeof versus strlen, string literals, initialization, and out-of-bounds traps. Every question has the answer and a short explanation hidden below it, and the trickier ones link to a full walkthrough with compilable code.
These questions come from our free Android app, C Programming Quiz — 150+ questions across 9 categories with score tracking, if you prefer to practise on your phone.
How to use this page: answer each question yourself before tapping Show answer. If you get one wrong, follow the linked explanation — knowing why the wrong options are wrong is what interviews test.
Question 1 of 18 (Easy)
What is the index of the first element of an array in C?
- 0
- 1
- -1
- Depends on the compiler
Show answer
Answer: A — 0
C arrays are zero-indexed: the first element is arr[0] and the last is arr[n-1].
Question 2 of 18 (Medium)
What is the output of this code?
char str[] = "hi";
printf("%lu", sizeof(str));
- 2
- 3
- 4
- 1
Show answer
Answer: B — 3
A string literal includes a terminating '\0'. "hi" needs 'h', 'i', and '\0' — 3 bytes.
Full walkthrough with compilable code: read the detailed explanation.
Question 3 of 18 (Easy)
What does strlen("hello") return?
- 5
- 6
- 4
- Compile error
Show answer
Answer: A — 5
strlen counts characters up to but not including the terminating null byte. "hello" has 5 characters.
Question 4 of 18 (Easy)
Which standard function copies one string into another?
- strcpy
- strcmp
- strcat
- strchr
Show answer
Answer: A — strcpy
strcpy copies a source string (including its null terminator) into a destination buffer. strcmp compares, strcat concatenates, strchr searches.
Question 5 of 18 (Medium)
What is the output of this code?
int a[5] = {1, 2};
printf("%d", a[3]);
- A garbage value
- 0
- 2
- Compile error
Show answer
Answer: B — 0
When an array has an initializer list shorter than its size, the remaining elements are zero-initialized. So a[3] is 0.
Full walkthrough with compilable code: read the detailed explanation.
Question 6 of 18 (Easy)
How many total elements does 'int a[2][3];' hold?
- 5
- 6
- 9
- 23
Show answer
Answer: B — 6
A 2D array a[2][3] has 2 rows of 3 columns each: 2 x 3 = 6 elements.
Question 7 of 18 (Medium)
For an array 'arr', what is true about 'arr' and '&arr[0]'?
- They yield the same address
- arr is larger by one element
- &arr[0] is invalid
- They differ by sizeof(arr)
Show answer
Answer: A — They yield the same address
In most expressions an array name decays to a pointer to its first element, so arr and &arr[0] evaluate to the same address.
Question 8 of 18 (Hard)
What is the output of this code?
char s[] = "hello";
s[1] = 'a';
printf("%s", s);
- hello
- hallo
- Compile error
- Undefined behavior
Show answer
Answer: B — hallo
s is an array (copy of the literal), so modifying it is legal. s[1] changes 'e' to 'a', giving "hallo".
Full walkthrough with compilable code: read the detailed explanation.
Question 9 of 18 (Hard)
What happens when you modify a string via 'char *p = "hello"; p[0] = 'H';'?
- The string becomes "Hello"
- Undefined behavior — string literals are read-only
- Compile error
- Nothing — C ignores the write
Show answer
Answer: B — Undefined behavior — string literals are read-only
String literals reside in read-only memory. Writing through a pointer to a literal is undefined behavior and typically causes a segfault at runtime.
Question 10 of 18 (Medium)
What does strcmp("abc", "abd") return?
- 0
- A positive value
- A negative value
- Depends on the platform
Show answer
Answer: C — A negative value
strcmp compares lexicographically. 'c' < 'd', so "abc" comes before "abd" and the function returns a negative value.
Question 11 of 18 (Medium)
Which function safely limits the number of characters copied to prevent buffer overflow?
- strcpy
- strncpy
- memcpy
- sprintf
Show answer
Answer: B — strncpy
strncpy takes a max-length argument, so it will not write beyond the specified limit. strcpy has no length limit and is a classic source of buffer overflows.
Question 12 of 18 (Easy)
Which character marks the end of a C string?
- '\n'
- '\0'
- '.'
- EOF
Show answer
Answer: B — '\0'
C strings are null-terminated: a '\0' (null character, ASCII 0) marks the end. String functions stop reading at this character.
Question 13 of 18 (Hard)
What is the output of this code?
char s[10] = "abc";
strcat(s, "def");
printf("%s", s);
- abc
- def
- abcdef
- Undefined behavior
Show answer
Answer: C — abcdef
s has enough space. strcat appends "def" after "abc", placing the null terminator after 'f'. printf prints "abcdef".
Full walkthrough with compilable code: read the detailed explanation.
Question 14 of 18 (Medium)
What does the atoi function do?
- Converts an int to a string
- Converts a string to an int
- Converts ASCII to integer type
- Checks if a string is a valid integer
Show answer
Answer: B — Converts a string to an int
atoi (ASCII to integer) converts a string like "42" to the integer 42. It is declared in <stdlib.h>. Prefer strtol for error checking.
Question 15 of 18 (Medium)
What does strchr('hello', 'l') return?
- The index of the first 'l'
- A pointer to the first occurrence of 'l' in the string
- The count of 'l' characters
- NULL
Show answer
Answer: B — A pointer to the first occurrence of 'l' in the string
strchr searches for the first occurrence of a character and returns a pointer to it, or NULL if not found. It returns a char*, not an index.
Question 16 of 18 (Hard)
What is the output of this code?
char *p = "hello";
printf("%d", (int)strlen(p));
- 4
- 5
- 6
- Compile error
Show answer
Answer: B — 5
strlen counts characters up to (not including) the null terminator. "hello" has 5 characters.
Full walkthrough with compilable code: read the detailed explanation.
Question 17 of 18 (Hard)
What is the output of this code?
int a[3] = {1, 2, 3};
printf("%d", *(a + 1));
- 1
- 2
- 3
- Address of a[1]
Show answer
Answer: B — 2
a decays to a pointer to its first element. a + 1 points to the second element. *(a + 1) dereferences it, giving 2.
Full walkthrough with compilable code: read the detailed explanation.
Question 18 of 18 (Hard)
What is the output of this code?
char s[] = "abcd";
printf("%d", sizeof(s) - strlen(s));
- 0
- 1
- 4
- 5
Show answer
Answer: B — 1
sizeof(s) is 5 (4 chars + null terminator). strlen(s) is 4. 5 – 4 = 1.
Full walkthrough with compilable code: read the detailed explanation.
How Did You Score?
All 18 correct means this topic is interview-ready — try the harder timed rounds in the C Programming Quiz app. Missed a few? The guides below cover everything these questions test.
Keep Practising
- sizeof vs strlen in C
- char Array vs String Literal in C
- C Programs List — 130+ Examples
- All quiz question walkthroughs
More C Quizzes
- C Basics Quiz — 28 Questions with Answers
- C Operators Quiz — 20 Questions with Answers
- C Memory Management Quiz — 19 Questions with Answers
- C Pointers Quiz — 18 Questions with Answers
- C Functions Quiz — 15 Questions with Answers
- C Structs and Unions Quiz — 13 Questions with Answers
- C File I/O Quiz — 13 Questions with Answers
- C Preprocessor Quiz — 11 Questions with Answers