This set of C Multiple Choice Questions & Answers focuses on “printf – 2”.
1. If by mistake you specify more number of arguments, the excess arguments will ____________
a) be ignored
b) produce compile error
c) produce run-time error
d) produce logical error
Explanation: The excess arguments will simply be ignored.
2. What happens when zero flag is used with left justification?
a) data is padded with zeros
b) zero flag is ignored
c) data is padded with blank spaces
d) will give error
Explanation: Zero flag is not considered when used with left justification because adding zeros after a number changes its value.
3. For floating point numbers, the precision flag specifies the number of decimal places to be printed. When no precision modifier is specified, printf() prints _______
a) six decimal positions
b) five decimal positions
c) four decimal positions
d) three decimal positions
Explanation: Its format can be given as “. m”, where m specifies the number of decimal digits when no precision modifier is specified, printf prints six decimal positions.
4. What will the given code result in printf(“\n you are\”awesome \" ");?
a) compile error
b) run-time error
c) you are "awesome"
d) you are awesome
Explanation: The above given code uses \”<word>\” to display the word within double inverted commas on standard output screen.
5. What will be the output for the given code printf(“\n The number is %07d”,1212);
a) The number is 0001212
b) The number is 1212
c) The number is 1212
d) The number is 1212000
Explanation: 0 in the above code is Flags. The number is left-padded with zeros(0) instead of spaces.
6. What will be the output of the following code?
char t=’N’; printf(“\n %c \n %3c \n %5c”,t,t,t);
d) N N N
Explanation: In the given code each argument is printed on a new line due to control character \n. Width mentioned in the above code is 1,3,5 hence the character is printed on a new line after being padded with blank spaces.
7. Select the right explanation to the given code.
printf(“%*. *f”, 5,4,5700);
a) the minimum field width has to be 4, the precision is given to be 5, and the value to be displayed is 5700
b) the minimum field width is 5, the precision is 4, and the value to be displayed is 5700
c) compile error
d) run-time error
Explanation: The minimum field width and precision specifiers are usually constants. They can also be provided by arguments to printf(). This is done by using * modifier as shown in the given code.
8. What will be the output of the following code?
char str = "Hello Nancy“; printf(“\n %.7s”, str) ;
a) Hello Nan
c) Hello N
d) Hello Nancy
Explanation: The output for the code must be 7 characters including white spaces.
9. What will be the output of the following code?
char str =”Too Good”; printf(“\n %7s”,str);
a) Too Good
b) Too G
c) Too Go
Explanation: The complete string “Too Good” is printed. This is because if data needs more space than specified, then printf overrides the width specified by the user.
10. What will be the output of the following code?
printf(“\n Output: %5d \t %x \t %#x”, 234,234,234);
b) Output:00234 EA 0xEA
c) Output: 234 EA 0xEA
Explanation: The control character \t is used to provide gap between the words. %5d – the width of the string is set to 5, characters are printed after being padded with blank spaces.%x, %#x is additional specifiers for octal and hexadecimal values.
Sanfoundry Global Education & Learning Series – C Programming Language.