Ask Question

Consider the following class declaration.

public class Thing

{

private String color;

public Thing ()

{

color = "Blue";

}

public Thing (String setColor)

{

color = setColor;

}

}

Which of the following code segments, when appearing in a class other than Thing, would create a reference of type Thing with a value of null?

Thing someThing = new Thing ("Green");

A

Thing someThing = new Thing ("");

B

Thing someThing = new Thing ();

C

Thing someThing;

D

Thing ("Green");

E

+1
Answers (1)
  1. 23 April, 22:59
    0
    Thing someThing; is the right answer

    Explanation:

    A. Thing someThing = new Thing ("Green");

    This would create a reference type with value "Green". This is because this calls the constructor with one parameter.

    B. Thing someThing = new Thing ("");

    This would create a reference type with value "" (empty). This is because once again it calls constructor with one parameter.

    C. Thing someThing = new Thing ();

    This would set an object with value as "blue". This is because in no argument constructor, the value of the color is set to blue.

    D. Thing someThing;

    This is the right answer, because it will declare an object but it will not create it. So it has only null value by default.

    E. Thing ("Green"); This is an invalid statement.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Consider the following class declaration. public class Thing { private String color; public Thing () { color = "Blue"; } public Thing ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers