aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Problem in output and mock tests? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Problem in output and mock tests?" Watch "Problem in output and mock tests?" New topic
Author

Problem in output and mock tests?

George Gates
Ranch Hand

Joined: Jul 11, 2008
Posts: 34
Hi All,

I was going through some of the programs. The following two program's output caught me confused.


1)


The answer is:
0 0 3 0.

I could not understand why does it print 3 at the 2nd index.


2)

Answer:
It does not compile.

But if we replace Boolean by string it complies. Why we cant sort a boolean array?


About quote the source:
I am sorry, I dont remember the exact source. I have been solving from miscellaneous sources on the internet and I put all my doubts in a word file as and when I encounter them. Henceforth, I will take the responsibility of making a note of the source too. (Its unlikely to get the real questions though )



MOCK TESTS:

I had previously also posted queries regarding mock tests. I got the following link as a reference http://faq.javaranch.com/java/ScjpMockTests.

Can someone tell me precisely which mock exams (that are free and available online) map with the objectives of SCJP 5?

I have purchased Javabeat's 350 mock questions. Can someone tell me how much does it map with the SCJP 5 objectives?

I would appreciate if some one could thoroughly guide me as after reading K&B, which mock exams should be practiced before appearing for SCJP 5.0?

Thanks All for your time.

Regards
[ August 10, 2008: Message edited by: Badal Chowdhary ]

Regards,<br />George Gates
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Badal Chowdhary,
Welcome to JavaRanch.

A couple tips:

1.) Anytime you post code examples to a testing forum, we require that you include a valid source.
See:
Quote Your Sources for more information.
You can add a source to this post by clicking on the icon in the top post of this thread.

2.) Whenever you post more than 1 or 2 lines of code to JavaRanch, be sure to wrap your code in a set of UBB CODE tags.
Doing so will preserve your indentation making the code much easier to read.
If your code is easier to read, you stand a much better chance of having someone knowledgeable read it and answer your question for you.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Quirino Gervacio
Greenhorn

Joined: Aug 09, 2008
Posts: 23
Hi Badal!

It is indeed hard to trace the program just by looking at it, but I tried in
in code anyway.

#1


And here's the trace

1st iteration
i=1
arr[i]=0
your array is now arr{1, 0, 3, 4}

2nd iteration
i=0
arr[i] = 0
your array is now arr{0, 0, 3, 4}

3rd iteration
i=3
arr[i] = 0
your array is now arr{0, 0, 3, 0}

4th iteration
i=0
arr[i] = 0
your array is now arr{0, 0, 3, 0}

I hope this helps
[ August 09, 2008: Message edited by: Quirino Gervacio ]

SCJP 6.0
Quirino Gervacio
Greenhorn

Joined: Aug 09, 2008
Posts: 23
Hi again Badal!

#2


Arrays are also objects whether the elements of your arrays are primitives. In your problem, notice that this is valid.

but in the API of Arrays helper class, there is no Array.sort(Object), only Arrays.sort(Object[]), and then some..

I hope this helps.
[ August 09, 2008: Message edited by: Quirino Gervacio ]
George Gates
Ranch Hand

Joined: Jul 11, 2008
Posts: 34
Hi Quirino,

Thanks for answering the questions.

I still have some doubts.

#1)
Can you tell how the values of 'i' changes in the 4 iterations?

#2)
If arr is a boolean array, how is it possible to say


Shouldn't it be something like this...


Thanks again
Steve Ng
Greenhorn

Joined: Aug 10, 2008
Posts: 17
Originally posted by Badal Chowdhary:
Hi Quirino,

Thanks for answering the questions.

I still have some doubts.

#1)
Can you tell how the values of 'i' changes in the 4 iterations?

#2)
If arr is a boolean array, how is it possible to say


Shouldn't it be something like this...


Thanks again


Dear Badal,

#1) the fourth iteration i is assigned with a value = 0 beecause the fourth element is 0 due to the third iteration. Third iteration:

array[3] = 0 which is the fourth element as array is 0-based indexed

Result in: array[0] = 0 (first element)

#2) It simply does NOT compile as Arrays class does not have a method to sort an array of boolean. It DOES have methods to sort arrays of other primitive types. Read Java API for more info

Hope that helps
[ August 10, 2008: Message edited by: Steve Ng ]
Quirino Gervacio
Greenhorn

Joined: Aug 09, 2008
Posts: 23

is valid

Notice when you perform a dot operation in arr, you'll be able to use the methods of the Object class.

Not

Because arr is not an array of object.

Also, just like Steve posted, there is no Arrays.sort(boolean[])
The API is also your best friend. :-)

I hope this helps
[ August 10, 2008: Message edited by: Quirino Gervacio ]
George Gates
Ranch Hand

Joined: Jul 11, 2008
Posts: 34
Yes, I get it now.
Thanks
 
 
subject: Problem in output and mock tests?