IS5103 LESSON 6 QUIZ

1.  Which of the following statements can be inserted in the blank so that  the code will compile successfully? 
abstract class Snake {} 
class Cobra extends Snake {} 
class GardenSnake extends Cobra {} 
public class SnakeHandler { 
private Snake snakey; 
public void setSnake(Snake mySnake) { this.snakey = mySnake; } 
public static void main(String[] args) { 
 new SnakeHandler().setSnake(_________________); 

}
 Each correct answer represents a complete solution. Choose all that apply

A) new Cobra() 
new GardenSnake() 
null 

2. Which of the following statements about inheritance are correct? 
Each correct answer represents a complete solution. Choose all that apply.

A) 1. A class can implement any number of interfaces. 
2.If class C implements interface D, then C is a subtype of D. 
3.Multiple inheritance is the property of a class to have multiple  direct superclasses. 

 3. What does the following program print? 
1. class Person { 
2. static String name; 
3. void setName(String q) { name = q; } } 
4. public class Child extends Person { 
5. static String name; 
6. void setName(String w) { name = w; } 
7. public static void main(String[] p) { 
8. final Child m = new Child();
9. final Person t = m; 
10. m.name = “Elysia”; 
11. t.name = “Sophia”; 
12. m.setName(“Webby”); 
13. t.setName(“Olivia”);
14. System.out.println(m.name + ” ” + t.name); 15. } }

A) Olivia Sophia 

4. 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 this(2);. 

5. 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) The code will compile if line 9 is changed. 

6. Which of the following complete the constructor so that this code prints out  50? 
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) super.numSpots = numSpots; 

7. Which of the following pairs, when inserted into the blanks, allow the code to  compile? 
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. } 
Each correct answer represents a part of the solution. Choose all that apply.

A) this(3) at line 3,this(“”) at line 14
this((short)1) at line 3,this(null) at line 14 

8. Which of these classes compile and will include a default constructor  created by the compiler? 
Each correct answer represents a complete solution. Choose all that apply.

A) public class Bird {} 
public class Bird { public Bird bird() { return null; }} 

9. Which of the following are true? 
Each correct answer represents a complete solution. Choose all that apply.

A) this.variableName can be called from any instance method in the class. 
You can access a private constructor with the main() method in the  same class. 

10. What is the result of the following code? 
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) Tacbf 

11. 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) 2 

12. What is the result of the following? 
1. class Arachnid { 
2. static StringBuilder sb = new StringBuilder(); 
3. { sb.append(“c”); }
4. static
5. { sb.append(“u”); } 
6. { sb.append(“r”); }
7. } 
8. public class Scorpion extends Arachnid
{ 9. static 
10. { sb.append(“q”); } 
11. { sb.append(“m”); } 
12. public static void main(String[] args) {
13. System.out.print(Scorpion.sb + ” “);
14. System.out.print(Scorpion.sb + ” “);
15. new Arachnid(); 
16. new Scorpion(); 
17. System.out.print(Scorpion.sb);
18. } } 

A) uq uq uqcrcrm 

13. What is the output of the following program? 
1. class Canine { 
2. public Canine(boolean t) { logger.append(“a”); }
3. public Canine() { logger.append(“q”); }
4. 
5. private StringBuilder logger = new StringBuilder();
6. protected void print(String v) { logger.append(v); }
7. protected String view() { return logger.toString(); }
8. } 
9. 
10. class Fox extends Canine { 
11. public Fox(long x) { print(“p”); }
12. public Fox(String name) { 
13. this(2); 
14. print(“z”);
15. } 
16. } 
17. 
18. public class Fennec extends Fox { 19. public Fennec(int e) {
20. super(“tails”); 
21. print(“j”); 
22. } 
23. public Fennec(short f) { 
24. super(“eevee”); 
25. print(“m”); 
26. } 
27. 
28. public static void main(String… unused) { 29. System.out.println(new Fennec(1).view()); 30. } } 

A) Qpzj 

14. What is printed by the following program? 
1. class Antelope { 
2. public Antelope(int p) { 
3. System.out.print(“4”); 
4. } 
5. { System.out.print(“2”); } 
6. static { System.out.print(“1”); } 
7. } 
8. public class Gazelle extends Antelope { 
9. public Gazelle(int p) { 
10. super(6); 
11. System.out.print(“3”); 
12. } 
13. public static void main(String hopping[]) {
14. new Gazelle(0); 
15. } 
16. static { System.out.print(“8”); } 
17. { System.out.print(“9”); } 
18. } 

A) 182493 

15. Which of the following statements about methods are true?
Each correct answer represents a complete solution. Choose all that apply A) Overridden methods must have the same signature. 

A) Hidden methods must have the same signature. 

16. 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 

17. Which of the following statements about overridden methods are true? 
Each correct answer represents a complete solution. Choose all that apply.

A) An overridden method may declare a new exception, provided it is  not checked. 
If an inherited method returns void, then the overridden version of he  method must return void. 

18. Which statements about the following classes are correct? 
1. public class Mammal { 
2. private void eat() {}
3. protected static void drink() {} 
4. public Integer dance(String p) { return null; } 
5. } 
6. class Primate extends Mammal { 
7. public void eat(String p) {} 
8. } 
9. class Monkey extends Primate { 
10. public static void drink() throws RuntimeException {} 
11. public Number dance(CharSequence p) { return null; } 
12. public int eat(String p) {} 
13. } 
Each correct answer represents a complete solution. Choose all that apply. 

A) The drink() method in Mammal is correctly hidden on line 10.
The dance() method in Mammal is correctly overloaded on line 11.

19. What is the output of the following code? 
1. class Reptile { 
2. {System.out.print(“A”);} 
3. public Reptile(int hatch) {} 
4. void layEggs() { 
5. System.out.print(“Reptile”); 
6. } } 
7. public class Lizard extends Reptile { 
8. static {System.out.print(“B”);} 
9. public Lizard(int hatch) {} 
10. public final void layEggs() { 
11. System.out.print(“Lizard”); 
12. } 
13. public static void main(String[] args) { 
14. var reptile = new Lizard(1); 
15. reptile.layEggs(); 
16. } } 

A) None of these

20. Which statement about the following program is correct? 
1. class Bird { 
2. int feathers = 0; 
3. Bird(int x) { this.feathers = x; } 
4. Bird fly() { 
5. return new Bird(1); 
6. } } 
7. class Parrot extends Bird { 
8. protected Parrot(int y) { super(y); } 
9. protected Parrot fly() { 
10. return new Parrot(2); 
11. } } 
12. public class Macaw extends Parrot { 
13. public Macaw(int z) { super(z); } 
14. public Macaw fly() { 
15. return new Macaw(3); 
16. } 
17. public static void main(String… sing) { 
18. Bird p = new Macaw(4); 
19. System.out.print(((Parrot)p.fly()).feathers); 20. } } 

A) The program compiles and prints 3. 

21. Which modifier pairs can be used together in a method  declaration? 
Each correct answer represents a complete solution. Choose all that apply.

A) static and final 
private and static 
private and final 

22. 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) Wow-Oh-Pelican 

23. Which statement about the following program is correct? 
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) The code will not compile because of line 2. 

24. Which of the following are true about a concrete class? 
Each correct answer represents a complete solution. Choose all that apply. A) A concrete class must implement all inherited abstract methods. 

A concrete class can be marked as final. 

25. What is the output of the following code? 
1. public abstract class Whale { 
2. public abstract void dive(); 
3. public static void main(String[] args) { 
4. Whale whale = new Orca(); 
5. whale.dive(3); 
6. } 
7. } 
8. class Orca extends Whale {
9. static public int MAX = 3; 
10. public void dive() { 
11. System.out.println(“Orca diving”); 
12. } 
13. public void dive(int… depth) { 
14. System.out.println(“Orca diving deeper “+MAX); 15. } } 

A) The code will not compile because of line 8. 

26. Which of the following declare immutable classes? 
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(); 

Each correct answer represents a part of the solution. Choose all that apply.

A) Elk 
Deer 

27. Which of the following are properties of immutable classes? Each correct answer represents a complete solution. Choose all that apply. 

A) The class must not be able to be extended outside the class  declaration. 
The data for mutable instance variables may be read, provided they  cannot be modified by the caller.

Other Links:

Statistics Quiz

Networking Quiz

See other websites for quiz:

Check on QUIZLET

Check on CHEGG

Leave a Reply

Your email address will not be published. Required fields are marked *