Go Date and Time MCQ

1. Which package is primarily used for date and time in Go?

a) time
b) date
c) datetime
d) timestamp

Answer:

a) time

Explanation:

The 'time' package in Go provides functionality for measuring and displaying time.

2. How do you get the current time in Go?

a) time.Now()
b) time.Current()
c) time.GetTime()
d) time.CurrentTime()

Answer:

a) time.Now()

Explanation:

The 'time.Now()' function is used to get the current local time in Go.

3. What is the return type of time.Now() in Go?

a) string
b) Time
c) int64
d) Date

Answer:

b) Time

Explanation:

The 'time.Now()' function returns a 'Time' struct representing the current local time.

4. How do you format a date in Go?

a) date.Format("2006-01-02")
b) time.Format("2006-01-02")
c) Time.Format("2006-01-02")
d) Now.Format("2006-01-02")

Answer:

b) time.Format("2006-01-02")

Explanation:

In Go, the 'Format' method of the 'Time' type is used to format a date. The layout string "2006-01-02" is a reference time used to specify the format.

5. How do you parse a string into a time object in Go?

a) time.Parse(layout, value)
b) time.Convert(layout, value)
c) time.ToDateTime(layout, value)
d) time.FromString(layout, value)

Answer:

a) time.Parse(layout, value)

Explanation:

The 'time.Parse' function is used to parse a string representation of time into a 'Time' object in Go.

6. How do you add a duration to a time in Go?

a) time.Add(duration)
b) time.Plus(duration)
c) time.Increase(duration)
d) time.AddTime(duration)

Answer:

a) time.Add(duration)

Explanation:

The 'Add' method of the 'Time' type in Go is used to add a duration to a time.

7. How do you find the difference between two times in Go?

a) time1.Sub(time2)
b) time.Diff(time1, time2)
c) time1 – time2
d) time.Difference(time1, time2)

Answer:

a) time1.Sub(time2)

Explanation:

The 'Sub' method in Go is used to find the duration between two 'Time' objects.

8. What is a 'Duration' in Go?

a) A specific point in time
b) A time zone
c) A time interval
d) A time format

Answer:

c) A time interval

Explanation:

A 'Duration' in Go represents a length of time as an int64 nanosecond count.

9. How do you get the current Unix timestamp in Go?

a) time.Now().Unix()
b) time.Unix()
c) time.Now().UnixTime()
d) time.Timestamp()

Answer:

a) time.Now().Unix()

Explanation:

The 'Unix' method of the 'Time' object in Go returns the Unix timestamp, the number of seconds elapsed since January 1, 1970 UTC.

10. How do you create a time object with a specific date in Go?

a) time.Date(year, month, day, hour, min, sec, nsec, loc)
b) time.New(year, month, day, hour, min, sec, nsec, loc)
c) time.Set(year, month, day, hour, min, sec, nsec, loc)
d) time.Create(year, month, day, hour, min, sec, nsec, loc)

Answer:

a) time.Date(year, month, day, hour, min, sec, nsec, loc)

Explanation:

The 'time.Date' function in Go is used to create a new 'Time' object with a specific date and time.

11. How do you find the day of the week for a given date in Go?

a) date.DayOfWeek()
b) time.Day()
c) time.Now().Weekday()
d) time.Weekday()

Answer:

c) time.Now().Weekday()

Explanation:

The 'Weekday' method of a 'Time' object in Go returns the day of the week for that date.

12. What type is used to represent a time zone in Go?

a) time.TimeZone
b) time.Location
c) string
d) time.Zone

Answer:

b) time.Location

Explanation:

The 'Location' type in Go represents a geographical location and time zone.

13. How do you convert a time to a different time zone in Go?

a) time.Convert(location)
b) time.ChangeZone(location)
c) time.In(location)
d) time.ToLocation(location)

Answer:

c) time.In(location)

Explanation:

The 'In' method of a 'Time' object in Go is used to convert a time to the specified location's time zone.

14. What is the purpose of the 'After' function in the 'time' package in Go?

a) To check if one time is after another
b) To delay code execution until a future time
c) To format a time as 'after' a certain date
d) To add a duration to a time

Answer:

a) To check if one time is after another

Explanation:

The 'After' function in the 'time' package in Go is used to check if one time is after another.

15. How do you create a timer that fires once after a specified duration in Go?

a) time.NewTimer(duration)
b) time.After(duration)
c) time.SetTimer(duration)
d) time.Timer(duration)

Answer:

a) time.NewTimer(duration)

Explanation:

The 'NewTimer' function in Go returns a new 'Timer' that will send the current time on its channel after at least the duration has elapsed.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top