C Multiple Choice Questions & Answers on “Date and Time Function – 2”.
1. Which of the following format specifiers is used to represent the name of the time zone?
a) %A
b) %B
c) %H
d) %Z
Answer: d
Clarification: The format specifier %Z is used to specify the name of the time zone. %A- full weekday, %B- full month name, %H-hours(0-23).
2. What will be the output of the following C code if the system time is 4:27 PM?
#include#include int main() { struct tm *ptr; time_t t; char str[100]; t = time(NULL); ptr = localtime(&t); strftime(str,100,"%H %p %M minutes",ptr); puts(str); return 0; }
a) 16 27 minutes
b) 4 27 minutes
c) 16 PM 27 minutes
d) 4 PM 27 minutes
Answer: c
Clarification: %H is a format specifier used to represent the hours (0-23) and %I specifies the hours in the format(0-12), %p is used to specify AM or PM, %M is used to specify the minutes. Hence output will be: 16 PM 27 minutes
3. What will be the output of the following C code if the system date is 8/22/2016?
#include#include int main() { struct tm *ptr; time_t t; char str[100]; t = time(NULL); ptr = localtime(&t); strftime(str,100,"%B",ptr); puts(str); return 0; }
a) 9
b) August
c) Aug
d) Error
Answer: b
Clarification: %B is a format specifier which is used to specify the full month name. Hence the output will be August.
4. What will be the output of the following C code if the system date is 6/2/2017 (Friday)?
#include#include int main() { struct tm *ptr; time_t t; char str[100]; t = time(NULL); ptr = localtime(&t); strftime(str,100,"%A",ptr); puts(str); return 0; }
a) Error
b) Fri
c) Friday
d) 6
Answer: c
Clarification: %A specifies the full weekday. Hence the output is Friday.
5. Which of the following library functions is used to read location dependent information?
a) localtime()
b) localeconv()
c) localcon()
d) local()
Answer: b
Clarification: localeconv() is used to read the location dependent information. The function used to find the current time is localtime().
6. Which of the following functions is used to convert the date and time into a calendar format?
a) difftime()
b) clock()
c) mktime()
d) ctime()
Answer: c
Clarification: The function mktime() is used to convert the date and time into a calendar format. The function difftime() is used to find the difference between two specified timings, the function clock() is used to return the number of ticks.
7. What will be the output of the following C code?
#include#include main() { struct tm t; time_t tc; t.tm_year=2017-1900; t.tm_mday=25; t.tm_mon=5; t.tm_hour=1; t.tm_min=30; t.tm_sec=0; t.tm_isdst=0; tc=mktime(&t); printf(ctime(&tc)); }
a) Sun Jun 25 01:30:00 2017
b) Sun June 25 1:30 2017
c) Sun Jun 25 1:30 117
d) Sun June 25 1:30:00 117
Answer: a
Clarification: The function mktime() converts the time and date into a calendar format. Hence the output of the code shown above is: Sun Jun 25 1:30:00 2017.
8. The value of tm_isdst is ____ when DST( Daylight Savings Time) is in effect, ______ when DST is not in effect and ______ when the DST status is unknown.
a) -1, 1, 0
b) 1, 0, -1
c) 0, 1, -1
d) 1, -1, 0
Answer: b
Clarification: The value of tm_isdst is 1 when Daylight Savings Time is in effect, 0 when DSP is not in the effect and -1 when DST status is not known.
9. The library function clock() returns the number of _________ elapsed since the start of the program.
a) minutes
b) clock ticks
c) milli-seconds
d) micro-seconds
Answer: b
Clarification: The library function clock() returns the number of clock ticks elapsed since the start of the program. To get the number of seconds used by the CPU, we should divide by CLOCKS_PER_SEC.
10. What will be the output of the following C code if the name entered is “TOM” and time taken to enter this name is 2 seconds?
#include#include int main () { time_t time1,time2; char get_input [256]; double dif_sec; time (&time1); printf ("Please enter the name of your pet: "); gets (get_input); time (&time2); dif_sec = difftime (time2,time1); printf ("%.2fn", dif_sec ); return 0; }
a) Error
b) 2
c) 2.0
d) 2.00
Answer: d
Clarification: The library function difftime() returns the difference in seconds between time1 and time 2. Since the format specifier is %.2f, the output will be 2.00.