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.

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

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.
Free 30-Day Java Certification Bootcamp is Live. Join Now!

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

  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.

To practice all areas of C language, here is complete set of 1000+ Multiple Choice Questions and Answers.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). 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!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.