• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Chapter 1. Question 9

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 400
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you guys for explanation, recommendation and moral support.

 
Ranch Hand
Posts: 91
Notepad
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic