C Multiple Choice Questions & Answers (MCQs) on “Date and Time Function – 3”.
1. What will be the output of the following C code, if the system date is 6/23/2017?
#include#include int main() { struct tm *local; time_t t; t=time(NULL); local=localtime(&t); printf("%d",local->tm_mday); return 0; }
a) 6
b) 22
c) 23
d) error
Answer: c
Clarification: tm_mday returns the day of the month in terms of an integer (date). Hence the output of the code shown above will be 23 (since the system date is 6/23/2017).
2. What will be the output of the following C code, if the system date is 6/24/2017?
#include#include #include int main() { time_t ct; time(&ct); printf("%sn",(&ct)); }
a) Error
b) Junk value
c) 6
d) June
Answer: b
Clarification: The output to the above code will be some junk value (not in the human readable form). This is because we have not used the ctime() to convert the date and time into a string.
3. What is the meaning of the following C code if output is 0?
#include#include int main() { struct tm *local; time_t t; t=time(NULL); local=localtime(&t); printf("%d",local->tm_isdst); return 0; }
a) DST is in effect
b) DST status is unknown
c) DST is not in effect
d) DST is corresponding with local time
Answer: c
Clarification: Daylight Savings time is in effect when the value of tm_isdt is 1. It is not in effect when the value of tm_isdst is 0 and it’s status is unknown when the value of tm_isdst is -1.
4. The following C code results in an error. State whether true or false.
#include#include int main() { struct tm *local; time_t t; t=time(NULL); local=asctime(localtime(&t)); printf("%d",local->tm_wday); return 0; }
a) True
b) False
Answer: b
Clarification: Although the code shown above does not give the correct answer, it does not result in an error.
5. Which of the following format specifiers is used to specify whether the given time is in AM or PM?
a) %P
b) %I
c) %X
d) %p
Answer: d
Clarification: %p (small letter) is used to specify whether the given time is in AM or PM. %I is used to represent the number of hours in the 12 hour clock format. %X is used to represent the standard time zone.
6. What will be the output of the following C code if the system time is 1:52 PM (Sunday)?
#include#include int main() { struct tm *ptr; time_t t; char str[100]; t = time(NULL); ptr = localtime(&t); strftime(str,100,"%I",ptr); puts(str); return 0; }
a) 13
b) 01
c) 1
d) error
Answer: b
Clarification: %I is a format specifier which is used to represent the number of hours in 12 hour clock format. Hence the output of the code shown above will be 01.
7. Which of the following format specifiers is used to represent the hours in the 24 hour clock (0-23) format?
a) %I
b) %H
c) %i
d) %h
Answer: b
Clarification: %H is a format specifier used to represent the number of hours in 24 hour clock format.
8. What will be the output of the following C code?
#include#include int main () { double d; d = difftime (5,17); printf ("%.2fn", d ); return 0; }
a) 12.00
b) -12.00
c) Error
d) 12
Answer: b
Clarification: The library function difftime(time1,time2) is used to find the difference between time1 and time2 such as: time1-time2. Hence the output of the code shown above will be 5-17=12.00.
9. The value returned by the library function mktime(), on failure is _________
a) -1
b) 0
c) 1
d) -2
Answer: a
Clarification: The library function mktime() converts the date and time into calendar format and returns the value -1 on the failure of this action.
10. Which of the following is defined under the header file time.h?
a) strnct()
b) fabs()
c) iscntrl()
d) null
Answer: d
Clarification: NULL is defined under the header file time.h, in addition to being defined under many other header files. The function strncat() is defined under string.h. The function fabs() is defined under math.h. The function iscntrl() is defined under ctye.h.
