Which of the following statements can be inserted in the blank so that the code will compile successfully? (Choose all that apply)public class Snake {} public class Cobra extends Snake {} public class GardenSnake {} public class SnakeHandler { private Snake snake; public void setSnake(Snake snake) { this.snake = snake; } public static void main(String[] args) { new SnakeHandler().setSnake( ); } } *3 pointsA. new Cobra()B. new GardenSnake()C. new Snake()D. new Object()E. new String("Snake")F. null
Question
Which of the following statements can be inserted in the blank so that the code will compile successfully? (Choose all that apply)public class Snake {} public class Cobra extends Snake {} public class GardenSnake {} public class SnakeHandler { private Snake snake; public void setSnake(Snake snake) { this.snake = snake; } public static void main(String[] args) { new SnakeHandler().setSnake( ); } } *3 pointsA. new Cobra()B. new GardenSnake()C. new Snake()D. new Object()E. new String("Snake")F. null
Solution
The correct answers are:
A. new Cobra() C. new Snake() F. null
Explanation:
A. new Cobra() - This is valid because Cobra is a subclass of Snake, so a Cobra object can be passed where a Snake object is expected (this is called polymorphism).
C. new Snake() - This is valid because it's directly creating a new instance of the Snake class.
F. null - This is valid because null can be a value for an object reference, which includes those of type Snake.
B. new GardenSnake() - This is not valid because GardenSnake is not a subclass of Snake.
D. new Object() - This is not valid because Object is a superclass of Snake, not a subclass. You can't pass a superclass where a subclass is expected.
E. new String("Snake") - This is not valid because String is not a subclass of Snake.
Similar Questions
Which of the following statements can be inserted in the blank line so that the code will compile successfully? (Choose all that apply) public interface CanHop {} public class Frog implements CanHop { public static void main(String[] args) {______________ frog = new TurtleFrog(); } } public class BrazilianHornedFrog extends Frog {} public class TurtleFrog extends Frog {} *4 pointsA. FrogB. TurtleFrogC. BrazilianHornedFrogD. CanHopE. ObjectF. Long
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).
Which of the following will compile when inserted in the following code? (Choose all that apply)public class Order3 { final String value1 = "1"; static String value2 = "2"; String value3 = "3"; { // CODE SNIPPET 1 } static { // CODE SNIPPET 2 }}*A. value1 = "d"; instead of // CODE SNIPPET 1B. value2 = "e"; instead of // CODE SNIPPET 1C. value3 = "f"; instead of // CODE SNIPPET 1D. value1 = "g"; instead of // CODE SNIPPET 2E. value2 = "h"; instead of // CODE SNIPPET 2F. value3 = "i"; instead of // CODE SNIPPET 2
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.
Which code can be inserted to have the code print 2?public class BirdSeed { private int numberBags; boolean call; public BirdSeed() { // LINE 1 call = false; // LINE 2 } public BirdSeed(int numberBags) { this.numberBags = numberBags; } public static void main(String[] args) { BirdSeed seed = new BirdSeed(); System.out.println(seed.numberBags);} }*A. Replace line 1 with BirdSeed(2);B. Replace line 2 with BirdSeed(2);C. Replace line 1 with new BirdSeed(2);D. Replace line 2 with new BirdSeed(2);E. Replace line 1 with this(2);F. Replace line 2 with this(2);
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.