This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Associate Certification (OCAJP 7) and the fly likes Q> about Mala gupta's Chapter 2 exam Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Associate Certification (OCAJP 7)
Bookmark "Q> about Mala gupta Watch "Q> about Mala gupta New topic
Author

Q> about Mala gupta's Chapter 2 exam

Winston Liek
Ranch Hand

Joined: Jan 19, 2014
Posts: 62
Q2-5. Which of the following options contain correct code to declare and initialize
variables to store whole numbers?
a bit a = 0;
b integer a2 = 7;
c long a3 = 0x10C;
d short a4 = 0512;
e double a5 = 10;
f byte a7 = -0;
g long a8 = 123456789;
Answer: c, d, f, g

why e is not correct? I tried it on my jdeveloper and it compiled and run well.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5147
    
  12

The question clearly states a whole number (integer). double is not used to store whole numbers (integers), but for decimal values (floating points). So this question is not only about knowing which code fragments will compile and which don't, but also about picking the correct data type for the job


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Winston Liek
Ranch Hand

Joined: Jan 19, 2014
Posts: 62
but isn't it in the choices, 10 is a whole number and was stored in a5??
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5147
    
  12

And what's the data type of a5? I rest my case.
Winston Liek
Ranch Hand

Joined: Jan 19, 2014
Posts: 62
^ a5 is of type double.

As per my analysis on the question:
Which of the following options contain correct code to declare and initialize
variables to store whole numbers


If I will replace the word 'whole numbers with 10 (since 10 is a whole number) it would be:

Which of the following options contain correct code to declare and initialize
variables to store 10?


and the following code successfully declared, was initialized and stored 10, and successfully compiled:


What I am not sure is the word 'correct'. Correct is vaguely used here (as per my understanding :p) since correct may mean syntactically correct? Programatically correct?

Please advise
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5147
    
  12

a5 is of type double.

Exactly! double is a primitive data type that's used to store floating-point (decimal) values, not whole numbers (integers). To store whole numbers (integers) you need to use byte, short, int or long. Of course you can store the literal 10 in a double, but then it's not a whole number anymore, but a decimal value.

Really obvious when you execute the following snippet:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Q> about Mala gupta's Chapter 2 exam
 
Similar Threads
Correct EL expression
New Mock Exam questions...
question on abstract
Question 9, Chapter 6 in K&B
what are you learning/reading these days?