Test your Java language skills – Quiz 2

Test your Java skills and check if you are in touch with the core language. Here’s the second of our mini Java quizzes.

These questions are courtesy of Whizlabs Software and are meant to 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 or check out Java Questions Quiz One.

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

class PrintTest{

public static void main(String[] args) {

double d=222.4578;
System.out.printf(“% .2f”,d);



A.Does not compile
B.Throws IllegalFormatConversionException
C.Prints 222.46
D.Prints 222.45
E.Prints 2.224578
F.Prints .2224578

Question No. 2
What is the result of compiling and running the following code, assuming that the file bb.txt does not exist?

class WriterTest{

public static void main(String[] args) throws IOException{

Writer w=new BufferedWriter(new FileWriter(“bb.txt”)); // Line1
w.write(1); // Line2



A.Compiler error
B.FileNotFoundException thrown at line 1
C.IOException thrown at line 2
D.None of these

Question No. 3
Which of the following statements are true about the default implementation of the public int hashCode() method of the Object class?

A.The Object class does not provide any implementation for the hashCode method; every class must override it.
B.As far as it may be practically possible, the hashCode method defined by the Object class does return distinct integers for distinct objects.
C.For two object references referring to the same object, the hashCode method returns the same integer.
D.It returns a fixed number that internally represents the Object class for the JVM.
E.Only choice D is correct.

The next page has the answers and explanations


Question 1 Correct Choice: C

Choice C is the correct answer. For floating-point conversions using %f, the precision is the number of digits after the decimal separator. So 0.2f has 2 as the precision, i.e., there must be 2 digits after the decimal point, rounding off is done if necessary. Here the value is rounded to 222.46. So choices D, E, and F are incorrect.

There are no compiler errors or exceptions, so choices A and B are also incorrect.

You can get more information about the printf method at

Question 2 Correct Choice: D

Choice D is the correct answer. The FileWriter constructor does not throw FileNotFoundException, if the named file does not exist. In this case, a new file is created by the constructor, if the underlying platform allows this operation. If the named file exists, but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason, the constructor itself throws IOException. So choice D is correct, while choice C is incorrect. Choice A is incorrect because there are no compiler errors in the given code.

Refer to the FileWriter API at

Question 3 Correct Choice: B & C

Choices B and C are the correct answers. The Object class does implement the hashCode method. This default implementation of the hashCode method is in accordance with the default implementation of the equals() method in the Object class. This is what the API says about it –
. Returns a hash code value for the object.
. This method is supported for the benefit of hashtables such as those provided by java.util.Hashtable.

The general contract of the hashCode method is:

Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.

. If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer as the result.

. It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects might improve the performance of hashtables.

Also, as much as is reasonably practical, the hashCode method defined by the Object class does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the Java programming language.)

Choice D is incorrect because the returned number has no relation with the class of the object. Hence, choice E is also incorrect.

More information is available at

Test your Java language skills – Quiz 1
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!

10 thoughts on “Test your Java language skills – Quiz 2

Leave a Reply