You are on page 1of 46

Abhijit.A.

Nayak

KCT,Coimbatore

Sun Certified Programmer for the Java 2


Platform, Standard
Exam 310-055
Edition 5.0
Explanation Abhijit.A.Nayak abhijit.a.nayak@gmail.com
Question 1
All constants in an interface are declared public,static,final
Page 22
Question 2
Option A is wrong because the syntax is wrong.
Option C is wrong because only collections can be checked for
hasnext() and next()
Question 3
On line 17,the short circuit operator && evaluates itself to be
false. if((x==4) && !b2) , here once x==4 is evaluated to be
false then other condition is not checked. So option D is right.
Refer page 294
Question 4
Finally surely executes even if there is an exception or not.
Refer page 347
Question 5
When attempting to cast a reference variable to a type that
fails the IS-A test, a ClassCastException is Thrown.
Refer page 370

Question 6
Locale.getDefault(); is the right method to create an instance
of the Locale. Refer Page 471. In option D,
1

Abhijit.A.Nayak

KCT,Coimbatore

df.setDateFormat(d) is wrong because the question asked is


about the output not to set the date. Hence Option B is the
right answer.
Question 7
In Encapsulation all instance variables in a class should be
marked as Private. Refer page 83 for the rules.
Question 8
In the line 25,Constructor call to the private variable of the
Money class is wrong. Hence there is a compilation error.Even
though the argument passed to public String
getCountry(String timeZone) ie String timeZone is irrelevant ,
it follows the correct syntax in accessing the instance variable
via a method which has been marked as public.
Question 9
Has a Relationship is based on usage.Refer page 92.
Man has a bestfriend instance variable or class member of
Man is bestfriend. Bestfriend is a (type of)Dog.
Question 10
Option C is wrong cause in a Hashset no dulicates are
allowed.Second Entry is not allowed inside the set.
Option D Hashset is a Set , not a map.Option B is wrong
because in a map when we delete an object first we check if
the hashcode() is equal then we check the equals() method
to see if both the instances are the same.
Question 11
The format of the question is correct. But in arrays and
collections the elements inside the collection/array must be
mutually comparable. This will be known as an error only
during the runtime, because the complier does not know what
is inside the array until it runs.Refer page 556.
Question 12
This is postfix increment operator inside the dostuff method.
But the variable inside the method is always a local and the
scope of it dies after it leaves the method.hence dostuff=5.

Abhijit.A.Nayak

KCT,Coimbatore

Once it reaches the main, it realizes that x is a local to the


main() and x was initialized as 5. so main=5. for increment
operator refer page 290.
Question 13
A is correct since we are importing the class in a usual way
and accessing the static
variable using the class name.
C is correct since we are importing the class using the static
imports and accessing the
static variable without class name.
Refer page no 776 for information on static imports
Question 14
To use an enum constant from out side of the class the only
way is to use fully qualified name i.e option D.
Refer page no 60 and 61 for similar examples
Question 15
It is an Argument-Defined Anonymous Inner class.Option D is
the wrong syntax because of class.Option A is wrong because
a new keyword is not there.Option B is wrong because of the
absence of the () after foo.
Refer page number 653 for an example.
Question 16
Read the question carefully cause it is tricky & perplex.
Once new Beta.testfoo() is invoked, line number 28
System.out.println( fubar( new A())); is invoked.The
invocation then calls the method fubar on line number 20.In
fubar the foo.bar(); takes the version which was inside the
testfoo() as it was overridden ie on the line 24,25,26(option
B). If these 3 lines were deleted then the call goes to Beta
version s bar() method on line numbers 16,17,18 (Option E).
Similarly for option F.
Question 17
From the Code we know that for like 4 to work properly we a
fully qualified name for sure. So in option E
Import static sun.scjp.Color.GREEN
Tells us that this is the right option.

Abhijit.A.Nayak

KCT,Coimbatore

Then out of the other options. We first need to import the


package which must contain the enum, so we eliminate A
because there is no static import. Remember static import is
used for accessing static members. So here inside the enum
all 3 colors are static.But we also need to access the color
enum so we must have import sun.scjp.color.*;
Option C is the right answer.

Question 18
This is straight forward and easy. Interface can only extend
another interface. so option A is right
Question 19
Method to be called depends on the actual object type,not on
the return type.
Question 20
It is an simple argument - defined inner anonymous class
.refer page 653 for an example
Question 21
Since we can not create an instance for abstract class options
A and B are invalid.
There is no operator such as -> in java so D is invalid, We
cannot use both object and
class for calling a method so E is invalid.
Option C is the correct way to call the method of Circle by
creating a reference on the abstract class Shape.
Question 22
Rules for overriding
Class members cant have an access modifier which is
narrower than what is declared.But it can be broader.
protected abstract double getSalesAmount(); so proctected
and public are allowed to override but not private and default
as its narrower than what has been declared in Employee
class.
Question 23

Abhijit.A.Nayak

KCT,Coimbatore

A is the correct answers since the class extending the abstract


class and
Implementing the interface. Ans it also implements the
method.
Question 24
A) one class extends another class but not implements
B) an abstract class can extend another abstract class without
overriding the methods
declared in the base class
C) Since draw(); method is not defined it should be declared
as abstract and the class
should also be declared as abstract
D) It is overriding the method properly but the implements
word cannot be used for
extending the abstract class
E) It is a valid overriding and extending the abstract class
F) implements keyword is wrong it should be extends
Question 25
When we are implementing the Runnable interface we must
override run method and
when implementing the Clonable interface we must override
the clone() method. And we
use the keyword implements for implementing one or more
interfaces only once, so C
and D are valid.
In A we are not giving the implementation for methods so its
invalid.
In B we can not use the key word extends for implementing
interfaces so it is
also not valid.
In E we are using the implements keyword twice its not
required
Question 26
When a.go(); is invoked go in sente is printed.
When b.go(); is invoked go in goban is printed.
So The output so far is
Go in sente
Go in goban

Abhijit.A.Nayak

KCT,Coimbatore

Only option C is there with this order. Then Go in Goban is


printed.
Question 27
Since f is declared in try block its scope is in try block only it
cannot be accessed from
out side of the try block i.e finally block also. So the compiler
complains that the symbol f
cannot be resolved in line 17.
Question 28
This is too easy. Anyways, when x+=x is invoked then x=x+x
takes place. Hence x =5+5 and x =10
Question 29
This is also too easy. We are creating an array of integers x[]
and adding elements. Now integer array y[] is assigned to x[]
and so y[2] is 3 because all arrays have a zero based index.
Question 30
Identifier can begin only with a letter,an underscore, or a
currency character.
Refer two minute drill first point on page 68.

Question 31
A is a simple way to create an static array (in C style). B is
way which
follows Java rules i.e, declaring, constructing and initializing.
option C looks like an Anonymous array, but its not.the int
should not have a number between the brackets [].
If it was an Anonymous Array it should look like
static final int[] a;
a= new int[]{100,200};
Question 32
This is an example to iterate through the enum elements
using the enhanced for loop. Color.values() is the type which
has to be passed in for the enum Color.

Abhijit.A.Nayak

KCT,Coimbatore

Question 33
This is an example of a method,constructor inside an enum.
The instance variable is private final int rgb;
The constructor is Color( int rgb) { this.rgb = rgb; }
The method is public int getRGB() { return rgb; }
In order for the enum to work properly we need to make an
enum instance. For that the proper syntax is
Option B Color treeColor = Color.GREEN;
Option A is wrong because the syntax for the enum which is
enumname.enumvalue
Option C is wrong because we cant add new values to the
enum after its declaration.
Now for the 2nd pick
Option D is wrong because of wrong syntax like option A
enumname.enumvalue
Option E is wrong again because purple cant be added or be
assigned as its invalid.
So option E is right.
Question 34
As soon as line 20 has been invoked,the format uses the
overridden method of the enum Title.
But first the constructor runs and then the format method
runs.
Question 35
It is a straight forward question for the new For Each for loop.
Here Anonymous Array has been used to initialize the object
obj[] array. Somearray takes only integers as obj[] array has
been casted with (int[]) type.

Question 36
A) Since beta is not static it cannot be invoked using class
name, it can be invoked
using reference variable
B) Since alpha is static it can be invoked using the class name
directly

Abhijit.A.Nayak

KCT,Coimbatore

C) static method (alpha) can be called from non static method


directly in the
same class (beta)
D) non static method (beta) cannot be referenced from the
static method
alpha)
Refer page no 144 and 145 for more information on static
methods.
Question 37
It is a straight forward question for variable arguments.
Question 38
JavaBeans Property Naming rules refer page 8,9
Question 39
Static Method should access the Static instance variables. So
the code does not compile.
Since the static method getInstanceCount cannot reference
the non static variable
counter the compiler fails to compile in line 6 saying that A
non static variables cannot
be referenced by static method. So option A is correct.
Refer page 142,143
Question 40
If the property is Boolean, the getter methods prefix is either
get or is. Refer page 9.The setter method should be void and
the getter method must consists the Boolean.
Hence option A and C are the right answers.

Question 41
The question here is about legal return types.
Refer page 123.
When option C has been used we know that Two IS-A One. So
it is allowed.(Co variant Return Type).
Option D is overriding the super classes foo() method so it is
allowed as well.

Abhijit.A.Nayak

KCT,Coimbatore

Question 42
A is invalid since the return type changes. D is invalid since
the modifier can be less
restrictive but it should not be more restrictive, so public and
protected is allowed
but private is not allowed. So B,C and E are correct.
Question 43
When overriding the overridden method can return the same
or sub type but not the
super type. Since in line6 of class C it is returning the super
type the compiler throws an
error.
Refer pages 122, 123 and 124 for information on
covariant return types.
Question 44
Refer page 239 and 240.The complier will always choose to
the older style than the never one to keep the code more
robust.
So option A.
Question 45
The option here is C. If we are overriding then the rules are no
changing the return type and the arguments .But If its
overloading then we have to change the Arguments as well as
the return type.
Question 46
This is inner class question.Refer page 641,642.
But the option B should be like this Line.Point p = new
Line().Point(); Guess there was a typing error.
To instantiate an inner class
MyOuter.MyInner myinner= new MyOuter().MyInner();
Or
If you already have an instance of the outer class then
MyOuter outterObjRef=new MyOuter();
MyOuter.MyInner myinner= MyOuter.new MyInner();
Question 47

Abhijit.A.Nayak

KCT,Coimbatore

The return type should be Line.Point because that correctly


retrieves a local instance of a Point object.so option A and C
are wrong.we should instantiate line inorder to get a new
instance and from that instance we invoke getpoint() method.
So option D is right.
Question 48
When Three is instantiated. calls the super constructors are
invoked because it in a built in call.So option C.
Question 49
Simple but tricky flow since the subclass invokes the super
class default or no-args constructor but
there is no such constructor in class Person it fails to compile
(it fails to invoke the super
class constructor in line 18). So option D is correct.
Question 50
Since the subclass constructor invokes the base class default
or no-args constructor and there is no such constructor it
throws compilation error so to stop this we need to insert
the statement given in option D. B,C and E are invalid because
since we cannot call constructor using the class name, we can
invoke the constructor using the this() or super() only.
Question 51
Follow the flow to know the out put . Remember that this() is
used to invoke the current class constructors this. is used to
access the current object variables.
Question 52
The answer is Compilation fails since the syntax for default
case is " default" not "case
default".
Refer page no 327 for valid example.
Question 53
Since there is no break statement, after the case collie the
flow of control goes to the next line and produces the output
as collie harrier

10

Abhijit.A.Nayak

KCT,Coimbatore

Question 54
The output is really simple,In line 12 , there is a method
invocation to line 18 which returns true.so the output is A.
Question 55
This is a Wrapper class example so its simple and easy. Both
the Wrapper class objects are converted to integers and
added.
Question 56
The format for if is if(---){ } elseif(---){}else{}
So compilation error.
Question 57
There cant be a finally if there is no try/catch.
Question 58
After the first iteration
x:1 y:9
After the second iteration x:2 y:8
After the third iteration
x:3 y:7
After the fourth iteration x:4 y:6
During the 5th iteration
First y5 x5
Then x value is checked and it exits the loop.
Question 59
The while loop never executes even once.hence the option is
C.
Question 60
This is an infinite loop.as its a do while loop, the source code
inside the loop executes once which makes x=9 from then on
it goes on looping.so the option here is D.
Question 61
To sort in reverse order it is not the correct way ,

11

Abhijit.A.Nayak

KCT,Coimbatore

to sort in reverse order we first need to apply


reverse method followed by sort method.
Refer page no 624 for correct syntax.
Question 62
LinkedList keeps the elements in the order of the index i.e
with insertion positions so the elements are displayed as in
the insertion order itself. (Dont confuse with the object
name sorted, it is just a trick but the LinkedList wont keep
the elements in sorted order)
Note: we can keep the elements in sorted order using TreeSet
and TreeMap
Refer page no 542 and 560 to know abt LinkedList and Lists
Question 63
Since i is declared inside the for loop it can be accessed inside
the for loop only, it is not visible outside the for block so the
compilation will fail in line 15. when accessing i the compiler
says that cannot resolve symbol i.
Question 64
To use to assertions first we need to enable assertions , for
this we need to use the ea or enableassertions so options A
and C are invalid. If we use C and F the condition will be true
so it wont throw any Error, If we use B and E the assertions
are enabled and the condition will be false (length!=1) so it
throw AssertionError.Option D is the correct syntax it wont
produce and error.Option F enables assertions only for the
test class and two strings test , file1 are passed as command
line argument.
Question 65
This is a simple assertion example. During java AssertStuff
the assert is skipped and the output is passed. During java -ea
AssertStuff since the value of the assertion is false ,
AssertionError is thrown.
Refer page 373.
Question 66

12

Abhijit.A.Nayak

KCT,Coimbatore

javac -source 1.3 Test.java makes sure that if assert is an


identifier.Refer pages 375,376,377.
Due to this when we use source 1.3 it complies fine but shows
warnings.anything above java1.3 the compilation fails.
Question 67
Assertions cannot be used to validate the public method
arguments
So line 26 ( assert (x>0)) is not legal because we cant use
assertions to validate arguments of a public method and
assert (dostuff4()) is also not valid because it can cause a
side effect as in it should leave the program as it is. A method
call inside the assert keyword is wrong syntax.Refer page no 380
for usage information of assertions
Question 68
When overriding a method the overriding method cannot
throw any checked exceptions or user defined exceptions(
user defined exceptions are also treated as checked
exceptions) that is not thrown by super class method. So the
compilation in line 2 of class B will fails.
Refer page no 99 for overriding rules.
Question 69
The option is D and E because when a.sayHello(john) is
invoked,it throws an exception which either has to be handled
by a try/catchOption E and or has to be declared in the
called method option D.handle or declare rule.
Question 70
If a null pointer exception takes place then the exception is
caught and then a is printed and the finally block executes.
Hence option D.
Question 71
In line 14 we are overriding the process() method.when we
override exceptions only narrower exceptions can be
declared.In class As process() method there is no
exception.hence compilation error on line 14.

13

Abhijit.A.Nayak

KCT,Coimbatore

Question 72
This is a Runtime Exception,it is an unchecked exception and
therefore need not be handled or declared unless you wish.
Therefore when ((A)new B()).process(); is called then
super.process(); is called in class A the exception need not be
declared or handled as it is an unchecked exception.In the
If(true) condition an exception is thrown which propagates to
to the process methods which rethrows it to the calling
method where it is handled and hence Exception is printed.
Question 73
Again the compilation error on line 19 because the calling
method should either handle or declare the exception. On line
12, throw new Exception(); is called which has to be handled
or declared in the calling method.
Question 74
Here the option is B because the calling method calls a
method which does not throw any exception in class B the
void process method is overridden without any exception.This
is a valid rule of overriding.Exception can be anything broader
or it simply not exist but is should not be narrower.
Question 75
The connection is made on line 85. On line 86 the exception
occurs which is handled via a catch loop. So the lines after 86,
dont execute.Which leaves the resource connection open
still.so option C is the right answer.
Question 76
The question is tricky once the a.method1(); is called and then
b.method2(); is called from there and c.method3(); is called
from there.Remember NullPointerException is a Runtime
Exception.So all three methods need not handle/declare it.
c.method3(); propagates to b.method2();a.method1();
which is the calling code and it handles the exception.so
option E is right. The exception is caught by it and the lines
inside the catch executes. So option B is correct too.
Question 77
Dont be tricked because the exception thrown by the
catch (TestException te) {
throw new RuntimeException(te);

14

Abhijit.A.Nayak

KCT,Coimbatore

} is a run time exception it need not be handled or declared


by the calling method.so option b.Remember runtime
exceptions are unchecked exceptions.

Question 78
When handling exception the subclass type catch block should
be first then the super class type should be last. (here
NullPointerException is subclass of Exception class) So
it wont compile, to compile it correctly we need to exchange
the lines 16 and 18
Refer page no 557 for more information.
Question 79
Remember Errors are like unchecked exception. It need not be
declare or handled. Refer page 363.Errors and Exceptions
share a common super class called throwable.here on the line
12. if (true) throw new AssertionError(); an error is thrown to
the calling code which is the main function. So the next line
13 in test() and the exception is not caught in main. Hence
nothing is printed.
Question 80
Exception is caught on line 14. But every time the finally runs
first and then the exception propagates to main and is caught
there.
Question 81
The flow is simple it is a runtime error which is an unchecked
exception. try { test(); } calls test()prints test and throws
new runtime exception which is caught exception is printed
because it is a superclass exception and runtime exceptions
need not be caught so it is handled here only. Thenend is
printed in main as it is already been caught and cause its a
runtime exception.
For question 82- please refer page 370 in the K&B text book.
Question 82

15

Abhijit.A.Nayak

KCT,Coimbatore

IllegalArgumentException is thrown when a method receives


an argument formatted differently than the method
expects.Hence option D.

Question 83
This is a NullPointerException.Null pointer exception is thrown
when attempting to access an object with a reference variable
whose current value is null. classB.getValue(); is a null value.
Because class Bs getvalue() method has to access class cs
getvalue() method inorder to get a string.hence it is a runtime
exception.
Question 84
java.lang.ExceptionlnlnitializerErrorThrown when
attempting to initialize a static variable or an initialization
block.
Question 85
StackOverflowError thrown when a method recourses too
deeply. 3 is passed in and the method is a recursive method it
calls itself and hence option A is right cause it count(); keeps
calling itself.
Question 86
Wrapper class Integer i is never initialized to zero
automatically. Hence we get a runtime exception of 0.where
as int x initializes itself to zero after compiling.
Question 87
This is simple.str+=42 means str=str+42;
Question 88
args[0] represents a String bur we are assigning it to Integer
it throws
NumberFormatExceptin when assigning a String to Integer.
The correct way to use is:
Integer i=(Integer)args[0]; or
int i= Integer.parseInt(args[0]);

16

Abhijit.A.Nayak

KCT,Coimbatore

Question 89
Option D:Since StringBuffer and StringBuilder methods are
same we can replace with no other changes.
Option A:Since StringBuffer is thread safe A is wrong
Option C:Since + is not overridden in StringBuffer ( and also in
StringBuilder) it is not possible.
Option B:Since StringBuilder methods are not thread safe it
increases the performance
Question 90
If we use string buffer instead of string builder we wont get
complete synchronized data. To get proper sync we should
make the method log synchronized.Option A is correct.
Question 91
Fred this is appended with 47.Fred is also lost because it is
appended with 47.47 is lost
S=Fred47 (1st one,this is also lost)
ed47
(2nd one,this is also lost)
ED47
(final one)
Question 92
Observe the following output sequence for both the outputs
b) 123456789.delete(0,3) results in 456789 ( observe the
second parameter of delete method is not zero based index, it
is 1 based where as the first param is zerobased) Now
456789.replace(1,3,24) = 424789 (same as above).
Now424789.delete(4,6)= 4247 (here also same thing first
param zero based n second
param 1 based)
e)
123456789.delete(0,3)=456789.replace(1,3,7)=47789
.delete(2,5)=47.insert(1,24)=4247
Refer 425,426 and 427 for the methods explanation
Question 93
When invoking method the matching is the priority is direct
mapping widening autoboxing varargs.So go(y) invokes
the int method by widening short to int since the direct

17

Abhijit.A.Nayak

KCT,Coimbatore

mapping is not available.go(z) invokes the Long version by


auto boxing to Long since the direct mapping and widening is
not available.
Refer page no 238 for widening , auto boxing and varargs
rules.
Question 94
Here the methods go(short s) ans go(Short s) both are same
in the sense of compiler so the compiler fails to compile
saying multiple declarations.
Question 95
This is quiet simple.Unless you carefully look into the question
you might not spot it.The directory object is created but not
the actual directory in the hard disk because
directory.mkdir(); was not called and hence this causes an
IOException during runtime.Refer page 440 for an example.
Question 96
Refer page 436. writing a line separator to the stream is the
only extra thing in buffered writer when compared to
filewriter.
Question 97
As per the Serialization rules the classes that are involving in
serialization also should implement Serializable interface but
here the class Tree is not implementing the Serializable
interface, so it throws an Exception when it is trying to
serialize the Forest class.
Refer page no 450 for similar example.
Question 98
When overriding writeobject() and readobject() we should
make sure than we should read the data in the same order as
we wrote it.
Refer page 451.

Question 99

18

Abhijit.A.Nayak

KCT,Coimbatore

For option E refer to page 453.


For option D refer to page 449.
Question 100
Since there is no problem in code and no complex flow, it is a
general flow. So the corresponding values will be printed.
Refer page no 444 for similar example
Question 101
If the super class in not implementing Serializable interface
when deserializing the class the super class constructor will
run.So the execution in object creation is lines 19,14,16
output 12.
When serializing no output.
When deserializing the Food constructor will run so output is
1
A the line 22 output is restored and size 42
So the complete output is 121restored42.
Refer page no 452, 453 and 454 for same example
Question 102
Refer page 451.
s.defaultWriteObject(); is used to normalize all the data in a
normal way.
Question 103
This is very easy. Refer pages 469,470.
(3.1415926); is rounded off to 4 decimal points
Question 104
Parse(input) is wrong parse(numbers only) same with
option D
So equals is wrong cause it does not make sense in formatting
problems.
So option B is correct.
Parse() method takes a String formatted in the style of The
DateFormat instance.it coverts the string to date
Refer page 465
Question 105

19

Abhijit.A.Nayak

KCT,Coimbatore

This is simple .Refer page 471 and look at the valid instance
creation options.
Question 106
There is not method such like that setLocale. To set the locale
we need to create a
Locale object using the following syntax
Local locItaly=new Local(it,IT) and format() method is
available in
DateFormat class and it is not available in the Date class.
Refer page no 466,467 and 468
Question 107
getLocalTime() and setLocaltime() are invalid . so option C &
D are wrong . getTime() & setTime() are correct.
Option B is right because the static method used to get an
instance of date format is getDateInstance();
Refer page 461 for date and Refer 471 for DateFormat.
Question 108
Parse() method takes a String formatted in the style of The
DateFormat instance.it coverts the string to date
Refer page 465.Parse() must be placed inside a try/catch
block.Refer page 470.
Question 109
Sorry I dont know why the compilation fails, here test.split
and tokens.length both are correct but may be problem with
\s may be it should be \\s.
Refer page no 475 and 486 for some information.
Question 110
split(\\d ) splits the string by separating with the a digit.
So the tokens array becomes tokens={a,b,c}
Refer page no.s 475 and 486 for tokenizing the Strings.
Question 111
String regex = \\.\\s*; means the regular expression is .
Followed by a white space character s.

20

Abhijit.A.Nayak

KCT,Coimbatore

Question 112
%d is printing integer values but Math.PI returns double so it
throws NumberFormatException.
Refer page no 489, 490 and 491 for information on formatting
Question 113
For retrieving the next integer first we need to test with
hasNextInt and then retrieve using the nextInt. If we are not
checking with the hasNextInt it refers to the string only and
throws parsing exception.
Refer page no.s 477 and 489 for explanation on Scanner class.
Question 114
A separate thread will be created only when we call start
method on a thread object. So in the given options we are
calling the start methods in D and F only.
Question 115
Calling run method wont start a new thread and wont throw
any error or exception, since calling run() is similar to calling
any other method so it is treated as just calling run()
method two times but it doesnt start the thread.Calling
start() method makes the t.run(); t.run(); run . and then start
calls run();
So the run() method will be executed three times and out put
is runnungrunningrunning.
Refer page no 682 for the explanation on run() method.

Question 116
Exception is created because t.start(); is called twice.So a
runtime Exception is created. Refer Page 730 3rd point in the
two minute drill(objective 4.1)
Question 117
It is an example for anonymous inner class and it is following
all the rules for a thread implementation that is creating a

21

Abhijit.A.Nayak

KCT,Coimbatore

thread object by passing Runnable reference, overriding the


run() method and invoking the start method on the thread
object. So the output is bar.
Question 118
Run() method in the Thread t s anonymous class creation gets
overridden and so Dog is the Right answer.
Question 119
In the main method x which is initialized is not shown
because it is a non static variable.
As soon as new Starter().makeItSo(); is called. First the
constructor is called.
Here x=5;
From here the start(); is made which calls the run();
Here x *= 2;
Hence x=10 inside run
Only now the method makeitso is called after the constructor
and run(from start) method calls are completed.
Here the x is x=x-1
So the answer is 9.
Question 120
Here the target for the thread t is a new instance of the class
thread2 which implements runnable.When t.start(); is invoked
run is printed for sure and Runtime exception is caught and
then End of method is also definitely printed.the order is
decided by the JVM.

Question 121
Since we are declared main as the throws Exception it wont
create any problem it compiles and runs successfully, so the
out put is sleep after minimum of 3000 secs.
If the main is not declared as throws Exception we need a try
catch block since sleep method throws InterruptedException
otherwise the compilation fails.
Refer page no 693 and 694 for more information.
Question 122
The possible results are

22

Abhijit.A.Nayak

KCT,Coimbatore

Started0123Completed and 0123StartedCompleted


(Ofcourse other possibilities are
Started123Completed,01StartedCompleted,012StartedComple
ted)
First output: Main thread (the thread which is executing the
current program) executes first and prints Started but bcoz of
join method it waits till thread completes so thread t
will be executed and prints 0123 after t completed it joins
with main thread now main thread continues and prints
Completed.
Second Output: t thread starts and prints 0123 and main
thread prints Started and since Thread t is already completed,
main thread continues and prints Completed.But the
Completed will not be executed until t thread is completed
bcoz of join.
Refer page no 698 and 699 for explanation.
Question 123
Main Thread Thread1 Thread2
First StartMain is called in the main thread
Thread1 is executed start1 is printed.
In Thread1 Delay(6000) is executed.
Then first delay(1000) in the main thread is executed.
Then 2nd delay(1000) in the main thread is executed.
Then 3rd delay(1000) in the main thread is executed.
Then EndMain from thread1 is executed
End1 is printed from Thread1.
Then Thread2 is executedstart2 is printed.
In Thread2 Delay(2000) is executed.
Then thread2 is interrupted in main(); t2.interrupt();
So Exception is created in Delay(2000) and caught and Error
is printed.
End2 is printed from Thread2.
This took me one hour to decode properly with lot of
modifications to trace the Thread.This is the way it should be
traced.Hopefully I am right :-) .
Question 124

23

Abhijit.A.Nayak

KCT,Coimbatore

The rule for synchronizing threads are keep instance variables


private and method accessing them should be named public
and synchronized keyword should be present.
Question 125
The option is A & C.
Compilers give different output mine gave me 0,0,2,2,4,4,6,6,
What we have to figure out is 0, 2, 4, 4, 6, 8, 10, 6,
In option A . Say one thread executes the loop and prints is
0, 2, 4, and pre-empts and then second thread uses that value
4 which was the latest and prints 4, 6, 8, 10. remember in this
sum what we have to realize is only 8 numbers are printed
and only in the ascending order. Then 6 is printed by the first
thread.
Option B: 0, 2, 4, 6, 8, 10, 12, 14.This is when the x value is
being increment until the loop is completed by thread one 0,
2, 4, 6 and then it continued to increment by 2nd threads loop
8, 10, 12, 14
Question 126
This is a tricky one.Option E is correct Because A,B,C should
run thrice each once.
Dont know about option G

Question 127
Unless a synchronized on the (This) is not made there wont
be a lock on the current object accessed by the thread.Which
means X can be changed randomly.So Option D is a must.
Option A is a must because if we place the X inside a
synchronized method it will be better to get the lock on the
object.
Question 128
The synchronized keyword can be applied in two ways..i.e for
method and block. When it is applied to method there is no
need to pass object and when it is applied to block we
must pass the object on which object we want to get lock

24

Abhijit.A.Nayak

KCT,Coimbatore

A)Incorrect bcoz we cannot apply synchronized keyword for


any variables
B)Incorrect bcoz we are not passing the object for the
synchronized block
D)Incorrect bcoz there is no need to pass object when we are
applying it to method
Question 129
Static variables should be accessed from static methods in
order to be thread safe.option E is right.
Question 130
doTransfer(r1, r2, 5); r1 = 5,r2=15
doTransfer(r2, r1, 2);r1= 7 r2= 13
doTransfer(r1, r2, 1); r1=6,r2=14
These are the 3 options which are the possible outputs but
The code may deadlock (without completing) with no output
is also correcting as a can be waiting for b and b can be
waiting for a

Question 131
No syntax errors and exceptions, so follow the code to know
the output
Question 132
Wait and Notify should be applied for locking the object but
not thread.Here we are passing the Thread but not the object .
Correct examples are synchronized( this ) locks on the current
object,synchronized(MyClass.class) locks on the MyClass
object. But in this we are passing the Thread object.
Note: A thread acquires the lock on object.
Refer page nos 707,708,709
Question 133

25

Abhijit.A.Nayak

KCT,Coimbatore

wait is applied on bar object so notify or notifyAll should be


invoked on bar object, but there are so many threads so the
better way is to use the notifyAll . So bar.notifyAll() is correct.
Question 134
As the encapsulation rules A & D are correct.
Refer page no.s: 82, 83 and 84 for encapsulation rules.
Question 135
Here the string is public but the class is default . Since the
class has package level access u can access the String only in
the same package.
Question 136

A is invalid since i++ is different from ++i so it changes


the output.
B is invalid since i=1; differs with i=0; when initialization
and in output
C is invalid since if it is private it cannot be accessed in
Client class.
D is the correct way which uses the autoboxing Integer
i=0;In ++i it is unboxed and incremented.
Refer page no.s 234 and 236 for information.
Question 137
Base is having public modifier but the class is having default
modifier, so the class can be accessed only within the package
hence the variable base can be accessed in the same package.
Refer Q. No : 21 for same example
Question 138
A It is correct because p0=p1, Class B extends Class A.
All that is in Class A can be accessed by Class B.
BIt is wrong because Class C and Class B extend Class
A.
So there will be methods that are different in Class B &
Class C which cannot be the same.
CThe Return type of p4 is Class A but Return Type of p2
is Class C. They cannot be assigned together.
DIt is wrong because p1 cant be cast to the type ClassB
Ep1 can be assigned to a variable which is cast to
ClassB because both can access only the Class Bs data.

26

Abhijit.A.Nayak

KCT,Coimbatore

FSame as option E.

Question 139
Animal animal = new Dog(); is a dog object which is of the
type Animal.It can only access the Animal Methods,
cat.makenoise(); cant be accessed because it is a dog object
originally.
Question 140
This is a simple but tricky overriding problem, All three are
RaceCar() objects but refer to each of the classes. Hence all
the methods have been overridden successfully.
Refer Page 100,101 for examples
Question 141
z.c1(); Class A does not have c1() method.
y.c1(); technically it is correct. Y instance of class C can
access Cs method.Dont know why it is marked
wrong.probably because the question was different.
All the other Examples are correct because of Simple
overriding rules,Inheritence and Abstract methods,Classes.
void y(); should have been declared in the interface.
Question 143
[lines 24 and 25 are a.x(); a.y() ]
When invoking a method with super class reference variable
the method should be declared in super class otherwise it will
be failed to compiles. ( Even though at run time it invokes the
subclass method).
Since here y method declared in B but it is not declared in
interface A, so the compilers checks for that and the
compilation fails. (If y() is declared in A then at runtime it
invokes the y() method of class B only but at compile time it
checks in A since we are used the reference variable of type
A)
Refer page no 100 (last para) for explanation.
Question 144
Class E overrides bMethod{} even though a return cast type
of Class D is present only the Overridden version of the Class
E is exectuted.

27

Abhijit.A.Nayak

KCT,Coimbatore

This is an Example of UpCasting Refer page


114.Dog,Beagle,Pet example on 115.
Question 145
Option A,E it is wrong because Class C does not Implement
the InterfaceA.
Other Options are correct Because Either ClassB or InterfaceA
have declared/implemented the method public int getValue();
Question 146
It is a call from Subclasses constructor to Superclasses
Overriden Construction(Anything which is other than NoArgument Constructor is Overridden).
Question 147
Based on the class name the output:
Base.FOO = foo (since it is static Base.FOO works)
Sub.FOO = bar
(same as above)
b.FOO = Base.FOO = foo
s.FOO = Sub.FOO = bar
((Base)s).FOO) = (Base).FOO = foo
(If the FOO is not static then the last result is bar
since the object is of type Sub).
Question 148
A) final and abstract keywords cannot be applied at a time a
method, since the abstract method must be overridden where
as final not allows to override
B) Since protected method is visible in any subclass of X it can
be overridden in any subclass
c) Static variables can be accessed from static as well as
instance methods there is no such restriction
D) A final method cannot be overridden in any subclass
whether it is static or non static or public
E)Since static variables are class variables we call a static
method without creating an instance for that, just we can call
it using the class name itself
F) Once it is declared as private it is not visible in any of the
subclasses so if we override the method it is not actually
overriding, it is simply just new declaration.

28

Abhijit.A.Nayak

KCT,Coimbatore

G)Protected Member can be accessed through inheritance by a


subclass even if the sub class is in a different package.so it is
wrong.only default should be in the same package.
Refer page no 99 for more information on Overriding
Question 149
Are you confused with a long codedont worry just observe
the addTopping method in
line 3. It is final so it can be overridden in sub lass i.e in
Pepperon1. So the compilation
fails in line 8.
Refer page no 40 for information on final methods.
Question 150
The variable a is private in class Super so it is not accessible
in class Sub and there is no default constructor in class Super.
So we need to make the changes accordingly and they must
be done independently.Options C and D are correct to change
the code independently (if it is togetherthere are other
options also correct).
Refer questions 28 and 122 for similar example.
Question 151
Since the methods are overridden , the execution of the
method depends on the current
object but not on the reference type and super.method
invokes the super class method.
So the execution of the statements is..
MultiCalc Line 8
MultiCalc Line 9
MultiCalc Line 10
MultiCalc Line 3 now multiply = 2
MultiCalc Line 4
MultiCalc Line 2 now value= 0 - 3 = -3
MultiCalc Line 5
SimpleCalc Line 3 value= -3 + 7 = 4
MultiCalc Line 6 value= 4*2 = 8
MultiCalc Line 11 Output value is 8
Question 152

29

Abhijit.A.Nayak

KCT,Coimbatore

To call or invoke other constructor in the same class we need


to use the this() but not the class name. So the compilation
fails.
Question 153
This() calls the no argument constructor of the same class.
So option a,b is ruled out.
Super() without argument is wrong cause in superclass
constructor has been overridden with an argument.
So option C is wrong.
Option F is wrong because wheelcount is private it is not
inherited.
Question 154
This is a simple question . Super() constructor and assigning
the variable with this operator.

Question 155
Aoverriding
Binvalid overriding
COverloading (long argument)
Dinvalid override return type cant be changed.
EOverloading ( long argument)
FOverloading
GOverloading
Question156
this.a +=5 makes class Bar integer a to be incremented to 13
Foo foo = new Bar(); is an object of type Bar but which refers
to Foo. So in the print statement foo.a returns the Foos a
which is 3.
Question 157
super.multiply(a, b); here is wrong. Super(); or
super(arguments) are correct superclass constructor calls .
we cannot call the super classes methods using a constructor
call method super(); so compilation error.
Question 158

30

Abhijit.A.Nayak

KCT,Coimbatore

The code with compile because there is no error in the syntax.


It is not a proper design of is-a Relationship cause we are
extending from java.util.Linkedlist we are not overriding and
cannot override any of the linkedlist methods,we are just
using them.add(p);
There no has-a relationship .Refer page number 92.
By default inheritining java.util.Linkedlist also allows us to
remove from the list.
Question 159
Option ahas-a can be encapsulated.
Option b has-a can be implemented using interfaces
Indirectly
Option Factress is not a movie

Question 160
Option CInterfaces can be used , not a must
Option Dinheritance represents extends,implements which is
is-a relationship

Question 161
BCat implements Jumper
CDog extends Animal
FTail tail; this is default access so Beagle can access the tail
So Beagle has a tail
GBeagle implements Jumper is a
Question 162
A treeset is a sorted set by nature. Its output will be in
ascending order.Order or insertion or latest access is not
considered.
Question 163
Poll() retrieves the head element and removes it from the
queue.Peek() retrieves the element at the head of the queue
but doesnt delete it .

31

Abhijit.A.Nayak

KCT,Coimbatore

Question 164
Since ws1 ans ws2 are user defined objects they refer to the
different objects. But after creating s1, s1 will be placed in
String pool, when s2 is created the JVM sees that same
object is existing so s2 also refers to the s1 So s1 and s2 both
are fully equal. As per the HasSet rules only one object will be
taken the duplicate object will not be added. So in
total 3 objects will exist in the list ws1,ws2,s1

Refer page no 562 for more information


Question 165
The set variable contains only three elements from the coil
collection, but the order is NOT guaranteed to be
preserved.set s dont take in duplicates and the elements
which are added inside are not in the same order.they are
randomly inserted.
Question 166
public int compareTo(Score other) {/*more code here*/} is
correct because Comparable<Score> we should take an
argument of the type youre sorting.Refer page 552.
Question 167
add(0,object), is used by the list interface method Refer page
569.Random Access is used by Vector and ArrayList types of
list.
So option D is correct.
Question 168
hashCode(), hashCode()/2 and hashCode()/2-age*3 all
return same value for few of the

32

Abhijit.A.Nayak

KCT,Coimbatore

elements, but hashCode()+age*7 returns diff values so


options B is correct.
Try with example values for clarifications.
Refer page no 533 for explanation on hashCode legal return
values
Question 169
Hashcode and equals should be overridden.Equals must take
in an argument of type Object.
Question 170
To the hashset during compilation wat matters is there should
not be any duplicates.creating an different objects of type
person and adding it to the set is allowed regardless of
checking if the name is the same or not. The equals method
does NOT properly override the Object.equals method.
Question 171
The equals method does NOT properly override the Object.equals
method. The syntax of equals method is wrong, since equals
takes Object as param but not the
user defined object, so it throws compilation error. The correct
syntax is public Boolean equals(Object o)
Refer page no 526 for more information on equals method
Question 172

Options C and E are correct as per the hashCode implications.


Refer page no 534 and 530 for hashCode implications.
Question 173
Option A is correct since both are always equals since it enum
by default implements
Comparable
Refer page no 60 - 63 to know more about enums
Question 174
Set cant take duplicates, so only one set.add(k1); set.add(k2);
works.
The size of the set now is 2
k2.i = 1;
nothing affects the set cause k2 is not used in the next print
statement

33

Abhijit.A.Nayak

KCT,Coimbatore

output is again 2
k1 is removed now k1 which is stored has a value of i=1 so
it is checked using equals and also hashcode. And successfully
removed
output is one
k2.i=1 makes sure that k2s i is not removed from the set
collection because while equals method and hashcode method
checks it equals return false and hashcode returns i=2 for
k2. hence output is one.
Option F is correct.
Question 175
Option AGet(index) is used only by list
Option Biterator is used by list and set.
Option Cit is an method which returns an array containing
the elements of the collection.but the return type is object not
a collection.
Object[] toArray(); (note there are no arguments in
toArray();)
Option D iterator is used by list and set.
Option E it is an method which returns an array containing
the elements of the collection.but the return type is collection.
T[] toArray(T[])
Option Fiterator syntax wrong. Should be only
(Iterator(return type)) iterator();
Question 176
B is not valid since the type is not specified.<?> means we
cant add anything to the collection.it means anytype.refer
page 595.what we are looking for here is of type object.
C is not valid since the method return type is Object but we
are receiving it to String without casting
A is valid since we are passing legacy list to list and return
types are same.
D is valid since Specific type to legacy is allowed and return
types are matching.
E is valid since Specific type to legacy type is allowed and we
are type casting

34

Abhijit.A.Nayak

KCT,Coimbatore

the return value.


Refer page no 576 for more information on
mixing generics and legacy code.
Question 177
This is a simple example.inputs 0042 and retrives and prints
it.If you dont know this please read Colletions again.Make
sure you know
add() method is used by set,list
remove() used by all
put() by map
get() by list and map
contains()list,set
ContainsKey or ContainsValue() Map
Iterator() Set,List
These are the important interface methods refer to page 569.
Question 178
Since with the generics implementation there is no need of
explicit type conversion from
wrappers to primitives line 14
And to make the code easy
we can remove the complex for loop and can be replaced with
the enhanced version.
List<Integer> intList makes sure only Integer List is created.
options A,C,F are enough to use generics.
Question 179

Option A it is wrong because any list cant be assigned


to The type List<A>
Option BList<B> cant be assigned to List<A> cause
<B> might have methods which A doesnt. it should
always be List<A> a = b; where b = List <B>
Option CList<Object> and List<?> when equated
means the same.
Option D Class D extends B so it can be assigned.
Option EList<A> cant be assigned to anything like
List<? Extends A> because that class which extends A
will have extra methods,variable which Class A wont
have.

35

Abhijit.A.Nayak

KCT,Coimbatore

Option FWrong it should be assigned only to


List<Object>
Option GList<? extends A> means Class B,C and
List<? extends B> means Class D. so it can be assigned.

Question 180
D is correct because List<? super String> means any type
that is a super class of String or of Type String. So it
accepts objects too.

Question 181
Double forms 123.000 for option D and C .but for option A
and C integer types are only passed in.Option A it is passed in
as a string but it is mutually comparable as the collections
dont see what has been stored it , it just checks if it follows
the syntax.
Question 182
If we use the "?" place holder it takes any type object but it
not allows to add the objects to the list so the options A,B and
E are wrong (since we are having add method).
If we use C it allows all types of objects so it also not valid.
If we use D it allows only the numeric type objects i.e Integer,
Float, Double, Short ,Long
and Byte (these are all the subclasses of Number class)
If we use the F option it allows the Integer types i.e Integer,
Short Long and Byte.
Refer page no 592, 596 and 610 for more information on ? and
? extends
Question 183
Hint 1 :generic class MinMax so all options are possible
Hint 2:Type parameter of MinMAx
all options are possible because type is E
it should implement Comparable . Comparable is a Class not
an interface that means E extends Comparable<E>
so option B,D is ruled out
From Option A & C

36

Abhijit.A.Nayak

KCT,Coimbatore

Put is used to add elements to the collections and the return


type <E> may be normal.but look at the instance variables
<E> E min . That is wrong . it should be of the form E min .
Refer pages 598-602 for making our own generic class.
Question 184
one.name= Coffee;
two.name= Tea;
these two lines makes sure that when they are added to the
set what happens is compare(object o) return a zero with it
and all the objects which are of type Drink are assumed to be
the same so while iterating over the collection we get Coffee
again n again.
Question 185
reverseOrder(); method returns a Comparator that sorts the
reverse of the collectionss current sort sequence.
Its synyax is static Comparator reverseOrder(Comparator)
Refer page 568 for the syntax.
So the sort method takes the syntax which is shown below
Static void sort(List, Comparator);
So option D is correct.
Question 186
Option B is correct because asList(T []) is the correct
syntax.there must be an argument of the list.Refer page 568.
Question 187
Searching an array or collection that is not sorted is a big
gotcha.We can also use the comparator in either the sort or
the search.Refer page 558.
Question 188
The code is correct and when sorting spaces will come first
then caps and then smalls
so the output is C.
Refer page no 556 and 557 for sorting information
Question 189
C is right because Comparator when not used inside
binarySearch() method we get an undefined answer.

37

Abhijit.A.Nayak

KCT,Coimbatore

For other rules refer to question above this.


Question 190
Keyset() returns the keeps in a Map.
In order to sort this we use option B because sort method in
option A and C are used only with lists.not with maps.
Option D is wrong because there is nothing called the
Sortedset.
TreeSet() by default is a sorted set. So that is the correct
answer.
Question 191
If ur not importing the package which u want to use for a
method or a class the only way is to use the full name in this
example the full name is
packagename.classname.methodname i.e
Utils.BitUtils.process(bytes); The option E seems to be correct
but in the given question u can not use the import statement
in the middle of the program.
Refer Page no 769
Question 192
private static void process(byte[] b) { } in the BitUtil class so
we cant access the process In the other package.so option F
Question 193
String getText() is got a default access, so it cannot be
inherited to other packages.only protected and public through
inheritance.
Question 194
Only if we make the consume() method public in salomon.java
we can access it is griskly bear.option A is correct
We need not use fully qualified name look at the code we
are making an instance of the Saloman class so option D is
correct.if we didnt make an instance we should ve picked
option E
Question 195

38

Abhijit.A.Nayak

KCT,Coimbatore

Static import is used here to use Twice method .Twice method


is static and when we use the fully qualified name it can be
accessed easily without typing much code.
Refer static imports in chapter 10.
Question 196
The line in 20 is treated as s= s+ (EASY++ + s);
Since EASY is final it can not be incremented so it fails to
compile.
Question 197
The flow is correct. So option A
Question 198
Args is an array. Arrays have the variable length.but strings
have a method lenth();
So option B is correct
Question 199
The current directory is /foo/ and we ve have used a fully
qualified name henceforth we dont need a classpath.
Hence option B
Question 200
This question looks very deceptive and correct. Look
carefully.dostuffX is not in main. We get an exception
Exception in thread "main" java.lang.NoSuchMethodError:
main
Thrown if an application tries to call a specified method of a
class (either static or instance), and that class no longer has a
definition of that method.
Normally, this error is caught by the compiler; this error can
only occur at run time if the definition of a class has
incompatibly changed.
Question 201
java Yippee

39

Abhijit.A.Nayak

KCT,Coimbatore

no arguments passed nothing is printed.


java Yippee 1234
for(int x = 1; x < args.length; x++)
see this x starts with 1 args[] is zero based. So
234 is correct.
Question 202
Same explanation as the previous one
java Yippee a b c
for(int x = 1; x < args.length; x++)
see this x starts with 1 args[] is zero based
so b , c
Question 203
D,E follow the correct syntax.
See page 764.
Question 204
A is correct because it follows the current syntax.
Question 205
It is a general flow for the program. It seems the modifyDesc
is modifying the desc of it (in lines 6,7,8) but actually it is
creating a new object which is local to that method and
assigning the desc to the item desc. So the output is
GobStopper FizzyLifting.
Question 206
When the object is created the constructor assigns the value
42 to id.But in line 14 using the method we are trying to
modify the value so it throws fails to compile since final
variable cannot be modified. this.id in line 13 and id in line
both refers to the id declared in line 12, (since we are having
one more local variable in the constructor with the same name
we are referred
it using this in line 13).

40

Abhijit.A.Nayak

KCT,Coimbatore

Question 207
Substitute the options in the questions and write down the
flow u will get the correct options as B,C,F.
Question 208
The flow is
Foo foo = new Foo( 300); 300
Foo fooFoo = fooBar( foo); 100
foo = fooBar( fooFoo); 100
Question 209
Since intObj is added to the numbers objects, it is in use by
the numbers object till line 18. So it is becomes candidate for
garbage collection in line 19 only ( it is not used by
any thing) ( See the numbers object is declared inside the
method the scope of thatobject is inside that method only so
outside the method it is not accessible, and intObj life is inside
the for loop only but it is in use with numbers object so it is
not eligible for garbage collection there in line 17th or 18th )
Refer page no 248 for similar example
Question 210
This is the direct way to request garbage collector for garbage
collection .For garbage collection we can use System.gc() or
Runtime rt = Runtime.getRuntime(); rt.gc();
Refer page no 251 and 252 for more information
Question 211
On line 22,snooch and booch are created with the help of
snoog.when snoog is referenced to null,the snooch and booch
are useless here after.They are not referenced by snoog and
they left on the heap without any purpose.so they are eligible
for the garbage collection.
Question 212
5. Object o = new Object();

41

Abhijit.A.Nayak

KCT,Coimbatore

6. doSomethingElse(o);
Here new object is created and passed to the line 6.
7.o = new Object();
here again new object is created and refered to variable oof
type Object .
8. doSomethingElse(null);
Now when null is passed through then the object o on line
5 will be refered to null. Not the new object created on line 7.
Hence on line 8,object o on line 5 becomes available for
garbage collection.
Question 213
Refer page 253 on finalize() method.
For any given object,finalize() will be called only once
(atmost) by the garbage collector.
Calling finalise() can actually result in saving an object from
deletion.
Question 214
The correct syntax for using jar file with class path is
java/javaC -cp JarName.jar ClassName.java
so from this we get option C & F to be correct.
And inorder to type the command :java games.cards.Poker

SEE THE IMAGE IN THE NEXT PAGE.


Now we have to move the Poker.jar to the stuff/java only
then we can access like
java cp /stuff/java games.cards.Poker.
This is the correct way to do it.
Option is C.

42

Abhijit.A.Nayak

KCT,Coimbatore

Question 215
This question is very easy.just jolt down the path names and
the fully qualified names to classes and draw directories , the
option is jar A.
Question 216
By default the compiler will search for the directory tree
named jre/lib/ext.The jar files hold all the locations to the file
and Book can access paper from the jar file.
So option B is correct.
Option D is correct because it follows the correct command
which includes the classpath environment
variable/foo/myLib.jar.

43

Abhijit.A.Nayak

KCT,Coimbatore

Option E is wrong because syntax for using jar file in


classpath is wrong.
Option C is wrong because environment variable is placed
wrongly.
Option A is wrong because the complier does not search there
by default.
Option F is wrong because d is used which is originally used
for telling the compiler to put the class files in a particular
directory.
Option G is correct because
Jar file is placed in /foo/myLib.jar this has access to
Paper.java
Classpath environment variable is also /foo/myLib.jar and the
file to be compiled is Book.java.
Question 217
OptionA
It is wrong because

44

Abhijit.A.Nayak

KCT,Coimbatore

If we run mainclass from apps/


It cant access the mainclass
Option B is correct because
com.company.application.MainClass can be accessed from
apps
Option C is correct because java -classpath /apps
com.company.application.MainClass if run
from any directory,we are directly accessing the mainclass.
Option D is wrong because it can search for directories inside
application as well if that has a MainClass then there will be
name collision.
Option E is wrong because we are searching from
/apps/com/company/application and t he current directory
which is /apps.
Option F is wrong because
com.company.application.MainClass doesnt exist from
application directory . check the figure above.
Question 218
The class file chess must be either in the subdirectories of the
test or
Jar file inside test:/home/bob/downloads/*.jar games.Chess
When the classpath environment variable in the command has
been invoked nothing shows up .so it should be in the test folder
somewhere.so test folder is in either option A or option C .
After that even I dont know the logic sorry.

Question 219

Since str is receiving the value null and we are applying the
length() method on the null object it throw
NullPointerException..
Question 220
In line 13 usage of if condition is wrong (it is correct in
c/c++). In java if condition takes only boolean expression it
may be boolean value or an expression which results in

45

Abhijit.A.Nayak

KCT,Coimbatore

Boolean. So the compilation fails.Refer page no 320 321 and


322 for more information
Question 221
I2 is a type Integer(wrapper class). but startingI is an
primitive integer.so the == test fails
Their values are equal because equals classes take only object
inside. So it is true.
Question 222
It refers to a cup but it is an object of poison cup
Cup cup = new PoisonCup(); see this.
Question 223
Simple example for conditional operator. Since
elements.length >0, the Condition becomes true and the
elements[0] is assigned to first.
Question 224
if (value || (setting && title == Hello)) is wrong . value is
an integer ,title is a string .
ALL THE BEST !

46

You might also like