C - String Practice Test.1

  1. In C, which data type is commonly used to represent a string?

    A) int

    B) char

    C) float

    D) double

    Answer: B) char

  2. What is the purpose of the null character ('\0') at the end of a C string?

    A) It indicates the start of the string.

    B) It marks the end of the string.

    C) It represents a space character.

    D) It separates words within the string.

    Answer: B) It marks the end of the string.

  3. Which C library function is used to calculate the length (number of characters) of a string?

    A) strlength()

    B) strlen()

    C) strcount()

    D) strsize()

    Answer: B) strlen()

  4. What is the correct way to initialize a C string with the text "Hello"?

    A) char str[] = "Hello";

    B) char str = "Hello";

    C) char str[5] = {'H', 'e', 'l', 'l', 'o'};

    D) char str[] = {'H', 'e', 'l', 'l', 'o'};

    Answer: A) char str[] = "Hello";

  5. Which C string function is used to concatenate (combine) two strings?

    A) strcat()

    B) strconcat()

    C) strjoin()

    D) strmerge()

    Answer: A) strcat()

  6. What does the C library function `strcmp()` do?

    A) Compares two strings and returns their lengths.

    B) Compares two strings and returns their concatenation.

    C) Compares two strings lexicographically and returns an integer value.

    D) Copies one string into another.

    Answer: C) Compares two strings lexicographically and returns an integer value.

  7. How can you input a string with spaces in C using the `scanf()` function?

    A) `scanf("%s", str);`

    B) `scanf("%c", str);`

    C) `scanf("%d", str);`

    D) `scanf("%[^\n]", str);`

    Answer: D) `scanf("%[^\n]", str);`

  8. Which C string function is used to find the first occurrence of a substring within a string?

    A) strstr()

    B) strfind()

    C) strlocate()

    D) strsearch()

    Answer: A) strstr()

  9. What is the purpose of the `strcpy()` function in C?

    A) It calculates the length of a string.

    B) It compares two strings.

    C) It copies one string into another.

    D) It converts a string to uppercase.

    Answer: C) It copies one string into another.

  10. Which C string function is used to convert a string to all uppercase characters?

    A) strupper()

    B) strtoupper()

    C) strconvert()

    D) strcase()

    Answer: B) strtoupper()

  11. In C, how are strings typically stored in memory?

    A) As arrays of characters

    B) As integer values

    C) As floating-point numbers

    D) As boolean values

    Answer: A) As arrays of characters

  12. What is the maximum length of a string that can be stored in a character array in C?

    A) 256 characters

    B) 512 characters

    C) It depends on available memory

    D) 1024 characters

    Answer: C) It depends on available memory

  13. Which C library function is used to copy one string into another?

    A) `strcat()`

    B) `strncpy()`

    C) `strcpy()`

    D) `strdup()`

    Answer: C) `strcpy()`

  14. What happens if you exceed the allocated memory for a string in C?

    A) The program crashes

    B) The excess characters are automatically discarded

    C) Overflowed characters are stored in a separate buffer

    D) It's not possible to exceed the allocated memory

    Answer: A) The program crashes

  15. Which function in C is used to safely concatenate two strings and prevent buffer overflows?

    A) `strcat()`

    B) `strncat()`

    C) `strconcat()`

    D) `strmerge()`

    Answer: B) `strncat()`

  16. What does the C library function `strncpy()` do?

    A) Compares two strings

    B) Copies one string into another with a specified length

    C) Concatenates two strings

    D) Searches for a substring in a string

    Answer: B) Copies one string into another with a specified length

  17. In C, how do you determine the length of a string stored in a character array?

    A) Use the `strlength()` function

    B) Use the `strlen()` function

    C) Use the `strcount()` function

    D) Count the characters manually

    Answer: B) Use the `strlen()` function

  18. Which option allows you to store a string with spaces in a character array using `scanf()` in C?

    A) `%s`

    B) `%c`

    C) `%d`

    D) `%[^\n]`

    Answer: D) `%[^\n]`

  19. What is the purpose of the `strdup()` function in C?

    A) It calculates the length of a string

    B) It compares two strings

    C) It duplicates a string and returns a pointer to the duplicate

    D) It converts a string to lowercase

    Answer: C) It duplicates a string and returns a pointer to the duplicate

  20. When storing strings in C, what character is used to mark the end of a string?

    A) '\n'

    B) '\r'

    C) '\t'

    D) '\0'

    Answer: D) '\0'

  21. What is a string delimiter in C?

    A) A character that marks the end of a string

    B) A character used to separate words in a string

    C) A character that represents a space in a string

    D) A character used for encryption in strings

    Answer: A) A character that marks the end of a string

  22. Which character is commonly used as the string delimiter in C?

    A) '@'

    B) '#'

    C) '\n'

    D) '\0'

    Answer: D) '\0'

  23. In C, what happens when the string delimiter character '\0' is encountered in a string?

    A) It is ignored, and the string continues

    B) It marks the end of the string

    C) It is treated as a space character

    D) It causes a runtime error

    Answer: B) It marks the end of the string

  24. What is the purpose of a string delimiter when reading input using functions like `scanf()`?

    A) It indicates the start of the string

    B) It separates words in the string

    C) It helps identify numeric values

    D) It marks the end of the input

    Answer: B) It separates words in the string

  25. Which of the following characters is NOT suitable as a string delimiter in C?

    A) ','

    B) '!'

    C) '\0'

    D) ' '

    Answer: D) ' '

  26. How is a string delimiter represented in C code?

    A) '\\d'

    B) '\\s'

    C) '\\0'

    D) '\\n'

    Answer: C) '\\0'

  27. What is the role of the `gets()` function in C regarding string delimiters?

    A) It removes string delimiters

    B) It adds string delimiters

    C) It reads input until a string delimiter is encountered

    D) It converts string delimiters to spaces

    Answer: C) It reads input until a string delimiter is encountered

  28. In C, which function is used to find the length of a string up to the first occurrence of a specified delimiter character?

    A) `strlength()`

    B) `strlen()`

    C) `strtok()`

    D) `strdelim()`

    Answer: C) `strtok()`

  29. What is the primary advantage of using a custom string delimiter in C instead of the null character ('\0')?

    A) Custom delimiters provide better memory management

    B) Custom delimiters are easier to type

    C) Custom delimiters can represent multiple characters

    D) Custom delimiters can be used for encryption

    Answer: C) Custom delimiters can represent multiple characters

  30. When using the `strtok()` function in C, how do you specify a custom delimiter character?

    A) By providing it as an argument to `strtok()`

    B) By setting a global variable `str_delim`

    C) By using the `strdelimiter()` function

    D) By modifying the string's null character

    Answer: A) By providing it as an argument to `strtok()`

  31. What is a C string literal?

    A) A variable that stores strings

    B) A sequence of characters enclosed in double quotation marks

    C) A data type for representing strings

    D) A function for manipulating strings

    Answer: B) A sequence of characters enclosed in double quotation marks

  32. Which of the following is a valid C string literal?

    A) 'Hello, World!'

    B) "Hello, World!"

    C) (Hello, World!)

    D) "Hello, World!"

    Answer: D) "Hello, World!"

  33. What is the data type of a C string literal?

    A) char

    B) int

    C) string

    D) text

    Answer: A) char

  34. What character must always appear before and after a C string literal?

    A) Single quotation marks (')

    B) Double quotation marks (")

    C) Square brackets ([])

    D) Parentheses ()

    Answer: B) Double quotation marks (")

  35. Can a C string literal span multiple lines in the source code?

    A) Yes, but only if it's enclosed in single quotation marks

    B) Yes, by using the backslash (\) at the end of each line

    C) No, C string literals must be on a single line

    D) Yes, without any special characters

    Answer: B) Yes, by using the backslash (\) at the end of each line

  36. Which of the following is a valid escape sequence in a C string literal?

    A) \a

    B) \e

    C) \s

    D) \x

    Answer: A) \a

  37. What does the escape sequence '\n' represent in a C string literal?

    A) Null character

    B) Backspace

    C) Newline

    D) Tab

    Answer: C) Newline

  38. How can you include a double quotation mark (") within a C string literal?

    A) It's not possible to include a double quotation mark

    B) Use the escape sequence '\"'

    C) Use single quotation marks instead of double quotation marks

    D) Use square brackets []

    Answer: B) Use the escape sequence '\"'

  39. What happens if a C string literal is too long to fit in memory?

    A) It's truncated to fit

    B) It causes a compile-time error

    C) It automatically allocates additional memory

    D) It's not possible for a string literal to be too long

    Answer: B) It causes a compile-time error

  40. In C, can you modify the characters of a string literal after it's defined?

    A) Yes, using string manipulation functions

    B) No, string literals are immutable

    C) Only if you use the 'const' keyword

    D) Yes, by directly assigning new values to individual characters

    Answer: B) No, string literals are immutable

  41. Which C function is commonly used to read a string of characters from the standard input (keyboard)?

    A) `scanf()`

    B) `gets()`

    C) `input_string()`

    D) `read_string()`

    Answer: B) `gets()`

  42. What is the primary drawback of using the `gets()` function for reading strings?

    A) It cannot read strings with spaces.

    B) It doesn't null-terminate the string.

    C) It is slow in reading large strings.

    D) It is not a standard C library function.

    Answer: A) It cannot read strings with spaces.

  43. Which C library function is recommended for safely reading strings from standard input while avoiding buffer overflow?

    A) `scanf()`

    B) `gets()`

    C) `fgets()`

    D) `read_line()`

    Answer: C) `fgets()`

  44. What is the purpose of the `fgets()` function's third argument in C?

    A) It specifies the maximum number of characters to read.

    B) It determines the starting position for reading.

    C) It defines the buffer size for the input.

    D) It sets the file pointer to the end of the file.

    Answer: A) It specifies the maximum number of characters to read.

  45. Which function is commonly used to read a single character from standard input without buffering?

    A) `getc()`

    B) `getchar()`

    C) `read_char()`

    D) `input_character()`

    Answer: B) `getchar()`

  46. In C, how do you read a string containing spaces using the `scanf()` function?

    A) Use `%s` format specifier.

    B) Use `%c` format specifier.

    C) Use `%d` format specifier.

    D) Use `%f` format specifier.

    Answer: A) Use `%s` format specifier.

  47. What is the advantage of using the `fgets()` function over the `gets()` function for reading strings?

    A) `fgets()` automatically null-terminates the string.

    B) `fgets()` is faster in reading large strings.

    C) `fgets()` can read multiple lines of text.

    D) `fgets()` is not a standard C library function.

    Answer: C) `fgets()` can read multiple lines of text.

  48. Which function is used to read a character from the standard input without consuming the newline character (Enter key)?

    A) `getch()`

    B) `getchar()`

    C) `read_char()`

    D) `input_character()`

    Answer: B) `getchar()`

  49. What is the primary advantage of using `fgets()` for reading strings from a file in C?

    A) It automatically converts strings to integers.

    B) It can read binary data.

    C) It handles newline characters correctly.

    D) It is faster than other string input functions.

    Answer: C) It handles newline characters correctly.

  50. Which function is used to clear the input buffer in C when needed?

    A) `clear_input()`

    B) `flush_input()`

    C) `fflush(stdin)`

    D) There is no standard function for clearing the input buffer.

    Answer: D) There is no standard function for clearing the input buffer.