Input / Output Questions
Practice Input / Output MCQs with answers and explanations. Page 1 of 2.
Category
C Programming
Topic
Input / Output
Page
1 / 2
Mode
Practice
Questions
Open any question to view the answer and explanation.
C scanf formatting: Given float a; and double b;, which scanf statement uses the correct conversion specifiers to read both values from input?
Open
View answer
Safe line input in C: Between fgets() and gets(), which function is considered safe for reading a line of text (and why)?
Open
View answer
C printf formatting: With float a = 3.14; and double b = 3.14;, which printf statement uses correct specifiers to print both values?
Open
View answer
Behavior of fgets with Windows newlines: If a file contains the line "I am a boy\r
", what exact byte sequence will the char array str contain after calling fgets(str, size, fp)?
Open
View answer
C file I/O API correctness: In the given program with three opened files, what happens when calling fclose(fp, fs, ft);?
Open
View answer
Meaning of mode string in fopen: In the call fp = fopen("source.txt", "rb"); what does the mode "rb" instruct the C library to do?
Open
View answer
Understanding fileno: In the program that opens "DUMMY.C" for writing and assigns t = fileno(fp);, what does t represent?
Open
View answer
What does the FILE* returned by fopen actually point to in C? Consider FILE fp; fp = fopen("trial", "r");
Open
View answer
Effect of fseek in a read loop: Given the code that copies from source.txt to target.txt but calls fseek(fs, 4L, SEEK_CUR) after each character read, what will be written to 'target.txt' if the source line is "To err is human"?
Open
View answer
C file modes: Given FILE *fp; fp = fopen('NOTES.TXT', 'r+'); which operations on the file are permitted by the mode 'r+' (read/update without truncation)?
Open
View answer
In C file handling, two streams open the same file in write mode. After writing a character through each stream, what remains in the file?
#include <stdio.h>
int main()
{
FILE *fp1, *fp2;
fp1 = fopen("file.c", "w");
fp2 = fopen("file.c", "w");
fputc('A', fp1);
fputc('B', fp2);
fclose(fp1);
fclose(fp2);
return 0;
}
Open
View answer
In C formatting, what is the exact output produced by adjacent string literal concatenation and a ternary expression?
#include <stdio.h>
int main()
{
int k = 1;
printf("%d == 1 is" "%s
", k, k == 1 ? "TRUE" : "FALSE");
return 0;
}
Open
View answer
In C file I/O, what happens when fgetc() is compared to NULL and the character variable is of type char?
#include <stdio.h>
int main()
{
FILE *ptr;
char i;
ptr = fopen("myfile.c", "r");
while ((i = fgetc(ptr)) != NULL)
printf("%c", i);
return 0;
}
Open
View answer
In C printf formatting, what is the rounded output for a floating value with one digit after the decimal point?
#include <stdio.h>
int main()
{
float a = 3.15529f;
printf("%2.1f
", a);
return 0;
}
Open
View answer
In C printf, assume the intent is to print a decimal integer with a minimal field width of 1. What does this program output (ignoring a stray non-printing control glyph in the source)?
#include <stdio.h>
int main()
{
int a = 250;
printf("%1d", a); /* treat the strange trailing glyph as a typo and consider %1d */
return 0;
}
Open
View answer
In C file positioning, given a file containing "This is Nagpur", what substring is printed after seeking forward and reading with fgets?
#include <stdio.h>
int main()
{
FILE fp;
char ch, str[7];
fp = fopen("try.c", "r"); / file 'try.c' contains: This is Nagpur /
fseek(fp, 9L, SEEK_CUR); / move 9 bytes from the current position (start) /
fgets(str, 5, fp); / read up to 4 chars plus '\0' */
puts(str);
return 0;
}
Open
View answer
In C, what does scanf return when it successfully reads one decimal integer? Consider the following minimal program and assume the user enters 25.
#include <stdio.h>
int main()
{
int i;
printf("%d
", scanf("%d", &i));
return 0;
}
Open
View answer
In C file I/O, what does this program print from a file until EOF, and what happens in the final printf call?
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE fp;
unsigned char ch;
/ file 'abc.c' contains: "This is CuriousTab " /
fp = fopen("abc.c", "r");
if (fp == NULL)
{
printf("Unable to open file");
exit(1);
}
while ((ch = getc(fp)) != EOF)
printf("%c", ch);
fclose(fp);
printf("
", ch); / prints just a newline; extra argument is ignored */
return 0;
}
Open
View answer
In C programming, evaluate the character printed by this expression that mixes arithmetic and bitwise operators. What will be the output?
#include<stdio.h>
int main()
{
printf("%c
", ~('C' * -1));
return 0;
}
Open
View answer
In C, pointer arithmetic on a format string: what does this print?
#include<stdio.h>
int main()
{
char *p;
p = "%d
";
p++;
p++;
printf(p - 2, 23);
return 0;
}
Open
View answer
Practice smarter
Solve a few questions daily and revisit weak topics regularly to improve accuracy.