Kevins MCQs Flashcards
(32 cards)
What is a direct superclass?
The superclass from which the subclass explicitly inherits.
What is an indirect superclass?
Any class above the direct superclass in the class hierarchy.
What is single inheritance?
Java supports only single inheritance, in which each class is derived from exactly one direct superclass.
Is-a and Has-A: Which represents inheritance and which represents composition?
Is-a represents inheritance - In an is-a relationship, an object of a subclass can also be treated as an object of its superclass – i.e. Lecturer is a Faculty member
Has-a represents composition - In a has-a relationship, an object contains as members references to other objects.
What are the members of a class?
Instance variables & methods.
What is difference between aggregation and composition?
In a Has-A relationship:
Composition is a strong relationship: A Composes B, means A contains and owns B.
Aggregration between A and B means, A contains but not owns B.
What is polymorphism?
When you define a supertype for a group of classes, any subclass of that supertype can be substituted where the supertype is expected.
What has an overloaded method got to do with inheritance and polymorphism?
Nothing.
What are the four access modifier levels?
Public, Protected, Default (Package-Private), Private.
Explain four levels of access modifiers:
Public (all classes can access), Protected(Classes in the same package as well as ‘extends’), Default (Classes in the same package), Private (just the class it is in).
What can interfaces contain (5 things)
Constants, method signatures, default methods, static methods, nested types. (Method bodies exist only for default and static methods).
Can interfaces be instantiated?
No, they can only be implemented by a class or extended by another interface.
What is the difference between a concrete and an abstract class?
An abstract class can not be instantiated. It is declared using the abstract keyword. A concrete class is on one that is not abstract.
How do you create an iterator for an Arraylist called employees with abstract superclass called SalesEmployee?
import java.util.Iterator;
Iterator itr = employees.iterator();
while (itr.hasNext()){
System.out.println(itr.next());
How do you print out toString of objects in an Arraylist called employees of type SalesEmployee, using an enhanced for loop?
for (SalesEmployee temp : employees) {
System.out.println(“Now: “ + temp);
How do you declare an interface?
interface MyInterface { }
Interfaces are explicitly public and abstract so there is no need to declare them as such.
How do you abstract classes and interfaces differ?
A class can implement many interfaces but extend just 1 abstract class. An abstract class can have code that implements, an interface cannot.
Create an ArrayList of type SalesEmployee.
import util.java.ArrayList; import util.java.List; List employees = new ArrayList();
When do you use serialization over writing a plain text file?
If your data will be used only by the java program that generated it use serialization.
Why is the serializable interface known as a marker or tag interface?
It doesn’t have any methods to implement.
What is the keyword for an instance variable that can’t be saved (serializable).
transient
What happens to transient variable when they are deserialized?
null for objects, default for primitives.
Write out code for serializing an arraylist called employees
Object must implement Serializable and import java.io.Serializable;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
try { FileOutputStream fileStream = new FileOutputStream("EmployeeInfo.dat"); ObjectOutputStream os = new ObjectOutputStream(fileStream); os.writeObject(employees); os.close(); } catch (Exception e) { e.printStackTrace(); }
Write out code deserializing an arraylist of type SalesEmployee.
import java.io.FileInputStream;
import java.io.ObjectOutputStream;
try { FileInputStream fileStream = new FileInputStream("EmployeeInfo.dat"); ObjectInputStream os = new ObjectInputStream(fileStream); List employees2 = (List)os.readObject(); for (SalesEmployee element : employees2) { System.out.println(element); } os.close(); } catch (Exception e) { e.printStackTrace(); }