C Programming Strings MCQs

Basic Strings in C (30 Questions)

1. How is a string represented in C?
A) As a primitive data type
B) As an array of characters
C) As a linked list of characters
D) As a structure
Answer: B) As an array of characters
In C, strings are represented as arrays of characters terminated by a null character '\0'.
2. What is the correct way to declare a string in C?
A) string str = "Hello";
B) char str[] = "Hello";
C) char *str = "Hello";
D) Both B and C
Answer: D) Both B and C
Strings can be declared as character arrays or as pointers to character literals.
3. What is the null character in C strings?
A) '\n'
B) '\0'
C) '0'
D) NULL
Answer: B) '\0'
The null character '\0' marks the end of a string in C.
4. What is the output of: char str[] = "Hello"; printf("%d", sizeof(str));
A) 5
B) 6
C) 4
D) Compilation error
Answer: B) 6
"Hello" has 5 characters plus the null terminator, totaling 6 bytes.
5. Which function is used to find the length of a string?
A) strlen()
B) strlength()
C) len()
D) length()
Answer: A) strlen()
strlen() from string.h returns the length of a string excluding the null terminator.
6. What is the output of: char *str = "Hello"; printf("%c", str[1]);
A) H
B) e
C) l
D) o
Answer: B) e
str[1] accesses the second character 'e' (indexing starts at 0).
7. Which header file is needed for string manipulation functions?
A) stdio.h
B) string.h
C) stdlib.h
D) strings.h
Answer: B) string.h
string.h contains functions like strlen(), strcpy(), strcat(), etc.
8. What is the output of: char str[10] = "Hello"; printf("%d", strlen(str));
A) 5
B) 6
C) 10
D) Undefined
Answer: A) 5
strlen() returns the number of characters before the null terminator.
9. How do you concatenate two strings in C?
A) str1 + str2
B) strcat(str1, str2)
C) concat(str1, str2)
D) str1.append(str2)
Answer: B) strcat(str1, str2)
strcat() from string.h concatenates the second string to the first.
10. What is the difference between char str[] = "Hello"; and char *str = "Hello";?
A) No difference
B) First is modifiable, second is not
C) First is pointer, second is array
D) Second takes more memory
Answer: B) First is modifiable, second is not
Array version can be modified, while pointer to string literal should not be modified.
11. Which function copies one string to another?
A) strcopy()
B) strcpy()
C) copy()
D) stringcpy()
Answer: B) strcpy()
strcpy(dest, src) copies the source string to destination.
12. What is the output of: char str[5] = "Hello"; printf("%s", str);
A) Hello
B) Undefined behavior
C) Hell
D) Compilation error
Answer: B) Undefined behavior
Array is too small to hold "Hello" plus null terminator, causing buffer overflow.
13. Which function compares two strings lexicographically?
A) strcmp()
B) strcompare()
C) compare()
D) strdiff()
Answer: A) strcmp()
strcmp() returns 0 if equal, negative if first is less, positive if first is greater.
14. What is the output of: printf("%d", strcmp("apple", "Apple"));
A) 0
B) 1
C) -1
D) Positive value
Answer: D) Positive value
Lowercase 'a' has higher ASCII value than uppercase 'A'.
15. How do you find a substring within a string?
A) strfind()
B) strstr()
C) substr()
D) findstr()
Answer: B) strstr()
strstr(haystack, needle) returns pointer to first occurrence or NULL.
16. What is the output of: char str[] = "Hello\0World"; printf("%s", str);
A) HelloWorld
B) Hello\0World
C) Hello
D) World
Answer: C) Hello
printf() stops at the first null character encountered.
17. Which function converts a string to uppercase?
A) toupper()
B) strupper()
C) strtoupper()
D) No standard function
Answer: D) No standard function
C doesn't have a standard function - you need to implement it using toupper() in a loop.
18. What is the output of: char str[10]; scanf("%s", str); if input is "Hello World"?
A) Hello World
B) Hello
C) World
D) Undefined behavior
Answer: B) Hello
scanf() with %s stops at the first whitespace.
19. How do you safely read a line of input including spaces?
A) scanf("%s", str);
B) gets(str);
C) fgets(str, size, stdin);
D) readline(str);
Answer: C) fgets(str, size, stdin);
fgets() is safe as it limits input size, unlike gets() which is dangerous.
20. What is the output of: printf("%c", "Hello"[1]);
A) H
B) e
C) l
D) o
Answer: B) e
String literals can be indexed like arrays (0-based index).
21. Which function converts a string to a number?
A) atoi()
B) strtoi()
C) parseint()
D) convert()
Answer: A) atoi()
atoi() converts ASCII string to integer (also atof(), atol()).
22. What is the output of: char *str = "Hello"; str[0] = 'M'; printf("%s", str);
A) Mello
B) Hello
C) Runtime error
D) Compilation error
Answer: C) Runtime error
String literals are read-only - attempting modification causes undefined behavior.
23. How do you tokenize a string using delimiters?
A) strtok()
B) tokenize()
C) split()
D) explode()
Answer: A) strtok()
strtok() breaks string into tokens based on delimiters.
24. What is the safer alternative to strcpy()?
A) strcpy_s()
B) strncpy()
C) strlcpy()
D) All of the above
Answer: D) All of the above
All these functions provide bounds checking unlike strcpy().
25. What is the output of: printf("%d", sizeof("Hello"));
A) 5
B) 6
C) 4
D) 8
Answer: B) 6
sizeof includes the null terminator for string literals.
26. Which function reverses a string in place?
A) strrev()
B) reverse()
C) strreverse()
D) No standard function
Answer: D) No standard function
C doesn't have a standard string reverse function - must implement manually.
27. What is the output of: char str[] = {'H','e','l','l','o'}; printf("%s", str);
A) Hello
B) Undefined behavior
C) Compilation error
D) Hel
Answer: B) Undefined behavior
Missing null terminator causes printf() to read beyond array bounds.
28. How do you format a string into a buffer?
A) sprintf()
B) printf()
C) format()
D) strfmt()
Answer: A) sprintf()
sprintf() writes formatted output to a string buffer.
29. What is the output of: printf("%d", strcmp("123", "1234"));
A) 0
B) 1
C) -1
D) 4
Answer: C) -1
First string is shorter, so returns negative value.
30. Which function locates the first occurrence of a character in a string?
A) strchr()
B) strfind()
C) strloc()
D) charfind()
Answer: A) strchr()
strchr() returns pointer to first occurrence of character or NULL.

String Functions in C (20 Questions)

1. What does strncpy() do that strcpy() doesn't?
A) Adds null terminator automatically
B) Limits number of characters copied
C) Works with overlapping strings
D) Returns success/failure
Answer: B) Limits number of characters copied
strncpy() takes a maximum length parameter to prevent buffer overflow.
2. What is the output of: char dest[5]; strncpy(dest, "HelloWorld", 5); printf("%s", dest);
A) Hello
B) Undefined behavior
C) HelloWorld
D) No output
Answer: B) Undefined behavior
strncpy() doesn't add null terminator if source is longer than count.
3. What is the safer alternative to strcat()?
A) strncat()
B) strlcat()
C) strcat_s()
D) All of the above
Answer: D) All of the above
All these functions provide bounds checking unlike strcat().
4. What is the output of: char str[10] = "Hello"; strncat(str, "World", 3); printf("%s", str);
A) HelloWorld
B) HelloWor
C) HelloWorl
D) HelloWor
Answer: B) HelloWor
strncat() appends at most 3 characters plus null terminator.
5. What does memcpy() do that strcpy() doesn't?
A) Handles overlapping memory
B) Copies null terminator
C) Copies specified number of bytes
D) Works with any data type
Answer: C) Copies specified number of bytes
memcpy() copies raw bytes without checking for null terminators.
6. When should you use memmove() instead of memcpy()?
A) When copying strings
B) When source and destination overlap
C) When copying between different types
D) When you need faster copying
Answer: B) When source and destination overlap
memmove() handles overlapping memory regions correctly.
7. What is the output of: char str[] = "Hello"; memset(str, 'A', 3); printf("%s", str);
A) AAAAA
B) AAllo
C) AAAlo
D) Hello
Answer: B) AAllo
memset() sets first 3 bytes to 'A', leaving the rest unchanged.
8. What does strspn() return?
A) Length of string
B) Length of initial segment containing only characters from accept set
C) Position of first character not in accept set
D) Number of distinct characters
Answer: B) Length of initial segment containing only characters from accept set
strspn() scans string until it finds a character not in accept set.
9. What is the output of: printf("%d", strcspn("Hello123", "123"));
A) 3
B) 5
C) 6
D) 8
Answer: B) 5
strcspn() returns position of first character in reject set ("123"), which is '1' at index 5.
10. What does strpbrk() return?
A) Pointer to first occurrence of any character from accept set
B) Position of first character not in reject set
C) Pointer to last occurrence of any character
D) Length of matching prefix
Answer: A) Pointer to first occurrence of any character from accept set
strpbrk() is like strchr() but searches for any of multiple characters.
11. What is the output of: printf("%s", strrchr("HelloHello", 'l'));
A) lloHello
B) llo
C) lo
D) loHello
Answer: D) loHello
strrchr() finds last occurrence and returns pointer to it.
12. What is the output of: printf("%d", strspn("123abc", "0123456789"));
A) 0
B) 3
C) 6
D) 1
Answer: B) 3
First 3 characters are digits, then 'a' is not in accept set.
13. Which function is safer than gets() for reading input?
A) scanf()
B) fgets()
C) readline()
D) getline()
Answer: B) fgets()
fgets() allows specifying buffer size to prevent overflow.
14. What is the output of: char str[10]; fgets(str, 5, stdin); if input is "HelloWorld"?
A) Hello
B) Hell
C) HelloWorl
D) HelloWorld
Answer: B) Hell
fgets() reads at most size-1 characters (4) plus adds null terminator.
15. What is the output of: printf("%d", strcmp("apple", "APPLE"));
A) 0
B) Positive value
C) Negative value
D) Compilation error
Answer: B) Positive value
Lowercase letters have higher ASCII values than uppercase.
16. Which function performs case-insensitive string comparison?
A) stricmp()
B) strcasecmp()
C) strcmpi()
D) All of the above
Answer: D) All of the above
These are common non-standard extensions (POSIX/Windows).
17. What is the output of: printf("%d", strncmp("apple", "application", 3));
A) 0
B) Positive value
C) Negative value
D) 3
Answer: A) 0
First 3 characters "app" match in both strings.
18. What is the output of: printf("%s", strstr("HelloWorld", "Wo"));
A) World
B) Wo
C) W
D) HelloWorld
Answer: A) World
strstr() returns pointer to first occurrence of substring.
19. What is the output of: printf("%s", strtok("Hello,World,Test", ","));
A) Hello
B) Hello,World,Test
C) World
D) Test
Answer: A) Hello
strtok() returns first token delimited by comma.
20. How do you convert a string to lowercase in C?
A) strlwr()
B) tolower() in a loop
C) strtolower()
D) Both A and B
Answer: D) Both A and B
strlwr() is non-standard but common, otherwise use tolower() in a loop.