Java MCQs on text formatting in Java Programming Language.
1. Which of these package is used for text formatting in Java programming language? Answer: a 2. Which of this class can be used to format dates and times? Answer: c 3. Which of these method returns an instance of DateFormat that can format time information? Answer: b 4. Which of these class allows us to define our own formatting pattern for dates and time? Answer: b 5. Which of these formatting strings of SimpleDateFormat class is used to print AM or PM in time? Answer: a 6. Which of these formatting strings of SimpleDateFormat class is used to print week of the year? Answer: a 7. What will be the output of the following Java program? Note : The program is executed at 3 hour 55 minutes and 4 sec (24 hours time). 8. What will be the output of the following Java program? Note : The program is executed at 3 hour 55 minutes and 4 sec (24 hours time). 9. What will be the output of the following Java program? Note: The program is executed at 3 hour 55 minutes and 4 sec on Monday, 15 July(24 hours time). 10. What will be the output of the following Java program? Note : The program is executed at 3 hour 55 minutes and 4 sec on Monday, 15 July(24 hours time).
a) java.text
b) java.awt
c) java.awt.text
d) java.io
Clarification: java.text allows formatting, searching and manipulating text.
a) Date
b) SimpleDate
c) DateFormat
d) textFormat
Clarification: DateFormat is an abstract class that provides the ability to format and parse dates and times.
a) getTime()
b) getTimeInstance()
c) getTimeDateinstance()
d) getDateFormatinstance()
Clarification: getTimeInstance() method returns an instance of DateFormat that can format time information.
a) DefinedDateFormat
b) SimpleDateFormat
c) ComplexDateFormat
d) UsersDateFormat
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.
a) a
b) b
c) c
d) d
Clarification: By using format string “a” we can print AM/PM in time.
a) w
b) W
c) s
d) S
Clarification: By using format string “w” we can print week in a year whereas by using ‘W’ we can print week of a month.
import java.text.*;
import java.util.*;
class Date_formatting
{
public static void main(String args[])
{
Date date = new Date();
SimpleDateFormat sdf;
sdf = new SimpleDateFormat("mm:hh:ss");
System.out.print(sdf.format(date));
}
}
a) 3:55:4
b) 3.55.4
c) 55:03:04
d) 03:55:04
Clarification: None.
Output:
$ javac Date_formatting.java
$ java Date_formatting
55:03:04
import java.text.*;
import java.util.*;
class Date_formatting
{
public static void main(String args[])
{
Date date = new Date();
SimpleDateFormat sdf;
sdf = new SimpleDateFormat("hh:mm:ss");
System.out.print(sdf.format(date));
}
}
a) 3:55:4
b) 3.55.4
c) 55:03:04
d) 03:55:04
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
import java.text.*;
import java.util.*;
class Date_formatting
{
public static void main(String args[])
{
Date date = new Date();
SimpleDateFormat sdf;
sdf = new SimpleDateFormat("E MMM dd yyyy");
System.out.print(sdf.format(date));
}
}
a) Mon Jul 15 2013
b) Jul 15 2013
c) 55:03:04 Mon Jul 15 2013
d) 03:55:04 Jul 15 2013
Clarification: None.
Output:
$ javac Date_formatting.java
$ java Date_formatting
Mon Jul 15 2013
import java.text.*;
import java.util.*;
class Date_formatting
{
public static void main(String args[])
{
Date date = new Date();
SimpleDateFormat sdf;
sdf = new SimpleDateFormat("z");
System.out.print(sdf.format(date));
}
}
a) z
b) Jul
c) Mon
d) PDT
Clarification: format string “z” is used to print time zone.
Output:
$ javac Date_formatting.java
$ java Date_formatting
PDT