C Strings & Functions - Tricky MCQ

Previous C Strings & String Functions Next

Tricky Questions on Strings & Functions

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.

Previous C Strings & String Functions Next