C - String Practice Test.2

  1. Which C function is commonly used to print a string to the standard output (console)?

    A) `print()`

    B) `display()`

    C) `puts()`

    D) `output_string()`

    Answer: C) `puts()`

  2. What is the purpose of the `puts()` function in C?

    A) To put characters on the screen.

    B) To put integers into a string.

    C) To print a string followed by a newline character.

    D) To put characters in an array.

    Answer: C) To print a string followed by a newline character.

  3. Which C library function is used to format and print data to the standard output in a flexible way?

    A) `print()`

    B) `output()`

    C) `printf()`

    D) `write()`

    Answer: C) `printf()`

  4. In the `printf()` function, what does the `%s` format specifier represent?

    A) Integer

    B) Float

    C) String

    D) Character

    Answer: C) String

  5. What is the difference between `printf()` and `puts()` in C?

    A) `printf()` is used for printing integers, and `puts()` is used for strings.

    B) `printf()` adds a newline character by default, while `puts()` always requires it.

    C) `printf()` can print multiple values in a single call, while `puts()` prints only one string.

    D) There is no difference; they are used interchangeably.

    Answer: B) `printf()` adds a newline character by default, while `puts()` always requires it.

  6. Which function is used to write a single character to the standard output without buffering in C?

    A) `putc()`

    B) `putchar()`

    C) `write_char()`

    D) `output_character()`

    Answer: B) `putchar()`

  7. How do you format a floating-point number with two decimal places using `printf()` in C?

    A) `%f`

    B) `%d`

    C) `%2f`

    D) `%.2f`

    Answer: D) `%.2f`

  8. What happens if you use the `%d` format specifier with `printf()` to print a string in C?

    A) It prints the string correctly.

    B) It prints the string as an integer.

    C) It causes a compile-time error.

    D) It prints the address of the string.

    Answer: B) It prints the string as an integer.

  9. In C, which function is used to write formatted data to a file?

    A) `fread()`

    B) `write()`

    C) `fprintf()`

    D) `file_output()`

    Answer: C) `fprintf()`

  10. Which C library function is used to write a character to a file in unformatted mode?

    A) `fputc()`

    B) `putchar()`

    C) `fwrite()`

    D) `file_write_char()`

    Answer: A) `fputc()`

  11. Which C function is commonly used to read formatted data from a file using a specified format string?

    A) `scanf()`

    B) `gets()`

    C) `fread()`

    D) `fscanf()`

    Answer: D) `fscanf()`

  12. What is the primary advantage of using `fscanf()` for reading input from a file compared to `scanf()` for standard input?

    A) `fscanf()` is faster.

    B) `fscanf()` can read from both files and standard input.

    C) `fscanf()` provides more formatting options.

    D) `fscanf()` does not require a format specifier.

    Answer: B) `fscanf()` can read from both files and standard input.

  13. When using `fscanf()` for reading strings from a file, which format specifier is typically used?

    A) `%s`

    B) `%d`

    C) `%c`

    D) `%f`

    Answer: A) `%s`

  14. What is the primary purpose of `fprintf()` in C?

    A) To read formatted data from a file.

    B) To print a string to the console.

    C) To write formatted data to a file.

    D) To concatenate two strings.

    Answer: C) To write formatted data to a file.

  15. In `fscanf()`, what does the format specifier `%d` represent?

    A) String

    B) Integer

    C) Character

    D) Float

    Answer: B) Integer

  16. How can you open a file named "data.txt" in read mode using `fscanf()` in C?

    A) `fscanf("data.txt", "r")`

    B) `fopen("data.txt", "r")`

    C) `fopen("data.txt", "read")`

    D) `open("data.txt", "r")`

    Answer: B) `fopen("data.txt", "r")`

  17. What does the `fprintf()` function return on successful write to a file in C?

    A) 0

    B) 1

    C) The number of characters written

    D) -1

    Answer: C) The number of characters written

  18. In `fscanf()`, what does the format specifier `%f` represent?

    A) String

    B) Integer

    C) Character

    D) Float

    Answer: D) Float

  19. How do you close a file named "output.txt" that you have opened using `fopen()` in C?

    A) `fclose("output.txt")`

    B) `close("output.txt")`

    C) `fclose(file)`

    D) `file.close()`

    Answer: C) `fclose(file)`

  20. What is the primary advantage of using `fprintf()` over other methods for writing to a file in C?

    A) It automatically opens the file.

    B) It can write multiple data types without conversion.

    C) It provides real-time data output.

    D) It is faster than other file writing methods.

    Answer: B) It can write multiple data types without conversion.

  21. Which C library function is used to check if a character is an uppercase letter?

    A) `isalpha()`

    B) `isupper()`

    C) `toupper()`

    D) `isupperchar()`

    Answer: B) `isupper()`

  22. What does the `isalnum()` function in C check for?

    A) Whether a character is an uppercase letter.

    B) Whether a character is a digit.

    C) Whether a character is alphanumeric (either a letter or a digit).

    D) Whether a character is an alphabet letter.

    Answer: C) Whether a character is alphanumeric (either a letter or a digit).

  23. Which C function is used to convert a lowercase character to its corresponding uppercase character?

    A) `toupper()`

    B) `tolower()`

    C) `isupper()`

    D) `islower()`

    Answer: A) `toupper()`

  24. What is the purpose of the `isblank()` function in C?

    A) To check if a character is blank (whitespace).

    B) To check if a character is a special symbol.

    C) To check if a character is a digit.

    D) To check if a character is an uppercase letter.

    Answer: A) To check if a character is blank (whitespace).

  25. Which C library function is used to check if a character is a digit (0-9)?

    A) `isdigit()`

    B) `isalpha()`

    C) `isnumeric()`

    D) `isnumber()`

    Answer: A) `isdigit()`

  26. What is the return type of the `isalpha()` function in C?

    A) `int`

    B) `char`

    C) `bool`

    D) `double`

    Answer: A) `int`

  27. Which C function is used to convert an uppercase character to its corresponding lowercase character?

    A) `tolower()`

    B) `toupper()`

    C) `islower()`

    D) `isupper()`

    Answer: A) `tolower()`

  28. What does the `isprint()` function in C check for?

    A) Whether a character is printable (visible).

    B) Whether a character is a letter.

    C) Whether a character is a control character.

    D) Whether a character is a digit.

    Answer: A) Whether a character is printable (visible).

  29. What is the purpose of the `iscntrl()` function in C?

    A) To check if a character is a control character.

    B) To check if a character is a digit.

    C) To check if a character is lowercase.

    D) To check if a character is printable.

    Answer: A) To check if a character is a control character.

  30. Which C library function is used to check if a character is a punctuation symbol?

    A) `ispunct()`

    B) `isalnum()`

    C) `isalpha()`

    D) `isdigit()`

    Answer: A) `ispunct()`

  31. Which C library function is used to determine the length of a null-terminated string?

    A) `strlength()`

    B) `strlen()`

    C) `strcount()`

    D) `strsize()`

    Answer: B) `strlen()`

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

    A) To compare two strings.

    B) To concatenate two strings.

    C) To copy one string into another.

    D) To find the first occurrence of a character in a string.

    Answer: C) To copy one string into another.

  33. Which C string function is used to concatenate two strings?

    A) `strcat()`

    B) `strncat()`

    C) `strconcat()`

    D) `strmerge()`

    Answer: A) `strcat()`

  34. What does the `strcmp()` function in C do?

    A) Compares two strings and returns 1 if they are equal.

    B) Concatenates two strings.

    C) Compares two strings and returns 0 if they are equal.

    D) Copies one string into another.

    Answer: C) Compares two strings and returns 0 if they are equal.

  35. Which C library function is used to find the first occurrence of a substring in a string?

    A) `strstr()`

    B) `strfind()`

    C) `strsearch()`

    D) `strlocate()`

    Answer: A) `strstr()`

  36. What is the purpose of the `strncpy()` function in C?

    A) To compare two strings.

    B) To concatenate two strings.

    C) To copy a specified number of characters from one string into another.

    D) To find the length of a string.

    Answer: C) To copy a specified number of characters from one string into another.

  37. Which C string function is used to find the last occurrence of a character in a string?

    A) `strchr()`

    B) `strrchr()`

    C) `strlast()`

    D) `strfindlast()`

    Answer: B) `strrchr()`

  38. What does the `strtok()` function in C do?

    A) Compares two strings.

    B) Tokenizes a string into smaller parts based on a delimiter.

    C) Concatenates two strings.

    D) Copies one string into another.

    Answer: B) Tokenizes a string into smaller parts based on a delimiter.

  39. Which C library function is used to compare two strings, ignoring the case (uppercase/lowercase) of characters?

    A) `strcmpi()`

    B) `stricmp()`

    C) `strcasecmp()`

    D) `strcomparei()`

    Answer: B) `stricmp()`

  40. What is the purpose of the `memset()` function in C?

    A) To find the length of a string.

    B) To copy one string into another.

    C) To fill a block of memory with a specified value.

    D) To concatenate two strings.

    Answer: C) To fill a block of memory with a specified value.

  41. What is the purpose of the `memset()` function in C?

    A) To find the length of a string.

    B) To copy one string into another.

    C) To fill a block of memory with a specified value.

    D) To concatenate two strings.

    Answer: C) To fill a block of memory with a specified value.

  42. Which library should you include to use the "strcpy" function in C?

    A) <stdio.h>

    B) <string.h>

    C) <stdlib.h>

    D) <ctype.h>

    Answer: B) <string.h>

  43. Which C function is used to compare two character arrays?

    A) strcmp

    B) strcat

    C) strrev

    D) strupr

    Answer: A) strcmp

  44. What is the maximum number of characters that can be stored in a character array of size 10, including the null terminator?

    A) 9

    B) 10

    C) 11

    D) 12

    Answer: A) 9

  45. Which function in C is used to concatenate two character arrays?

    A) strcat

    B) strlen

    C) strcpy

    D) strrev

    Answer: A) strcat

  46. What does the "fgets" function do in C when working with character arrays?

    A) Reads a single character from the keyboard

    B) Reads a line of text from a file or standard input

    C) Compares two strings

    D) Converts a string to lowercase

    Answer: B) Reads a line of text from a file or standard input

  47. To declare a character array named "myString" with a size of 20 characters in C, what is the correct syntax?

    A) char myString[20];

    B) char myString = "20";

    C) char[20] myString;

    D) char myString = 20;

    Answer: A) char myString[20];

  48. What is the purpose of the "strtok" function in C?

    A) Tokenize a string into smaller substrings

    B) Convert a string to uppercase

    C) Reverse the order of characters in a string

    D) Count the number of characters in a string

    Answer: A) Tokenize a string into smaller substrings

  49. Which function can be used to convert a character to uppercase in C?

    A) toupper

    B) tolower

    C) strupr

    D) isupper

    Answer: A) toupper

  50. What does the "strchr" function in C do?

    A) Search for the last occurrence of a character in a string

    B) Copy a string to another string

    C) Search for the first occurrence of a character in a string

    D) Concatenate two strings

    Answer: C) Search for the first occurrence of a character in a string

  51. How do you declare an array of strings in C?

    A) `char[] strings;`

    B) `string[] array;`

    C) `char* strings[];`

    D) `string* array;`

    Answer: C) `char* strings[];`

  52. What is the maximum number of strings that can be stored in an array declared as `char* strings[10];`?

    A) 5

    B) 10

    C) 15

    D) It depends on the length of the strings.

    Answer: B) 10

  53. Which C library function is commonly used to read a string from standard input and store it in an array?

    A) `scanf`

    B) `gets`

    C) `fgets`

    D) `readline`

    Answer: C) `fgets`

  54. How do you access the third character of the second string in an array of strings named `strArray`?

    A) `strArray[1][2]`

    B) `strArray[2][1]`

    C) `strArray[2, 1]`

    D) `strArray[1, 2]`

    Answer: A) `strArray[1][2]`

  55. What is the purpose of the `strcmp` function in C when working with arrays of strings?

    A) Convert strings to uppercase

    B) Compare two strings lexicographically

    C) Concatenate two strings

    D) Find the length of a string

    Answer: B) Compare two strings lexicographically

  56. Which function can be used to determine the number of strings in an array of strings until a null pointer is encountered?

    A) `countStrings`

    B) `strlength`

    C) `strlen`

    D) `strarraylen`

    Answer: A) `countStrings`

  57. To declare an array of 5 strings with a maximum length of 20 characters each, what is the correct syntax in C?

    A) `char strings[20][5];`

    B) `char* strings[5][20];`

    C) `char strings[5][20];`

    D) `char* strings[20][5];`

    Answer: C) `char strings[5][20];`

  58. How do you initialize an array of strings in C?

    A) `strArray = {"one", "two", "three"};`

    B) `strArray[] = {"one", "two", "three"};`

    C) `char strArray[][10] = {"one", "two", "three"};`

    D) `char* strArray[] = {"one", "two", "three"};`

    Answer: C) `char strArray[][10] = {"one", "two", "three"};`

  59. What function is used to concatenate two strings in C when working with arrays of strings?

    A) `strcat`

    B) `concat`

    C) `strncat`

    D) `concatenate`

    Answer: A) `strcat`

  60. How can you terminate a loop that iterates through an array of strings when you encounter a specific string, such as "stop"?

    A) `break`

    B) `exit`

    C) `terminate`

    D) `endloop`

    Answer: A) `break`