pre midterm Flashcards
Can you change the return type when overriding a method?
Not for primitive types.
What is a covariant return type?
When the return type of an overriding method is changed to a descendent of the original method’s return type.
In Java, a function with code that might throw an exception, and that is not caught in said function, must declare it by using the throws.
True
If a parent class has a method int func(), a child class can reduce its access modifier (make it more restrictive).
False, the child can only make it less restrictive.
A child class automatically overrides every method of its parent class so long as they have the same signature.
False, not “every” method can be overridden (private, static, final).
The super() constructor can always be called in a child constructor as long as it is called first.
False, the superclass may not have a default constructor.
In Java, a class can both inherit from multiple classes and implement multiple interfaces.
False, a class can only inherit from 1 other class.
Is this line of code correct? : public class PartTimeEmployee implements Serializable extends Employee { … }
False, “extend” must come first.
Java allows you to declare a variable of an abstract class.
True, you cannot create instances of it but you can declare pointers of it.
All types of exceptions must follow the Catch or Declare rule.
False, this isn’t true for unchecked exceptions.
Abstract classes can have contructors.
True.
When using PrintWriter, there is no way to append to a file - it is always overriten.
False
A grand-child class can easily call its grandparent class using super().super()
false
Order of catch blocks are wirtten is unimportant
false - more specific first
Super constructor is always called, either explicitily or implicitely.
True.
A derived class with an overridden method can add exceptions to the list of thrown exceptions.
You can only add unchecked exceptions or checked exceptions that are children of the original method’s thrown exceptions. Generally, false.
The finally block is executed after an exception is successfully caught.
True, but not only after an exception is caught, and not if a catch containing system.exit(0); executes.
In java it is possible to throw an exception, catch it, then re-throw the same exception if desired.
True, but using another try-catch within the catch block that “re-throws” the exception.
In order for polymorphism to work, it is sometimes crucial to create an abstract base class (parent) that has at least one private method.
False - nonsense.
If a base class has a method such as: public int fun1(), an inherited class cannot reduce accessibility to this method by declaring it as protected.
True
Assume class B is inherited from class A. The following statement, where a1 and b1 are references to Objects A and B, would result in compilation error:
a1 = (A)b1;
False, we’re allowed to up-cast. Even if we were down-casting, we wouldn’t get a compilation error because we are forcing the cast.
A constructer of an inherited class has the right to call the this() constructor and the super() constructer, but super() must be called first.
False : this() and super() cannot be used in the same constructor under any conditions.
Even if a class A is created as an abstract class, the following code will still be valid :
A a1;
True
What’s the order of access modifiers from most restrictive to least?
Private, Default (package), Protected, Public
What does the equals() method do when it is not defined by the object calling it?
It compares addresses.
Which method from the BufferedReader may return -1?
read()
How do you detect the end of a binary file?
When an exception is thrown.
When does overriding occur in place of overloading?
When the new method has the same number and types of parameters as in the base class.
When a subclass overloads a method from the base class, does the subclass still inherit the method of the same name from the baseclass?
Yes.
A final method cannot be overridden.
True.
A final class can be used to derive other classes.
False.
If super is not used, a call to the default
constructor of the base class is always issued.
False, a (compilation) error occurs in cases where the base class has no default constructor.
A derived type can be use in place of an ancestor type.
True.
What three things can we change about a method definition to overload a method?
Change number of parameters, change type of parameters or change order of parameters to overload.
The super constructor must be the first action of a constructor.
True.