• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

OCP Java SE 11 Programmer I Study Guide (Sybex) Bad Explanation in Bonus Exam 2 Explanation

 
Saloon Keeper
Posts: 1277
38
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The correct answer is marked as correct for this question.

Question 5 tb584704.JaSE11PrgISG.be2.63
Which of the following can fill in the blank to have the code compile and run without error? (Choose all that apply.)


int numberReindeer = 9;
System.out.println( (_____________) numberReindeer);

A. double
B. int
C. long
D. String
E. Object
F. None of the above
You Answered Correctly!
Since the number 9 is small, it can successfully fit into any of the numeric types, making options A, B, and C correct. Additionally, an int can be autoboxed into an Integer. Since this is an Object, option E is also correct.



The answer is silly (except for E), it isn't because the number is small, as this would work just fine too:
jshell> int numberReindeer = Integer.MAX_VALUE;
numberReindeer ==> 2147483647

jshell> (double) numberReindeer
$2 ==> 2.147483647E9

jshell> (int) numberReindeer
$3 ==> 2147483647

jshell> (long) numberReindeer
$4 ==> 2147483647

jshell> (Object) numberReindeer
$5 ==> 2147483647


Even further, primitive casting never generates "errors" or runtime exceptions, so calling the bit truncation "running with an error" is rather confusing to, well, at least me:
That is, are we calling the following executions errors for the exam?
They are defined behaviors I don't love but know to expect:
jshell> (short) numberReindeer
$6 ==> -1

jshell> (byte) numberReindeer
$7 ==> -1

jshell> (char) numberReindeer
$8 ==> '?'
 
author & internet detective
Posts: 40747
827
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
True. I've made a note about wrap arounds in our proviate list of stuff that can be clearer.
 
Is this the real life? Is this just fantasy? Is this a tiny ad?
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic