C Multiple Choice Questions & Answers (MCQs) on “scanf – 1”.
1. The syntax of the scanf() is scanf(“control string “, arg1,arg2,arg3,….,argn); the prototype of control string is ____________
a) [=%[width][modifiers]type=]
b) [=%[modifiers][width]type=]
c) [=%[width] [modifiers]]
d) [width][modifiers]
Answer: a
Clarification: scanf() starts with the symbol % followed by the width, modifier, type of the argument.
2. What is the use of symbol * in the control string as shown [=%[*][width] [modifiers] type=]?
a) * is optional and used when the data should be read from the stream but ignored
b) * is not optional, used to read data from the stream but it is not ignored
c) * is not optional, it is used to read data stream but ignored
d) * is optional and used to read data from stream but it is not ignored
Answer: a
Clarification: * is an optional argument, it indicates that data should be read from the steam but ignored (not stored in a memory location)
3. What action is carried out by scanf if a user enters any blank spaces, tabs, and newlines?
a) consider as input
b) ignores it
c) produces error
d) nothing can be said
Answer: b
Clarification: The scanf() function ignores any blank spaces, tabs, and newlines entered by the user. This scanf() function just returns the number of input fields successfully scanned and stored.
4. What error will generate if the read and write parameters are not separated by commas?
a) run-time error
b) compile error
c) logical error
d) no error
Answer: b
Clarification: A compile error will be generated if the read and write parameters are not separated by commas.
5. What will be the output of the following C code?
char str[] =”Good”; scanf(“%s”, str);
a) compile error
b) run-time error
c) good
d) logical error
Answer: c
Clarification: String can be read from the stream without the use of address of operator (&).
6. What will be the output of the following C code?
scanf(“ %d %d %d”,&n1,&n2);
a) read data for two
b) generate error
c) read data for three
d) nothing can be said
Answer: b
Clarification: The following scanf() statement will generate an error as no variable address is given for the third conversion specification.
7. What form the data must be entered for the given C code?
scanf(“%d / %d”, &n1,&n2);
a) 6 9
b) 6/9
c) compile error
d) run-time error
Answer: b
Clarification: The slash in the control String are neither white space characters nor a part of conversion specification, so the user must enter data of the form 6/9.
8. A fatal error will be generated if the format string is ended with a white space character.
a) true
b) false
Answer: a
Clarification: An error will be generated if the format string %s is ended with white space character.
9. Explain the format string “%5d%s %c”
a) five characters as a decimal integer, then reads the remaining as a string and then scans the first non-whitespace character
b) compile error
c) run-time error
d) read first five characters as a decimal and ignore the rest
Answer: a
Clarification: The above format string reads the first five characters as a decimal integer, then reads the remaining as a string until a space, newline or tab is found, then reads the first non-whitespace character.
10. _____ is an optional argument that gives the maximum number of characters to be read.
a) modifiers
b) width
c) precision
d) length
Answer: b
Clarification: Width is the argument that gives the maximum number of characters to be read. Few characters will be read if the scanf function encounters white space and it will stop processing further.