Chapter 6 Class Design Flashcards
Review Questions
1. 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) { var seed = new BirdSeed(); System.out.print(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);.
G. The code prints 2 without any changes.
2. Which modifier pairs can be used together in a method declaration? (Choose all that apply.)
A. static and final
B. private and static
C. static and abstract
D. private and abstract
E. abstract and final
F. private and final
3. Which of the following statements about methods are true? (Choose all that apply.)
A. Overloaded methods must have the same signature.
B. Overridden methods must have the same signature.
C. Hidden methods must have the same signature.
D. Overloaded methods must have the same return type.
E. Overridden methods must have the same return type.
F. Hidden methods must have the same return type.
4. What is the output of the following program?
1: class Mammal { 2: private void sneeze() {} 3: public Mammal(int age) { 4: System.out.print("Mammal"); 5: } } 6: public class Platypus extends Mammal { 7: int sneeze() { return 1; } 8: public Platypus() { 9: System.out.print("Platypus"); 10: } 11: public static void main(String[] args) { 12: new Mammal(5); 13: } }
A. Platypus
B. Mammal
C. PlatypusMammal
D. MammalPlatypus
E. The code will compile if line 7 is changed.
F. The code will compile if line 9 is changed.
5. Which of the following complete the constructor so that this code prints out 50? (Choose all that apply.)
class Speedster { int numSpots; } public class Cheetah extends Speedster { int numSpots; public Cheetah(int numSpots) { // INSERT CODE HERE } public static void main(String[] args) { Speedster s = new Cheetah(50); System.out.print(s.numSpots); } }
A. numSpots = numSpots;
B. numSpots = this.numSpots;
C. this.numSpots = numSpots;
D. numSpots = super.numSpots;
E. super.numSpots = numSpots;
F. The code does not compile regardless of the code inserted into the
constructor.
G. None of the above
6. Which of the following declare immutable classes? (Choose all that apply.)
public final class Moose { private final int antlers; } public class Caribou { private int antlers = 10; } public class Reindeer { private final int antlers = 5; } public final class Elk {} public final class Deer { private final Object o = new Object(); }
A. Moose
B. Caribou
C. Reindeer
D. Elk
E. Deer
F. None of the above
7. What is the output of the following code?
1: class Arthropod { 2: protected void printName(long input) { 3: System.out.print("Arthropod"); 4: } 5: void printName(int input) { 6: System.out.print("Spooky"); 7: } } 8: public class Spider extends Arthropod { 9: protected void printName(int input) { 10: System.out.print("Spider"); 11: } 12: public static void main(String[] args) { 13: Arthropod a = new Spider(); 14: a.printName((short)4); 15: a.printName(4); 16: a.printName(5L); 17: } }
A. SpiderSpiderArthropod
B. SpiderSpiderSpider
C. SpiderSpookyArthropod
D. SpookySpiderArthropod
E. The code will not compile because of line 5.
F. The code will not compile because of line 9.
G. None of the above
8. What is the result of the following code?
1: abstract class Bird { 2: private final void fly() { System.out.println("Bird"); } 3: protected Bird() { System.out.print("Wow-"); } 4: } 5: public class Pelican extends Bird { 6: public Pelican() { System.out.print("Oh-"); } 7: protected void fly() { System.out.println("Pelican"); } 8: public static void main(String[] args) { 9: var chirp = new Pelican(); 10: chirp.fly(); 11: } }
A. Oh-Bird
B. Oh-Pelican
C. Wow-Oh-Bird
D. Wow-Oh-Pelican
E. The code contains a compilation error.
F. None of the above
9. Which of the following statements about overridden methods are true? (Choose all that apply.)
A. An overridden method must contain method parameters that are the same or covariant with the method parameters in the inherited method.
B. An overridden method may declare a new exception, provided it is not checked.
C. An overridden method must be more accessible than the method in the parent class.
D. An overridden method may declare a broader checked exception than the method in the parent class.
E. If an inherited method returns void, then the overridden version of the method must return void.
F. None of the above
10. Which of the following pairs, when inserted into the blanks, allow the code to compile? (Choose all that apply.)
1: public class Howler { 2: public Howler(long shadow) { 3: \_\_\_\_\_\_\_\_\_\_\_\_; 4: } 5: private Howler(int moon) { 6: super(); 7: } 8: } 9: class Wolf extends Howler { 10: protected Wolf(String stars) { 11: super(2L); 12: } 13: public Wolf() { 14: \_\_\_\_\_\_\_\_\_\_\_\_; 15: } 16: }
A. this(3) at line 3,this(“”) at line 14
B. this() at line 3,super(1) at line 14
C. this((short)1) at line 3,this(null) at line 14
D. super() at line 3,super() at line 14
E. this(2L) at line 3,super((short)2) at line 14
F. this(5) at line 3,super(null) at line 14
G. Remove lines 3 and 14.
11. What is the result of the following?
1: public class PolarBear { 2: StringBuilder value = new StringBuilder("t"); 3: { value.append("a"); } 4: { value.append("c"); } 5: private PolarBear() { 6: value.append("b"); 7: } 8: public PolarBear(String s) { 9: this(); 10: value.append(s); 11: } 12: public PolarBear(CharSequence p) { 13: value.append(p); 14: } 15: public static void main(String[] args) { 16: Object bear = new PolarBear(); 17: bear = new PolarBear("f"); 18: System.out.println(((PolarBear)bear).value); 19: } }
A. tacb
B. tacf
C. tacbf
D. tcafb
E. taftacb
F. The code does not compile.
G. An exception is thrown.
12. How many lines of the following program contain a compilation error?
1: public class Rodent { 2: public Rodent(Integer x) {} 3: protected static Integer chew() throws Exception { 4: System.out.println("Rodent is chewing"); 5: return 1; 6: } 7: } 8: class Beaver extends Rodent { 9: public Number chew() throws RuntimeException { 10: System.out.println("Beaver is chewing on wood"); 11: return 2; 12: } }
A. None
B. 1
C. 2
D. 3
E. 4
F. 5
13. Which of these classes compile and will include a default constructor created by the compiler? (Choose all that apply.)
A.
~~~
public class Bird {}
~~~
B.
~~~
public class Bird {
public bird() {}
}
~~~
C.
~~~
public class Bird {
public bird(String name) {}
}
~~~
D.
~~~
public class Bird {
public Bird() {}
}
~~~
E.
~~~
public class Bird {
Bird(String name) {}
}
~~~
F.
~~~
public class Bird {
private Bird(int age) {}
}
~~~
G.
~~~
public class Bird {
public Bird bird() { return null; }
}
~~~
14. Which of the following statements about inheritance are correct? (Choose all that apply.)
A. A class can directly extend any number of classes.
B. A class can implement any number of interfaces.
C. All variables inheritjava.lang.Object.
D. If classA is extended byB, thenB is a superclass ofA.
E. If classC implements interfaceD, thenC is a subtype ofD.
F. Multiple inheritance is the property of a class to have multiple direct superclasses.
15. Which statements about the following program are correct? (Choose all that apply.)
1: abstract class Nocturnal { 2: boolean isBlind(); 3: } 4: public class Owl extends Nocturnal { 5: public boolean isBlind() { return false; } 6: public static void main(String[] args) { 7: var nocturnal = (Nocturnal)new Owl(); 8: System.out.println(nocturnal.isBlind()); 9: } }
A. It compiles and printstrue.
B. It compiles and printsfalse.
C. The code will not compile because of line 2.
D. The code will not compile because of line 5.
E. The code will not compile because of line 7.
F. The code will not compile because of line 8.
G. None of the above