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);
Question
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);
Solution
The correct answer is E. Replace line 1 with this(2);
Explanation:
In Java, 'this' is a reference variable that refers to the current object. Here, 'this(2)' is used to call the current class constructor BirdSeed(int numberBags). So, when an object of BirdSeed is created with the default constructor, 'this(2)' will call the other constructor with the argument 2, which will initialize the variable 'numberBags' to 2. Therefore, when 'numberBags' is printed, it will output 2.
Similar Questions
Which option, inserted at line 4, produces the output 12?1. public class Test{2. public static void main(String [] args){3. int x = 0;4. // insert code here5. do{ } while(x++ < y);6. System.out.println(x);7. }8. }int y = x;int y = 10;int y = 11; int y = 12;
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
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 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.
Analyze the below program, and fill the correct code so that it produces the below output: 0 101class Book { private int bookId; private double bookPrice; public int getBookId() { return bookId; } public void setBookId(int bookId) { this.bookId = bookId; } public double getBookPrice() { return bookPrice; } public void setBookPrice(double bookPrice) { this.bookPrice = bookPrice; }}public class Test { public static void main(String a[]) { Book bobj=new Book(); blank blank blank }}Note : Same drag option can be used multiple times. Analyse and use the correct optionSystem.out.println(bobj.getBookPrice()); bobj.setBookId(101); System.out.println(bobj.getBookId());
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.