Python Date and Time Basics Quiz

Python
0 Passed
0% acceptance

A 35-question quiz covering datetime objects, current timestamps, formatting, parsing, and calculating time differences.

35 Questions
~70 minutes
1

Question 1

Which standard library module provides classes for manipulating dates and times in Python?

A
datetime
B
time
C
calendar
D
date
2

Question 2

Analyze the code below. What does `dt` represent?

python
from datetime import datetime
dt = datetime.now()
A
The current local date and time.
B
The current UTC date and time.
C
The date 1970-01-01.
D
A random date.
3

Question 3

Which class represents a duration or difference between two dates?

A
timedelta
B
diff
C
interval
D
span
4

Question 4

Analyze the code below. What is the output format of `s`?

python
from datetime import date
d = date(2023, 12, 25)
s = d.strftime("%Y-%m-%d")
A
2023-12-25
B
25-12-2023
C
12/25/2023
D
December 25, 2023
5

Question 5

Which method is used to convert a string into a datetime object?

A
strptime()
B
strftime()
C
parse()
D
convert()
6

Question 6

Analyze the code below. What is the value of `future_date`?

python
from datetime import date, timedelta
today = date(2023, 1, 1)
future_date = today + timedelta(days=10)
A
2023-01-11
B
2023-01-10
C
2023-02-01
D
Error
7

Question 7

What is the result of subtracting two datetime objects?

python
diff = datetime(2023, 1, 2) - datetime(2023, 1, 1)
A
A timedelta object.
B
An integer (number of seconds).
C
A float.
D
A new datetime object.
8

Question 8

Which format code represents the full name of the month (e.g., 'January')?

A
%B
B
%b
C
%m
D
%M
9

Question 9

Analyze the code below. What happens if the string doesn't match the format?

python
from datetime import datetime
dt = datetime.strptime("2023/01/01", "%Y-%m-%d")
A
ValueError is raised.
B
It returns None.
C
It guesses the format.
D
It returns the current date.
10

Question 10

How do you get the UNIX timestamp (seconds since epoch) from a datetime object `dt`?

A
dt.timestamp()
B
dt.unix()
C
dt.epoch()
D
dt.seconds()
11

Question 11

Analyze the code below. What is `t`?

python
from datetime import time
t = time(14, 30, 0)
A
A time object representing 2:30 PM.
B
A duration of 14 hours and 30 minutes.
C
A datetime object for today at 14:30.
D
A string '14:30:00'.
12

Question 12

Which method returns the day of the week as an integer (Monday is 0, Sunday is 6)?

A
weekday()
B
isoweekday()
C
day()
D
get_weekday()
13

Question 13

Analyze the code below. How do you create a new datetime with the year changed to 2024?

python
dt = datetime(2023, 5, 17)
# What goes here?
A
new_dt = dt.replace(year=2024)
B
dt.year = 2024
C
new_dt = dt.set_year(2024)
D
new_dt = dt + 1
14

Question 14

What is a 'naive' datetime object?

A
A datetime object that does not contain timezone information.
B
A datetime object created by a beginner.
C
A datetime object with only the date part.
D
A datetime object that is incorrect.
15

Question 15

Analyze the code below. What is the total duration in seconds?

python
from datetime import timedelta
delta = timedelta(minutes=2, seconds=30)
print(delta.total_seconds())
A
150.0
B
120.0
C
32.0
D
2.5
16

Question 16

Which function creates a datetime object from a timestamp (e.g., 1672531200)?

A
datetime.fromtimestamp()
B
datetime.totimestamp()
C
datetime.parse()
D
datetime.create()
17

Question 17

Analyze the code below. What is the output format of `isoformat()`?

python
dt = datetime(2023, 1, 1, 12, 0, 0)
print(dt.isoformat())
A
2023-01-01T12:00:00
B
2023-01-01 12:00:00
C
01/01/2023 12:00:00
D
Sun Jan 1 12:00:00 2023
18

Question 18

Can you add two datetime objects together (e.g., `dt1 + dt2`)?

A
No, it raises a TypeError.
B
Yes, it combines them.
C
Yes, it returns a timedelta.
D
Yes, it returns a list of dates.
19

Question 19

Which format code represents the 24-hour clock hour (00-23)?

A
%H
B
%I
C
%h
D
%p
20

Question 20

Analyze the code below. What does `datetime.combine()` do?

python
d = date(2023, 1, 1)
t = time(10, 30)
dt = datetime.combine(d, t)
A
Creates a datetime object: 2023-01-01 10:30:00.
B
Creates a string.
C
Adds the time to the date.
D
Raises an error.
21

Question 21

What is `datetime.min`?

A
The earliest representable datetime (0001-01-01 00:00:00).
B
The Unix Epoch (1970-01-01).
C
The beginning of the current year.
D
Negative infinity.
22

Question 22

Analyze the code below. What is the value of `x`?

python
from datetime import timedelta
x = timedelta(hours=1) == timedelta(minutes=60)
A
True
B
False
C
Error
D
None
23

Question 23

How do you access the year attribute of a datetime object `dt`?

A
dt.year
B
dt.get_year()
C
dt.strftime('%Y')
D
dt['year']
24

Question 24

What happens if you try to create `date(2023, 2, 30)`?

A
ValueError: day is out of range for month
B
It automatically rolls over to March 2nd.
C
It creates the date successfully.
D
It returns None.
25

Question 25

Which method would you use to get the current date without the time?

A
date.today()
B
date.now()
C
datetime.date()
D
datetime.today_date()
26

Question 26

Analyze the code below. What is `d.month`?

python
d = date(2023, 5, 17)
A
5
B
May
C
17
D
2023
27

Question 27

How does `datetime` handle leap years?

A
Correctly; date(2024, 2, 29) is valid, but date(2023, 2, 29) raises ValueError.
B
It ignores them.
C
It assumes every year has 365 days.
D
It assumes every February has 29 days.
28

Question 28

What is the result of `datetime.now().date()`?

A
A date object (year, month, day) with no time info.
B
A string.
C
A timestamp.
D
The same datetime object.
29

Question 29

Analyze the code below. What is the output?

python
from datetime import timedelta
t = timedelta(days=1, hours=1)
print(t.days)
A
1
B
25
C
0
D
24
30

Question 30

Which format code represents the full 4-digit year?

A
%Y
B
%y
C
%YYYY
D
%yr
31

Question 31

Can `timedelta` represent negative time intervals?

A
Yes.
B
No, it raises an error.
C
No, it takes the absolute value.
D
Only if created with `abs()`.
32

Question 32

Analyze the code below. What is the type of `res`?

python
d1 = date(2023, 1, 1)
d2 = date(2023, 1, 2)
res = d1 < d2
A
bool
B
datetime
C
timedelta
D
int
33

Question 33

What is the default value for hours, minutes, and seconds if not provided to `datetime()`?

A
0
B
12
C
None
D
Current time
34

Question 34

Which method allows you to parse a string with a timezone offset like '2023-01-01T12:00:00+00:00'?

A
datetime.fromisoformat()
B
datetime.parse_tz()
C
datetime.strptime_iso()
D
It requires a third-party library.
35

Question 35

Analyze the code below. What is the result?

python
from datetime import time
t = time(25, 0, 0)
A
ValueError: hour must be in 0..23
B
It rolls over to 1:00 AM.
C
It creates the time object.
D
It returns None.

QUIZZES IN Python