Test your Java language skills – Quiz 1

Most professional Java developers tend to get so busy with their specialization that they often lose touch with the basic Java language itself. So if you wish to test your Java skills and check if you still are in touch with the language, here’s the first of our mini Java quizzes.

These questions are courtesy of Whizlabs Software
and will also help you prepare for the Sun Certified Java Programmer
Exam 5.0 (SCJP).

If you are looking for a user contributed database of
100s of Java questions, check out the Java Interview Questions Bank .

Question No. 1:
If the complete date/time information is Mon May 23 09:05:28 PM GMT+05:30 2005, what will be printed out by the following program?

class Format2 {

public static void main(String[] args){

Formatter fmt=new Formatter();
Calendar cal=Calendar.getInstance();
fmt.format(“%tr %tZ”,cal,cal);
System.out.println(fmt);

}

}

A. 09:05:28 PM GMT+05:30
B. 21:05:28 2005
C. Mon 21:05:28 GMT+05:30 2005
D. 21:05:28 GMT+05:30
E. May 23 09:05:28 PM GMT+05:30

Question No. 2:
What is the result of compiling and running the following code?

class StringBufferTest{

public static void main(String[] args){

1. StringBuffer result = new StringBuffer();
2. StringBuffer s=null;
3. result.append(s);
4. result.insert(0,”123″);
5. System.out.println(result);

}

}

A.Compiler error at line 1
B.Compiler error at line 3
C.Compiler error at line 4
D.NullPointerException
E.StringIndexOutOfBoundsException
F.Prints “123null”

Try your best to solve these before you jump to the next page for answers.

[Answers]

1 – Correct Choice:
A

Explanation
Choice A is the correct answer.

The Formatter class provides format conversions that let you display numbers, strings, time, and date according to the specified format string. The %t specifier lets you format date and time information; it uses a suffix to describe the exact format required. The suffix ‘r’ is replaced by the ‘hh:mm:ss’ format (12 hr format), which in this case, is ’09:05:28 PM’. The suffix ‘Z’ gives the time zone, which in this case is ‘GMT+05:30’.

To know more about the Formatter, refer to
http://java.sun.com/developer/JDCTechTips/2004/tt1005.html#2
http://java.sun.com/j2se/1.5.0/docs/api/java/util/logging/Formatter.html

2 – Correct Choice: F

Explanation:
Choice F is the correct answer.

This code compiles fine and prints “123null”. The StringBuffer object s is constructed using the parameterless constructor, so it is empty at first. If the argument passed to the append method is null, then the four characters “null” are appended to the invoking StringBuffer object. So now sb refers to null.

The insert method causes the characters of the String argument to be inserted, in order, into this StringBuffer at the indicated offset, moving up any characters originally above that position and increasing the length of this sequence by the length of the argument. Here the insert method is invoked with an offset of 0, and the argument passed is “123”. So the result is “123null” which is printed out.

For more information, you may refer to http://java.sun.com/j2se/1.5.0/docs/api/java/lang/StringBuffer.html

Related
Get your JSP / Servlet skills certified
A guide to the Sun Certified Java Programmer exam for J2SE 5.0
Learn J2EE programming with passion and for free
Recruiting like crazy

Content Team

The IndicThreads Content Team posts news about the latest and greatest in software development as well as content from IndicThreads' conferences and events. Track us social media @IndicThreads. Stay tuned!

0 thoughts on “Test your Java language skills – Quiz 1

  • September 11, 2006 at 10:04 am
    Permalink

    Ruby has been around for a decade. There must be a reason it never picked up. I will only adopt Ruby if it survives beyond the Rails Hype, maybe after an year

Leave a Reply