Java Inheritance, Abstract Classes, and Polymorphism Assignment

Verified

Added on  2022/11/29

|10
|1491
|154
Homework Assignment
AI Summary
This document presents the solutions to a Java homework assignment focusing on inheritance, polymorphism, and abstract classes. The assignment covers key concepts such as the Object class, constructors in subclasses, method overriding, and the use of 'super'. It includes multiple-choice questions exploring class relationships, method definitions (including toString, addEm, and changeEm), and the implications of abstract classes and the 'final' modifier. The solutions demonstrate how to correctly define constructors, override methods, and utilize the 'super' keyword to call superclass methods. Furthermore, the assignment explores inheritance scenarios with multiple classes and evaluates code snippets to identify correct behavior and potential errors related to method overriding and access modifiers.
Document Page
Question 4:
All classes in Java are directly or indirectly subclasses of the ________ class.
Question 4 options:
A) Object
B) this
C) String
D) Wrapper
Solution:
A) Object
Question 5
For the questions below, consider the following class definition:
1. public class AClass{
2. protected int x;
3. protected int y;
4. public AClass(int a, int b){
5. x = a;
6. y = b;
7. }
8. public int addEm( ){
9. return x + y;
10. }
11. public void changeEm( ){
12. x++;
13. y--;
14. }
15. public String toString( ){
16. return "" + x + " " + y;
17. }
18. }
Assume you are going to add a
public class BClass exends AClass{
private int z;
}, which of the following choice works best as the constructor for BClass?
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
Question 5 options:
A) public BClass(int a, int b, int c){
super(a, b, c);
}
B) public BClass(int a, int b, int c){
super( );
}
C) public BClass(int a, int b, int c){
super(a, b);
z = c;
}
D) public BClass(int a, int b, int c){
x = a;
y = b;
z = c;
}
Solution:
C) public BClass(int a, int b, int c){
super(a, b);
z = c;
}
Question 6 (3 points)
Which of the following would best redefine the toString method for BClass?
Question 6 options:
A) public String toString( ){
Document Page
return super.toString( ) + " " + z;
}
B) public String toString(int z){
return " " + x + " " + y + " " + z;
}
C) public String toString( ){
return super.toString( );
}
D) public String toString( ){
return super.toString( ) + " " x + " " + y + " " + z;
}
Solution:
A) public String toString( ){
return super.toString( ) + " " + z;
}
Question 7 (3 points)
You want addEm to now add all three values and return the sum and changeEm to change x and
y, but leave z alone. Which should you do?
Question 7 options:
A) Redefine addEm to return the value of z + super.addEm( ), but leave changeEm alone
B) Redefine addEm and changeEm without referencing super.addEm( ) or
super.changeEm( )
C) Redefine changeEm to call super.changeEm( ) and then set z = x + y, but leave addEm
alone
D) Redefine addEm to return the value of z + super.addEm( ) and redefine changeEm to
call super.changeEm( ) and then set z = x + y
Solution:
Document Page
A) Redefine addEm to return the value of z + super.addEm( ), but leave changeEm alone
Question 8 (3 points)
What is wrong with the following code?
public class ClassC extends ClassA{
public ClassC(){
int init = 10;
super(40);
}
}
Question 8 options:
A) No values may be passed to super.
B) Nothing is wrong with the code.
C) The method super is not defined.
D) The call to the method super must be the first statement in the constructor
Solution:
D) The call to the method super must be the first statement in the constructor
Question 9
Look at the following code. The method in line ________ will override the method in line
________.
Line 1 public class ClassA
Line 2 {
Line 3 public ClassA() {}
Line 4 public int method1(int a){}
Line 5 public double method2(int b){}
Line 6 }
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
Line 7 public ClassB extends ClassA
Line 8 {
Line 9 public ClassB(){}
Line 10 public int method1(int b, int c){}
Line 11 public double method2(double c){}
Line 12 }
Question 9 options:
A) None of the above
B) 0, 4
C) 11, 5
D) Both A and B
Solution:
A) None of the above
Question 10 (1 point)
When a method is declared with the ________ modifier, it cannot be overridden in a subclass.
Question 10 options:
A) extends
B) public
C) final
D) super
Solution:
C) final
Question 11 (3 points)
Look at the following code.
Line 1 public class ClassA
Document Page
Line 2 {
Line 3 public ClassA() {}
Line 4 public void method1(){}
Line 5 }
Line 6 public class ClassB extends ClassA
Line 7 {
Line 8 public ClassB(){}
Line 9 public void method1(){}
Line 10 }
Line 11 public class ClassC extends ClassB
Line 12 {
Line 13 public ClassC(){}
Line 14 public void method1(String s){}
Line 15 }
Which method1 will be executed as a result of the following statements?
ClassC item1 = new ClassC();
item1.method1();
Question 11 options:
A) none of the above
B) Line 14
C) Line 9
D) Line 4
Solution:
C) Line 9
Document Page
Question 12 (3 points)
If a class contains an abstract method:
Question 12 options:
A) the method must be overridden in subclasses, otherwise, the subclass has to be abstract
B) the method will have only a header, but not a body, and end with a semicolon
C) you cannot create an instance of the class
D) All of the above
Solution:
D) All of the above
Question 13 (1 point)
A subclass may call an overridden superclass method by:
Question 13 options:
A) using the extends keyword before the method is called
B) prefixing its name with the name of the superclass in parentheses
C) calling the superclass method first and then calling the subclass method
D) prefixing its name with the super key word and a dot (.)
Solution:
D) prefixing its name with the super key word and a dot (.)
Question 14 (3 points)
public class classA {
protected int val;
public classA() {
val =10;
}
public classA(int num){
val =num;
}
public void setVal(int num){
val = num;
}
public int getVal(){
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
return val;
}
}
public class classB extends classA{
public classB(int num)
{
super(num);
}
}
public class classC extends classB{
public classC(){
super(10);
setVal(super.getVal()+20);
}
}
(BONUS) what is the output for
classC test = new classC();
System.out.println(test.val);
Question 14 options:
A) 20
B) none of the above
C) 10
D) 30
Solution:
D) 30
Question 15 (1 point)
A super class can have multiple sub classes.
Question 15 options:
Document Page
True
False
Solution:
True
Question 16 (1 point)
A sub class can have multiple super classes in Java.
Question 16 options:
True
False
Solution:
False
Question 17 (1 point)
An abstract class can be a parent class of a non-abstract class.
Question 17 options:
True
False
Solution:
True
Question 18 (1 point)
A non-abtract class can be a parent of a abstract sub class.
Question 18 options:
True
False
Solution:
True
Question 19 (1 point)
Document Page
An abstract class must have at least one abstract method.
Question 19 options:
True
False
Solution:
True
Question 20 (1 point)
An abstract class can have more than one construtors.
Question 20 options:
True
False
Solution:
True
chevron_up_icon
1 out of 10
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]