aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Discussing errata for K&B, SCJP 6 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Discussing errata for K&B, SCJP 6" Watch "Discussing errata for K&B, SCJP 6" New topic
Author

Discussing errata for K&B, SCJP 6

Andreas Schreiber
Greenhorn

Joined: Jun 17, 2010
Posts: 5
In thread "Compiling errata for K&B, SCJP 6"
Bert Bates wrote:Hi Guys,

First off, thanks for taking the time to post these errata!

Second, I'd like to request that before an entry goes on this list, it's discussed in a separate thread - to be sure it's actually an error.

Thanks,

Bert

therefore I have opened this thread.

Here are some proposals for improving the book:
Page 333: "Because of the short-circuit &&, ..." replace with "Because of the operator precedence, ..."
Page 336: "final variable that is assigned" replace with "final variable that is immediately initialized with"
Page 347: "for (int x = 0; ((((x < 10) && (y-- > 2)) | x == 3)); x++) {}" one can safely remove one pair of the outer parentheses around the conditional test
Page 373 in the middle: "IOException subclasses Exception" would be even clearer "IOException directly subclasses Exception"
Page 382: It would be appropriate to add IOException to the table as the only checked exception type, because it was discussed in length around page 373.
Page 419 in answer to question 12: "terminates the current loops" should be "terminates the labeled loop"
Page 436/7: The explanation of substring(int begin, int end) seems quite confusing to me, because the concept of "zero-based index" is mixed with "one-based position". My proposal is to speak only of zero-based indexes like in the API doc:
"If the call has two arguments, the substring returned will end just one before the character located at index n of the original string where n is the second argument. So, if the second argument is 7, the last character in the returned String will be at index 6 of the original String."
"The second example should be read as follows: start at index 5 and return the characters up to but excluding index 8."

Page 278 in answer to question 2: "the argument 7 needs to be cast to a short". The underlying rule (no implicit narrowing when passing an argument to a method, opposed to in an assignment) is not mentioned in the book, but in Java Language Spec ยง5.3. I propose to describe it in Exam Objective 7.3 section of chapter 3, or give more detail in this answer.

Page 391 Table 5-4: There are two Command-line Examples in the table which have the same effect: "java -ea" and "java -ea -dsa". Though the descriptions are correct, the
supposed intent was to show different options. Therefore I propose the following changes:
"java -ea Enable assertions" should be "java -ea Enable assertions. Does not enable assertions in system classes."
"java -ea -dsa" should be "java -ea -esa" and the description "Enable assertions in general and enable assertions in system classes."
Compare the documentation in http://java.sun.com/javase/6/docs/technotes/tools/windows/java.html:
"The -enableassertions and -ea switches apply to all s loaders and to system classes (which do not have a class loader). There is one exception to this rule: in their no-argument form, the switches do not apply to system. This makes it easy to turn on asserts in all classes except for system classes. A separate switch is provided to enable asserts in all system classes; see -enablesystemassertions below."
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

I agree with the first 3.
Saying IOException is a subclass of Exception or direct subclass doesn't really make a difference. Exceptions which are unchecked are clearly stated everywhere that they subclass from RuntimeException, so there should be no confusion.
I can't say about the 5th one (page 382), when I read the book I never felt some exception was missing out.
Agreed on the labeled break one.
For the substring one, an image like this can be added to make it easier to understand. Because if you go by the indexes in that diagram, that is how both arguments of substring (and delete, insert etc methods of StringBuffer class) work.
I can't say about the assertions thing either. I didn't have any problem in understanding that part...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8764
    
    5
Cool idea Andreas!


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Andrea Chiavazza
Greenhorn

Joined: Jul 02, 2010
Posts: 14
Page 583, middle of page:

(((Dog)o).name == name)) {

should be

(((Dog)o).name.equals(name))) {
Andrea Chiavazza
Greenhorn

Joined: Jul 02, 2010
Posts: 14
page 607, exam watch on top of page
Was the first 3 liner code meant as a pre-Java 5 example ?
Then maybe it should be:
test.add(new Integer(43));
since pre-Java 5 didn't have auto-boxing
Andrea Chiavazza
Greenhorn

Joined: Jul 02, 2010
Posts: 14
page 627, at the top
page 628, middle

"And you can use a form of wildcard notation in a class definition..."
Only an "extends" wildcard can be used, not a "super", I think it should be made more clear.

Similarly in the next page, when talking about generic methods:
"You're also free to put boundaries on the type you declare..."
It should be made clearer only "extends" is allowed, not "super".
Andrea Chiavazza
Greenhorn

Joined: Jul 02, 2010
Posts: 14
page 437, just after the box

"Unfortunately, the ending argument is not zero-based..."
That's correct, so this might not really be an errata, but I find it quite a confusing way to explain it.
I would have rather considered both the arguments zero-based, with the first being inclusive, and the second exclusive, and by the way this is also the way it is described in the API documentation.
It is also more consistent with the rest of the API, if you consider methods like:
subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive)

Probably there will be no reprints of this book, but I hope this could help if this part will be brought to a SCJP 7 version of the book.
Andrea Chiavazza
Greenhorn

Joined: Jul 02, 2010
Posts: 14
"A var-arg parameter can be treated by the body of the function just as if it was a regular array"
Is that right ? It is what I figured out at the end, since this short sentence is nowhere in the book, but maybe it should be!
I know it might be obvious, but I was puzzled for a while thinking about how to access a var-args parameter...
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8764
    
    5
Hey Guys,

When it's time to do a reprint, we're allowed to make minor fixes to the book! So this is really helpful stuff. You should also know that, because of a misspent youth, my brain doesn't shift gears as readily as it used to... so I tend to wait for a while, let these suggestions accumulate, and then look at them in a big batch.

Thanks!

Bert
Andrea Chiavazza
Greenhorn

Joined: Jul 02, 2010
Posts: 14
page 807, bottom
"You can do a static import on static object references, constants (remember they're static and final), and static methods."

It seems to me a variable must be static, but not necessarily final.
This works just fine, and outputs:
12 34
56 34
Andrea Chiavazza
Greenhorn

Joined: Jul 02, 2010
Posts: 14
page 63, middle
"You can add constructors, instance variables, methods, and something really strange known as a constant specific class body."

Referring to enums.
Variables don't have to be instance variables, can be static too.
Static methods work too, you can even define a main method in an enum and have it as a starting point for your application.
enums can also be nested.
Andrea Chiavazza
Greenhorn

Joined: Jul 02, 2010
Posts: 14
page 629, the whole paragraph "Creating Generic Methods"

It might be worth mentioning that the generic type constructor can not be invoked.

On the other hand, it's interesting to note that it is possible to define a generic method without an argument of the generic type, like:
<T> void work() { }
The JVM will have no way of knowing what that type is!
or even

<T> T work() {
T t = null;
return t;
}

Since the constructor T() can't be invoked, t can only be initialized to null. It is also legal to cast objects of other types to T.

Anyway I doubt there could be any scenario where a generic method with no generic type argument could possibly be useful !
Andrea Chiavazza
Greenhorn

Joined: Jul 02, 2010
Posts: 14
Unreachable code compile error ?

I might be wrong but I don't think this is ever mentioned anywhere in the book.
I just came across a question in Whizlabs SCJP 6.0 (quiz 1 question 5) that fails to compile due to unreachable code, so I think this might as well be in the real exam !
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8764
    
    5
Hi Andrea,

We appreciate your help, and I'd like to ask you to to make distinctions between errors and suggestions.

Thanks,

Bert
Piotr Sobczyk
Greenhorn

Joined: Dec 06, 2009
Posts: 10
Chapter 10, question 11.

Answer marked as correct is A that is:

javac -classpath MyJar.jar GetJar.java
java GetJar


But java launcher cannot find myApp.Foo class that resides inside MyJar.jar and MyJar.jar isn't explicitly pointed by -classpath argument in java launcher.
For certainity I prepared the same enviroment as in question:



and invocation of

java GetJar

returns:
Exception in thread "main" java.lang.NoClassDefFoundError: myApp/Foo
at GetJar.main(GetJar.java:3)
Caused by: java.lang.ClassNotFoundException: myApp.Foo
...


For answer A to be correct there should be Foo.class instead of Foo.java in myApp branch OR java part should look like this:

java -classpath .:MyJar.jar GetJar
Sarah Archer
Greenhorn

Joined: Jul 25, 2010
Posts: 19
Firstly, thank you Kathy and Bert for K&B SCJP. It has been a great help getting me through the exam.

Possible errors

Page xxxi states there are 72 questions on the exam and you need to get 47 of them correct to pass. On the exam I took there were only 60 questions. Perhaps the specification has changed over time or perhaps the British version is easier

Page 803 defines a jar called MyJar.jar but page 804 refers to a jar myApp.jar, which is presumably meant to be the same jar.

Page 823 - the answer to question 5 states "D is correct. A -classpath included with a java invocation overrides a system classpath". I think this is ambiguous. -classpath overrides the CLASSPATH environment variable. However, I took "system classpath" to mean something like jre/lib/ext which is not overridden.

Page 798 states "When searching for class files, the java and javac commands don't search the current directory by default". I believe this is incorrect as I understand that if CLASSPATH is not set and no -classpath option is used, the user class path consists of the current directory (.). [Sun docs]. However, if CLASSPATH is set or -classpath is used, then the current directory will not be searched unless it has been added to the classpath - both scenarios are tested by the questions at the end of Chapter 10.

Possible improvements

I cannot recall any material in Chapter 10 stating that the class option of the java command has to be a fully qualified class name. All the examples seemed to have the class that was to be executed defined outside of a package.

Also, it has been suggested above that the book should cover "unreachable code" leading to a compiler error. I agree with this suggestion.

Both of these possible improvements would have been useful for the exam.


Finally, the book is far tougher than the exam!!! Perhaps there could be some (more) reassurance that difficulties with questions in the book do not necessarily indicate difficulties with the actual exam. After struggling with the questions in K&B, I then got 91% in one of Sun's mock exams and 96% in the actual exam.


Regards
Sarah
Mahmoud Hossam
Ranch Hand

Joined: Feb 20, 2010
Posts: 74

Page xxxi states there are 72 questions on the exam and you need to get 47 of them correct to pass. On the exam I took there were only 60 questions


when SCJP first came out,it had 72 questions with 210 minutes of allowable answer time

now it has less questions with less time
Faisal A Khan
Greenhorn

Joined: Jul 02, 2010
Posts: 11
Chapter 4 refers to Enhanced for loops as new to Java 6. Please correct this as this was a new feature as from Java 5

Regards
Kebir Selim
Greenhorn

Joined: Jul 29, 2010
Posts: 1
I've found a little techincal error at page 281.
Here is the paragraph that contains the error followed by the explanation :


Question 7 :

Given:
3. public class Bridge {
4. public enum Suits {
5. CLUBS(20), DIAMONDS(20), HEARTS(30), SPADES(30),
6. NOTRUMP(40) { public int getValue(int bid) {
return ((bid - 1) * 30) + 40;} };
7. Suits(int points) { this.points = points;}
8. private int points;
9. public int getValue(int bid) {return points * bid;}
10. }
11. public static void main(String[] args) {
12. System.out.println(Suits.NOTRUMP.getBidValue(3));
13. System.out.println(Suits.SPADES + " " + Suits.SPADES.points);
14. System.out.println(Suits.values());
15. }
16. }

Which are true? (Choose all that apply.)
A. The output could contain 30
B. The output could contain @bf73fa
C. The output could contain DIAMONDS
D. Compilation fails due to an error on line 6
E. Compilation fails due to an error on line 7
F. Compilation fails due to an error on line 8
G. Compilation fails due to an error on line 9
H. Compilation fails due to an error within lines 12 to 14

Answer:
- A and B are correct. The code compiles and runs without exception. The values()
method returns an array reference, not the contents of the enum, so DIAMONDS is never
printed.
- C, D, E, F, G, and H are incorrect based on the above. (Objective 1.3)


Explanation :
The answer should be H because the getBidValue(int) (Marked with red bold above) method does not exists in the Class Suits and not in any of its subclasses.

Excuse my english,
Sincerly.
Sarah Archer
Greenhorn

Joined: Jul 25, 2010
Posts: 19
@Kebir

It looks like a mistake was made when the question was repeated with the answer. When the question is first given on page 272 line 12 calls getValue(3).

Sarah
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

@Kebir yes that is an error (has been reported here)
@Faisal the Java 6 for loop error is also correct (has been reported here)

Sarah wrote:Page 803 defines a jar called MyJar.jar but page 804 refers to a jar myApp.jar, which is presumably meant to be the same jar.

I think you are right.

Sarah wrote:I took "system classpath" to mean something like jre/lib/ext which is not overridden.

Well generally system classpath does mean the CLASSPATH environment variable not the jre/lib/ext directory so I don't think this is an error. The statement can be improved though if many people are confused with it.

Sarah wrote:Page 798 states "When searching for class files, the java and javac commands don't search the current directory by default".

This is correct (has been reported here)

@Piotr that is an error in the answer, the classpath should include the current directory for it to work as you suggested (has been reported here)...
Alexander Exner
Greenhorn

Joined: Jul 30, 2010
Posts: 10
Ad page 194 (Implicit cast from long to double):

I posted some stuff at thread http://www.coderanch.com/t/467890/java-programmer-SCJP/certification/Compiling-errata-SCJP regarding
No cast is needed in this case because d double can hold every piece of information that a long can store.

I did assume the issue is evident but maybe its better to discuss it.

I agree that all said is absolutely correct as long as you dont put it into one sentence. I wrote this code:

281474976710654 vs. 281474976710654 ok
281474976710654 vs. 281474976710654 ok

280925211459582 vs. 280925211459582 ok
280925211459582 vs. 280925211459582 ok

52776457469950 vs. 52776457469950 ok
52776457469950 vs. 52776457469950 ok

175921859657726 vs. 175921859657726 ok
175921859657726 vs. 175921859657726 ok

9223372036854775806 vs. 9223372036854775807 failed
9223372036854775806 vs. 9223372036854775806 ok

9223372036854775790 vs. 9223372036854775807 failed
9223372036854775790 vs. 9223372036854775790 ok

9223372036854775780 vs. 9223372036854775807 failed
9223372036854775780 vs. 9223372036854775780 ok

9223372036854772062 vs. 9223372036854771712 failed
9223372036854772062 vs. 9223372036854772062 ok

9223372036854772707 vs. 9223372036854772736 failed
9223372036854772707 vs. 9223372036854772707 ok

9223372036854772334 vs. 9223372036854772736 failed
9223372036854772334 vs. 9223372036854772334 ok

9223372036854773486 vs. 9223372036854773760 failed
9223372036854773486 vs. 9223372036854773486 ok

9223372036854775796 vs. 9223372036854775807 failed
9223372036854775796 vs. 9223372036854775796 ok


Please tell me if i am wrong.
Alexander Exner
Greenhorn

Joined: Jul 30, 2010
Posts: 10
Page 292 ("Equality Operators")

It would probably be worth noting that things like this can happen as well:

oops
Alexander Exner
Greenhorn

Joined: Jul 30, 2010
Posts: 10
Some misleading code on

Page 579
does the job as well. To set the length of ia2 to 3 may cause somebody thinking its necessary to create an array of the same size first.

Page 583f
The main reason why the Dog was found is not that some methods were overridden but that the same reference was used for construction and searching.

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

Alexander Exner wrote:Some misleading code on Page 579
does the job as well. To set the length of ia2 to 3 may cause somebody thinking its necessary to create an array of the same size first.

If I remember correctly, the book clearly states that if the List fits into the array we pass to the toArray method, then that array is used otherwise a new array is created...
Alexander Exner
Greenhorn

Joined: Jul 30, 2010
Posts: 10
The only thing i found in the book is on page 579:
[...] it comes in two flavors: one that returns a new Object array, and one that uses the array you send it as the destination array:

As far as i can see there is no hint that the behavior is different depending on the length of the parameter array. I found more detail in the Java 6 JDK Javadoc of <T> T[] toArray(T[] a) methods from List and ArrayList:
Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array. If the list fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this list.

If the list fits in the specified array with room to spare (i.e., the array has more elements than the list), the element in the array immediately following the end of the collection is set to null. (This is useful in determining the length of the list only if the caller knows that the list does not contain any null elements.)


I wrote this example:


The output was not obvious to me, even reading the javadoc first:

java ListTest
1feed786
-3--55--2143-
-3--55--2143-
7987aeca

3ae48e1b
-3--55--2143-
-3--55--2143-
3ae48e1b

732dacd1
-3--55--2143--null--7-
-3--55--2143--null--7-
732dacd1


Btw i can hardly imagine that it is useful in most cases to insert a null after the original length of the array.
Alexander Exner
Greenhorn

Joined: Jul 30, 2010
Posts: 10
Page 583

Regarding to the introduction of chapter "Using Maps" i wrote this example:


Output:
Searching D: null
Searching D: 3


JDK 6 Javadoc of TreeMap states:
The behavior of a sorted map is well-defined even if its ordering is inconsistent with equals; it just fails to obey the general contract of the Map interface.

Alexander Exner
Greenhorn

Joined: Jul 30, 2010
Posts: 10
Page 750, 751:

I did complete the FabricMachine example and added a switch to encourage the JVM to do some thread switching in the while loop of Machines run() method. I did input the machine steps quite quick after the operator was ready. Of cause your output may vary depending on the JVM:




$ javac FabricMachine.java && java FabricMachine please work fine
Operator is ready for instructions
a
Operator calculates steps
Sending calculated steps a to hardware begins
Operator is ready for instructions
b
Sending calculated steps a to hardware ends
Operator calculates steps
Sending calculated steps b to hardware begins
Operator is ready for instructions
c
Sending calculated steps b to hardware ends
Operator calculates steps
Sending calculated steps c to hardware begins
Operator is ready for instructions
d
Sending calculated steps c to hardware ends
Operator calculates steps
Sending calculated steps d to hardware begins
Operator is ready for instructions
e
Sending calculated steps d to hardware ends
Operator calculates steps
Sending calculated steps e to hardware begins
Operator is ready for instructions
f
Sending calculated steps e to hardware ends
Operator calculates steps
Sending calculated steps f to hardware begins
Operator is ready for instructions
Sending calculated steps f to hardware ends
^C$
$ javac FabricMachine.java && java FabricMachine please dont work fine
Operator is ready for instructions
a
Operator calculates steps
Operator is ready for instructions
b
Operator calculates steps
Sending calculated steps b to hardware begins
Operator is ready for instructions
c
Sending calculated steps b to hardware ends
Operator calculates steps
Operator is ready for instructions
d
Operator calculates steps
Operator is ready for instructions
e
Operator calculates steps
Sending calculated steps e to hardware begins
Operator is ready for instructions
f
Sending calculated steps e to hardware ends
Operator calculates steps
Operator is ready for instructions
^C


After putting the while loop in the synchronized block the program did meet my expectations.
Steve Entire
Greenhorn

Joined: Jan 19, 2011
Posts: 4
Hello,

on page 62 there is a somewhat misleading comment concerning enums:

The semicolon comes right after the closing brace, and is indeed optional there. But what really the point is and what was probably meant is:

To confuse us even more, the following two versions are correct, too:


Sincerely Yours
Steve
Steve Entire
Greenhorn

Joined: Jan 19, 2011
Posts: 4
Hello,

on page 629 there is a typo:

It says:

but for the given explanation to be correct the last 'x' must also be the capital letter 'X':



Yours sincerely
Steve
Steve Entire
Greenhorn

Joined: Jan 19, 2011
Posts: 4
On the bottom of page 716 - in the box 'exam watch' - is a typo: The text refers twice to a method 'getld()' of the Thread-class, but apparently it must be 'getId()'.
Steve Entire
Greenhorn

Joined: Jan 19, 2011
Posts: 4
On page 494 paragraph 3 the regex negation operator ^ is called 'carat', but it should be 'caret'.
Cristian Bontoiu
Greenhorn

Joined: Nov 19, 2011
Posts: 1
Hello!
At page 173, Self Test 2 Answers, Question 4, is is true that super(); must be inserted in the overloaded constructor public Kinder(int x)? The code compiles without, given the fact that you write the AgedP(){} constructor in the superclass and this confirms Table 2.4 which mentions that super(); will be added by the compiler automatically even fr overloaded constructors. Thanks!
John Stark
Ranch Hand

Joined: Jul 19, 2011
Posts: 185
As it says in the explanation to this question the parenthesis of super(); are already in place. So you have to add super just to make the following (); making sense. The compiler will automatically insert super(); but in the case of the question you have to add super to make the syntax right.

John
Graham Polley
Greenhorn

Joined: Dec 04, 2011
Posts: 1
I think I may have found something on Chapter 6, Page 524, Q13 ("public class Slice" which is testing parsing and number formatting)

This question has 2 issues:

1) As the code is clearly using println(..) and not print(...) none of the outputs can be correct as all answers assume print(..)
2) The answer is assuming a default Locale (US most likely) and this should be, in my opinion, stated. Thus, when I compile and run it I get the following output on my German machine:

987,12346
987123456


When, I explicitly set the Locale to US via Locale.setDefault(Locale.US), I now get:

987.12346
987.123456


Here is the code (with the changes needed on my machine to match the correct output answer given):



And now I get:

987.12346 987.123456
David Cornette
Greenhorn

Joined: Dec 16, 2011
Posts: 1

Page 776-777, Chapter 9, Self Test Answer 8

The answer to this one is wrong. Or at least, if the given answer is right, then the question needs to specify the usage of this class in a way that is not currently indicated.

The given answer is E, that is, that adding synchonize to both methods is sufficient to make this class thread safe. But that is not enough. True, that would prevent one thread from trying to read the message while another thread is halfway through writing it. But that will not prevent a second thread from overwriting the message before the first one has the chance to read it. The answer should be F.

The problem does not say precisely how this code would be used, but the only thing that really makes sense is that the user would call log() to format the log string (which means adding the timestamp and thread ID), and then would call getContents() to retrieve string to be logged. That's not how I would design a logger class, but the Logger doesn't send or store the message anywhere.
If the use case where changed to indicate that there will be one thread writing and one thread reading, then E would be sufficient, but the explanation for why B is wrong indicates that there could be two threads calling log() at once.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Discussing errata for K&B, SCJP 6
 
Similar Threads
Assertion - what are system classes?
Assertion command-line switches
Enable assertions in current package.
Enabling or disabling assertions
list of All keys that I can pass in -D option