String in C Programming MCQ Questions and Answers

1. Which of the following correctly declares a modifiable string containing “hello”?
A) char *s = “hello”;
B) const char *s = “hello”;
C) char s[] = “hello”;
D) char s[5] = “hello”;
Answer: C

2. What is the value returned by strlen(“abc\0def”)?
A) 0
B) 3
C) 7
D) 6
Answer: B

3. Which function stops reading input at the first whitespace?
A) gets()
B) fgets()
C) scanf(“%s”, buf)
D) fread()
Answer: C

4. Which of these statements is true about string literals?
A) They are always stored on the stack.
B) They may be stored in read-only memory; modifying them causes undefined behavior.
C) They are automatically freed at program end only if allocated with malloc.
D) They are stored in the heap by default.
Answer: B

5. Which function copies at most n-1 characters and always null-terminates the destination (if n > 0)?
A) strcpy()
B) strncpy()
C) strcat()
D) snprintf()
Answer: D

6. After char s[6] = “hello”; printf(“%zu”, sizeof(s)); — what prints?
A) 5
B) 6
C) Implementation-defined
D) 7
Answer: B

7. What does strcmp(a, b) return when a is lexicographically less than b?
A) negative value
B) zero
C) positive value
D) 1
Answer: A

8. Which function finds the first occurrence of a character in a string?
A) strstr()
B) strchr()
C) strrchr()
D) strtok()
Answer: B

9. What does strncpy(dest, src, n) do if strlen(src) < n?
A) Leaves dest unchanged.
B) Copies src to dest and pads with null bytes up to n.
C) Causes buffer overflow.
D) Copies only part and does not null-terminate.
Answer: B

10. Which function concatenates two strings?
A) strcpy()
B) strncpy()
C) strcat()
D) strcmp()
Answer: C

11. Which of these is a safe replacement to read a line into buf of size N?
A) gets(buf);
B) fgets(buf, N, stdin);
C) scanf(“%s”, buf);
D) gets_s(buf);
Answer: B

12. What must you ensure before calling strcat(dest, src)?
A) dest is NULL
B) dest has enough space to hold original dest + src + ‘\0’
C) src is longer than dest
D) dest and src are the same pointer
Answer: B

13. Which of the following returns a pointer to the first occurrence of the substring needle in haystack?
A) strchr()
B) strstr()
C) strpbrk()
D) strcspn()
Answer: B

14. What happens when you do char *p = NULL; printf(“%s”, p);?
A) Prints “(null)” portably
B) Undefined behavior (likely crash)
C) Prints nothing
D) Prints garbage safely
Answer: B

15. What does strcspn(s, t) return?
A) Length of prefix in s consisting only of chars from t
B) Index of first occurrence of any char from t in s
C) Index of first char in t not in s
D) Number of initial characters in s that are not in t
Answer: D

16. Which standard function tokenizes a string and modifies it by writing nulls into it?
A) strtok()
B) strstr()
C) strdup()
D) strtol()
Answer: A

17. When using strtok() in multithreaded code, which function is preferred?
A) strtok()
B) strtok_r() (reentrant)
C) strstr()
D) strdup()
Answer: B

18. Which function compares at most n characters of two strings?
A) strcmp()
B) strncmp()
C) strcmpn()
D) strncat()
Answer: B

19. Given char s[] = “abc”; char *p = s+1; what is p[1]?
A) ‘a’
B) ‘b’
C) ‘c’
D) ‘\0’
Answer: C

20. Which header must be included to use strlen, strcpy, strcmp?
A) <stdio.h>
B) <string.h>
C) <stdlib.h>
D) <ctype.h>
Answer: B

21. What is the result of sizeof(“abc”)?
A) 3
B) 4
C) Implementation defined
D) Sizeof returns pointer size
Answer: B

22. Which function converts a string to a long integer with range checking and error detection?
A) atoi()
B) strtol()
C) atol()
D) itoa()
Answer: B

23. Which of these functions can be used to safely append at most n chars to a destination buffer (C11/C99)?
A) strncat()
B) strcat()
C) strcpy()
D) memcpy()
Answer: A

24. For strncat(dest, src, n), what does n mean?
A) Total size of dest buffer
B) Maximum number of characters to append from src
C) Total length of result string
D) Number of bytes to zero-fill dest
Answer: B

25. Which function returns the length of the initial segment of s which consists entirely of characters in accept?
A) strspn()
B) strcspn()
C) strpbrk()
D) strchr()
Answer: A

26. Which function finds the last occurrence of a character in a string?
A) strchr()
B) strrchr()
C) strstr()
D) strpbrk()
Answer: B

27. If char a[5] = “test”; what is a[4]?
A) ‘t’
B) ‘\0’
C) Undefined
D) ‘s’
Answer: B

28. Which of the following uses no library call and correctly computes string length n?
A) Loop until s[n] == ‘\0’ then n++
B) Use while(*s) { s++; len++; } starting with len=0
C) Use sizeof(s) on char *s
D) Both A and C
Answer: B

29. Which function compares strings according to the current locale?
A) strcmp()
B) strcoll()
C) strncmp()
D) strxfrm()
Answer: B

30. Which of the following copies n bytes from src to dest and correctly handles overlapping regions?
A) memcpy()
B) memmove()
C) strcpy()
D) strncpy()
Answer: B

31. What is returned by strcpy(dest, src)?
A) NULL
B) Pointer to dest
C) Pointer to src
D) Number of bytes copied
Answer: B

32. Given char s[] = “abc”; char *p = “abc”; which is true?
A) Both s and p are modifiable arrays.
B) s points to read-only memory.
C) p points to a string literal (may be read-only) while s is a mutable array copy.
D) Both are identical in type and storage.
Answer: C

33. Using fgets(buf, sizeof buf, stdin) — if the input fits fully, what may buf contain at the end?
A) No terminating \0
B) A trailing newline \n before \0
C) Always ends with \r\n
D) fgets removes newline always
Answer: B

34. Which function returns a pointer to the first character in s that matches any of the characters in accept?
A) strspn()
B) strcspn()
C) strpbrk()
D) strchr()
Answer: C

35. What is the behavior of sprintf(dest, “%s”, src) if dest is too small?
A) Truncation with guaranteed null-termination
B) Buffer overflow and undefined behavior
C) Function returns error code -1
D) It reallocates dest automatically
Answer: B

36. Which function does not add a terminating null character if the source length is >= n?
A) strcpy()
B) strncpy()
C) strcat()
D) snprintf()
Answer: B

37. Which function returns the number of characters in the initial segment of s which are not in reject?
A) strspn()
B) strcspn()
C) strpbrk()
D) strtok()
Answer: B

38. Which function is intended to transform strings in a way suitable for locale-aware comparison?
A) strcoll()
B) strxfrm()
C) strcpy()
D) strcmp()
Answer: B

39. Which of the following is true for char *s = malloc(6); strcpy(s, “hello”);?
A) You forgot to include space for ‘\0’.
B) Memory allocated is enough and string fits with null terminator.
C) strcpy will not write a null terminator.
D) Undefined because malloc returns const pointer.
Answer: B

40. Which is true: printf(“%s”, “abc\0def”);
A) Prints “abc\0def” including the \0.
B) Prints “abc” only.
C) Prints “abcdef”.
D) Undefined behavior.
Answer: B

41. Which function returns a pointer to the first location in s where any of the characters in accept are found, or NULL if none found?
A) strspn()
B) strcspn()
C) strpbrk()
D) strstr()
Answer: C

42. Which function converts a floating-point number to a string?
A) sprintf() with %f
B) strtof()
C) strtod()
D) atof()
Answer: A

43. Which is true about memset(buf, 0, sizeof buf) for a char array?
A) Makes every char equal to 0 — same as empty string at start.
B) Causes undefined behavior.
C) Only sets first byte to zero.
D) Equivalent to strcpy(buf, “”) for pointers.
Answer: A

44. Which function does not belong to <string.h>?
A) strcpy
B) memcpy
C) printf
D) strstr
Answer: C

45. Which of the following is a portable way to duplicate a string (standard C)?
A) strdup()
B) malloc(strlen(s) + 1); strcpy(…)
C) strcpy(malloc(strlen(s)), s)
D) alloca(strlen(s)+1)
Answer: B

46. What happens if you call free() on a pointer not returned by malloc/calloc/realloc?
A) No effect
B) Undefined behavior
C) Free returns -1
D) It zeroes the pointer automatically
Answer: B

47. Which is true about strncat(dest, src, n) if n is 0?
A) Appends no characters, but still overwrites dest’s null with null.
B) Appends up to 0 characters and ensures result is null-terminated (no change).
C) Causes undefined behavior.
D) Copies entire src.
Answer: B

48. Which returns the number of bytes copied when using memcpy?
A) The function returns the destination pointer; no count returned.
B) The function returns number of bytes.
C) Returns NULL on success.
D) Returns source pointer.
Answer: A

49. How does strtok() treat consecutive delimiters by default?
A) Returns empty tokens for consecutive delimiters
B) Skips consecutive delimiters and returns next non-empty token
C) Crashes
D) Returns delimiter as token
Answer: B

50. Which of the following compares strings case-insensitively in portable ANSI C?
A) strcasecmp() (POSIX)
B) No standard C function; must write custom or use locale-aware functions.
C) strcmpi()
D) stricmp()
Answer: B

51. For dynamic string assembly, which is recommended to avoid frequent reallocations?
A) Use many strcat() calls on small buffers.
B) Pre-calculate total length and allocate once with malloc.
C) Use gets() to receive variable length input.
D) Use stack arrays only.
Answer: B

52. Which behavior is undefined?
A) Accessing s[strlen(s)] (the terminating null) for read.
B) Writing to a string literal via char *p = “x”; p[0] = ‘y’;
C) Passing a NULL pointer to strlen()
D) All of B and C are undefined; A is defined for read.
Answer: D

53. Which function parses a string to double with error detection?
A) atof()
B) strtod()
C) strtol()
D) atoi()
Answer: B

54. Which of these statements about strtok is true?
A) It is reentrant.
B) It modifies its input string by inserting ‘\0’ characters.
C) It returns tokens including delimiters.
D) It can be used on string literals safely.
Answer: B

55. If you need to search for substring “abc” in a very large text repeatedly and performance matters, which could help?
A) Use repeated strstr() calls without pre-processing.
B) Use more efficient algorithms (Boyer–Moore, KMP) or pre-process text.
C) strchr() alone suffices.
D) Use strcmp() on every index.
Answer: B

56. Which of the following is a safe use of sprintf?
A) sprintf(buf, “%s”, src) where buf length unknown — safe.
B) snprintf(buf, sizeof buf, “%s”, src) — safe if sizeof buf known.
C) sprintf(NULL, “%s”, src) — safe.
D) sprintf(buf, “%s”, src) with buf smaller than src — safe.
Answer: B

57. Which function returns a pointer to the first occurrence in s of any character in accept?
A) strpbrk()
B) strspn()
C) strcspn()
D) strchr()
Answer: A

58. Which of the following is true about strncpy and destination null-termination?
A) Always null-terminates dest.
B) Never null-terminates dest.
C) Null-terminates only if strlen(src) < n.
D) Null-terminates only if strlen(src) >= n.
Answer: C

59. Which will correctly remove the newline (\n) from a string read by fgets into buf?
A) buf[strcspn(buf, “\n”)] = ‘\0’;
B) buf[strlen(buf)-1] = ‘\0′; without checks
C) buf = strtok(buf, “\n”); directly is safe when buf may not contain newline
D) memset(buf, 0, strlen(buf))
Answer: A

60. Which is true about using %s in scanf?
A) It reads a whole line including whitespace.
B) It reads up to whitespace; must give a width to avoid overflow.
C) It always null-terminates and will not overflow.
D) It strips the terminating null from the input.
Answer: B

61. Which returns the pointer to the character in s after converting with strtol if endptr provided?
A) strtol sets errno but returns nothing.
B) strtol returns the end pointer as its return value.
C) strtol writes into endptr the address where conversion stopped and returns the numeric value.
D) strtol returns number of characters converted.
Answer: C

62. Which function counts bytes until first character from set appears and returns count?
A) strspn()
B) strcspn()
C) strpbrk()
D) strrchr()
Answer: B

63. Which method ensures string copy is safe and null-terminated for buffer dest of size n?
A) strcpy(dest, src) always safe
B) strncpy(dest, src, n) then dest[n-1]=’\0’
C) memcpy(dest, src, n)
D) strcat(dest, src)
Answer: B

64. What does strnlen(s, maxlen) (POSIX) return?
A) Length of s if less than maxlen, otherwise maxlen
B) Always strlen(s) regardless of maxlen
C) maxlen – strlen(s)
D) Undefined
Answer: A

65. Which function is best to compare binary memory regions (not necessarily strings)?
A) strcmp()
B) memcmp()
C) strncmp()
D) strcoll()
Answer: B

66. Which of the following is a correct way to initialize a 2D array of strings literal pointers?
A) char *arr[] = {“one”, “two”, “three”};
B) char arr[][] = {“one”,”two”};
C) char arr[3][6] = {“one”,”two”}; with appropriate sizes — acceptable
D) Both A and C (with correct sizes)
Answer: D

67. Which indicates end-of-string in C strings?
A) ‘\n’
B) ‘\0’
C) EOF
D) 0xFF
Answer: B

68. Which is true about strcpy(dest, dest+2) when regions overlap?
A) Behavior is well-defined and copies correctly.
B) Undefined behavior; use memmove instead.
C) Copies backwards safely.
D) Equivalent to strncpy.
Answer: B

69. Which function converts a string to an integer and on error returns 0 but cannot detect all errors?
A) atoi()
B) strtol()
C) sscanf()
D) strtoul()
Answer: A

70. Which function returns pointer to substring found or NULL if not found?
A) strcat()
B) strstr()
C) strtok()
D) strcmp()
Answer: B

71. For char s[] = “abc”;, which expression yields pointer to the terminating null character?
A) s + 2
B) s + 3
C) s[3]
D) &s[3]
Answer: B

72. Which of the following will NOT compile in standard C?
A) char s[] = “a\0b”;
B) char *s = “a\0b”;
C) char s[1] = “ab”;
D) char s[] = { ‘a’, ‘\0’, ‘b’, ‘\0’ };
Answer: C

73. Which of the following assesses whether two strings are equal ignoring case in portable C (no POSIX)?
A) strcasecmp(a, b) — standard C
B) No standard function; convert both strings to same case and use strcmp or write custom
C) strcmpi(a,b)
D) stricmp(a,b)
Answer: B

74. Which will safely print a pointer to a string that may be NULL without crashing (portable)?
A) printf(“%s”, p); where p may be NULL — safe
B) Check if(p) printf(“%s”, p); else printf(“(null)”); — safe
C) puts(p); when p may be NULL — safe
D) printf(“%s”, p ? p : “(null)”); — safe
Answer: D

75. Which function should be used to copy memory when the source and destination might overlap?
A) memcpy()
B) memmove()
C) strcpy()
D) strncpy()
Answer: B

76. Which statement about char *s = “hello”; s = s + 1; is true?
A) Illegal — cannot change pointer to literal.
B) Legal — pointer arithmetic allowed; s now points to “ello”.
C) Modifies literal memory.
D) Undefined behavior.
Answer: B

77. Which of the following does not modify its input string?
A) strtok()
B) strstr()
C) strsep()
D) strcpy()
Answer: B

78. Which function would you use to format into a fixed-size buffer with guaranteed null-termination (if size>0)?
A) sprintf()
B) snprintf()
C) strcpy()
D) strncpy()
Answer: B

79. What is the effect of s += strlen(s); where s points to a null-terminated string?
A) s points to the first character.
B) s points to ‘\0’ at end of string.
C) s points one past the end — undefined pointer arithmetic.
D) s becomes NULL.
Answer: B

80. Which of these will find the first character in s that is not a letter (assuming locale not used)?
A) strpbrk(s, “abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ”)
B) strspn(s, “abcdefghijklmnopqrstuvwxyz…”) and compare lengths
C) Use loop with isalpha((unsigned char)c) from <ctype.h>
D) All of the above approaches can work; C is most readable and portable.
Answer: D

81. Which is true when converting wide strings to narrow C strings (multibyte) using wcstombs?
A) Always one-to-one mapping for all unicode — guaranteed.
B) Behavior depends on current locale and multibyte encoding.
C) wcstombs is locale-independent and fixed.
D) wcstombs never fails.
Answer: B

82. What is returned by strspn(“abcd123”, “abcd”)?
A) 7
B) 4
C) 3
D) 0
Answer: B

83. Which of the following indicates that strtod failed to convert anything?
A) Returns 0.0 and endptr points to original string start.
B) Returns huge value.
C) Sets errno to 0.
D) Returns NULL.
Answer: A

84. Which function splits a string into tokens but can return empty tokens when delimiters are adjacent?
A) strtok()
B) strtok_r()
C) Custom parser using strchr()/pointer arithmetic can return empty tokens
D) strstr()
Answer: C

85. Which of these will print the size (in bytes) of the pointer variable p pointing to a string on a 64-bit machine?
A) printf(“%zu”, sizeof(p)); prints 8 typically
B) printf(“%zu”, strlen(p)); prints pointer size
C) printf(“%zu”, sizeof *p); prints pointer size
D) printf(“%zu”, sizeof(p[0])); prints pointer size
Answer: A

86. Which function is best to compare two strings up to n characters, but ensuring no undefined read past either string?
A) strncmp() used carefully with known bounds
B) memcmp() — but must ensure null termination or correct lengths
C) strnlen with strncmp combined is safer
D) All of the above depending on context; C suggests combining length checks first is safest.
Answer: D

87. Which of the following will allocate dynamic memory and copy a string s into it (standard C)?
A) char *d = strdup(s); — POSIX but not standard C
B) char *d = malloc(strlen(s)+1); if (d) strcpy(d, s); — standard C correct
C) char *d = malloc(strlen(s)); strcpy(d, s); — misses space for \0
D) char *d = malloc(0); strcpy(d, s);
Answer: B

88. Which of the following is true about puts()?
A) It prints the string and a trailing newline.
B) It does not print a newline.
C) It is unsafe with NULL argument (undefined behavior).
D) A and C are true.
Answer: D

89. What does strtok(NULL, “,”) do on subsequent calls?
A) Continues tokenizing the same string where it left off.
B) Starts tokenizing a new string.
C) Returns NULL always.
D) Resets tokenizer to start of last string.
Answer: A

90. Which of these helps avoid off-by-one when allocating for a string copy?
A) Allocate strlen(s) bytes.
B) Allocate strlen(s) + 1 bytes.
C) Allocate sizeof s where s is pointer.
D) Allocate strlen(s) – 1 bytes.
Answer: B

91. Which of the following about %s and printf is correct?
A) printf(“%s”, p) reads from p until \0.
B) If p is NULL, behavior is undefined.
C) %.*s can be used to print at most N chars from a string safely.
D) All of the above.
Answer: D

92. Which is true when converting integer to string portably in C?
A) Use itoa() — standard C function.
B) Use snprintf(buf, size, “%d”, val) — portable and safe if size provided.
C) sprintf(buf, “%d”, val) is always safe.
D) Use atoi() for conversion from int to string.
Answer: B

93. Which of the following can be used to locate any character from a set in a string and return pointer to it?
A) strspn()
B) strcspn()
C) strpbrk()
D) strchr()
Answer: C

94. What should you do before calling strcpy(dest, src) if dest was dynamically allocated?
A) Ensure dest has at least strlen(src) + 1 bytes.
B) Ensure src is NULL.
C) Free dest first always.
D) Ensure dest and src are the same pointer.
Answer: A

95. Which of the following is true about memchr() when used on char buffers?
A) Searches for a byte value in a memory block and returns pointer if found.
B) Only works for strings and stops at \0.
C) Always returns index instead of pointer.
D) Converts to uppercase characters.
Answer: A

96. Which approach avoids modifying original input when tokenizing?
A) Use strtok() on a duplicate strdup() copy (or malloc+strcpy) and tokenize the copy.
B) Use strtok() directly on original if you want to preserve it.
C) Use strpbrk() only.
D) Use fgets() instead.
Answer: A

97. Which of the following functions interprets escape sequences in a string literal at runtime?
A) printf with format “%s” does not interpret \n again — it’s already interpreted at compile time.
B) scanf(“%s”, &s) interprets escape sequences — true.
C) C runtime functions reinterpret escape sequences in string contents — false; escapes are processed at compile time in literals.
D) strtok() interprets escape sequences.
Answer: C

98. Which of the following can be used to append formatted data to an existing buffer with size checking?
A) sprintf(dest + strlen(dest), “%s”, more) — unsafe unless buffer size tracked.
B) snprintf(dest + strlen(dest), remaining, “%s”, more) — safer if remaining computed correctly.
C) strcat(dest, more) — unsafe unless dest size known and sufficient.
D) All of the above are safe without additional checks.
Answer: B

99. Which of the following is true about pointer comparison of strings a and b?
A) a == b checks if strings have equal content.
B) a == b checks if a and b point to same address.
C) strcmp(a, b) checks pointer equality.
D) memcmp(a, b, sizeof a) checks content correctly for pointers.
Answer: B

100. When would memcpy(dest, src, n) be preferable to strcpy(dest, src)?
A) When copying known fixed binary data that may contain ‘\0’.
B) When copying C strings of unknown length — always prefer strcpy.
C) memcpy does not exist for strings.
D) memcpy always null-terminates.
Answer: A