C Arrays and Strings Quiz — 18 Questions with Answers

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?

  1. 0
  2. 1
  3. -1
  4. 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));
  1. 2
  2. 3
  3. 4
  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?

  1. 5
  2. 6
  3. 4
  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?

  1. strcpy
  2. strcmp
  3. strcat
  4. 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]);
  1. A garbage value
  2. 0
  3. 2
  4. 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?

  1. 5
  2. 6
  3. 9
  4. 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]'?

  1. They yield the same address
  2. arr is larger by one element
  3. &arr[0] is invalid
  4. 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);
  1. hello
  2. hallo
  3. Compile error
  4. 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';'?

  1. The string becomes "Hello"
  2. Undefined behavior — string literals are read-only
  3. Compile error
  4. 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?

  1. 0
  2. A positive value
  3. A negative value
  4. 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?

  1. strcpy
  2. strncpy
  3. memcpy
  4. 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?

  1. '\n'
  2. '\0'
  3. '.'
  4. 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);
  1. abc
  2. def
  3. abcdef
  4. 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?

  1. Converts an int to a string
  2. Converts a string to an int
  3. Converts ASCII to integer type
  4. 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?

  1. The index of the first 'l'
  2. A pointer to the first occurrence of 'l' in the string
  3. The count of 'l' characters
  4. 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));
  1. 4
  2. 5
  3. 6
  4. 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. 1
  2. 2
  3. 3
  4. 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));
  1. 0
  2. 1
  3. 4
  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

More C Quizzes