aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Chapter 1. Question 9 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 "Chapter 1. Question 9 " Watch "Chapter 1. Question 9 " New topic
Author

Chapter 1. Question 9

vijaya alla
Greenhorn

Joined: Aug 29, 2010
Posts: 2
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

Joined: Jan 22, 2007
Posts: 400

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.


SCJP6 96% | SCWCD5 81% | SCDJWS5 79%
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

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


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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.


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Welcome to JavaRanch.Please USECODETAGS while posting your queries.


SCJP6.0,My blog Ranchers from Delhi
vijaya alla
Greenhorn

Joined: Aug 29, 2010
Posts: 2
Thank you guys for explanation, recommendation and moral support.

sumit kothalikar
Ranch Hand

Joined: Apr 15, 2010
Posts: 91

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


Thanks & Regards
Sumit Kothalikar
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Chapter 1. Question 9