C Questions and Answers – Date and Time Function – 3

«
»

This set of C Multiple Choice Questions & Answers (MCQs) focuses 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?

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

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

2. What will be the output of the following C code, if the system date is 6/24/2017?

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
    time_t ct;
    time(&ct);
    printf("%s\n",(&ct));
}

a) Error
b) Junk value
c) 6
d) June
View Answer

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

3. What is the meaning of the following C code if output is 0?

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

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

4. The following C code results in an error. State whether true or false.

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

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

Answer: d
Explanation: %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<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,"%I",ptr);
    puts(str);
    return 0;
}

a) 13
b) 01
c) 1
d) error
View Answer

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

Answer: b
Explanation: %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 <stdio.h>
#include <time.h>
int main ()
{
	double d;
	d = difftime (5,17);
	printf ("%.2f\n", d );
	return 0;
}

a) 12.00
b) -12.00
c) Error
d) 12
View Answer

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

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

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

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