C Questions and Answers – Date and Time Functions – 1

«
»

This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Date and Time Functions – 1”.

Pre-requisite for this C MCQ set: Advanced C Programming Video Tutorial.

advertisement

1. Which of the following library functions returns the time in UTC (Greenwich mean time) format?
a) localtime()
b) gettime()
c) gmtime()
d) settime()
View Answer

Answer: c
Explanation: The library function gmtime() returns the time in UTC format. To find the current time, we use the function localtime().

2. What will be the output of the following C code? (By date we mean: date, month and year)

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
    time_t ct;
    time(&ct);
    printf("%s\n",ctime(&ct));
}

a) only current date
b) only current date and current time
c) current date, current time and the day of the week
d) only current time
View Answer

Answer: c
Explanation: The code shown above will print the current date, current time and the day of the week as output.
advertisement

3. What will be the output of the following C code if the current system date is 6/22/2017?

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
    time_t ct;
    time(&ct);
    struct tm *mt=localtime(&ct);
    printf("%d\n",mt-> tm_mon+2);
}

a) 8
b) 7
c) 5
d) 6
View Answer

Answer: b
Explanation: Since the given date is 22nd of June, 2017. Since June is the sixth month of the year, the output will be 5. (0-Jan, 1-Feb, 2-March, 4-April, 5-May, 6-June……..)
advertisement

4. What will be the output of the following C code if the current system date is 6/22/2017?

advertisement
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
typedef struct tm tm;
int main()
{
    time_t ct;
    time(&ct);
    tm *mt=localtime(&ct);
    printf("%d\n",mt-> tm_year);
}

a) 17
b) 2017
c) error
d) 117
View Answer

Answer: d
Explanation: The output of the code shown above is the number of the current year, counted from the year 1900. Hence, since the current year is 2017, the output will be: 2017-1900 = 117.

5. What will be the output of the following C code if the current system date is 6/22/2017?

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
    time_t ct;
    time(&ct);
    struct tm *mt=localtime(&ct);
    printf("%d\n",mt-> tm_date);
}

a) 22
b) 6
c) 22/6
d) error
View Answer

Answer: d
Explanation: The code shown above results in an error. This is because tm_date is not a specified function under time.h.

6. The purpose of the function ctime() is that ___________
a) it returns a string representing the local time
b) it returns a void string
c) it returns a string representing the time in UTC format
d) it returns a string representing the time stored in a structure
View Answer

Answer: a
Explanation: The library function ctime() returns a string representation of the local time. The function asctime() returns a string representing the time stored in a structure.

7. What will be the output of the following C code?

#include<time.h>
int main (void)
{
    float n = time(NULL);
    printf("%.2f\n" , n);
}

a) time in seconds from 1 January, 1970
b) time in minutes from 1 January, 1970
c) time in seconds from 1 January, 1980
d) time in minutes from 1 January, 1980
View Answer

Answer: a
Explanation: The output of the code shown above will be the time in seconds from 1 January, 1970.

8. 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 *local, *gm;
    time_t t;
    t=time(NULL);
    local=localtime(&t);
    printf("%d",local->tm_wday);
    return 0;
}

a) 6
b) 5
c) error
d) 0
View Answer

Answer: b
Explanation: Since the given question has clearly specified that the current weekday at the time of execution of the code is Friday, the output of the code shown above is 5. (0-Sunday, 1-Mon, 2-Tue, 3-Wed, 4 – Thurs, 5-fri, 6-Sat)

9. Which of the following functions returns a pointer to a string representing the date and time stored in a structure?
a) ctime()
b) time()
c) asctime()
d) localtime()
View Answer

Answer: c
Explanation: The function asctime() returns a pointer to a string representing the date and time stored in a structure.

10. What will be the output of the following C code if it is executed on 2nd January, 2017 (system date)?

#include<stdio.h>
#include<time.h>
int main()
{
    struct tm *local, *gm;
    time_t t;
    t=time(NULL);	
    local=localtime(&t);
    printf("%d",local->tm_yday);
    return 0;
}

a) 0
b) 1
c) 2
d) Error
View Answer

Answer: b
Explanation: The output of the code shown above is 1. In the question, it is given that the current date is 2nd January, 2017. (1st Jan – 0, 2nd Jan – 1, 3rd Jan – 2)

Sanfoundry Global Education & Learning Series – C Programming Language.

To practice all areas of C language, here is complete set of 1000+ Multiple Choice Questions and Answers.

advertisement
advertisement

Leave a Comment

Subscribe to our Newsletters (Subject-wise). 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!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.