C Multiple Choice Questions & Answers 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
Answer: a
Clarification: 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
Answer: b
Clarification: 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
Answer: a
Clarification: 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
Answer: c
Clarification: The above given code uses ”
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
Answer: a
Clarification: 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);
a) N
N
N
b) N
N
N
c) N
N
N
d) N N N
Answer: b
Clarification: 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
Answer: b
Clarification: 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 C code?
char str[] = "Hello Nancy“; printf(“n %.7s”, str) ;
a) Hello Nan
b) Hello
c) Hello N
d) Hello Nancy
Answer: c
Clarification: The output for the code must be 7 characters including white spaces.
9. What will be the output of the following C code?
char str[] =”Too Good”; printf(“n %7s”,str);
a) Too Good
b) Too G
c) Too Go
d) Too
Answer: a
Clarification: 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 C code?
printf(“n Output: %5d t %x t %#x”, 234,234,234);
a) Output:234EA0xEA
b) Output:00234 EA 0xEA
c) Output: 234 EA 0xEA
d) ERROR
Answer: c
Clarification: 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.