Which of the following complete the constructor so that this code prints out 50? (Choose all that apply)public class Cheetah {int numSpots;public Cheetah(int numSpots) {// INSERT CODE HERE}public static void main(String[] args) {System.out.println(new Cheetah(50).numSpots);}}*A. numSpots = numSpots;B. numSpots = this.numSpots;C. this.numSpots = numSpots;D. numSpots = super.numSpots;E. super.numSpots = numSpots;F. None of the above.
Question
Which of the following complete the constructor so that this code prints out 50? (Choose all that apply)public class Cheetah {int numSpots;public Cheetah(int numSpots) {// INSERT CODE HERE}public static void main(String[] args) {System.out.println(new Cheetah(50).numSpots);}}*A. numSpots = numSpots;B. numSpots = this.numSpots;C. this.numSpots = numSpots;D. numSpots = super.numSpots;E. super.numSpots = numSpots;F. None of the above.
Solution
The correct answer is C. "this.numSpots = numSpots;"
Here's why:
A. numSpots = numSpots; This would just assign the parameter to itself, not changing the instance variable.
B. numSpots = this.numSpots; This would assign the instance variable to itself, not changing it with the parameter value.
C. this.numSpots = numSpots; This is correct. It assigns the instance variable numSpots the value of the parameter numSpots.
D. numSpots = super.numSpots; This is not correct because super refers to the parent class, and in this case, the parent class is not specified and does not have a numSpots variable.
E. super.numSpots = numSpots; This is also not correct for the same reason as D.
F. None of the above. This is not correct because option C is a valid answer.
Similar Questions
Analyze the following code. Please select all that apply from the options below. public class Test { public static void main(String[] args) { A a = new A(); a.print(); } } class A { String s; A(String s) { this.s = s; } void print() { System.out.println(s); } } Group of answer choicesThe program has a compile error because class A is not a public class.The program has a compile error because class A does not have a default constructor.The program compiles and runs fine and prints nothing.The program would compile and run if you change A a = new A() to A a = new A("5")
Analyze the following code carefully. public class Test { int x; public Test(String t) { System.out.println("Test"); } public static void main(String[] args) { Test test = new Test(); System.out.println(test.x); } } Group of answer choicesThe program has a compile error because System.out.println method cannot be invoked from the constructor.The program has a compile error because x has not been initialized.The program has a compile error because you cannot create an object from the class that defines the object.The program has a compile error because Test does not have a default constructor.
Analyze the following code carefully. Please select the one that applies.public class Test { public static void main(String[] args) { A a = new A(); a.print(); }}class A { String s; A(String s) { this.s = s; }void print() { System.out.println(s); }}
Which of the following are true about the following code? (Choose all that apply)public class Create { Create() { System.out.print("1 "); } Create(int num) { System.out.print("2 "); } Create(Integer num) { System.out.print("3 "); } Create(Object num) { System.out.print("4 "); } Create(int... nums) { System.out.print("5 "); } public static void main(String[] args) { new Create(100); new Create(1000L); }}*A. The code prints out 2 4.B. The code prints out 3 4.C. The code prints out 4 2.D. The code prints out 4 4.E. The code prints 3 4 if you remove the constructor Create(int num).F. The code prints 4 4 if you remove the constructor Create(int num).G. The code prints 5 4 if you remove the constructor Create(int num).
Given1. class Person{2. //What would be correct constructor for this class3. } What would be correct constructor for Person class?*Person(){ System.out.print(“Person”); } // new return line super();void Person(String s){ }final Person(){ System.out.print(“Person”); //new return line }Persons(){ }private Person(int x){ }
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.