Oracle Certified Professional Java Se 11 Developer · Free Practice Question Easy
Question 1
Question ID: UKOCP26808
Given code:
- package com.udayankhattry.ocp;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- public class Test {
- public static void main(String [] args) {
- var date = new Date();
- var format = new SimpleDateFormat("'Time is:' hh:mm:ss a");
- System.out.println(format.format(date));
- }
- }
Which of the following statement is correct?
- A It throws an exception at runtime
- B If program execution time is 1:32:22 PM, output is: Time is: 13:32:22
- C If program execution time is 1:32:22 PM, output is: Time is: 13:32:22 PM
- D If program execution time is 1:32:22 PM, output is: Time is: 1:32:22 PM
- E If program execution time is 1:32:22 PM, output is: Time is: 1:32:22
- F If program execution time is 1:32:22 PM, output is: Time is: 01:32:22 PM
-
G
If program execution time is 1:32:22 PM, output is: Time is: 01:32:22
Reveal correct answer
Correct answer: F
Explanation
UKOCP26808:
Variable 'date' infers to Date type and variable 'format' infers to SimpleDateFormat type.
In the format string:
h represents Hour in am/pm (1-12); hh represents 2 digits for the hour (01-12).
m represents Minute in hour (0-59); mm represents 2 digits for the minute (00-59).
s represents Second in minute (0-59); ss represents 2 digits for the second (00-59).
a represents AM/PM marker.
In the format string, anything between opening and closing quote [''] is displayed as is, Hence 'Time is:' will be displayed as Time is:
Output will be:
Time is: 01:32:22 PM
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
