Question
For any object obj, a call obj.getClass().getName() returns the name of the obj's class. Suppose System.out.println(new A() + + + new B()); displays
For any object obj, a call obj.getClass().getName() returns the name of the obj's class.
Suppose System.out.println(new A() + " + " + new B()); displays A + B
Which of the following implementations would produce that result?
I. Class A has a method
public String toString() { return "A"; }
and class B has a method
public String toString() { return "B"; }
II. Both class A and class B extend class X that has a method
public String toString() { return getClass().getname(); }
III. Both class A and class B extend an abstract class X that has methods
public abstract String getName();
public String toString() { return getname(); }
Class A has a method
public String toString() { return "A"; }
and class B has a method
public String toString() { return "B"; }
(A) I only
(B) II only
(C) I and II
(D) II and III
(E) I, II, and III
Consider the following class:
public class SaleItem implements Comparable
{
public SaleItem(int p) { prcice = p; }
< Comparison method header > { < Code not shown > }
public String toString() { return String.valueOf(price); }
private int price;
}
Which of the following could replace < Comparison method header > to make this class
compile with no errors?
I. public int compare(SatleItem item1, SaleItem item2)
II. public boolean compareTo(SaleItem item1)
III. public int compareTo(Object obj)
Page 9 of 14
(A) I only
(B) II only
(C) III only
(D) I or II
(E) II or III
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started