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
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
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
Explanation: 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
View Answer
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
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
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?
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));
}
}
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
Explanation: None.
Output:
$ javac Date_formatting.java $ java Date_formatting 55:03:04
8. What will be the output of the following Java program?
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));
}
}
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
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?
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));
}
}
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
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?
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));
}
}
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
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.
- Apply for Java Internship
- Practice Programming MCQs
- Apply for Computer Science Internship
- Check Programming Books
- Practice Information Technology MCQs