Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Chapter 1. Question 9

 
vijaya alla
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I'm planning to take SCJP 1.6 in Dec, 2010 and reading Kathy Sierra and Bert Bates' SCJP study guide. Just completed chapter 1 and took SELF TEST.

Last question reads:

9. Given:
4. public class Frodo extends Hobbit {
5. public static void main(String[] args) {
6. Short myGold = 7;
7. System.out.println(countGold(myGold, 6));
8. }
9. }
10. class Hobbit {
11. int countGold(int x, int y) { return x + y; }
12. }
What is the result?
A. 13
B. Compilation fails due to multiple errors
C. Compilation fails due to an error on line 6
D. Compilation fails due to an error on line 7
E. Compilation fails due to an error on line 11


I selected C because 'Short' is not in list of keywords for java1.6. It was supposed to be 'short' (all-lowercase). The answer was given as D.

Is there something I got confused/did not understand about keywords or variable types ?
Thankyou.
 
Minhaj Mehmood
Ranch Hand
Posts: 400
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vijaya alla wrote:Hello,

I'm planning to take SCJP 1.6 in Dec, 2010 and reading Kathy Sierra and Bert Bates' SCJP study guide. Just completed chapter 1 and took SELF TEST.

Last question reads:

9. Given:
4. public class Frodo extends Hobbit {
5. public static void main(String[] args) {
6. Short myGold = 7;
7. System.out.println(countGold(myGold, 6));
8. }
9. }
10. class Hobbit {
11. int countGold(int x, int y) { return x + y; }
12. }
What is the result?
A. 13
B. Compilation fails due to multiple errors
C. Compilation fails due to an error on line 6
D. Compilation fails due to an error on line 7
E. Compilation fails due to an error on line 11


I selected C because 'Short' is not in list of keywords for java1.6. It was supposed to be 'short' (all-lowercase). The answer was given as D.

Is there something I got confused/did not understand about keywords or variable types ?
Thankyou.


Yes the answer is D because you cannot call a non-static method inside a static method without creating it's class object try following.
Furthermore, your confusion about java reserve keywords, Actually Short is a class in java.lang package for details click here.


Hope this helps

Minhaj.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Short is one of the wrapper classes of the primitives. This enables autoboxing. But just forget that for now. Autoboxing is just a couple of chapters away. And you're not the first one that got that question wrong. I got it wrong for exactly the same reason when I was just a java grasshopper.

Edit: oh and welcome to the JavaRanch
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vijaya alla wrote:Hello,

I selected C because 'Short' is not in list of keywords for java1.6. It was supposed to be 'short' (all-lowercase). The answer was given as D.

Is there something I got confused/did not understand about keywords or variable types ?
Thankyou.

short is primitive data type in java, but Short is a class, and your myGold is an object. This question is related to Autoboxing, Wouter already mentioned it.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.Please USECODETAGS while posting your queries.
 
vijaya alla
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys for explanation, recommendation and moral support.

 
sumit kothalikar
Ranch Hand
Posts: 91
Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think the compiler error at line 7 is because we are not calling the method properly.

if we would called the method properly int the main method the code would complile

and the answer will be 13
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sumit kothalikar wrote:i think the compiler error at line 7 is because we are not calling the method properly.


Check the answer at the bottom of the OP.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic