• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt about question in Mock Exam #2 (Java OCA 8 Programmer I Study Guide, Sybex)

 
Julie P Chen
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, the answer to the following question is D, but I thought it would be E because y will need to be initialized or declared first before it is used on the left-side of the expression.
Please clarify.

3: int x = 9;
4: int y = x * (long) (++x);
5: System.out.println(y);

A. -1
B. 9
C. 81
D. 90
E. The code will not compile because of line 4.
 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Julie P Chen,

First of all, a warm welcome to CodeRanch!

Julie P Chen wrote:Hi, the answer to the following question is D, but I thought it would be E because y will need to be initialized or declared first before it is used on the left-side of the expression.

Given the code snippet you have posted, the correct answer would indeed be option E, but for a different reason! The right-hand side of the assignment operator evaluates to a long value and (as you probably know) you can't assign a long value to an int variable without using an explicit cast.

Honestly I am a bit confused about the explanation/reason you have provided: you can declare a variableand you can initialize a variableor you can declare and initialize a variable in one statementBut I'm really wondering how you would initialize/declare a variable before it is used on the left-side of the expression. You are probably a bit confused with using an unitialized (local) variable in an expression, like in this code snippetWhen you try to compile these statement, you'll get a compiler error on line1. Reason: it's not allowed to use (access/read) an unitialized variable in an expression. But you can assign a value to an unitialized variable. So this statement will compileAnd this compiles too (which might be a bit surprising)But this code won't compile, because the compiler doesn't know if variable c will always be initializedIf you assign a value to variable c in the else branch, the code compiles successfully (because it's guaranteed that variable c always is initialized)

Now back to the mock question in the second mock exam. The actual code snippet of this mock question is different from the code snippet you have posted. This is the actual code snippetIn this code snippet the right-hand side of the assignment operator still evaluates to a long value and a long value can (of course) be assigned to a long variable. So the code compiles successfully. And the expression is evaluated as 9 * 10, so 90 is assigned to variable y (and that's why option D is correct).

Hope it helps!
Kind regards,
Roel
 
Julie P Chen
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the explanation thus far, but there is still some confusion.

First of all, there was a typo in my question, so your answer has the correct code in question:



I think the confusion comes from Line 2. Line 2 is declaring y and initializing it with the result of x*(long) (++x), at the same time.

Perhaps I was confused with another question that has something similar to the following code for Line 2.
I thought I read



In this case, there is an Mock Exam explanation that variable y would need to be declared before Line 2 - something about if y is used on the left-hand side (or should be right-hand side).

Please clarify some more.
 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Julie P Chen wrote:In this case, there is an Mock Exam explanation that variable y would need to be declared before Line 2 - something about if y is used on the left-hand side (or should be right-hand side).

Please clarify.

Okay, now I see what's causing your confusion.

Consider this (very simple) code snippet:On line1 variable c is declared and on line2 it is initialized with 50 (the result of the multiplication of variables a and b). And on the next line variable c is printed. Because variable c is declared and initialized prior to this line, the code snippet compiles successfully. If you would remove line2, the compiler will give you a compiler error on the println statement, because variable c is uninitialized and you can never use (access/read) an unitialized variable.

Now let's have a look at the same code snippet with 1 very minor (but very important) changeIf you look closely, you'll notice that on line2 we now use a compound assignment operator (instead of the assignment operator). And that makes a huge difference! Because now the compiler is unhappy with the code snippet and gives a compiler error on line2. Why? It is really well hidden, but you use (access/read) an unitialized variable. Let's make it much more obvious why that statement results in a compiler error. line2 is equivalent withSo you are trying to assign a value to variable c and to calculate this value, you need to take the value of c and add the result of the multiplication of variables a and b. But wait a minute... variable c is only declared at line1 but not initialized, so it's impossible to know its value. And that's why this code snippet fails to compile.

The second code snippet you have posted will also not compilebecause you can't use the compound assignment operator in combination with the declaration of a variable. On line1 you can only use an assignment operator to assign a value to the declared variable y. If you want to use the compound assignment operator, you need to get rid of the type of the variable (but the variable must be declared and initialized prior to this statement).

Hope it helps!
Kind regards,
Roel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic