2.4 - Input/output.


Back Button Home

2.4.1 - Formatted Functions.


1. Which of the following functions is used to display output on the screen in C?
  • (A) scanf()
  • (B) gets()
  • (C) printf()
  • (D) puts()
Correct Answer: (C) printf().
2. What is the purpose of the scanf() function in C?
  • (A) To display output on the screen
  • (B) To perform calculations
  • (C) To receive input from the user
  • (D) To write data to a file
Correct Answer: (C) To receive input from the user.
3. What is the correct format specifier to scan an integer using scanf()?
  • (A) %f
  • (B) %d
  • (C) %c
  • (D) %s
Correct Answer: (B) %d.
4. Which of the following format specifiers is used to scan or print a floating-point number?
  • (A) %d
  • (B) %f
  • (C) %c
  • (D) %s
Correct Answer: (B) %f.
5. What does the format specifier %c represent in C's scanf() and printf() functions?
  • (A) Scan or print a string
  • (B) Scan or print an integer
  • (C) Scan or print a floating-point number
  • (D) Scan or print a character
Correct Answer: (D) Scan or print a character.
6. Which of the following format specifiers is used to scan or print a string in C?
  • (A) %f
  • (B) %c
  • (C) %d
  • (D) %s
Correct Answer: (D) %s.
7. In C, the format specifier %lf is used to scan or print which type of data?
  • (A) Integer
  • (B) Character
  • (C) Double
  • (D) String
Correct Answer: (C) Double.
8. Which of the following is a formatted input/output function in C?
  • (A) gets()
  • (B) puts()
  • (C) scanf()
  • (D) fgetc()
Correct Answer: (C) scanf().
9. What header file is required for using scanf() and printf() functions in C?
  • (A) stdlib.h
  • (B) stdio.h
  • (C) math.h
  • (D) string.h
Correct Answer: (B) stdio.h.
10. What is the correct output for the following C code?
                #include <stdio.h>
                int main() {
                    int num;
                    printf("Enter an integer: ");
                    scanf("%d", &num);
                    printf("The integer is %d", num);
                    return 0;
                }
                
  • (A) Enter an integer: (user input)
  • (B) Enter an integer: (user input) The integer is (user input)
  • (C) The integer is (user input)
  • (D) Compilation error
Correct Answer: (B) Enter an integer: (user input) The integer is (user input).

2.4.2 - Unformatted Functions.


11. What does the getchar() function do in C?
  • (A) Reads a single character from the standard input and returns it as an integer.
  • (B) Writes a single character to the standard output.
  • (C) Reads a string from the standard input until a newline is encountered.
  • (D) Writes a string to the standard output with a trailing newline.
Correct Answer: (A) Reads a single character from the standard input and returns it as an integer.
12. How does the putchar() function work?
  • (A) It reads a single character from the standard input.
  • (B) It writes a single character to the standard output.
  • (C) It reads a string from the standard input until a newline is encountered.
  • (D) It writes a string to the standard output with a trailing newline.
Correct Answer: (B) It writes a single character to the standard output.
13. What is the purpose of the gets() function?
  • (A) To read a single character from the standard input.
  • (B) To write a single character to the standard output.
  • (C) To read a line of text from the standard input until a newline or end-of-file is encountered.
  • (D) To write a line of text to the standard output without a trailing newline.
Correct Answer: (C) To read a line of text from the standard input until a newline or end-of-file is encountered.
14. What does the puts() function do?
  • (A) Reads a line of text from the standard input.
  • (B) Writes a line of text to the standard output with a trailing newline.
  • (C) Writes a single character to the standard output.
  • (D) Reads a single character from the standard input and returns it.
Correct Answer: (B) Writes a line of text to the standard output with a trailing newline.
15. Which function would you use to read multiple characters in a loop?
  • (A) putchar()
  • (B) gets()
  • (C) puts()
  • (D) getchar()
Correct Answer: (D) getchar().
16. Which function is typically used to display a single character on the screen?
  • (A) gets()
  • (B) puts()
  • (C) getchar()
  • (D) putchar()
Correct Answer: (D) putchar().
17. What happens if you use gets() without a buffer large enough to hold the input?
  • (A) The input will be truncated.
  • (B) The program will crash.
  • (C) The input will be stored correctly, and the buffer will expand.
  • (D) The input will be ignored.
Correct Answer: (B) The program will crash.
18. Which function should be used if you want to display a string with a newline at the end?
  • (A) getchar()
  • (B) putchar()
  • (C) gets()
  • (D) puts()
Correct Answer: (D) puts().
19. How can you read more than one character using getchar()?
  • (A) Use it in a loop to read characters one by one.
  • (B) Use it with a buffer to read multiple characters at once.
  • (C) Use it to read a string directly.
  • (D) It cannot be used to read more than one character.
Correct Answer: (A) Use it in a loop to read characters one by one.
20. Which of the following is true about gets() and fgets()?
  • (A) gets() is safer than fgets().
  • (B) fgets() allows specifying the maximum number of characters to read, whereas gets() does not.
  • (C) gets() and fgets() are functionally identical.
  • (D) fgets() does not read input from the standard input.
Correct Answer: (B) fgets() allows specifying the maximum number of characters to read, whereas gets() does not.

2.4.3 - fprintf(), fscanf(),sprintf(), and sscanf().


21. What is the primary purpose of the fprintf() function in C?
  • (A) To print formatted data to the console.
  • (B) To read formatted data from a file.
  • (C) To write formatted data to a file.
  • (D) To write unformatted data to the console.
Correct Answer: (C) To write formatted data to a file.
22. How does the fscanf() function work in C?
  • (A) It writes formatted data to a file.
  • (B) It reads formatted data from a file based on the provided format.
  • (C) It prints formatted data to the console.
  • (D) It reads a single character from a file.
Correct Answer: (B) It reads formatted data from a file based on the provided format.
23. Which function is used to read formatted data from a string buffer and store it in variables?
  • (A) sprintf()
  • (B) fprintf()
  • (C) sscanf()
  • (D) fscanf()
Correct Answer: (C) sscanf().
24. What does the sprintf() function do?
  • (A) Reads formatted data from a file.
  • (B) Formats and stores values in a character array.
  • (C) Prints formatted data to the console.
  • (D) Reads formatted data from the console.
Correct Answer: (B) Formats and stores values in a character array.
25. What is the key difference between fprintf() and printf()?
  • (A) fprintf() writes to a file, while printf() writes to the console.
  • (B) fprintf() reads from a file, while printf() reads from the console.
  • (C) fprintf() formats data, while printf() does not.
  • (D) fprintf() and printf() are functionally identical.
Correct Answer: (A) fprintf() writes to a file, while printf() writes to the console.
26. Which function would you use to read formatted data from a string buffer?
  • (A) fprintf()
  • (B) fscanf()
  • (C) sscanf()
  • (D) sprintf()
Correct Answer: (C) sscanf().
27. When using sprintf(), what should be considered regarding the buffer size?
  • (A) The buffer size should be exactly the size needed for the output.
  • (B) The buffer size should be slightly larger than the anticipated output size.
  • (C) The buffer size is not important as sprintf() dynamically allocates memory.
  • (D) The buffer size should be the same as the size of the input string.
Correct Answer: (B) The buffer size should be slightly larger than the anticipated output size.
28. What is the role of the format parameter in fscanf() and fprintf() functions?
  • (A) To specify the output format of the data.
  • (B) To specify the type of data to read or write.
  • (C) To indicate the file to be used.
  • (D) To determine the size of the buffer.
Correct Answer: (B) To specify the type of data to read or write.
29. Which function would you use to print formatted data to the console?
  • (A) fprintf()
  • (B) fscanf()
  • (C) sscanf()
  • (D) printf()
Correct Answer: (D) printf().
30. If you need to read formatted data from a file using a file pointer, which function would you use?
  • (A) sprintf()
  • (B) fprintf()
  • (C) fscanf()
  • (D) sscanf()
Correct Answer: (C) fscanf().
31. What is the main purpose of the sscanf() function?
  • (A) To write formatted data to a file.
  • (B) To print formatted data to the console.
  • (C) To read formatted data from a string.
  • (D) To read formatted data from the console.
Correct Answer: (C) To read formatted data from a string.
32. How do you handle errors when using the fprintf() function?
  • (A) Check the return value; if it is negative, an error occurred.
  • (B) Use a specific error handling function for fprintf().
  • (C) fprintf() does not support error handling.
  • (D) Errors are automatically handled by the compiler.
Correct Answer: (A) Check the return value; if it is negative, an error occurred.
33. What should be done if fscanf() does not read data as expected?
  • (A) Verify the format specifier matches the data in the file.
  • (B) Ensure the file pointer is correctly initialized.
  • (C) Check the buffer size used in fscanf().
  • (D) Increase the file permissions for reading.
Correct Answer: (A) Verify the format specifier matches the data in the file.
34. Which function should you use if you need to format values into a character array without printing them?
  • (A) fscanf()
  • (B) sprintf()
  • (C) sscanf()
  • (D) fprintf()
Correct Answer: (B) sprintf().
35. How do sprintf() and sscanf() differ from printf() and scanf()?
  • (A) sprintf() and sscanf() handle strings, while printf() and scanf() handle console I/O.
  • (B) sprintf() and sscanf() are used for file I/O, while printf() and scanf() are used for string manipulation.
  • (C) sprintf() and sscanf() handle console I/O, while printf() and scanf() handle file I/O.
  • (D) sprintf() and sscanf() are used for reading and writing directly to/from files.
Correct Answer: (A) sprintf() and sscanf() handle strings, while printf() and scanf() handle console I/O.
36. In which scenario would you use sprintf() over printf()?
  • (A) When you need to store formatted data in a string rather than printing it.
  • (B) When you need to print formatted data to the console.
  • (C) When you need to read formatted data from a file.
  • (D) When you need to write formatted data to a file.
Correct Answer: (A) When you need to store formatted data in a string rather than printing it.