return (this == obj) ? true : super.equals(obj);

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?

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?

A.
false false

B.
true false

C.
true true

D.
Compilation fails

E.
An exception is thrown at runtime

Explanation:
Hint for line: public class Warehouse {
class WareHouse is public, should be declared in a file named WareHouse.java.
Compilation result:
Error: Main method not found in class product.Product, please define the main method as:
public static void main(String[] args)
Java Result: 1



Leave a Reply 10

Your email address will not be published. Required fields are marked *


Boris

Boris

on my opinion the answer should be A.
If to compile this source everything will work in compiled WareHouse.java

manish purohit

manish purohit

answer should be True,False if compile and run.file name and class name should be same if class is public.

Ray

Ray

The correct answer should be A.
false false

Serg

Serg

+1
false false => The correct answer should be A.

gelete

gelete

A

executed, output: false false

wojtek

wojtek

tested -> A

eee

eee

class 2 obj=mew class 2 ();
obj.print();

class2{
private p1=10;
private p2=9;
print(){
this.p1=9;
out.print(this.p1);
out.print(this.p2+1);
}

what the output of this statement pllz ?