2.4.1 - Formatted Functions.
1. Which of the following functions is used to display output on the screen in C?
Correct Answer: (C) printf().
2. What is the purpose of the scanf() function in C?
Correct Answer: (C) To receive input from the user.
3. What is the correct format specifier to scan an integer using scanf()?
Correct Answer: (B) %d.
4. Which of the following format specifiers is used to scan or print a floating-point number?
Correct Answer: (B) %f.
5. What does the format specifier %c represent in C's scanf() and printf() functions?
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?
Correct Answer: (D) %s.
7. In C, the format specifier %lf is used to scan or print which type of data?
Correct Answer: (C) Double.
8. Which of the following is a formatted input/output function in C?
Correct Answer: (C) scanf().
9. What header file is required for using scanf() and printf() functions in C?
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; }
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?
Correct Answer: (A) Reads a single character from the standard input and returns it as an integer.
12. How does the putchar() function work?
Correct Answer: (B) It writes a single character to the standard output.
13. What is the purpose of the gets() function?
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?
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?
Correct Answer: (D) getchar().
16. Which function is typically used to display a single character on the screen?
Correct Answer: (D) putchar().
17. What happens if you use gets() without a buffer large enough to hold the input?
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?
Correct Answer: (D) puts().
19. How can you read more than one character using getchar()?
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()?
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?
Correct Answer: (C) To write formatted data to a file.
22. How does the fscanf() function work in C?
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?
Correct Answer: (C) sscanf().
24. What does the sprintf() function do?
Correct Answer: (B) Formats and stores values in a character array.
25. What is the key difference between fprintf() and printf()?
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?
Correct Answer: (C) sscanf().
27. When using sprintf(), what should be considered regarding the buffer size?
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?
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?
Correct Answer: (D) printf().
30. If you need to read formatted data from a file using a file pointer, which function would you use?
Correct Answer: (C) fscanf().
31. What is the main purpose of the sscanf() function?
Correct Answer: (C) To read formatted data from a string.
32. How do you handle errors when using the fprintf() function?
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?
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?
Correct Answer: (B) sprintf().
35. How do sprintf() and sscanf() differ from printf() and scanf()?
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()?
Correct Answer: (A) When you need to store formatted data in a string rather than printing it.