Java Multiple Choice Questions & Answers on Text Formatting for Freshers

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

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

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

Explanation: By using format string “w” we can print week in a year whereas by using ‘W’ we can print week of month.
7. What is the output of this program?
  1.     import java.text.*;
  2.     import java.util.*;
  3.     class Date_formatting {  
  4.         public static void main(String args[]) {
  5.      Date date = new Date();
  6.      SimpleDateFormat sdf;
  7.             sdf = new SimpleDateFormat("mm:hh:ss");
  8.             System.out.print(sdf.format(date));
  9.         } 
  10.     }
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
8. What is the output of this program?
  1.     import java.text.*;
  2.     import java.util.*;
  3.     class Date_formatting {  
  4.         public static void main(String args[]) {
  5.      Date date = new Date();
  6.      SimpleDateFormat sdf;
  7.             sdf = new SimpleDateFormat("hh:mm:ss");
  8.             System.out.print(sdf.format(date));
  9.         } 
  10.     }
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

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.
9. What is the output of this program?
  1.     import java.text.*;
  2.     import java.util.*;
  3.     class Date_formatting {  
  4.         public static void main(String args[]) {
  5.      Date date = new Date();
  6.      SimpleDateFormat sdf;
  7.             sdf = new SimpleDateFormat("E MMM dd yyyy");
  8.             System.out.print(sdf.format(date));
  9.         } 
  10.     }
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
10. What is the output of this program?
  1.     import java.text.*;
  2.     import java.util.*;
  3.     class Date_formatting {  
  4.         public static void main(String args[]) {
  5.      Date date = new Date();
  6.      SimpleDateFormat sdf;
  7.             sdf = new SimpleDateFormat("z");
  8.             System.out.print(sdf.format(date));
  9.         } 
  10.     }
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

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

Related

Multiple Choice Questions 846080603993700571

Post a Comment

emo-but-icon

item