Win a copy of Head First Android this week in the Android forum!

Sachin Kapoor

Greenhorn
+ Follow
since Jul 01, 2007
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Sachin Kapoor

I have two doubts

1) I thought the following statement would be illegal but it isn't:
double d = 0x12345678;
The K&B book says nothing about double variables holding hexadecimal values. It just says, quote

All three integer literals (octal, decimal, and hexadecimal) are defined as int by default, but they may also be specified as long by placing a suffix of L or 1 after the number


If double can hold hexadecimal values, shouldn't the Double (wrapper) class's valueOf method be overloaded to take base/radix as argument?

2) My second question concerns this:

What letters get written to the standard output with the following code?
class Unchecked {

public static void main(String[] args) {
try {
method();
} catch (Exception e) {
}
}

static void method() {
try {
wrench();
System.out.println("a");
} catch (ArithmeticException e) {
System.out.println("b");
} finally {
System.out.println("c");
}
System.out.println("d");
}

static void wrench() {
throw new NullPointerException();
}
}

Select all valid answers.

1. "a"
2. "b"
3. "c"
4. "d"
5. none of these

The answer is 3 (only "c" is printed), but my question is that since NullPointerException is a RuntimeException, it doesn't have to be caught. So shouldn't the output be "a", "c" and "d"?
[ September 01, 2007: Message edited by: Sachin Kapoor ]
My question is why the following statement: System.out.println(stringbuffer.equals(string));

returns false (assuming stringbuffer and string are valid StringBuffer and String objects respectively). Instead, shouldn't it give a compiler error because the equals method in StringBuffer performs == operation, and == gives compiler error when objects are of different type?
The question is which line is first to cause an error?

1 class Char
2 {
3 public static void main(String arg[])
4 {
5 while(false)
6 {
7 System.out.println("Hello");
8 }
9 while(false)
10 {
11 }
12 do;
13 while(false);
14 do
15 {
16 ;
17 }
18 while(false);
19 }
20 }

Choices are
1. Line no. 5
2. Line no. 9
3. Line no. 12
4. Line no. 16

Answer is Choice 1 is correct. It will give you error for unreached statement. All other statements are valid.

Does anyone know why exactly does line 5 give an error? If there is some code inside a while(false) loop, does it always give an error?
Gotcha. So myMethod in subtype does not override but is unrelated to myMethod in supertype. If access modifier for myMethod in supertype is default (no modifier), and the two classes are in the same package, then the two methods are related and it's an override.

Originally posted by marcelo ribeiro:
Hello,

From Kathy's book page 30:



so at line 8 you're free to add either protected or public modifier.

Thanks

Question is: Given the code below, and making no other changes, which access modifiers (public, protected or private) can legally be placed before myMethod() on line 3? If line 3 is left as it is, which keywords can legally be placed before myMethod on line 8?

1.class HumptyDumpty
2.{
3.void myMethod() {}
4.}
5.
6.class HankyPanky extends HumptyDumpty
7.{
8.void myMethod() {}
9.}

Options are

A. private or nothing (i.e. leaving it as it is) on line 3. Nothing(i.e. leaving it as it is) or protected or public on line 8.

B. public or protected on line 3. private or nothing(i.e. leaving it as it is) on line 8.

C. nothing(i.e. leaving it as it is) or protected or public on line 3. private or nothing(i.e. leaving it as it is) on line 8.

d. None of the above.

The correct answer is A. However, I am confused because placing private on line 3 means a private method is being overridden, which is not possible. So how can A be correct?
That solves it. Thanks!

Originally posted by Jesper Young:
About the second question: For which version of SCJP / Java was this test?

There is indeed an interface Queue, but it was added in Java 5. So if this test was for Java 1.4, then Queue would have been a wrong answer.

I took four of Dan Chisholm's comprehensive tests. I got 62%, 55%, 48% and 59% on them. I think they are very hard and not representative of actual SCJP exam.

Originally posted by Jagjit Dhaliwal:
Yes I paid by card and dont remember exact price ...something like odd 700 bucks ..
HAS ANYONE GONE THROUGH DAN CHISHOLM'S TEST? Please share your results and experience with it.

Thanks. That solves 1 problem.

Originally posted by Henry Wong:


The wait() method is defined in the Object class. This is probably the piece of knowledge the question was trying to test.

Henry

I got these two wrong.

Which of the following statements are true. Select the two correct answers.

1. The wait method defined in the Thread class, can be used to convert a thread from Running state to Waiting state.
2. The wait(), notify(), and notifyAll() methods must be executed in synchronized code.
3. The notify() and notifyAll() methods can be used to signal and move waiting threads to ready-to-run state.
4. The Thread class is an abstract class.

I chose 1,2. The correct answer is 2,3. Why is 1 wrong? wait method causes thread to go from Running to Waiting state, doesn't it? Why is 3 right? notify method causes only 1 thread (not all waiting threads) to go from waiting to runnable, right?

-------------------

Which of these are core interfaces in the collection framework. Select the one correct answer.

1. Tree
2. Stack
3. Queue
4. Array
5. LinkedList
6. Map

I chose 3. The correct answer is 6. Queue extends from Collection, doesn't it? While Map does not. So why is 3 wrong and 6 right?

Originally posted by marc weber:
"An inner class is a nested class that is not explicitly or implicitly declared static." Ref: JLS - 8.1.3 Inner Classes and Enclosing Instances.

Does Exception inherit from Exception?



Then it's ok. K&B define static class (including method-local class declared in static method) to be inner class, so that got me confused.
First Java rule roundup question: Is this statement true or false: Inner class can access all members of outer class

Java rule roundup says 'Yes'

I say no because static inner class cannot access non-static members of outer class

-----------------------
Second Java rule roundup question: All exceptions inherit from ____ ?

Java rule roundup says Throwable

I say Exception because all RuntimeException and checked exceptions are subtypes of Exception
[ August 17, 2007: Message edited by: Sachin Kapoor ]
Of course! Thanks. It is gonna suck if I make such mistakes in the exam.

Originally posted by Fred Rosenberger:

there's your problem. newName is not assigned to variable name. newestName is.

[ August 14, 2007: Message edited by: Fred Rosenberger ]

In Question 2, option a is correct because you can get Singleton object via Runtime.getRuntime() and call method gc() on it.

In Question 1, when newName is assigned string "Jason", string "Nick" is left with no references. Then when newName is assigned variable name, string "Jason" is left with no references. So that's two String objects with no references, "Nick" and "Jason". That is why I thought the answer is 2.
I don't understand the answer of two MindQ garbage collection questions. The first is:

How many objects are eligible for garbage collection once execution has reached the line labeled Line A?

String name;
String newName = "Nick";
newName = "Jason";
name = "Frieda";
String newestName = name;
name = null;
//Line A

Answer is 1 object eligible for garbage collection, I selected 2 objects
name doesn't refer to any String object, newName refers to "Frieda" String object, so aren't both "Nick" and "Jason" String objects available for garbage collection?

------------------------------------------------------

The second question is this:

Which of the following statements about Java's garbage collection are true?

a) The garbage collector can be invoked explicitly using a Runtime object.
b) The finalize method is always called before an object is garbage collected.
c) Any class that includes a finalize method should invoke its superclass' finalize method.
d) Garbage collection behavior is very predictable.

Answer is a, b and c are true, I selected only a and c are true
If finalize method has been called once, then the finalize method isn't called again before garbage collection. So isn't option b false?
Thanks for the explanation