Here is a listing of C programming Objective Questions on “Ungetc Function” along with answers, explanations and/or solutions:
1. ungetc() can be used only with getc().
a) true
b) false
c) depends on the standard
d) depends on the platform
Answer: b
Clarification: None.
2. Which character of pushback is guaranteed per file?
a) True
b) False
c) Depends on the compiler
d) Depends on the platform
Answer: a
Clarification: None.
3. What will be the output of the following C code?
-
#include -
int main()
-
{ -
int n;
-
scanf("%d", &n);
-
ungetc(n, stdin);
-
scanf("%d", &n);
-
printf("%dn", n);
-
return 0;
-
}
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
Answer: b
Clarification: None.
4. What will be the output of the following C code?
-
#include -
int main()
-
{ -
char n[20];
-
fgets(n, 19, stdin);
-
ungetc(n[0], stdin);
-
scanf("%s", n);
-
printf("%sn", n);
-
return 0;
-
}
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
Answer: d
Clarification: None.
5. What will be the output of the following C code considering user typed jkl?
-
#include -
int main()
-
{ -
char n[20];
-
fgets(n, 19, stdin);
-
ungetc(n[0], stdin);
-
printf("%sn", n);
-
return 0;
-
}
a) jkl
b) kl
c) Undefined behaviour
d) jk
Answer: a
Clarification: 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
Answer: a
Clarification: 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);
Answer: b
Clarification: 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
Answer: c
Clarification: None.
