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

Which of the following methods is from the Object class and often overridden? co

ID: 3788361 • Letter: W

Question

Which of the following methods is from the Object class and often overridden?

compareTo

remove

add

equals

size

All classes in Java inherit from the ______ class.

Class

Object

Parent

Static

Void

You have written a class called Frog and have not written a toString method.

Frog f = new Frog();
System.out.println(f);

What happens when the code above is executed?

Error - Frog has no toString method.

"Ribbit" is printed.

The toString method in Object is called.

Error - a class cannot be instantiated without the toString method.

Nothing happens.

What is output by the following code?

String [] w = {"zebra", "blaze"};
System.out.println(w[1].charAt(w[1].length() - 1));

a

b

e

r

z

You have written a class called Tree. In a second program you have the line:

Tree.getTemperature();

Which of the following must be true about the method getTemperature() for the method call to work?

getTemperature() must be void.

getTemperature() must be declared static.

getTemperature() must return a value.

getTemperature() must be declared private .

Nothing, this method call is not legal and will cause an error.

Questions 6-8 refer to the following class hierarchy:

Square extends Quadrilateral
Quadrilateral extends Polygon

Which of the following is true?

This class hierarchy will cause an error since Square can only have one parent class.

When a Square is instantiated the constructors in Quadrilateral and Polygon are not called.

Square has access to all methods and variables in Quadrilateral.

Square does not have access to any methods in Quadrilateral or Polygon.

Square has access to all public methods and variables in Quadrilateral.

Which of the following statements must be true in order for Square to access the constructor in Polygon?

Square has access to all public methods and variables in Polygon and will never need to directly call a constructor.

The call to Polygon's constructor must be the last line in Square's constructor.

The call to Polygon's constructor must be the first line in Square's constructor.

Square must first access Quadrilateral's constructor, and Quadrilateral's constructor must call Polygon's constructor.

Square's constructor must be declared private.

If a class Shape is added, which of the following would make the most sense?

Shape should be a child of Square.

Shape should be a child of Quadrilateral.

Shape should be a child of Polygon.

Polygon should be a child of Shape.

Shape should not be included in the hierarchy.

Write the header for the default constructor of a class called House.

public static House()

public int House()

public House(int numRooms, int floors)

public House()

public void House()

Which of the following is not true about a constructor?

It must have a void or return type.

It must have the same name as the class.

It should be declared public.

It initializes the instance variables of the class.

It must not have a void or return type.

Explanation / Answer

Q)Which of the following methods is from the Object class and often overridden?

Ans)compareTo

Reason:

copareTo() is the method of Object class.Rest of the methods are not.

Q) All classes in Java inherit from the ______ class.

Ans)Object

Reason:

Every class in java must be the subclass of Object class.

Object is the parent class to every class in java.

Q) You have written a class called Frog and have not written a toString method.

Ans) The toString() method in Object is called.

Reason:

As Object is the super class to every class in java,then Frog class must be also the sub class of Object class.If we didn’t provide the toString() method in Frog class then the toString() method of Object class will get executed.

Q) What is output by the following code?

String [] w = {"zebra", "blaze"};
System.out.println(w[1].charAt(w[1].length() - 1));

Ans)e

Reason:

w[0] represents “zebra”

w[1] represents “blaze”

w[1].length()=5

w[1].charAt(w[1].length()-1)=4

b       l         a        z        e

0        1        2        3        4

System.out.println(w[1].charAt(w[1].length() - 1)); //e

Q) You have written a class called Tree. In a second program you have the line:

Tree.getTemperature();

Which of the following must be true about the method getTemperature() for the method call to work?

Ans) getTemperature() must be declared static.

Reason:

We can call the method of a class without creating the Object of the class if we declared the method as static .Then we can call the method by using Classname.methodname

Q) Square extends Quadrilateral
Quadrilateral extends Polygon

Which of the following is true?

Ans) Square has access to all public methods and variables in Quadrilateral.

Q) Which of the following statements must be true in order for Square to access the constructor in Polygon?

An) Square must first access Quadrilateral's constructor, and Quadrilateral's constructor must call Polygon's constructor.

Q) If a class Shape is added, which of the following would make the most sense?

Ans) Shape should be a child of Quadrilateral.

Q) Write the header for the default constructor of a class called House.

Ans) public House()

Q) Which of the following is not true about a constructor?

Ans) It must have a void or return type.

Reason:

A constructor shouldn’t return anything.even it shouldn’t have void as return type.

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