Text Formatting(Computer Science > Java Program ) Questions and Answers

Question 1.


What is the output of this 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).


  1.    z
  2.    Jul
  3.    Mon
  4.    PDT
Explanation:-
Answer: Option D. -> PDT

format string "z" is used to print time zone.
Output:
$ javac Date_formatting.java
$ java Date_formatting
PDT



Question 2.


What is the output of this 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).


  1.    Mon Jul 15 2013
  2.    Jul 15 2013
  3.    55:03:04 Mon Jul 15 2013
  4.    03:55:04 Jul 15 2013
Explanation:-
Answer: Option A. -> Mon Jul 15 2013

None.
Output:
$ javac Date_formatting.java
$ java Date_formatting
Mon Jul 15 2013



Question 3.


What is the output of this 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).


  1.    3:55:4
  2.    3.55.4
  3.    55:03:04
  4.    03:55:04
Explanation:-
Answer: Option D. -> 03:55:04

The code "sdf = new SimpleDateFormat("hh:mm:ss");" create a SimpleDataFormat 


Question 4.


What is the output of this 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).


  1.    3:55:4
  2.    3.55.4
  3.    55:03:04
  4.    03:55:04
Explanation:-
Answer: Option C. -> 55:03:04

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



Question 5.

Which of these formatting strings of SimpleDateFormat class is used to print 

week of the year?


  1.    w
  2.    W
  3.    s
  4.    S
Explanation:-
Answer: Option A. -> w

By using format string "w" we can print week in a year whereas by using 'W' we 


Question 6.

Which of these formatting strings of SimpleDateFormat class is used to 

print AM or PM in time?


  1.    a
  2.    b
  3.    c
  4.    d
Explanation:-
Answer: Option A. -> a

By using format string "a" we can print AM/PM in time.



Question 7.

Which of these class allows us to define our own formatting pattern for dates 

and time?


  1.    DefinedDateFormat
  2.    SimpleDateFormat
  3.    ComplexDateFormat
  4.    UsersDateFormat
Explanation:-
Answer: Option B. -> SimpleDateFormat

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.



Question 8.

Which of these package is used for text formatting in Java programming language?


  1.    java.text
  2.    java.awt
  3.    java.awt.text
  4.    java.io
Explanation:-
Answer: Option A. -> java.text

java.text allows formatting, searching and manipulating text.



Question 9.

Which of this class can be used to format dates and times?


  1.    Date
  2.    SimpleDate
  3.    DateFormat
  4.    textFormat
Explanation:-
Answer: Option C. -> DateFormat

DateFormat is an abstract class that provides the ability to format and parse 

dates and times.



Question 10.

Which of these method returns an instance of DateFormat that can format 

time information?


  1.    getTime()
  2.    getTimeInstance()
  3.    getTimeDateinstance()
  4.    getDateFormatinstance()
Explanation:-
Answer: Option B. -> getTimeInstance()

getTimeInstance() method returns an instance of DateFormat that can format 

time information.