250+ TOP MCQs on Text Formatting and Answers

Java MCQs 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

Answer: a
Clarification: 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

Answer: c
Clarification: 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()

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

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

Answer: b
Clarification: 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

Answer: a
Clarification: 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

Answer: a
Clarification: 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?

  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

Answer: c
Clarification: None.
Output:

$ 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

Answer: d
Clarification: 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

Answer: a
Clarification: 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

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

$ javac Date_formatting.java
$ java Date_formatting
PDT

Leave a Reply

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