0% found this document useful (0 votes)
3 views8 pages

C String Functions and Complexities Guide

The document contains a series of questions and answers related to strings in C and Python, covering topics such as string operations, time complexities, and specific functions. Key points include that strings in C are arrays of characters terminated by a null character, and various string functions like strcat(), strlen(), and strcmp() are discussed. Additionally, it addresses string matching algorithms and their complexities, as well as string manipulation methods in Python.

Uploaded by

Yash Agre
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views8 pages

C String Functions and Complexities Guide

The document contains a series of questions and answers related to strings in C and Python, covering topics such as string operations, time complexities, and specific functions. Key points include that strings in C are arrays of characters terminated by a null character, and various string functions like strcat(), strlen(), and strcmp() are discussed. Additionally, it addresses string matching algorithms and their complexities, as well as string manipulation methods in Python.

Uploaded by

Yash Agre
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

. Which of the following is true about strings in C?

a) Strings are arrays of characters.


b) Strings are terminated by a null character (\0).
c) Strings are immutable.
d) Both a and b.
Answer: d) Both a and b.

2. What is the time complexity of accessing a character at a specific index in a


string?
a) O(1)
b) O(n)
c) O(log n)
d) O(n^2)
Answer: a) O(1)

3. Which function is used to concatenate two strings in C?


a) strcat()
b) strcpy()
c) strlen()
d) strcmp()
Answer: a) strcat()

4. What is the output of the following C code snippet?


c
Copy
Edit
char str[] = "Hello";
printf("%c", str[1]);
a) H
b) e
c) l
d) o
Answer: b) e

5. Which of the following operations on a string has a time complexity of O(n)?


a) Finding the length of the string
b) Accessing a character by index
c) Checking if the string is empty
d) Both b and c
Answer: a) Finding the length of the string

6. What is the time complexity of comparing two strings of length n?


a) O(1)
b) O(n)
c) O(log n)
d) O(n^2)
Answer: b) O(n)

7. Which function is used to find the length of a string in C?


a) strcat()
b) strlen()
c) strcmp()
d) strcpy()
Answer: b) strlen()

8. Which of the following string matching algorithms has the best worst-case time
complexity?
a) Naive String Matching
b) Knuth-Morris-Pratt (KMP)
c) Rabin-Karp
d) Boyer-Moore
Answer: b) Knuth-Morris-Pratt (KMP)

9. What is the time complexity of the KMP (Knuth-Morris-Pratt) string matching


algorithm?
a) O(n)
b) O(m * n)
c) O(n^2)
d) O(n + m)
Answer: d) O(n + m)

10. Which of the following is true about the Boyer-Moore string matching algorithm?
a) It preprocesses the pattern to skip unnecessary comparisons.
b) It uses a bad character rule and a good suffix rule.
c) It is faster than the naive algorithm in most cases.
d) All of the above.
Answer: d) All of the above.

11. What is the output of the following C code snippet?


c
Copy
Edit
char str[] = "Data Structures";
printf("%d", strlen(str));
a) 15
b) 14
c) 16
d) 13
Answer: b) 14 (strlen counts characters, not including the null character.)

12. Which of the following is a valid way to declare a string in C?


a) char str[] = "Hello";
b) char str[] = {'H', 'e', 'l', 'l', 'o', '\0'};
c) char *str = "Hello";
d) All of the above
Answer: d) All of the above

13. What is the time complexity of reversing a string of length n?


a) O(n)
b) O(n log n)
c) O(1)
d) O(n^2)
Answer: a) O(n)

14. Which of the following string functions is used to copy one string into
another?
a) strcpy()
b) strcat()
c) strcmp()
d) strlen()
Answer: a) strcpy()

15. Which of the following is the correct way to compare two strings in C?
a) if (str1 == str2)
b) if (strcmp(str1, str2) == 0)
c) if (strlen(str1) == strlen(str2))
d) if ([Link](str2))
Answer: b) if (strcmp(str1, str2) == 0)
16. Which of the following is a substring search algorithm?
a) KMP Algorithm
b) Binary Search
c) Quick Sort
d) Heap Sort
Answer: a) KMP Algorithm

17. In the Rabin-Karp algorithm, what is the purpose of the hash function?
a) To convert the pattern into a numerical value.
b) To match the pattern and text in constant time.
c) To check for collisions between different substrings.
d) All of the above.
Answer: d) All of the above.

18. What is the average time complexity of the Rabin-Karp algorithm?


a) O(n)
b) O(n + m)
c) O(n * m)
d) O(n^2)
Answer: b) O(n + m)

19. Which of the following algorithms preprocesses the pattern to create a "prefix
table"?
a) KMP Algorithm
b) Boyer-Moore Algorithm
c) Rabin-Karp Algorithm
d) Naive String Matching
Answer: a) KMP Algorithm

20. What is the output of the following C code snippet?


c
Copy
Edit
char str1[] = "abc";
char str2[] = "abc";
if (strcmp(str1, str2) == 0) {
printf("Equal");
} else {
printf("Not Equal");
}
a) Equal
b) Not Equal
c) Undefined behavior
d) Compilation error
Answer: a) Equal

21. What is the time complexity of finding the longest palindromic substring in a
string of length n using dynamic programming?
a) O(n)
b) O(n log n)
c) O(n^2)
d) O(n^3)
Answer: c) O(n^2)

22. Which of the following is true about string immutability in Java?


a) String objects are immutable.
b) Once a string is created, it cannot be changed.
c) Any modifications result in a new string being created.
d) All of the above.
Answer: d) All of the above.

23. Which of the following functions is used to tokenize a string in C?


a) strchr()
b) strtok()
c) strcmp()
d) strstr()
Answer: b) strtok()

24. Which of the following is the best approach to find if a string is a


palindrome?
a) Reverse the string and compare it to the original.
b) Use a stack to compare the first half and the second half.
c) Use two pointers (one from the start and one from the end).
d) All of the above.
Answer: d) All of the above.

25. What is the output of the following C code snippet?


c
Copy
Edit
char str[] = "DSA";
str[1] = '\0';
printf("%s", str);
a) DSA
b) D
c) DS
d) Segmentation fault
Answer: b) D (Since str[1] is set to null character \0)

26. What is the time complexity of checking if two strings are anagrams?
a) O(n)
b) O(n log n)
c) O(n^2)
d) O(n + k), where k is the number of distinct characters
Answer: a) O(n) (If counting character frequencies)

27. Which of the following can be used to reverse a string in Python?


a) reversed()
b) Slicing ([::-1])
c) Loop through the string and reverse manually.
d) All of the above.
Answer: d) All of the above

28. What does the function strstr() do in C?


a) Compares two strings.
b) Returns a pointer to the first occurrence of a substring.
c) Copies one string into another.
d) Concatenates two strings.
Answer: b) Returns a pointer to the first occurrence of a substring.

29. In the Boyer-Moore string matching algorithm, what is the role of the "bad
character" heuristic?
a) It allows skipping sections of the text during the search.
b) It checks for pattern prefixes.
c) It hashes the characters in the pattern.
d) It matches the pattern in reverse.
Answer: a) It allows skipping sections of the text during the search.
30. What is the output of the following Python code snippet?
python
Copy
Edit
s = "hello"
print(s[1:4])
a) he
b) hel
c) ell
d) ello
Answer: c) ell

31. Which of the following string functions in C compares two strings


lexicographically?
a) strcmp()
b) strcpy()
c) strcat()
d) strlen()
Answer: a) strcmp()

32. What is the time complexity of finding the longest common subsequence (LCS) of
two strings of lengths m and n?
a) O(m + n)
b) O(m * n)
c) O(m^2)
d) O(n^2)
Answer: b) O(m * n)

33. Which of the following Python functions can be used to check if a string starts
with a specific prefix?
a) [Link]()
b) [Link]()
c) [Link]()
d) [Link]()
Answer: a) [Link]()

34. What is the output of the following C code snippet?


c
Copy
Edit
char str[] = "DataStructures";
printf("%s", strchr(str, 'S'));
a) DataStructures
b) Structures
c) Data
d) None of the above
Answer: b) Structures

35. Which algorithm is used to find the longest prefix that is also a suffix in a
string?
a) Rabin-Karp
b) KMP Algorithm
c) Naive Pattern Matching
d) Boyer-Moore
Answer: b) KMP Algorithm

36. Which of the following is not a valid string method in Python?


a) [Link]()
b) [Link]()
c) [Link]()
d) [Link]()
Answer: d) [Link]()

37. In Java, which method is used to split a string based on a given regular
expression?
a) split()
b) divide()
c) partition()
d) chop()
Answer: a) split()

38. What is the role of a hash function in string matching algorithms like Rabin-
Karp?
a) It converts the string into a numerical value.
b) It compresses the string.
c) It removes duplicate characters.
d) It compares two strings directly.
Answer: a) It converts the string into a numerical value.

39. What is the time complexity of constructing a prefix table in the KMP algorithm
for a string of length n?
a) O(n)
b) O(n^2)
c) O(log n)
d) O(n log n)
Answer: a) O(n)

40. What does the strncpy() function in C do?


a) Copies a specific number of characters from one string to another.
b) Concatenates two strings.
c) Compares two strings.
d) Finds the length of a string.
Answer: a) Copies a specific number of characters from one string to another.

41. What is the output of the following C code snippet?


c
Copy
Edit
char str1[] = "DSA";
char str2[] = "DSA";
if (str1 == str2) {
printf("Equal");
} else {
printf("Not Equal");
}
a) Equal
b) Not Equal
c) Compilation error
d) Undefined behavior
Answer: b) Not Equal (Pointer comparison, not string content comparison)

42. What is the best approach to find all anagrams of a pattern in a given string?
a) Use a sliding window with character frequency counting.
b) Sort the string and pattern and compare them.
c) Use the KMP algorithm.
d) Use a hash map to store all possible substrings.
Answer: a) Use a sliding window with character frequency counting.
43. Which of the following is true about the strtok() function in C?
a) It modifies the original string.
b) It returns the next token in the string.
c) It requires a delimiter to split the string.
d) All of the above.
Answer: d) All of the above.

44. Which of the following is used to remove leading and trailing whitespaces in
Python?
a) strip()
b) lstrip()
c) rstrip()
d) All of the above
Answer: d) All of the above

45. What is the time complexity of checking if two strings are rotations of each
other?
a) O(n)
b) O(n log n)
c) O(n^2)
d) O(n^3)
Answer: a) O(n) (By concatenating one string with itself and checking if the other
string is a substring)

46. What is the output of the following Python code snippet?


python
Copy
Edit
s = "hello"
print(s[::-1])
a) olleh
b) hello
c) helo
d) None of the above
Answer: a) olleh

47. Which of the following methods can be used to join a list of strings into a
single string in Python?
a) join()
b) concatenate()
c) merge()
d) append()
Answer: a) join()

48. What is the purpose of the strchr() function in C?


a) To find the first occurrence of a character in a string.
b) To compare two strings.
c) To copy one string to another.
d) To concatenate two strings.
Answer: a) To find the first occurrence of a character in a string.

49. What is the time complexity of finding the longest common prefix in an array of
strings?
a) O(n)
b) O(n * k), where k is the average length of the strings
c) O(n log n)
d) O(n^2)
Answer: b) O(n * k)
50. What is the output of the following C code snippet?
c
Copy
Edit
char str[] = "Geeks";
printf("%s", str + 1);
a) Geeks
b) eeks
c) eek
d) None of the above
Answer: b) eek

You might also like