• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

some odd answers from the K&B CD - part 4

 
John Schubert
Ranch Hand
Posts: 39
Android Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the part four! Today (or tonight, depending on where do you live) a couple of questions:

Open book #2 exam, question 22


The question is:
What is the result?


The answer was:
D fin Then a runtime exception


But actually if you execute the code, no exception (and no RuntimeException at all) is thrown. If you print the stack trace, it looks like this:

java.lang.Throwable
at Battery.main(Battery.java:6)


And inspecting the thrown object, it is a plain Throwable whose cause is itself. (BTW, I'm of the opinion that the Throwable class should be abstract).


Open book #2 exam, question 44
Given a class House, which are true (Choose all that apply)
A House has-a Roof would be considered tight coupling
...
D House has-a Door would be considered low coupling
...


A is incorrect and D is correct. What I don't understand is how, withouth examining the source code of at least one of the classes, can we say if a composition scheme is coupled or not.
The reference says nothing about why D is correct. About why A is incorrect, it says:
A is incorrect because a House almost certainly will have a Roof, so this is not considered tight coupling.


But I don't understand why. Perhaps it means that in absence of source code, every composition should be considered loose coupling?
 
Puneet Chadha
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I ran the code
and got the following output


fin
Exception in thread "main" java.lang.Throwable
at battery.Battery.main(Battery.java:14)
Java Result: 1


which is expected as the code is throwing an object of Throwable Class and trying to catch it with an Exception object in the argument of catch block.The Throwable object is not caught as shown in the output.finally will always be run unless System.out.exit() is called.
 
John Schubert
Ranch Hand
Posts: 39
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Similar result than mine. But the answer marked as correct is misleading because it uses the words "runtime exception" and no exception is thrown, since a Throwable IS-NOT-AN exception.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic