tg-me.com/topJavaQuizExplain/54
Last Update:
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);
Explanation:
Options A and B will not compile because constructors cannot be called without new. Options C and D will compile but will create a new object rather than setting the fields in this one. Option F will not compile because this() must be the first line of a constructor. Option E is correct.
BY Explanations “Top Java Quiz Questions”
Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283
Share with your friend now:
tg-me.com/topJavaQuizExplain/54