C Programming Questions and Answers – Ungetc – 1

«
»

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

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

advertisement

1. ungetc() can be used only with getc().
a) true
b) false
c) depends on the standard
d) depends on the platform
View Answer

Answer: b
Explanation: None.

2. Which character of pushback is guaranteed per file?
a) True
b) False
c) Depends on the compiler
d) Depends on the platform
View Answer

Answer: a
Explanation: None.
advertisement
advertisement

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

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         int n;
  5.         scanf("%d", &n);
  6.         ungetc(n, stdin);
  7.         scanf("%d", &n);
  8.         printf("%d\n", n);
  9.         return 0;
  10.     }

a) Compile time error
b) Whatever is typed by the user first time
c) Whatever is typed by the user second time
d) Undefined behaviour
View Answer

Answer: b
Explanation: None.
advertisement

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

advertisement
  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         char n[20];
  5.         fgets(n, 19, stdin);
  6.         ungetc(n[0], stdin);
  7.         scanf("%s", n);
  8.         printf("%s\n", n);
  9.         return 0;
  10.     }

a) Compile time error
b) Whatever string user types second time
c) Whatever string user types first time
d) First character of whatever user types first time and whatever user types second time
View Answer

Answer: d
Explanation: None.
advertisement

5. What will be the output of the following C code considering user typed jkl?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         char n[20];
  5.         fgets(n, 19, stdin);
  6.         ungetc(n[0], stdin);
  7.         printf("%s\n", n);
  8.         return 0;
  9.     }

a) jkl
b) kl
c) Undefined behaviour
d) jk
View Answer

Answer: a
Explanation: None.

6. How many characters for pushback is guaranteed per file while using ungetc(c, fp);?
a) Only 1 character
b) Characters within 1 word
c) Characters within 1st new-line
d) All characters upto NULL character
View Answer

Answer: a
Explanation: None.

7. Which of the following is the correct syntax for calling function ungetc?

Assume int c and FILE *fp

a) ungetc(c,*fp);
b) ungetc(c, fp);
c) ungetc(fp, c);
d) ungetc(*fp,c);
View Answer

Answer: b
Explanation: None.

8. ungetc() is used __________
a) to get a char
b) to get an int
c) to push a character back to file
d) nothing
View Answer

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

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.