C Questions and Answers – printf – 2

«
»

This set of C Multiple Choice Questions & Answers (MCQs) focuses on “printf – 2”.

Pre-requisite for this C MCQ set: Advanced C Programming Video Tutorial.

advertisement

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
View Answer

Answer: a
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
View Answer

Answer: b
Explanation: Zero flag is not considered when used with left justification because adding zeros after a number changes its value.
advertisement
advertisement

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
View Answer

Answer: a
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
View Answer

Answer: c
Explanation: The above given code uses \”<word>\” to display the word within double inverted commas on standard output screen.
advertisement

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
View Answer

Answer: a
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?

advertisement
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  
View Answer

Answer: b
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.
advertisement

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
View Answer

Answer: b
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 C code?

char str[] = &quot;Hello Nancy“;
printf(“\n %.7s”, str) ;

a) Hello Nan
b) Hello
c) Hello N
d) Hello Nancy
View Answer

Answer: c
Explanation: 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
View Answer

Answer: a
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 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
View Answer

Answer: c
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.

Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

advertisement
advertisement

Leave a Comment

Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter

Check his free tutorial videos on Advanced C Programming. Join his Sanfoundry-Classes telegram channel for all videos and queries on Programming, Data-Structures, Kernel, Personality development and Mentoring.