Carleton University - SYSC 2004
Part 1: Multiple Choice Questions
Final Exam Breakdown2) What is wrong with the following code?
public class ClassB extends ClassA {
public ClassB () {
int init = 10;
super(40);
}
}(a) Nothing is wrong with the code.
(b) The method super is not defined.
(c) No values may be passed to super.
(d) None of the above
The answer is (b).
In Java, if you want to call the constructor of a parent class using super(...), it must be the first statement in the constructor. In this code, the line int init = 10; comes first, which is not allowed. That’s why it causes a compile-time error. The fix would be to move super(40); to the very top of the constructor.
3) Provide one advantage of using generics in Java.
Generics allow your code to be more flexible and type-safe. This means:
You don’t need to cast values.
You reduce the chance of
ClassCastException.You make the code easier to read and maintain.
For example, using ArrayList<String> guarantees that only Strings go in, and Strings come out—no surprises.
4) Inheritance promotes code ___________.
(a) reinvention
(b) reuse
(c) repeats
(d) all of the above
The answer is (b).
The main point of inheritance is to reuse code from a base class in its subclasses. Instead of rewriting the same logic, a child class can just inherit it and use it as-is or override it if needed.
5) A derived class is also called a
(a) sub class
(b) super class
(c) base class
(d) all of the above
The answer is (a).
The term “derived class” is another way of saying “subclass.” It’s the class that extends another class. The superclass is the one being inherited from. You could also call it the Child class of a Parent class.
Keep reading this lesson
You've reached the end of the free preview. Unlock the rest of this lesson plus 3 more lessons in Carleton University - SYSC 2004.
- Unlimited coding questions
- Unlimited access to all classroom material
- Weekly live call for questions and guidance
- 3,000 AI tutor tokens / month
- Personal SQL database (3 MB)
- Progress tracking
Cancel any time.