Kevins MCQs Flashcards

1
Q

What is a direct superclass?

A
The	superclass from which	the	
subclass	explicitly	inherits.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is an indirect superclass?

A
Any	class	above	the	direct	superclass	
in	the	class	hierarchy.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is single inheritance?

A

Java supports only single inheritance, in which each class is derived from exactly one direct superclass.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Is-a and Has-A: Which represents inheritance and which represents composition?

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the members of a class?

A

Instance variables & methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is difference between aggregation and composition?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is polymorphism?

A

When you define a supertype for a group of classes, any subclass of that supertype can be substituted where the supertype is expected.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What has an overloaded method got to do with inheritance and polymorphism?

A

Nothing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the four access modifier levels?

A

Public, Protected, Default (Package-Private), Private.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Explain four levels of access modifiers:

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What can interfaces contain (5 things)

A

Constants, method signatures, default methods, static methods, nested types. (Method bodies exist only for default and static methods).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Can interfaces be instantiated?

A

No, they can only be implemented by a class or extended by another interface.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the difference between a concrete and an abstract class?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you create an iterator for an Arraylist called employees with abstract superclass called SalesEmployee?

A

import java.util.Iterator;
Iterator itr = employees.iterator();
while (itr.hasNext()){
System.out.println(itr.next());

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you print out toString of objects in an Arraylist called employees of type SalesEmployee, using an enhanced for loop?

A

for (SalesEmployee temp : employees) {

System.out.println(“Now: “ + temp);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you declare an interface?

A

interface MyInterface { }

Interfaces are explicitly public and abstract so there is no need to declare them as such.

17
Q

How do you abstract classes and interfaces differ?

A

A class can implement many interfaces but extend just 1 abstract class. An abstract class can have code that implements, an interface cannot.

18
Q

Create an ArrayList of type SalesEmployee.

A
import util.java.ArrayList;
import util.java.List;
List employees = new ArrayList();
19
Q

When do you use serialization over writing a plain text file?

A

If your data will be used only by the java program that generated it use serialization.

20
Q

Why is the serializable interface known as a marker or tag interface?

A

It doesn’t have any methods to implement.

21
Q

What is the keyword for an instance variable that can’t be saved (serializable).

A

transient

22
Q

What happens to transient variable when they are deserialized?

A

null for objects, default for primitives.

23
Q

Write out code for serializing an arraylist called employees

A

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();
	}
24
Q

Write out code deserializing an arraylist of type SalesEmployee.

A

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();
		}
25
Q

Can an inner class use it’s outer class private methods and variables?

A

Yes.

26
Q

Why use inner classes (3 reasons)?

A
It increases encapsulation.
It leads to more readable and maintainable code.
It is a logical way to embed a class that is only used by one other class.
27
Q

When should you choose an ArrayList over a LinkedList?

A

If you want fast random access of the elements.

28
Q

When would you choose a LinkedList?

A

If fast adding and removing of elements at the end of the list is desired.

29
Q

What does Vector have that ArrayList and LinkedList don’t?

A

It is thread-safe.

30
Q

How do you sort an ArrayList called employees by implementing the collections sort method?

A

import java.util.Collections;

Collections.sort(employees);

31
Q

How do you make sure SalesEmployee class is comparable? Also make sure abstract method compareTO is implemented.

A
public class SalesEmployee implements Comparable{
  public int compareTo(SalesEmployee p){
  return firstName.compareTo(p.getFirstName());
  }
}
32
Q

Add a comparator to a Tester class fro comparing type SalesEmployess using getPPS() method.

A

import java.util.Comparator;

Nested class:
public static class EmployeeCompare implements Comparator {
		public int compare(SalesEmployee a, SalesEmployee b) {
			return a.getPPS().compareTo(b.getPPS());
		}

In the main method:
EmployeeCompare byPPS = new EmployeeCompare();
Collections.sort(employees, byPPS);