C Programming Questions and Answers – Formatted Output – 1

«
»
This section on advanced C interview questions focuses on “Formatted Output”. One shall practice these advanced C questions to improve their C programming skills needed for various interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C Programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our advanced C questions come with detailed explanation of the answers which helps in better understanding of C concepts.

Here is a listing of advanced C interview questions on “Formatted Output” along with answers, explanations and/or solutions::”

1. What will be the output of the following C code?

advertisement
  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         int i = 10, j = 2;
  5.         printf("%d\n", printf("%d %d ", i, j));
  6.     }

a) Compile time error
b) 10 2 4
c) 10 2 2
d) 10 2 5
View Answer

Answer: d
Explanation: None.
advertisement
advertisement

2. What will be the output of the following C code?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         int i = 10, j = 3;
  5.         printf("%d %d %d", i, j);
  6.     }

a) Compile time error
b) 10 3
c) 10 3 some garbage value
d) Undefined behaviour
View Answer

Answer: c
Explanation: None.
advertisement

3. What will be the output of the following C code?

advertisement
  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         int i = 10, j = 3, k = 3;
  5.         printf("%d %d ", i, j, k);
  6.     }

a) Compile time error
b) 10 3 3
c) 10 3
d) 10 3 somegarbage value
View Answer

Answer: c
Explanation: None.
advertisement

4. What will be the output of the following C code?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         char *s = "myworld";
  5.         int i = 9;
  6.         printf("%*s", i, s);
  7.     }

a) myworld
b) myworld(note: spaces to the left of myworld)
c) myworld (note:followed by two spaces after myworld)
d) Undefined
View Answer

Answer: b
Explanation: None.

5. What will be the output of the following C code?

  1.     #include <stdio.h>
  2.     int main(int argc, char** argv)
  3.     {
  4.         char *s = "myworld";
  5.         int i = 3;
  6.         printf("%10.*s", i, s);
  7.     }

a) myw(note:7 spaces before myw)
b) myworld(note:2 spaces before myworld)
c) myworld (note:2 spaces after myworld)
d) myw(note:6 spaces after myw)
View Answer

Answer: a
Explanation: In the format represented by “%10.*s”, the width of the string will be 10 spaces which is aligned to the right, by default. Since we have asterisk (*) after the precision dot (.), the value of precision will be the value stored in the variable i. The value of i is 3, so this signifies max length of the string as 3 characters. So, the final formatted output will be a 10 character output with 3 characters “myw” printed with right alignment and the 1st 7 characters will be simply space characters.

6. What is the difference between %e and %g?
a) %e output formatting depends on the argument and %g always formats in the format [-]m.dddddd or [-]m.dddddE[+|-]xx where no.of ds are optional
b) %e always formats in the format [-]m.dddddd or [-]m.dddddE[+|-]xx where no.of ds are optional and output formatting depends on the argument
c) No differences
d) Depends on the standard
View Answer

Answer: b
Explanation: None.

7. Escape sequences are prefixed with ________
a) %
b) /
c) ”
d) None of the mentioned
View Answer

Answer: d
Explanation: None.

8. What is the purpose of sprintf?
a) It prints the data into stdout
b) It writes the formatted data into a string
c) It writes the formatted data into a file
d) None of the mentioned
View Answer

Answer: b
Explanation: None.

9. The syntax to print a % using printf statement can be done by ________
a) %
b) \%
c) ‘%’
d) %%
View Answer

Answer: d
Explanation: None.

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