Here is a listing of advanced C questions on “Formatted Output” along with answers, explanations and/or solutions:
1. What is the meaning of the following C statement?
printf(“%10s”, state);
a) 10 spaces before the string state is printed
b) Print empty spaces if the string state is less than 10 characters
c) Print the last 10 characters of the string
d) None of the mentioned
Answer: b
Clarification: None.
2. What are the Properties of the first argument of a printf() functions?
a) It is defined by a user
b) It keeps the record of the types of arguments that will follow
c) There may no be first argument
d) None of the mentioned
Answer: b
Clarification: None.
3. What will be the output of the following C code?
-
#include
-
int main()
-
{
-
int i = 10, j = 2;
-
printf("%dn", printf("%d %d ", i, j));
-
}
a) Compile time error
b) 10 2 4
c) 10 2 2
d) 10 2 5
Answer: d
Clarification: None.
4. What will be the output of the following C code?
-
#include
-
int main()
-
{
-
int i = 10, j = 3;
-
printf("%d %d %d", i, j);
-
}
a) Compile time error
b) 10 3
c) 10 3 some garbage value
d) Undefined behaviour
Answer: c
Clarification: None.
5. What will be the output of the following C code?
-
#include
-
int main()
-
{
-
int i = 10, j = 3, k = 3;
-
printf("%d %d ", i, j, k);
-
}
a) Compile time error
b) 10 3 3
c) 10 3
d) 10 3 somegarbage value
Answer: c
Clarification: None.
6. What will be the output of the following C code?
-
#include
-
int main()
-
{
-
char *s = "myworld";
-
int i = 9;
-
printf("%*s", i, s);
-
}
a) myworld
b) myworld(note: spaces to the left of myworld)
c) myworld (note:followed by two spaces after myworld)
d) Undefined
Answer: b
Clarification: None.
7. What will be the output of the following C code?
-
#include
-
int main(int argc, char **argv)
-
{
-
char *s = "myworld";
-
int i = 3;
-
printf("%10.*s", i, s);
-
}
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 myworld)
Answer: a
Clarification: 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.
8. 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
Answer: b
Clarification: None.