In: Computer Science
The questions in this assessment use the following. class R { ... } class A extends R { ... } abstract class B extends R { ... } final class C extends R { ...} class D extends A { ... } class E extends B { ... } class F extends B { ... } // none of the classes implement a toString() method [0] Draw a class hierarchy for the classes defined above. [1] No or Yes: class R extends Object [2] class G extends C does not compile. Why? [3] class G extends E, F does not compile. Why? [4] B doh = new B(); does not compile. Why? [5] System.out.println(new R()); prints: R@6bc7c054 Why? [6] No or Yes: class D can have subclasses (children). [7] If public String toString() { return "Arizona"; } is added to class A, then System.out.println(new D()); prints what?
[8] Assume the following is added to class C. public String toString() { int azAgeIn2018 = 2018 - 1912; return "Arizona is " + azAgeIn2018 + " years young!"; } What does System.out.println(new C()); print? [9] System.out.println(new R().hashCode()); prints: 1808253012. No public int hashCode() method is implemented in class R. Briefly explain why this compiles and runs? [10] If public int hashCode() { return 48; } is added to class R, then what does the following statement print? System.out.println("Arizona is state# " + new R().hashCode());
Please find your answers below and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
[0] Draw a class hierarchy for the classes defined above.
[1] No or Yes: class R extends Object
Answer: Yes, every classes created in Java extends Object class by default.
[2] class G extends C does not compile. Why?
Answer: Because C class is marked as final, which means class C cannot be used to inherit any other classes (C cannot be the super class of any class).
[3] class G extends E, F does not compile. Why?
Answer: Java does not support multiple inheritance, so it is not possible to extend a class from two classes directly, though it is possible using interfaces, but with limited features.
[4] B doh = new B(); does not compile. Why?
Answer: Because B is an abstract class, it cannot be used to create an object (cannot be instantiated), it can only be used to inherit other classes from them.
[5] System.out.println(new R()); prints: R@6bc7c054 Why?
Answer: Because R does not override toString() method, so it just calls the Object class’ toString() method which denotes the class name and hashcode to identify an object.
[6] No or Yes: class D can have subclasses (children).
Answer: Yes, class D can have subclasses since it is not final.
[7] If public String toString() { return "Arizona"; }
is added to class A, then System.out.println(new D());
prints what?
Answer: it will print ‘Arizona’ since the class D invokes super class R’s toString method by default.
[8] Assume the following is added to class C.
public String toString() {
int azAgeIn2018 = 2018 - 1912;
return "Arizona is " + azAgeIn2018 + " years young!";
}
What does System.out.println(new C()); print?
Answer: It will print ‘Arizona is 106 years young!’
[9] System.out.println(new R().hashCode()); prints: 1808253012.
No public int hashCode() method is implemented in class R.
Briefly explain why this compiles and runs?
Answer: As I mentioned earlier, all classes in Java inherits Object class. Object class has the method hashCode implemented by default. So if a class does not override hashCode() method, calling hashCode() method on the object of that class will invoke the super class’ (Object) hashCode method which returns the above value (note: depends on the system and architecture, this value can vary)
[10] If public int hashCode() { return 48; } is added to
class R, then what does the following statement print?
System.out.println("Arizona is state# " + new R().hashCode());
Answer: It will print ‘Arizona is state# 48’ since it will override the hashCode() method of Object class to return 48.