Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

What is the output from the following code segment? Comparable x = new Integer(1

ID: 3728449 • Letter: W

Question

What is the output from the following code segment?

       Comparable x = new Integer(123);       // Line 1

       System.out.println(x.compareTo("123"));   // Line 2

   (A)   0

   (B)   A positive integer

   (C)   A syntax error on Line 1

   (D)   A syntax error on Line 2

   (E)   A ClassCastException


Questions 16-17 refer to the following declaration:

   public interface Fillable{

       void fill(int x);

       int getCurrentAmount();

       int getMaximumCapacity();

   }

Which of the following would be the least likely to implement Fillable?

   (A)   A PencilCase class

   (B)   A GumBallMachine class

   (C)   A SwimmingPool class

   (D)   A DataBase class

   (E)   A MailBox class

Explanation / Answer

1. Comparable x = new Integer(123);       // Line 1

System.out.println(x.compareTo("123"));   // Line 2

In the first line, the new integer(123) returns an integer object with the value of 123. What the Comparable x it basically returns an object which can be compared to any other object. The class itself must implement the java.lang.Comparable interface to compare its instances.

talking about the line two it basically uses compareTo() method to compare x with "123", Since we know x is an object which refers to an inter value 123 but we compare it with a string value "123" and as we all know integer object in Java cannot be cast to a string object so this will throw an exception called A ClassCastException

If in the second line we remove the double quotes and replace it with something like this:

System.out.println(x.compareTo(123)); It will print 0 as both x and 123 are same.

System.out.println(x.compareTo(1)); It will print 1 as bith x and 1 are different.

2.

public interface Fillable{

       void fill(int x);

       int getCurrentAmount();

       int getMaximumCapacity();

   }

Here we are using an Interface, in which everything is abstract. So an interface does not have any constructors, Instance variables, or program code. A class which implements an Interface should implement all the methods in that interface.

So types of classes which can implement fillable could be-:

(A) A PencilCase class- A pencil class can implement fillable because it can have attributes like fill or the number of pencils you want to add, methods like int getCurrentAmount(); to get the present number of pencils in the case and int getMaximumCapacity(); to know the maximum capacity of the case.

(B) A GumBallMachine class-: A GumBallMachine class can implement fillable using the same methodology described above it can also have the same attributes as current gum balls, maximum gumballs etc.

(C) A SwimmingPool class-: A SwimmingPool class can implement fillable.

(D) A DataBase class-: A DataBase class is least likely to implement fillable Because a class which implements an Interface should implement all the methods in that interface but this class is the least likely to implement those methods.

(E) A MailBox class-: A MailBox class can implement fillable.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote