Here is a listing of C test questions on “Formatted Input” along with answers, explanations and/or solutions:
1. What will be the output of the following C code?
-
#include
-
int main()
-
{
-
int n;
-
scanf("%d", n);
-
printf("%dn", n);
-
return 0;
-
}
a) Compilation error
b) Undefined behavior
c) Whatever user types
d) Depends on the standard
Answer: b
Clarification: None.
2. What will be the output of the following C code?
-
#include
-
int main()
-
{
-
char *n;
-
scanf("%s", n);
-
return 0;
-
}
a) Compilation error
b) Undefined behavior
c) Nothing
d) None of the mentioned
Answer: b
Clarification: None.
3. What will be the output of the following C code?
-
#include
-
int main()
-
{
-
char n[] = "hellonworld!";
-
char s[13];
-
sscanf(n, "%s", s);
-
printf("%sn", s);
-
return 0;
-
}
a) hellonworld!
b)
hello world!
c) hello
d) hello world!
Answer: c
Clarification: The array n contains a string which has a newline character in between the strings “hello” and “world”. A newline character is considered as a whitespace character for inputs for the scanf(), sscanf() and fscanf() functions. So, the sscanf() function will only copy upto the string “hello” into the array s. Hence, the output of the printf() function be only the string “hello”.
4. What will be the output of the following C code?
-
#include
-
int main()
-
{
-
short int i;
-
scanf("%hd", &i);
-
printf("%hd", i);
-
return 0;
-
}
a) Compilation error
b) Undefined behavior
c) Whatever user types
d) None of the mentioned
Answer: c
Clarification: None.
5. What will be the output of the following C code?
-
#include
-
int main()
-
{
-
short int i;
-
scanf("%*d", &i);
-
printf("%hd", i);
-
return 0;
-
}
a) Compilation error
b) Somegarbage value
c) Whatever user types
d) Depends on the standard
Answer: b
Clarification: None.
6. What will be the output of the following C code?
-
#include
-
int main()
-
{
-
short int i;
-
scanf("%*hd", &i);
-
printf("%hd", i);
-
return 0;
-
}
a) Compilation error
b) Somegarbage value
c) Whatever user types
d) Depends on the standard
Answer: b
Clarification: None.
7. What will be the output of the following C code?
-
#include
-
int main()
-
{
-
short int i;
-
scanf("%h*d", &i);
-
printf("%hd", i);
-
return 0;
-
}
a) Compilation error
b) Undefined behavior
c) Somegarbage value
d) Depends on the standard.
Answer: a
Clarification: None.
8. Which of the following is NOT a delimiter for an input in scanf?
a) Enter
b) Space
c) Tab
d) None of the mentioned
Answer: d
Clarification: None.
9. If the conversion characters of int d, i, o, u and x are preceded by h, it indicates?
a) A pointer to int
b) A pointer to short
c) A pointer to long
d) A pointer to char
Answer: b
Clarification: None.