Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

K&B Study Guide - Chapter 6 Self Test - Question 9

 
Brian Brumpton
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The questions states:

Given

And given that line 7 will assign the value 0, 1, or 2 to sw, which are true? (Choose all that apply.)

A. Compilation fails
B. A C1assCastException might be thrown
C. A StackOVerflowError might be thrown
D. A Nu11PointerException might be thrown
E. An I11ega1StateException might be thrown
F. The program might hang without ever completing
G. The program will always complete without exception

The correct answers in the book are D and F.

I can see where those would be correct given the possibility that the assigned value would ever be 1 or 2, however, wouldn't that be a mathematical impossibility based on the code in line 7? If presented with questions such as this on the exam, is it advisable not to read into the code that far?
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see a problem unless random always returns 2. In that case it would hang, and then the null pointer exception would be when it ran out pf mempry for new Beyonds. I don't see any other problem. If new Beyond doesn't run main then no problem


 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian Brumpton wrote:I can see where those would be correct given the possibility that the assigned value would ever be 1 or 2, however, wouldn't that be a mathematical impossibility based on the code in line 7? If presented with questions such as this on the exam, is it advisable not to read into the code that far?

The question clearly states sw will be assigned 0, 1, or 2 on line 7. So you just have to evaluate these 3 possible options and see what result you'll get.

  • sw = 0: the for-loop is executed, this loop never finishes and keeps running forever and ever => answer F is correct
  • sw = 1: the statement int y = 7 * i; will be executed, in order to perform the multiplication the reference variable i will be unboxed. i is not assigned a value, so its value is null. When a reference variable with value null is unboxed, you'll get a Nu11PointerException => answer D is correct
  • sw = 2: a new Beyond instance is created and inf (a reference variable of superclass Infinity) refers to this instance/object. Then inf is cast to Beyond which is valid, because the actual object inf is referring to is of type Beyond => no answer is correct: a ClassCastException isn't thrown & the program won't always complete without exception (when sw = 1 you'll get a Nu11PointerException as discussed in the previous bullet point)


  • Hope it helps!
    Kind regards,
    Roel
     
    Guillermo Ishi
    Ranch Hand
    Posts: 789
    C++ Linux Python
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    This is why I hate this crap. I know instance objects get initialized to null but was thinking zero here for some reason. And for case 0 the silly formatting and silly code made me think break would get hit even though it doesn't. I hate these tests.


     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Guillermo Ishi wrote:This is why I hate this crap. I know instance objects get initialized to null but was thinking zero here for some reason. And for case 0 the silly formatting and silly code made me think break would get hit even though it doesn't. I hate these tests.

    I can't help it, but I really like this kind of questions, but that's probably just my quirky brain
     
    Guillermo Ishi
    Ranch Hand
    Posts: 789
    C++ Linux Python
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sorry, but I own quirky - I can't answer question I know the answer to. I treat the question like it was a typo.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic