Janet Smith

Greenhorn
+ Follow
since May 01, 2016
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Janet Smith

Ok, from both your comments looking at the code again. In the code snippet that does not compile we have more than one line of code after the first else statement. If we wanted this included in the else we’d surround that with curly braces.

I can then see if I add something similar to the code snippet that does compile I get the same issue.

And I can see if I surround the two lines beneath the else in curly braces both code snippets compile.

So in the code snippet that doesn’t compile does it take:


As one block. So the first if has no corresponding else, but the inner if does. Then it takes the rest:



As code that is executing after the if statement is complete so we then have an else that is floating around with no corresponding if?

If so that is a tricky one. I think once I got the first question I just had it in my head that any appearance of if else else would cause a compiler failure. So its interesting to come across this one.

Thanks for the help. I did a bit of googling and couldn’t come up with the answer so I’m not sure I would have figured that one out myself
Hi all,

When I took one of the tests in the OCA/OCP Java SE7 Programmer I and II Study Guide I came across the following question:



The question was about what happens next and the answer was that it does not compile for the following reason:

Without curly braces, the compiler makes the code 6 lines of code starting with the first if test a single block. That means the second else statement is floating without an if statement to call its own.



Straightforward enough. But then I took an Enthuware test where the following code was present:



Which to me, looked the same as the one in the other test. So I answered that it wouldn't compile. However this was incorrect. The correct answer was that it would compile and would print "False False" if passed false and "True False" if passed true.

I've tried both of these out by tying to compile them and both behave as each test says they will. The first example won't compile and the 2nd one will. It could be that I need to step away and come back later with fresh eyes but at the moment I can't see the difference between them. It looks like both of these have a 2nd floating else that does not correspond to an if. But the 2nd one clearly does correspond to an if. Can anyone shed any light?
Thanks both of you for those explanations.
Hi,

I'm currently working my way through the tests on the CD that comes with the OCA/OCP Java SE7 Programmer I & II Study guide. I got a question that contained the following code and was asked to predict the outcome:



I got this question wrong because I thought I would get a NullPointerException as the first for loop populates elements 1 and 2 of the array, but the second for loop prints all elements, including the one at position 0, which the code never populated.

The correct answer is that the output is "null b c"

I looked through the book and chapter 5 says "If you actually try to use that null reference by, say applying the dot operator to invoke a method on it, you'll get the infamous NullPointerException"

So I can do a System.out.print on sb[0] which will print "null", but if I tried to call sb[0].toLowerCase() I would get a NullPointerException?

I was wondering if someone could explain a little about this? I had it in my head that any reference to sb[0] would throw a NullPointerException so I'm interested to know what will and what won't throw it.

Roel De Nijs wrote:
This question is more about the very weird identifiers (class name, variable name and parameter name) than about the for loop itself.


Hi Roel,
Thanks for your reply. The rest of the question made sense and I was able to work it out. It was just when I saw the unusual loop I wasn't sure if it was valid. So I wasn't 100% sure whether my answer was correct or if it was going to have a problem during compilation or at runtime.

I'll definitely take your advice on board about tinkering around with the code and trying out different things.
Hi folks,

I'm currently studying for the OCAJP 7 exam at the moment. I'm using the OCA/OCP Java SE7 Programmer I and II Study guide by Kathy Sierra and Bert Bates. So far I've gone through the OCA part of the book once, taking all the self tests as I went along. However there were a couple of things that threw me a bit and made me worried about the exam. And that was the appearance of unusual looking code in the test questions. One in particular that I'm looking back at today is in the 1st Self Test:



The for loop is what threw me. This self test is from chapter one, before we've gone into any detail on loops. So I went forward to that section. This explains that the basic for loop has three parts 1. Initialization 2. Condition 3. Iteration. Towards the end of that section it says that you can leave all three empty essentially creating an endless loop. It also says that you can leave out the initialization and increment parts essentially creating a while loop.

But other than this the syntax of the for loop in the self test is alien to me. And this is the thing that is slightly worrying. When I first see it in the test I have to make an educated guess at what it will do, rather than knowing right away what its doing. This isn't the first time I've come across 'interesting' syntax in a test that is not mentioned, or is only covered by a single line in the book.

So my question is how to deal with things like this? Are there other sources I should be using in preparation for the test that might cover things like this? Possibly the Mala Gupta book? Or different online resources? I haven't downloaded the Enthuware tests yet because I don't want to tackle them until I'm more confident i.e. I don't want to waste them. I'm guessing a lot more coding would be a good idea. But its the fact that I don't know that these types of syntaxes exist in the first place, so how to know to use them in my own code is the question.

Anyway, any advice would be appreciated.