aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in OCP Practice Exams question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in OCP Practice Exams question" Watch "Doubt in OCP Practice Exams question" New topic
Author

Doubt in OCP Practice Exams question

Chandramohan Thalappally
Greenhorn

Joined: Dec 28, 2010
Posts: 4
In Question # 39, class Untuned, the answer given is as "A" (answer -1), which is wrong. Because the method toCompare takes Object a, Object b as arguments, so infact, it will give a compilation error saying that the interface Comparator is not correctly implemented. So, if the code should work, it should be like this - note the the Object a and Object b were correctly cast to String in the compareTo method.

Can Kathy or Bert can comment on this. Likewise, I have seen problems, elsewhere in the Practise Exam guide.



Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8829
    
    5
Hi Chandramohan,

Welcome to Javaranch.

It's great for you to ask questions like this, and perhaps you've found a bug too. But here's how to do this the official JavaRanch way:

- First off, since this is a question about the SCJP certification exam, you should put your question in the SCJP forum.
- Second, you should always post the exact code that's in the mock exam that you want to talk about. Remember that not everyone has a copy of all the mock exams. As long as you tell us where the mock exam question came from, it's fine to copy the question out of the book or wherever.
- Third, each time you have a question, it's best to start a new thread and the title should be something like:

"Doubt concerning a collections question in the XYZ mock exam"

Remember, sometimes you might find an error in a mock exam, and sometimes you might be wrong - so it's best to describe your question as a doubt.

Once you've posted the question (and any other code, like you've done), in the proper forum, we wait to see what the other ranchers think. Once we've all agreed, then, and only then, if it's an error, we'll make a note of it.

I didn't study your specific question because you didn't include the original code, so I can't agree or disagree at this point. Besides, it's better for certification candidates to discuss these kinds of things in the certification forums.

I look forward to seeing your "doubt" in the SCJP forum.

hth,

Bert


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

Joined: Dec 28, 2010
Posts: 4
Hello Bert,
Thanks for your prompt reply and kind guidance. The reason for posting this specific question (#39 of Practice Exam 1) as this thread is related to this specific book (OCP Java SE6 Programmer Practice Exam - Exam 310-065) written by Kathy and yourself. I have a Kindle edition of it. As the inner static class "MySort" implements Comparator, we should implement "public int compare (Object a, Objectb)" and then cast the two objects to String like - return ((String)b).compareTo((String)a), before returning the int value, otherwise compiler wont let you go ahead. So, this question is related to that book and this thread is about that book and thta's why I posted here. I hope you have a copy of that specific question in your hand. Sorry for the inconvenience. I will try to post the original question and corrected question in SCJP Exam thread shortly. I wish, I could have clarified more on this verbally. The JavaRanch site is enriching me enormous amount of knowledge on Java. I admire Kathy and you for maintaining it so great.

Regards
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

I pulled the rank and transferred the question to a new topic.
Because the method toCompare takes

The method name is actually compareTo which is a part of Comparable interface and the Comparator interface has a compare(Object a, Object b) method. I'm actually confused as to what the code in the book is (as I don't have the book) so if you could post the exact code of the book, it would be easier to understand if the book's code is wrong...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Frank Callahan
Greenhorn

Joined: Dec 30, 2010
Posts: 15
Hi Chandramohan,

Question 39 in the first exam in the OCPJP practice exam (page 77) reads as follows, at least in my copy:



The code does compile, and the output is -1, which is the correct answer as stated on page 121. There's some misdirection in the question that could lead you to answer as if the binary search had been made in a String[] sorted in reverse, i.e, [vail,t-ride,dillon,aspen], making C. 2 the right answer. But as the book explains, answer A. -1 is the most likely output, since the binarySearch method that is called is the two param version without a Comparator ref as the third param. Strictly speaking though, even if -1 is the actual output from the Oracle/Sun jdk1.6.0_23 version that I'm using, the javadocs for the java.util.Arrays class say that the result of a binary search in an unsorted (or not sorted in the natural ordering of the class) array is undefined:

public static int binarySearch(Object[] a, Object key)

Searches the specified array for the specified object using the binary search algorithm. The array must be sorted into ascending order according to the natural ordering of its elements (as by the sort(Object[]) method) prior to making this call. If it is not sorted, the results are undefined....


So, there is a conflict between the documentation and the observed behavior of the Arrays class, which may reliably produce a -1 index from running this code, but by the docs, could output any number at all, that is, the expected output is undefined. Use of the word likely in question 39 may be a way of getting at your experience of using binarySearch(), and not of just being familiar with the documentation. However, the question is kind of unfair in that if the word likely was removed, A, B, C and D could all be correct.

Regards,
Frank
Chandramohan Thalappally
Greenhorn

Joined: Dec 28, 2010
Posts: 4
Hello Frank
The code I have from my Kindle Edition (I purchased and downloaded from Javaranch site for 16 bucks) has a raw type Comparator implementation (quoted below). It does not have the <String> (the Comparator is not typed). That's where the whole ocnfusion arised. But I do not understand why your code has <String> (the type Comparator implementation and the code I have is a raw type Comparator implementation). So, as the code exists in my kindle edition and the compiler will give a warning saying that Comparator.compare(Object,Object) must be implemented.

So, the Kind Edition code I have is not a typed-Comparator. It is a raw Comparator. So, just for exercise, just removed the <String> from your Comparator implementation. the compiler will squak at you as I stated above.

import java.util.*;
public class Unturned {
public static void main(String[] args) {
String[] towns = {"aspen", "vail", "t-ride", "dillon"};
MySort ms = new MySort();
Arrays.sort(towns, ms);
System.out.println(Arrays.binarySearch(towns, "dillon"));
}
static class MySort implements Comparator {
public int compare(String a, String b) {
return b.compareTo(a);
}
}
}

Frank Callahan
Greenhorn

Joined: Dec 30, 2010
Posts: 15
Hmm, it looks like the Kindle version is different from the printed book. I typed in the exact code from the book. What explanation does the Kindle version give for -1 being the right answer?

Update - Amazon.com has a review of the Kindle version that explains what is going on:


This review is from: OCP Java SE 6 Programmer Practice Exams (Exam 310-065) (Kindle Edition)
I love the quality of McGraw Hills books and specially the ones to prepare the Java Certify exams. I'm also a Kindle user with very good experiences reading books in Kindle versions.

But this one is completely useless. As you may know, Kindle books are actually a kind of HTML code (with labels like: '<'html'>', '<'body'>', '<'p'>', etc.). Well, Java ALSO use these kind of labels for its GENERIC parameters, so when you open this book in Kindle version you lose every generic argument (and there are a lot of them in the exams) because the book reader thinks they are HTML labels and don't show them.
This problem also affects to EVERY comparation expression which uses the characters ">" or "<" or ">=" or "<="...


Frank
Chandramohan Thalappally
Greenhorn

Joined: Dec 28, 2010
Posts: 4
Thanks Frank on this help information. I also noticed that several less than symbol (<) are missing. Now, shame on Amazon and without properly testing the Kindle Edition for Java's syntax and semantics, they are selling for the public. I would like to have proper explanation from Amazon or its publisher. Either they should refund the money or after correcting these mistakes, they should give an update of the Kindle Edition to its already purchased users. People are struggling day and night by studying these mock-up questions in the best hope of passing these highly challenging examinations and these publishers are selling erroneous mock-up questions for making money. Is there any way to bring this to the attention of Amazon, and get an updated/corrected version of this Kindle Edition.

Thanks for your valuable help in this matter.
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3313
    
    7
It might be more beneficial if you contact the publisher (instead of Amazon) and ask for a pdf copy instead of kindle format.
Not sure if Amazon can do anything about it since they can't really check whether every book from every publisher works or not. I imagine it would be the publisher's responsibility.


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8829
    
    5
Hey Guys!

First off, I'm sorry you've encountered this problem!

grr...

I told the publisher about this kindle problem several weeks ago. They promise that they're working with Amazon to fix the problem.

I have also asked that they publish a notice when the problem is fixed and let kindle buyers re-download the fixed book for free.

Now, I don't know if that will happen, but that's what I'm asking for, and I promise I will be very persistent!!!

BTW, from the "Other than that Mrs. Lincoln how did you enjoy the play?", dept... How are you guys liking (or not) the book?

Bert

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in OCP Practice Exams question