C Strings & Functions - Tricky MCQ

Previous C Strings & String Functions Next

Tricky Questions on Strings & Functions

Basic Level (15 Questions)

1

What is the fundamental difference between a character array and a string in C?

Correct Answer: A) Strings must end with null character '\0'

In C, a string is a character array that ends with null terminator '\0'. Character arrays without null terminator are not considered strings. String functions rely on '\0' to determine end. Strings don't have built-in length tracking.

2

What does strlen() return for an uninitialized character array?

Correct Answer: C) Garbage value until null character found

strlen() counts characters until '\0'. Uninitialized arrays contain garbage values. strlen() continues counting until random '\0' is encountered, returning unpredictable length. This can cause buffer overruns or segmentation faults.

3

What is the return value of strcmp("Hello", "Hello")?

Correct Answer: A) 0 (strings are equal)

strcmp() returns 0 when strings are identical, negative if first string is lexicographically smaller, positive if first string is larger. Zero doesn't mean false; in C, 0 is false but for strcmp, 0 means match, non-zero means mismatch.

4

What is wrong with: char *str = "Hello"; str[0] = 'h';?

Correct Answer: D) All of the above

String literals are stored in read-only memory. Attempting to modify them causes undefined behavior (often segmentation fault). Should use: const char *str = "Hello"; or char str[] = "Hello"; for modifiable strings.

5

What does strcpy() return?

Correct Answer: B) Pointer to destination string

strcpy() returns pointer to destination string. This allows chaining: strcpy(strcpy(dest, src1), src2). It doesn't check destination buffer size, leading to buffer overflow if source is larger than destination.

6

What is the difference between strcmp() and strncmp()?

Correct Answer: D) All of the above

strncmp() limits comparison to n characters, making it safer against buffer overflows. It stops at null terminator or after n characters. Useful for comparing fixed-length strings or prefixes.

7

What happens with: char str[5] = "Hello"; printf("%s", str);?

Correct Answer: B) Buffer overflow, missing null terminator

"Hello" needs 6 bytes (5 chars + '\0'). str[5] only holds 5 bytes, so null terminator is omitted. printf() continues printing beyond array until random '\0', causing undefined behavior.

8

What does strcat() do if destination has no null terminator?

Correct Answer: B) Undefined behavior - searches for null

strcat() finds end of destination by searching for '\0'. Without null terminator, it continues past array bounds, causing buffer overflow and undefined behavior. Always ensure destination is properly null-terminated.

9

What is strchr() return value if character not found?

Correct Answer: A) NULL pointer

strchr() returns pointer to first occurrence of character, or NULL if not found. Always check return value before dereferencing. Similar functions: strrchr() (last occurrence), strstr() (substring search).

10

What is difference between strdup() and strcpy()?

Correct Answer: D) All of the above

strdup() allocates memory with malloc() and copies string. Not part of C standard but common extension (POSIX). Caller must free() returned memory. strcpy() requires pre-allocated destination buffer.

11

What is strtok()'s limitation with nested calls?

Correct Answer: D) All of the above

strtok() uses static internal pointer, making it non-reentrant. Can't tokenize multiple strings in nested loops. Replaces delimiters with '\0', modifying original string. strtok_r() (reentrant version) solves some issues.

12

What does sprintf() return?

Correct Answer: A) Number of characters written

sprintf() returns number of characters written (excluding null terminator). snprintf() is safer as it limits characters written. Neither checks if destination buffer is large enough - can cause buffer overflow.

13

What is memcpy() vs strcpy() difference?

Correct Answer: D) All of the above

memcpy() copies specified number of bytes regardless of content. strcpy() stops at null terminator. memcpy() works with any data type, strcpy() only null-terminated strings. memcpy() doesn't add null terminator.

14

What happens with: char *p = malloc(10); strcpy(p, "Hello World");?

Correct Answer: D) Both A and C

"Hello World" needs 12 bytes (11 chars + '\0'). malloc(10) allocates only 10 bytes. strcpy() causes buffer overflow, writing beyond allocated memory, leading to undefined behavior (corruption, crash).

15

What is the safe alternative to strcat()?

Correct Answer: D) All of the above

strncat() limits characters appended. snprintf() formats safely with size limit. Manual implementation with explicit bounds checking is most controlled. Always ensure destination has enough space including null terminator.

Hard Level (15 Questions)

16

Length of `strlen("a\0b")`?

Correct Answer: B) 1

strlen stops at first '\0'; only 'a' is counted before the embedded null.

17

`strcpy(dst, src)` requires:

Correct Answer: A) dst large enough — otherwise undefined

If dst buffer is too small, writing past end is undefined behavior.

18

`strcmp("\0", "")` returns:

Correct Answer: A) 0

Both strings are empty; first bytes are equal null terminators.

19

`strncpy(dst, "hello", 5)` on char dst[10]:

Correct Answer: B) dst has no null if strlen(src)>=5

strncpy pads with nulls only if room within n; if n==strlen exactly, may omit terminator.

20

`sprintf(buf, "%s", long_string)` risk:

Correct Answer: A) Buffer overflow if buf too small

sprintf does not bound output; long_string can exceed buf.

21

Concatenation `"ab" "cd"` in source:

Correct Answer: B) String literal "abcd"

Adjacent string literals are concatenated at translation phase.

22

`char *p = "x";` then `p[1]='y';`

Correct Answer: B) Undefined behavior

Modifying string literal storage is undefined.

23

`strchr(s, 'c')` returns NULL when:

Correct Answer: C) Both A and B for invalid s

No match returns NULL; s NULL is undefined; finding '\0' returns pointer to terminator.

24

Wide string L"hi" has elements of type:

Correct Answer: B) wchar_t

L prefix creates wide string literal of wchar_t.

25

`strdup` is:

Correct Answer: B) POSIX; allocates copy with malloc

strdup is POSIX, not standard C; caller frees the allocation.

26

`snprintf` with %s and null pointer (extension aside):

Correct Answer: B) Undefined in standard C

Passing null to %s is undefined behavior in standard C.

27

UTF-8 in char strings: `strlen` on valid UTF-8 text returns:

Correct Answer: B) Byte count

strlen counts bytes until '\0', not Unicode characters.

28

`strtok` modifies:

Correct Answer: B) Input string by writing '\0'

strtok inserts null bytes in the original string splitting tokens.

29

Array `char s[4] = "abc";` — valid?

Correct Answer: A) Yes, fits with terminator

"abc" needs 4 bytes including '\0'; exactly fits s[4].

30

`memcmp(a,b,0)` when a,b valid:

Correct Answer: A) 0

Comparing zero bytes always returns 0 per memcmp specification.

Previous C Strings & String Functions Next