Date Methods MCQs
Which package contains the Date class in Java?
A. java.util
B. java.sql
C. java.time
D. java.text
How do you create a Date object representing the current date and time?
A. new Date()
B. Date.getCurrentDate()
C. Date.now()
D. new Date(System.currentTimeMillis())
Which method is used to get the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by a Date object?
A. getTime()
B. getMilliseconds()
C. getEpochTime()
D. getMillis()
What does the Date method toString() return?
A. A string representation of the date in dd/MM/yyyy format.
B. The number of milliseconds since epoch.
C. A string representation of the date and time.
D. The date in yyyy-MM-dd format.
Which method sets the time represented by a Date object?
A. setTime(long time)
B. setMilliseconds(long time)
C. setDate(long time)
D. updateTime(long time)
How do you compare two Date objects to see if one is before the other?
A. compareTo(Date anotherDate)
B. isBefore(Date anotherDate)
C. before(Date anotherDate)
D. compare(Date anotherDate)
What does the Date method after(Date when) return?
A. true if the date is after the specified date.
B. true if the date is before the specified date.
C. true if the date is equal to the specified date.
D. false if the date is after the specified date.
Which method of the Date class is deprecated?
A. getDate()
B. getDay()
C. getHours()
D. All of the above
How can you set the year of a Date object to 2025?
A. setYear(2025)
B. setYear(125)
C. setYear(25)
D. setYear(2024)
Which method can be used to compare if two Date objects represent the same point in time?
A. equals(Object obj)
B. compareTo(Date anotherDate)
C. compare(Date anotherDate)
D. same(Date anotherDate)
What will the Date method before(Date when) return if the calling date is before the specified date?
A. true
B. false
C. null
D. Throws an exception
Which method in the Date class converts the date to a string of the form EEE MMM dd HH:mm:ss zzz yyyy?
A. format()
B. toString()
C. toLocaleString()
D. toGMTString()
How do you obtain the current date and time using java.util.Date?
A. Date date = new Date();
B. Date date = Date.getCurrent();
C. Date date = Date.now();
D. Date date = Date.today();
Which Date method is used to set the number of seconds after the minute, between 0 and 61?
A. setSeconds(int seconds)
B. setMinutes(int seconds)
C. setTime(int seconds)
D. setSeconds(Date date)
Which method is used to retrieve the time zone offset in hours of a Date object?
A. getTimezoneOffset()
B. getZoneOffset()
C. getOffset()
D. getTimeZone()
What is the return type of the compareTo(Date anotherDate) method?
A. boolean
B. int
C. long
D. void
How can you set the month of a Date object to February?
A. setMonth(2)
B. setMonth(1)
C. setMonth(3)
D. setMonth(0)
Which of the following methods can be used to get the day of the week from a Date object?
A. getDay()
B. getDayOfWeek()
C. getDayNumber()
D. getDayIndex()
Which method in Date is used to format the date as a string in the GMT time zone?
A. toGMTString()
B. toUTCTime()
C. toUniversalTime()
D. toGMT()
How can you set the date to the last day of the month in a Date object?
A. setDate(getLastDayOfMonth())
B. setDate(31)
C. setDate(0)
D. Manually calculate the last day and set it using setDate()
Answers:
A. java.util
A. new Date()
A. getTime()
C. A string representation of the date and time.
A. setTime(long time)
C. before(Date anotherDate)
A. true if the date is after the specified date.
D. All of the above
C. setYear(25)
A. equals(Object obj)
A. true
B. toString()
A. Date date = new Date();
A. setSeconds(int seconds)
A. getTimezoneOffset()
B. int
B. setMonth(1)
A. getDay()
A. toGMTString()
D. Manually calculate the last day and set it using setDate()
No comments:
Post a Comment