Java Questions & Answers – Text Formatting

This section of our 1000+ Java MCQs focuses on text formatting in Java Programming Language.

1. Which of these package is used for text formatting in Java programming language?
a) java.text
b) java.awt
c) java.awt.text
d) java.io
View Answer

Answer: a
Explanation: java.text allows formatting, searching and manipulating text.

2. Which of this class can be used to format dates and times?
a) Date
b) SimpleDate
c) DateFormat
d) textFormat
View Answer

Answer: c
Explanation: DateFormat is an abstract class that provides the ability to format and parse dates and times.

3. Which of these method returns an instance of DateFormat that can format time information?
a) getTime()
b) getTimeInstance()
c) getTimeDateinstance()
d) getDateFormatinstance()
View Answer

Answer: b
Explanation: getTimeInstance() method returns an instance of DateFormat that can format time information.
advertisement
advertisement

4. Which of these class allows us to define our own formatting pattern for dates and time?
a) DefinedDateFormat
b) SimpleDateFormat
c) ComplexDateFormat
d) UsersDateFormat
View Answer

Answer: b
Explanation: The DateFormat is a concrete subclass of DateFormat. It allows you to define your own formatting patterns that are used to display date and time information.

5. Which of these formatting strings of SimpleDateFormat class is used to print AM or PM in time?
a) a
b) b
c) c
d) d
View Answer

Answer: a
Explanation: By using format string “a” we can print AM/PM in time.

6. Which of these formatting strings of SimpleDateFormat class is used to print week of the year?
a) w
b) W
c) s
d) S
View Answer

Answer: a
Explanation: By using format string “w” we can print week in a year whereas by using ‘W’ we can print week of a month.

7. What will be the output of the following Java program?

advertisement
  1.     import java.text.*;
  2.     import java.util.*;
  3.     class Date_formatting
  4.     {	 
  5.         public static void main(String args[])
  6.         {
  7. 	    Date date = new Date();
  8. 	    SimpleDateFormat sdf;
  9.             sdf = new SimpleDateFormat("mm:hh:ss");
  10.             System.out.print(sdf.format(date));
  11.         }	
  12.     }

Note : The program is executed at 3 hour 55 minutes and 4 sec (24 hours time).
a) 3:55:4
b) 3.55.4
c) 55:03:04
d) 03:55:04
View Answer

Answer: c
Explanation: None.
Output:

advertisement
$ javac Date_formatting.java
$ java Date_formatting
55:03:04

8. What will be the output of the following Java program?

  1.     import java.text.*;
  2.     import java.util.*;
  3.     class Date_formatting
  4.     {	 
  5.         public static void main(String args[])
  6.         {
  7. 	    Date date = new Date();
  8. 	    SimpleDateFormat sdf;
  9.             sdf = new SimpleDateFormat("hh:mm:ss");
  10.             System.out.print(sdf.format(date));
  11.         }	
  12.     }

Note : The program is executed at 3 hour 55 minutes and 4 sec (24 hours time).
a) 3:55:4
b) 3.55.4
c) 55:03:04
d) 03:55:04
View Answer

Answer: d
Explanation: The code “sdf = new SimpleDateFormat(“hh:mm:ss”);” create a SimpleDataFormat class with format hh:mm:ss where h is hours, m is month and s is seconds.
Output:

$ javac Date_formatting.java
$ java Date_formatting
03:55:04

9. What will be the output of the following Java program?

  1.     import java.text.*;
  2.     import java.util.*;
  3.     class Date_formatting
  4.     {	 
  5.         public static void main(String args[])
  6.         {
  7. 	    Date date = new Date();
  8. 	    SimpleDateFormat sdf;
  9.             sdf = new SimpleDateFormat("E MMM dd yyyy");
  10.             System.out.print(sdf.format(date));
  11.         }	
  12.     }

Note: The program is executed at 3 hour 55 minutes and 4 sec on Monday, 15 July(24 hours time).
a) Mon Jul 15 2013
b) Jul 15 2013
c) 55:03:04 Mon Jul 15 2013
d) 03:55:04 Jul 15 2013
View Answer

Answer: a
Explanation: None.
Output:

$ javac Date_formatting.java
$ java Date_formatting
Mon Jul 15 2013

10. What will be the output of the following Java program?

  1.     import java.text.*;
  2.     import java.util.*;
  3.     class Date_formatting
  4.     {	 
  5.         public static void main(String args[])
  6.         {
  7. 	    Date date = new Date();
  8. 	    SimpleDateFormat sdf;
  9.             sdf = new SimpleDateFormat("z");
  10.             System.out.print(sdf.format(date));
  11.         }	
  12.     }

Note : The program is executed at 3 hour 55 minutes and 4 sec on Monday, 15 July(24 hours time).
a) z
b) Jul
c) Mon
d) PDT
View Answer

Answer: d
Explanation: format string “z” is used to print time zone.
Output:

$ javac Date_formatting.java
$ java Date_formatting
PDT

Sanfoundry Global Education & Learning Series – Java Programming Language.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.