C MCQs on “Date and Time Functions – 1”.
1. Which of the following library functions returns the time in UTC (Greenwich mean time) format?
a) localtime()
b) gettime()
c) gmtime()
d) settime()
Answer: c
Clarification: 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)
#include
#include
#include
int main()
{
time_t ct;
time(&ct);
printf("%sn",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
Answer: c
Clarification: The code shown above will print the current date, current time and the day of the week as output.
3. What will be the output of the following C code if the current system date is 6/22/2017?
#include
#include
#include
int main()
{
time_t ct;
time(&ct);
struct tm *mt=localtime(&ct);
printf("%dn",mt-> tm_mon+2);
}
a) 8
b) 7
c) 5
d) 6
Answer: b
Clarification: 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……..)
4. What will be the output of the following C code if the current system date is 6/22/2017?
#include
#include
#include
typedef struct tm tm;
int main()
{
time_t ct;
time(&ct);
tm *mt=localtime(&ct);
printf("%dn",mt-> tm_year);
}
a) 17
b) 2017
c) error
d) 117
Answer: d
Clarification: 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
#include
#include
int main()
{
time_t ct;
time(&ct);
struct tm *mt=localtime(&ct);
printf("%dn",mt-> tm_date);
}
a) 22
b) 6
c) 22/6
d) error
Answer: d
Clarification: 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
Answer: a
Clarification: 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
int main (void)
{
float n = time(NULL);
printf("%.2fn" , 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
Answer: a
Clarification: 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
#include
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
Answer: b
Clarification: 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()
Answer: c
Clarification: 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
#include
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
Answer: b
Clarification: 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)