READ Free Dumps For Oracle- 1z0-804
Question ID 21413 | Given:
class A {
int a = 5;
String doA() { return "a1 "; }
protected static String doA2 () { return "a2 "; }
}
class B extends A {
int a = 7;
String doA() { return "b1 "; }
public static String doA2() { return "b2 "; }
void go() {
A myA = new B();
System.out.print(myA.doA() + myA.doA2() + myA.a);
}
public static void main (String[] args) {
new B().go();
}
}
Which three values will appear in the output?
|
Option A | 5
|
Option B | 7
|
Option C | a1
|
Option D | a2
|
Option E | b1
|
Option F | b2
|
Correct Answer | A,D,E |
Explanation Explanation: static method of base class is invoked >> A myA = new B(); System.out.print(myA.doA() + myA.doA2() + myA.a); class B String doA() { return "b1 "; } class A protected static String doA2 () { return "a2 "; } class B int a = 7;
Question ID 21414 | Given:
class Product {
private int id;
public Product (int id) {
this.id = id;
}
public int hashCode() {
return id + 42;
}
public boolean equals (Object obj) {
return (this == obj) ? true : super.equals(obj);
}
}
public class WareHouse {
public static void main(String[] args) {
Product p1 = new Product(10);
Product p2 = new Product(10);
Product p3 = new Product(20);
System.out.print(p1.equals(p2) + " ");
System.out.print(p1.equals(p3) );
}
}
What is the result?
|
Option A | false false
|
Option B | true false
|
Option C | true true
|
Option D | Compilation fails
|
Option E | An exception is thrown at runtime
|
Correct Answer | A |
Explanation Explanation: (this == obj) is the object implementation of equals() and therefore FALSE, if the reference points to various objects and then the super.equals() is invoked, the object method equals() what still result in FALSE better override of equals() is to compare the attributes like: public boolean equals (Object obj) { if (obj != null){ Product p = (Product)obj; return this.id == p.id; } return false; }