4.3 - String Handling in C.


Back Button Home

4.3.1 - Introduction to Strings and its Functions ( Strcat(),Strlen(),Strcmp() etc).


1. What does the null character "\0" represent in a string in C?
  • (A) The first character of the string
  • (B) The end of the string
  • (C) A whitespace character
  • (D) A separator
Correct Answer: (B) The end of the string
2. Which function is used to find the length of a string in C?
  • (A) strlen()
  • (B) strcpy()
  • (C) strcat()
  • (D) strcmp()
Correct Answer: (A) strlen()
3. What is the return value of the strlen() function when applied to an empty string?
  • (A) -1
  • (B) 1
  • (C) 0
  • (D) Undefined
Correct Answer: (C) 0
4. Which function is used to copy one string to another in C?
  • (A) strcat()
  • (B) strcpy()
  • (C) strlen()
  • (D) strcmp()
Correct Answer: (B) strcpy()
5. Which function is used to concatenate two strings in C?
  • (A) strcpy()
  • (B) strcmp()
  • (C) strcat()
  • (D) strlen()
Correct Answer: (C) strcat()
6. What is the return value of strcmp() if two strings are identical?
  • (A) 0
  • (B) 1
  • (C) -1
  • (D) Undefined
Correct Answer: (A) 0
7. Which header file is required for using string functions like strlen(), strcpy(), and strcmp()?
  • (A) stdio.h
  • (B) stdlib.h
  • (C) string.h
  • (D) conio.h
Correct Answer: (C) string.h
8. If char s1[] = "hello"; and char s2[] = "world";, what does strcat(s1, s2); do?
  • (A) Combines the two strings and stores them in s2
  • (B) Combines the two strings and stores them in s1
  • (C) Compares the two strings
  • (D) Finds the length of the strings
Correct Answer: (B) Combines the two strings and stores them in s1
9. Which of the following string functions compares two strings lexicographically?
  • (A) strcpy()
  • (B) strcat()
  • (C) strcmp()
  • (D) strlen()
Correct Answer: (C) strcmp()
10. If char s1[] = "abc"; and char s2[] = "ABC";, what does strcmp(s1, s2); return?
  • (A) 0
  • (B) Positive integer
  • (C) Negative integer
  • (D) Undefined
Correct Answer: (C) Negative integer
11. Which function can be used to safely copy a specified number of characters from one string to another?
  • (A) strncpy()
  • (B) strncat()
  • (C) strncmp()
  • (D) strrev()
Correct Answer: (A) strncpy()
12. What does the strcmp() function return if the first string is lexicographically smaller than the second?
  • (A) 0
  • (B) 1
  • (C) -1
  • (D) A negative integer
Correct Answer: (D) A negative integer
13. What will strlen("hello world") return?
  • (A) 10
  • (B) 11
  • (C) 12
  • (D) 9
Correct Answer: (B) 11
14. Which of the following strings will be stored correctly in memory?
  • (A) "abc"
  • (B) "abc\0"
  • (C) 'abc'
  • (D) Both (A) and (B)
Correct Answer: (D) Both (A) and (B)
15. Which function can be used to append a specific number of characters from one string to another?
  • (A) strncat()
  • (B) strcat()
  • (C) strncpy()
  • (D) strncmp()
Correct Answer: (A) strncat()
16. What does strcpy() do with the null terminator \0 when copying a string?
  • (A) Ignores it
  • (B) Copies it
  • (C) Replaces it
  • (D) Appends it at the start
Correct Answer: (B) Copies it
17. What is the output of strcmp("apple", "banana")?
  • (A) 0
  • (B) Positive integer
  • (C) Negative integer
  • (D) Undefined
Correct Answer: (C) Negative integer
18. In C, strings are stored as arrays of characters terminated by which character?
  • (A) NULL
  • (B) '\0'
  • (C) ' ' (space)
  • (D) EOF
Correct Answer: (B) '\0'
19. Which of the following functions is not used for string comparison?
  • (A) strcmp()
  • (B) strncmp()
  • (C) strcpy()
  • (D) Both (A) and (B)
Correct Answer: (C) strcpy()
20. What will the function strcmp("apple", "apple") return?
  • (A) 1
  • (B) -1
  • (C) 0
  • (D) Undefined
Correct Answer: (C) 0

4.3.2 - String Handling Functions.


21. Which function is used for string concatenation in C?
  • (A) strcpy()
  • (B) strcat()
  • (C) strcmp()
  • (D) strlen()
Correct Answer: (B) strcat()
22. What does the strcat() function do?
  • (A) Copies one string into another
  • (B) Compares two strings
  • (C) Appends one string to another
  • (D) Finds the length of the string
Correct Answer: (C) Appends one string to another
23. What is the correct syntax of the strcat() function?
  • (A) int strcat(char* dest, char* src)
  • (B) char* strcat(char* dest, const char* src)
  • (C) char* strcat(const char* dest, char* src)
  • (D) int strcat(const char* dest, const char* src)
Correct Answer: (B) char* strcat(char* dest, const char* src)
24. What will strlen("Hello World") return?
  • (A) 11
  • (B) 10
  • (C) 12
  • (D) 9
Correct Answer: (A) 11
25. Which header file is needed for string functions like strcpy() and strlen()?
  • (A) stdlib.h
  • (B) conio.h
  • (C) string.h
  • (D) math.h
Correct Answer: (C) string.h
26. Which function is used to copy one string to another in C?
  • (A) strcpy()
  • (B) strcat()
  • (C) strcmp()
  • (D) strlen()
Correct Answer: (A) strcpy()
27. Which function is used to determine the length of a string in C?
  • (A) strcpy()
  • (B) strcmp()
  • (C) strlen()
  • (D) strcat()
Correct Answer: (C) strlen()
28. In strcat(), the source string is:
  • (A) Overwritten
  • (B) Appended to the destination string
  • (C) Compared with the destination string
  • (D) Copied before appending
Correct Answer: (B) Appended to the destination string
29. What does the strlen() function return?
  • (A) Length of the string including the null character
  • (B) Length of the string excluding the null character
  • (C) Always 0
  • (D) Undefined
Correct Answer: (B) Length of the string excluding the null character
30. If char str[] = "C language";, what will strlen(str) return?
  • (A) 9
  • (B) 10
  • (C) 11
  • (D) 12
Correct Answer: (C) 11
31. What is the output of strcpy(dest, src) if dest has a size smaller than src?
  • (A) Copies partially
  • (B) Causes undefined behavior
  • (C) Copies completely
  • (D) Returns error
Correct Answer: (B) Causes undefined behavior
32. Which function compares two strings lexicographically?
  • (A) strcat()
  • (B) strcpy()
  • (C) strcmp()
  • (D) strlen()
Correct Answer: (C) strcmp()
33. What does strcmp() return if the first string is lexicographically smaller than the second?
  • (A) A positive integer
  • (B) 0
  • (C) A negative integer
  • (D) Undefined behavior
Correct Answer: (C) A negative integer
34. What will strlen("") return?
  • (A) -1
  • (B) 1
  • (C) 0
  • (D) Undefined
Correct Answer: (C) 0
35. Which string function returns the concatenated string?
  • (A) strcpy()
  • (B) strlen()
  • (C) strcat()
  • (D) strcmp()
Correct Answer: (C) strcat()
36. What does strcpy(dest, src) do?
  • (A) Compares src and dest
  • (B) Copies src to dest
  • (C) Appends src to dest
  • (D) Returns the length of src
Correct Answer: (B) Copies src to dest
37. If char s[] = "apple";, what will strlen(s) return?
  • (A) 4
  • (B) 5
  • (C) 6
  • (D) 0
Correct Answer: (B) 5
38. What will be the result of strcmp("apple", "apple")?
  • (A) 0
  • (B) 1
  • (C) -1
  • (D) Undefined
Correct Answer: (A) 0
39. Which function is used to concatenate two strings in C?
  • (A) strcpy()
  • (B) strcat()
  • (C) strlen()
  • (D) strcmp()
Correct Answer: (B) strcat()
40. What happens if strcmp() finds two identical strings?
  • (A) Returns a positive integer
  • (B) Returns a negative integer
  • (C) Returns 0
  • (D) Causes an error
Correct Answer: (C) Returns 0

4.3.3 - String Pointers.


41. What does a pointer store in C?
  • (A) Value of a variable
  • (B) Address of a variable
  • (C) Size of a variable
  • (D) Type of a variable
Correct Answer: (B) Address of a variable
42. What is the primary use of pointers in C?
  • (A) To store values directly
  • (B) To store memory addresses
  • (C) To store array indexes
  • (D) To declare variables
Correct Answer: (B) To store memory addresses
43. What does the * symbol represent in pointer declaration?
  • (A) Value of the pointer
  • (B) Memory address
  • (C) Dereferencing the pointer
  • (D) Array indexing
Correct Answer: (C) Dereferencing the pointer
44. How do you declare a pointer to a string in C?
  • (A) char *ptr;
  • (B) int *ptr;
  • (C) float *ptr;
  • (D) double *ptr;
Correct Answer: (A) char *ptr;
45. What will char *ptr = "Hello"; store in ptr?
  • (A) The string "Hello"
  • (B) The first character of the string
  • (C) The address of the string
  • (D) A null value
Correct Answer: (C) The address of the string
46. Which of the following is true about pointers and arrays?
  • (A) Pointers and arrays are the same
  • (B) Arrays store addresses, pointers store values
  • (C) Arrays store values, pointers store addresses
  • (D) Pointers can’t access arrays
Correct Answer: (C) Arrays store values, pointers store addresses
47. How do you access the value of a variable through a pointer?
  • (A) By using &ptr
  • (B) By using *ptr
  • (C) By using ptr++
  • (D) By using ptr--
Correct Answer: (B) By using *ptr
48. What will *(ptr + 2) access in a string pointer ptr pointing to "Hello"?
  • (A) First character
  • (B) Second character
  • (C) Third character
  • (D) Null character
Correct Answer: (C) Third character
49. How can you move to the next character in a string using a pointer?
  • (A) ptr++
  • (B) ptr--
  • (C) *ptr + 1
  • (D) *ptr++
Correct Answer: (A) ptr++
50. Which of the following function uses pointers to pass variables by reference?
  • (A) strcat()
  • (B) scanf()
  • (C) printf()
  • (D) malloc()
Correct Answer: (B) scanf()
51. How does char *ptr = str; work in C?
  • (A) Assigns the first character of str to ptr
  • (B) Assigns the address of str to ptr
  • (C) Copies str to ptr
  • (D) Converts str to ptr
Correct Answer: (B) Assigns the address of str to ptr
52. What is the output of the following code?
            char str[6] = "Hello";
            char *ptr = str;
            printf("%c", *(ptr+1));
            
  • (A) H
  • (B) e
  • (C) l
  • (D) o
Correct Answer: (B) e
53. What does the expression ptr[2] mean if ptr is a pointer to a string?
  • (A) Pointer addition
  • (B) Pointer subtraction
  • (C) Accesses the third character of the string
  • (D) Multiplies the pointer value by 2
Correct Answer: (C) Accesses the third character of the string
54. What does char *ptr = "Hello"; do in memory?
  • (A) Copies "Hello" to ptr
  • (B) Points ptr to the address of "Hello"
  • (C) Initializes ptr with an empty string
  • (D) Assigns a null value to ptr
Correct Answer: (B) Points ptr to the address of "Hello"
55. Which operator is used to get the address of a variable in C?
  • (A) *
  • (B) &
  • (C) +
  • (D) -
Correct Answer: (B) &
56. What is the output of this code?
            char str[6] = "World";
            char *ptr = str;
            while(*ptr != '\0') {
                printf("%c", *ptr);
                ptr++;
            }
            
  • (A) World
  • (B) orld
  • (C) Worl
  • (D) Nothing
Correct Answer: (A) World
57. Which function can dynamically allocate memory for a string?
  • (A) malloc()
  • (B) free()
  • (C) sizeof()
  • (D) realloc()
Correct Answer: (A) malloc()
58. Which of the following is true about pointer arithmetic in C?
  • (A) ptr++ increments the pointer to point to the next element
  • (B) ptr++ decrements the pointer to point to the previous element
  • (C) Pointers cannot be incremented
  • (D) Pointers can only be decremented
Correct Answer: (A) ptr++ increments the pointer to point to the next element
59. What does *(ptr++) do?
  • (A) Increments the value of the pointer
  • (B) Moves the pointer to the next memory location after dereferencing
  • (C) Decrements the pointer
  • (D) Causes a segmentation fault
Correct Answer: (B) Moves the pointer to the next memory location after dereferencing
60. Why is it necessary to use free() with dynamically allocated memory?
  • (A) To prevent memory leakage
  • (B) To increase memory allocation
  • (C) To avoid pointer arithmetic errors
  • (D) To initialize variables
Correct Answer: (A) To prevent memory leakage