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
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.
Note: Join free Sanfoundry classes at Telegram or Youtube

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.

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

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

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
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.