250+ TOP MCQs on Formatted Input and Answers

C test on “Formatted Input”. One shall practice these test questions to improve their C programming skills needed for various interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C Programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C test questions come with detailed explanation of the answers which helps in better understanding of C concepts.

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?

  1.     #include 
  2.     int main()
  3.     {
  4.         int n;
  5.         scanf("%d", n);
  6.         printf("%dn", n);
  7.         return 0;
  8.     }

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?

  1.     #include 
  2.     int main()
  3.     {
  4.         char *n;
  5.         scanf("%s", n);
  6.         return 0;
  7.     }

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?

  1.     #include 
  2.     int main()
  3.     {
  4.         char n[] = "hellonworld!";
  5.         char s[13];
  6.         sscanf(n, "%s", s);
  7.         printf("%sn", s);
  8.         return 0;
  9.     }

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?

  1.     #include 
  2.     int main()
  3.     {
  4.         short int i;
  5.         scanf("%hd", &i);
  6.         printf("%hd", i);
  7.         return 0;
  8.     }

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?

  1.     #include 
  2.     int main()
  3.     {
  4.         short int i;
  5.         scanf("%*d", &i);
  6.         printf("%hd", i);
  7.         return 0;
  8.     }

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?

  1.     #include 
  2.     int main()
  3.     {
  4.         short int i;
  5.         scanf("%*hd", &i);
  6.         printf("%hd", i);
  7.         return 0;
  8.     }

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?

  1.     #include 
  2.     int main()
  3.     {
  4.         short int i;
  5.         scanf("%h*d", &i);
  6.         printf("%hd", i);
  7.         return 0;
  8.     }

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.

Leave a Reply

Your email address will not be published. Required fields are marked *