C Programming Multiple Choice Questions and Answers on Formatted Input Function scanf() for Freshers
https://www.computersprofessor.com/2018/01/c-programming-multiple-choice-questions_4.html
1. What is the output of this C code?
#includeint main()
{int n;
scanf("%d", n);
printf("%d\n", n);
return 0;
}
a) Compilation error
b) Undefined behavior
c) Whatever user types
d) Depends on the standard
b) Undefined behavior
c) Whatever user types
d) Depends on the standard
Answer: b
2. What is the output of this C code?
#includeint main()
{char *n;
scanf("%s", n);
return 0;
}
a) Compilation error
b) Undefined behavior
c) Nothing
d) None of the mentioned
b) Undefined behavior
c) Nothing
d) None of the mentioned
Answer: b
3. What is the output of this C code?
#includeint main()
{char n[] = "hellonworld!";
char s[13];
sscanf(n, "%s", s);
printf("%s\n", s);
return 0;
}
a) hellonworld!
b) hello
world!
c) hello
d) hello world!
b) hello
world!
c) hello
d) hello world!
Answer: c
4. What is the output of this C code?
#includeint 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
b) Undefined behavior
c) Whatever user types
d) None of the mentioned
Answer: c
5. What is the output of this C code?
#includeint 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
b) Somegarbage value
c) Whatever user types
d) Depends on the standard
Answer: b
6. What is the output of this C code?
#includeint 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
b) Somegarbage value
c) Whatever user types
d) Depends on the standard
Answer: b
7. What is the output of this C code?
#includeint 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.
b) Undefined behavior
c) Somegarbage value
d) Depends on the standard.
Answer: a
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
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
10. Which of the following doesn’t require an & for the input in scanf?
a) char name[10];
b) int name[10];
c) float name[10];
d) all of the mentioned
Answer: a
11. Which of the following is an invalid method for input?
a) scanf(“%d%d%d”,&a, &b, &c);
b) scanf(“%d %d %d”, &a, &b, &c);
c) scanf(“Three values are %d %d %d”,&a,&b,&c);
d) none of the mentioned
Answer: d
12. Which of the following represents the function for scanf?
a) void scanf(char *format, …)
b) int scanf(char *format, …)
c) char scanf(int format, …)
d) char *scanf(char *format, …)
Answer: b
13. scanf returns as its value
a) Number of successfully matched and assigned input items
b) Nothing
c) Number of characters properly printed
d) Error
Answer: a
14. What is the output of this C code?
#includevoid main()
{int n;
scanf("%d", n);
printf("%d", n);
}
a) Prints the number that was entered
b) Segmentation fault
c) Nothing
d) Varies
b) Segmentation fault
c) Nothing
d) Varies
Answer: c
15. int sscanf(char *string, char *format, arg1, arg2, …)
a) Scans the string according to the format in format and stores the resulting values through arg1, arg2, etc
b) The arguments arg1,arg2 etc must be pointers
c) Scans the string according to the format in format and stores the resulting values through arg1, arg2, etc, those arguments arg1,arg2 etc must be pointers
d) None of the mentioned
Answer: c
16. The conversion characters d, i, o, u, and x may be preceded by h in scanf to indicate
a) A pointer to short
b) A pointer to long
c) Nothing
d) Error
Answer: a
17. What is the output of this C code (when 4 and 5 are entered)?
#includevoid main()
{int m, n;
printf("enter a number");
scanf("%d", &n);
scanf("%d", &m);
printf("%d\t%d\n", n, m);
}
a) Error
b) 4 junkvalue
c) Junkvalue 5
d) 4 5
b) 4 junkvalue
c) Junkvalue 5
d) 4 5
Answer: d
