Input/Output in C Programming MCQ Questions and Answers
1. Which of the following functions in C is used to read a single character from the standard input device?
A) gets()
B) getchar()
C) scanf()
D) readchar()
Answer: B
2. What is the correct format specifier to print a floating-point number in exponential form?
A) %f
B) %lf
C) %e
D) %g
Answer: C
3. What does the printf() function return after execution?
A) Nothing (void)
B) The number of characters printed
C) The formatted string
D) The ASCII value of last character printed
Answer: B
4. Which of the following functions cannot be used for reading a string containing spaces?
A) scanf(“%s”, str);
B) gets(str);
C) fgets(str, n, stdin);
D) none of these
Answer: A
5. In C, what will printf(“%d”, ‘A’); print?
A) A
B) 65
C) ‘A’
D) Error
Answer: B
6. Which of the following is true about scanf() function in C?
A) It returns the number of input items successfully matched.
B) It always returns 0.
C) It returns a character pointer.
D) It never returns a value.
Answer: A
7. Which library is required to use printf() and scanf()?
A) stdlib.h
B) conio.h
C) stdio.h
D) string.h
Answer: C
8. What is the output of printf(“%5d”, 10);?
A) 10
B) 10 followed by 5 spaces
C) 10 preceded by 3 spaces
D) 10 preceded by 5 spaces
Answer: C
9. What will be the output of the following code?
printf(“%c”, ‘A’ + 2);
A) A
B) B
C) C
D) D
Answer: C
10. What is the use of the puts() function?
A) To write a character on the screen
B) To print an integer
C) To print a string followed by a newline
D) To read a string
Answer: C
11. Which of the following is not a valid file opening mode in C?
A) “r”
B) “rw”
C) “w”
D) “a”
Answer: B
12. Which function is used to close a file in C?
A) closefile()
B) fclose()
C) endfile()
D) stopfile()
Answer: B
13. The fopen() function returns:
A) File name
B) File descriptor
C) File pointer
D) Integer value
Answer: C
14. What happens if fopen() fails to open a file?
A) It returns 0
B) It returns -1
C) It returns NULL
D) It causes program termination
Answer: C
15. Which of the following statements correctly writes a string into a file?
A) write(file, str);
B) fprintf(fp, “%s”, str);
C) fputs(str, fp);
D) Both B and C
Answer: D
16. The fgetc() function reads:
A) A line from a file
B) A string from a file
C) A character from a file
D) The end of file character
Answer: C
17. The constant EOF in C represents:
A) End of Function
B) End of File
C) End of Format
D) End of Flow
Answer: B
18. Which function is used to move the file pointer to a specific location in a file?
A) fseek()
B) ftell()
C) rewind()
D) fmove()
Answer: A
19. Which function is used to determine the current position of the file pointer?
A) fseek()
B) ftell()
C) rewind()
D) fpos()
Answer: B
20. The format specifier %u is used to print:
A) Signed integer
B) Unsigned integer
C) Unicode character
D) Octal number
Answer: B
21. What is the default base for integer output in printf()?
A) Binary
B) Octal
C) Decimal
D) Hexadecimal
Answer: C
22. What will scanf(“%d%d”, &a, &b); do?
A) Reads two integers separated by space
B) Reads only one integer
C) Reads a floating-point number
D) Causes compilation error
Answer: A
23. Which function is used to read a line of text safely from input in modern C?
A) gets()
B) fgets()
C) scanf()
D) readln()
Answer: B
24. In C, the function putchar(‘A’); will:
A) Output character A
B) Output string “A”
C) Output ASCII code 65
D) Return A
Answer: A
25. The correct statement to open a binary file for writing is:
A) fopen(“file.dat”, “wb”);
B) fopen(“file.dat”, “bw”);
C) fopen(“file.dat”, “wb+”);
D) fopen(“file.dat”, “b”);
Answer: A
26. Which function in C is used to write a character to a file?
A) fprint()
B) fputc()
C) fwrite()
D) fputs()
Answer: B
27. What does the function rewind(fp); do?
A) Moves file pointer to end of file
B) Closes and reopens file
C) Moves file pointer to beginning
D) Deletes file contents
Answer: C
28. What is the correct way to print a hexadecimal number in C?
A) printf(“%h”, num);
B) printf(“%x”, num);
C) printf(“%hx”, num);
D) printf(“%XH”, num);
Answer: B
29. Which of the following is not a standard I/O stream in C?
A) stdin
B) stdout
C) stdfile
D) stderr
Answer: C
30. If the file is opened in mode “a”, where will the file pointer be positioned?
A) Beginning of the file
B) Middle of the file
C) End of the file
D) Undefined position
Answer: C
31. Which of the following can be used to detect the end of file while reading?
A) if(fgetc(fp) == -1)
B) if(feof(fp))
C) if(fp == NULL)
D) if(fread() == 0)
Answer: B
32. What will happen if you call scanf(“%d”, 10);?
A) It reads correctly
B) It causes segmentation fault
C) It assigns value to variable 10
D) It prints 10
Answer: B
33. Which of the following format specifiers is used to print a pointer address?
A) %p
B) %a
C) %x
D) %u
Answer: A
34. Which function in C writes binary data to a file?
A) fwrite()
B) fputs()
C) fprintb()
D) fputc()
Answer: A
35. What is the correct syntax to read binary data from a file?
A) fread(ptr, size, n, fp);
B) fread(fp, size, n, ptr);
C) fread(size, n, fp, ptr);
D) read(ptr, size, n, fp);
Answer: A
36. What is the output of the following code snippet?
printf(“%d”, printf(“NET”));
A) NET
B) 3
C) NET3
D) Compilation error
Answer: C
37. The buffer used in standard I/O operations is managed by:
A) The programmer
B) The compiler
C) The operating system
D) The C runtime library
Answer: D
38. Which function in C checks for errors in file operations?
A) ferror()
B) feof()
C) perror()
D) clearerr()
Answer: A
39. The perror() function in C:
A) Prints a user-defined error
B) Prints system-defined error message
C) Resets error indicator
D) Terminates the program
Answer: B
40. What is the difference between puts() and printf()?
A) puts() cannot print variables
B) puts() always appends a newline
C) printf() is slower
D) Both are identical
Answer: B
41. Which of the following statements about fprintf() is correct?
A) It writes formatted data to stdout.
B) It writes formatted data to a file.
C) It reads formatted data from stdin.
D) It reads formatted data from a file.
Answer: B
42. What is the output of the following code?
printf(“%5.2f”, 3.14159);
A) 3.14
B) 3.142
C) 3.14
D) 3.1
Answer: C
43. What is the purpose of the “r+” mode in fopen()?
A) Read and write from start
B) Append and read
C) Write only
D) Overwrite file
Answer: A
44. Which of the following statements is true regarding scanf()?
A) It skips leading white spaces for numeric inputs.
B) It cannot read characters.
C) It cannot handle multiple inputs.
D) It returns void.
Answer: A
45. Which function in C flushes the output buffer of a stream?
A) fflush()
B) clear()
C) flush()
D) cleanbuf()
Answer: A
46. In C, how can you print a literal % symbol using printf()?
A) printf(“%”);
B) printf(“%%”);
C) printf(“%p”);
D) printf(“%s%”);
Answer: B
47. What is the difference between fread() and fscanf()?
A) fread() is for binary files, fscanf() for text files.
B) Both are same.
C) fscanf() reads blocks of bytes.
D) fread() reads formatted data.
Answer: A
48. Which function moves the file pointer to the beginning after reading partially?
A) fseek(fp, 0, SEEK_SET);
B) ftell(fp);
C) rewind(fp);
D) reset(fp);
Answer: C
49. What is printed by printf(“%0.3f”, 3.2);?
A) 3.200
B) 3.2
C) 3.000
D) 3.20
Answer: A
50. What is the output of the following code?
int a = 5;
printf(“%d %d”, a, a++);
A) 5 5
B) 5 6
C) 6 5
D) Undefined behavior
Answer: D
51. Which function in C is used to set the error indicator of a stream to zero?
A) clearerr()
B) ferror()
C) feof()
D) perror()
Answer: A
52. What is the result of executing this code?
printf(“%c”, 65);
A) 65
B) A
C) Error
D) Nothing
Answer: B
53. What will be the output of the following?
printf(“%5s”, “Hi”);
A) Hi
B) Hi followed by 5 spaces
C) Hi preceded by 3 spaces
D) Syntax error
Answer: C
54. The stderr stream in C is typically used for:
A) Normal output
B) Error messages
C) File operations
D) Buffered input
Answer: B
55. What happens when you open a file in mode “w”?
A) Opens file for writing and deletes existing contents
B) Opens file for appending
C) Opens file for reading
D) Opens file without modifying it
Answer: A
56. Which statement correctly writes data to stdout using file pointer?
A) fprintf(stdout, “Hello”);
B) fputs(“Hello”, stdout);
C) fwrite(“Hello”, 1, 5, stdout);
D) All of the above
Answer: D
57. Which function in C can directly display formatted output to any open file stream?
A) printf()
B) fprintf()
C) sprintf()
D) display()
Answer: B
58. In C, what does scanf(“%c”, &ch); do?
A) Reads a character including spaces
B) Skips white spaces
C) Reads until newline
D) Reads only alphabetic characters
Answer: A
59. What is the return type of fgetc()?
A) int
B) char
C) FILE *
D) void
Answer: A
60. What will be printed by the following code?
int x = 10;
printf(“%4d”, x);
A) 10
B) 10
C) 10 followed by 4 spaces
D) 10 with no spacing
Answer: B
61. When using fscanf(fp, “%d”, &x);, where is input taken from?
A) Standard input
B) File associated with fp
C) Keyboard buffer
D) None of these
Answer: B
62. Which of the following functions can write multiple data items in one call?
A) fwrite()
B) fputc()
C) fputs()
D) putc()
Answer: A
63. Which function will move file pointer relative to its current position?
A) fseek(fp, offset, SEEK_CUR);
B) fseek(fp, offset, SEEK_END);
C) rewind(fp);
D) ftell(fp);
Answer: A
64. The output of the following code is:
printf(“%d”, ‘Z’ – ‘A’);
A) 25
B) 24
C) 26
D) 1
Answer: A
65. What is the purpose of “a+” mode in fopen()?
A) Read and write, appending new data to end
B) Read only
C) Write only
D) Read and overwrite
Answer: A
66. What happens if you use fprintf(fp, “%d”, 123); on a closed file pointer?
A) Writes 123 to file
B) Causes runtime error
C) Returns 0 silently
D) Reopens file automatically
Answer: B
67. Which function can be used to redirect stdout to a file?
A) freopen()
B) redirect()
C) fredirect()
D) setfile()
Answer: A
68. What will be printed by the following code?
printf(“%.*s”, 3, “Welcome”);
A) Wel
B) Welcome
C) We
D) Compilation error
Answer: A
69. Which function reads formatted data from a string?
A) scanf()
B) sscanf()
C) fscanf()
D) strread()
Answer: B
70. The “wb+” mode in fopen() allows:
A) Reading and writing in binary mode
B) Reading and writing in text mode
C) Writing only
D) Appending only
Answer: A
71. The function putc(c, fp); is equivalent to:
A) fputc(c, fp);
B) fwrite(c, 1, 1, fp);
C) fprintf(fp, “%c”, c);
D) Any of the above
Answer: A
72. The correct syntax to read a string of 10 characters from a file is:
A) fread(str, sizeof(char), 10, fp);
B) fread(fp, sizeof(char), 10, str);
C) read(str, fp, 10);
D) fgets(str, 10, fp);
Answer: A
73. Which of the following functions sets the position of the file pointer to the beginning?
A) rewind()
B) fseek()
C) ftell()
D) restart()
Answer: A
74. When fflush(stdin); is used, what is its effect?
A) Undefined behavior
B) Clears input buffer safely
C) Resets stdin
D) Flushes data to screen
Answer: A
75. The correct statement to print an integer with a width of 6 and left-justified is:
A) printf(“%6d”, n);
B) printf(“%-6d”, n);
C) printf(“%d-6”, n);
D) printf(“%d6”, n);
Answer: B
76. Which function in C returns a nonzero value if the end of file is reached?
A) feof()
B) ferror()
C) fclose()
D) clearerr()
Answer: A
77. Which format specifier is used for printing a long integer?
A) %l
B) %ld
C) %li
D) Both B and C
Answer: D
78. What does fprintf(stderr, “Error”); do?
A) Prints “Error” to the console immediately
B) Prints “Error” to file
C) Stores message in a buffer
D) Does nothing
Answer: A
79. What will printf(“%d”, sizeof(printf(“Hi”))); print?
A) 2
B) 3
C) 4
D) Implementation-dependent
Answer: D
80. Which function reads one block of data and returns number of blocks successfully read?
A) fread()
B) fscanf()
C) fgetc()
D) fscanb()
Answer: A
81. In C, scanf(“%f”, &x); expects variable x to be of type:
A) float
B) double
C) int
D) long
Answer: A
82. Which of the following can print both integers and strings using one call?
A) fprintf()
B) printf()
C) sprintf()
D) All of these
Answer: D
83. When opening a file with “r+” mode, if the file doesn’t exist:
A) It is created
B) fopen() returns NULL
C) It truncates the file
D) It appends data
Answer: B
84. What does the expression printf(“%3d”, 12345); produce?
A) 12345
B) 345
C) 123
D) 12345 with right alignment (no truncation)
Answer: D
85. Which of these statements correctly copies contents of one file to another?
A) fread() and fwrite()
B) fscanf() and fprintf()
C) Both A and B
D) Only fwrite()
Answer: C
86. The function ungetc(ch, fp); does what?
A) Pushes character back into input stream
B) Deletes last character from file
C) Writes character to file
D) None of these
Answer: A
87. Which header file contains prototypes of fread() and fwrite()?
A) stdio.h
B) stdlib.h
C) file.h
D) io.h
Answer: A
88. Which of the following is not a valid argument for the third parameter of fseek()?
A) SEEK_CUR
B) SEEK_SET
C) SEEK_END
D) SEEK_MID
Answer: D
89. If printf(“%4.2f”, 1.2345); is executed, what is the output?
A) 1.23
B) 1.24
C) 1.23
D) 1.24
Answer: D
90. The return type of printf() and scanf() functions is:
A) void
B) int
C) char
D) float
Answer: B
91. Which of the following writes formatted output into a string?
A) sprintf()
B) snprintf()
C) Both A and B
D) sformat()
Answer: C
92. If getc(fp) is called at the end of a file, what does it return?
A) -1
B) 0
C) EOF
D) Null character
Answer: C
93. What is the correct prototype of fopen()?
A) FILE *fopen(char *filename, char *mode);
B) FILE fopen(char *, char *);
C) int fopen(char *, char *);
D) void fopen(FILE *);
Answer: A
94. Which of the following statements about stderr is true?
A) It is always buffered.
B) It is never buffered.
C) It may be line-buffered.
D) It depends on compiler.
Answer: B
95. The difference between “w+” and “a+” file modes is:
A) “w+” truncates file, “a+” appends
B) “w+” appends, “a+” truncates
C) Both are identical
D) “a+” is for binary only
Answer: A
96. What happens when you try to read beyond end of file using fgetc()?
A) Returns EOF
B) Returns NULL
C) Throws runtime error
D) Rewinds automatically
Answer: A
97. In printf(“%-10s”, “C”);, what will be the output?
A) C
B) C followed by 9 spaces
C) 9 spaces followed by C
D) None of these
Answer: B
98. The correct way to check if file opening succeeded is:
A) if(fp != NULL)
B) if(fp == 1)
C) if(fopen() == 0)
D) if(fp == true)
Answer: A
99. The purpose of ftell(fp); is to:
A) Return current file pointer position
B) Set file pointer to beginning
C) Move file pointer to end
D) Close file
Answer: A
100. Which of the following functions can be used to print variable data to a string safely with size limitation?
A) snprintf()
B) sprintf()
C) strcpy()
D) strcat()
Answer: A
