C Questions and Answers – Date and Time Function – 2

«
»

This set of C Multiple Choice Questions & Answers focuses 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
View Answer

Answer: d
Explanation: 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).
advertisement

2. What will be the output of the following C code if the system time is 4:27 PM?

#include<stdio.h>
#include<time.h>
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
View Answer

Answer: c
Explanation: %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
advertisement
advertisement

3. What will be the output of the following C code if the system date is 8/22/2016?

advertisement
#include<stdio.h>
#include<time.h>
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
View Answer

Answer: b
Explanation: %B is a format specifier which is used to specify the full month name. Hence the output will be August.
advertisement

4. What will be the output of the following C code if the system date is 6/2/2017 (Friday)?

#include<stdio.h>
#include<time.h>
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
View Answer

Answer: c
Explanation: %A specifies the full weekday. Hence the output is Friday.
advertisement

5. Which of the following library functions is used to read location dependent information?
a) localtime()
b) localeconv()
c) localcon()
d) local()
View Answer

Answer: b
Explanation: 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()
View Answer

Answer: c
Explanation: 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<stdio.h>
#include<time.h>
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
View Answer

Answer: a
Explanation: 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
View Answer

Answer: b
Explanation: 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
View Answer

Answer: b
Explanation: 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 <stdio.h>
#include <time.h>
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 ("%.2f\n", dif_sec );
	return 0;
}

a) Error
b) 2
c) 2.0
d) 2.00
View Answer

Answer: d
Explanation: 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.

Sanfoundry Global Education & Learning Series – C Programming Language.

Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

advertisement
advertisement

Leave a Comment

Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter