Final Flashcards

1
Q

What is inheritance?

A

When a new class is derived from an existing class

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

The superclass _______ the subclass

A

The superclass generalizes the subclass

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

In the UML diagram what is used to describe a sub/super class relation?

A

an arrow pointing from the sub class to the super class

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

What does the subclass inherit from the superclass?

A

The subclass inherits all methods and fields

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

What does Overriding mean?

A

The subclass can have its own version of the super class’s method. The subclass method must have the exact same signature.
Can use ‘@Override’ to avoid errors.

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

What is the Java syntax for Inheritance?

A

public class Shape{
protected String name;
}

public class Circle extends Shape{
private double radius;
}

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

Benefits of class inheritance?

A

One superclass can be reused by many subclasses, allows for DRY code.

Improves the structure of large programs(hierarchical).

Modular Programming(encapsulation).

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

Given a class called Circle that is a subclass of Shape.

if Shape has a method called draw() and Circle overrides that method, how would you reference the parent class overridden method?

A

super.draw() can be used to call the overridden parent method

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

Can a subclass inherit private fields/methods from a method?

A

No, private fields and methods are only accessible to the class it is declared in

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

Are the following subclasses the same?
a. public class A extends B{
}

b. public class A extends B{
Public A(){
super();
}
}

A

yes they are equivalent, in the first example Java will implicitly imply the super. keyword to invoke the parent constructor.

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

In the UML diagram what is used to reference generalization?

A

an Arrowhead that points to the Superclass

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

Are superclass constructors inherited?

A

No, superclass constructors are invoked either implicitly or explicitly.

Explicitly using the super() keyword

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

Who do these visibility modifiers grant access to?

a. Public

b. Private

c. none

d. Protected

A

a. All objects anywhere

b. Only objects of the same class

c. All objects of the same package

d. Only objects of the same subclass( or same package)

So from most visibility to least visibility is the following:

Public -> Protected -> none -> Private

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

Where does the super() keyword when being referenced in a child constructor?

Also given a parent class Person which has a protected field String name. Write the syntax for a constructor of the child class called Student with private int gpa.

A

The first line of the child constructor

public Student(String name, int gpa){
super(name)
this.gpa = gpa
}

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

What is the difference between single and multilevel inheritance?

A

single level inheritance is when methods/fields are being inherited from one level above

Multilevel inheritance is when methods/fields are inherited from non parent classes(parents of their parents)

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

Given the following class and constructor

public class A{
private int num;

public A(int num){
this.num = num;
}
}

Would a child class of this need to call the constructor?

A

public class B extends A{

public B(int num, String name){
super(num);
this.name = name;
}
}

Yes when the parent class doesn’t use the default constructor you must explicitly invoke the super() constructor.

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

True or False?

When a derived object is created
the “super portion”
defined in the superclass
must also be
created and initialized?

A

True

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

What is the Object class? Also provide all methods it provides

A

All classes in Java are subclasses of the Object class whether they extend it or not.

toString()
equals()
getClass()
hashCode()
finalize()

Note: these methods can be Overriden

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

True or False?

All objects in Java instantiate the Object class.

A

True

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

Applying the final modifier to a class, who can inherit it?

How about applying final to one of its methods instead?

A

When applying final to a class, that class can no longer be extended(inherited).

When applying final to a method that method can no longer be overridden.

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

Why is the String class final?

A

The String class is immutable, therefore its objects cannot be modified after creation. Another reason for String being final is for security.

22
Q

Given a subclass that extends an abstract class, what must the subclass do otherwise a Java error occurs?

A

Assuming the subclass is not also an abstract class, the subclass must implement all abstract methods from the parent class, like so:

in parent: public abstract double calcArea()

in child: public double calcArea()

23
Q

What can hold abstract methods?

A

Abstract methods must be in an abstract class or interface, While an abstract class can hold non abstract methods

24
Q

Can a subclass override a method and make it abstract?

A

yes

25
Q

Can you instantiate a abstract class using the new keyword?

How about using it as a datafield?

A

You cannot instantiate an object of an abstract class

You can specify an abstract class as a data field.

26
Q

Explain the relation between java.util.Calendar
and java.util.GregorianCalendar

A

Calendar is an abstract class, and gregeorianCalendar is a concrete subclass of Calendar. Contains the get method for objects of the Calendar class.

GregorianCalender is formatted like year,month, date where month parameter is 0-based. January is 0.

27
Q

Why is an interface useful?

A

An interface is a blueprint that contains abstract methods and constants. The intent of an interface is to specify common behavior for objects.

note: Any fields declared in an interface are implicitly static and final

Examples of common interfaces(behaviours): Comparable, Edible, and Cloneable

28
Q

Create an interface called Edible, that has a string method called howToEat

A

public interface Edible {
public abstract String howToEat();
}

29
Q

Why are interfaces treated like special classes in Java?

A

Each interface is compiled into a separate bytecode file

30
Q

Given the following interface, what is implicitly implied?

public interface T1{
int NUM = 1;

void p();

}

A

public interface T1{
public static final int NUM = 1;

public abstract void p();
}

methods are always abstract and fields are always static and final.

31
Q

What does the Comparable interface consist of? and write the Comparable interface.

Also what does the method in it return?

A

Comes with the compareTo abstract method
package java.lang;

public interface Comparable<E> {
public int compareTo (E o);
}</E>

compareTo returns negative if object is less than o. returns 0 if object is equal to o. returns positive if object is greater than o.

32
Q

Write a simple class called Dog that implements the comparable class and has a name field and has the compareTo method

A

public class Dog implements Comparable< Dog > {
private String name;

@Override
public int compareTo(Dog other) {
33
Q

What is the bytesize of the following datatypes?

a. int
b. long
c. float
d. double
e. char

A

a. 4
b. 8
c. 4
d. 8
e. 2

34
Q

What are two classes we use that implement the Comparable interface by default.

A

The Integer and BigInteger Classes

35
Q

Write code in the main method to compare integers 3 and 5 without defining objects.

A

System.out.println(new Integer(3).compareTo(new Integer(5))

//This should output a negative int

36
Q

What does instanceof do and assume n is a Integer object write code checking n using instanceof

A

if(n instanceof Integer){
}

37
Q

What does the Cloneable interface do, and what are it’s contents?

A

Cloneable interface is used to clone an object.

Here are the contents:
package java.lang;
public interface Cloneable {
}

Yes the cloneable interface is empty. An empty interface is known as a marker interface.

38
Q

Write a class called Animal has a age field, has a explicit constructor and implements the cloneable interface.

A

public class Animal implements Cloneable {
private int age;

public Animal(int age) {
    this.age = age;
}

@Override
public Animal clone() {
    try {
        return (Animal) super.clone();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
39
Q

What is conflicting interfaces?

A

On rare occasions, a class may implement two interfaces with conflict information (e.g., two same constants with different
values or two methods with same signature but different
return type). This type of errors will be detected by the
compiler.

40
Q

What is Polymorphism

A

Essentially the idea of polymorphism is that an object of a subtype can be used wherever it’s supertype is required.
Example1 : Animal a1 = new Dog()

or

public static void m(Object x){
}

m(a1)
Notice the m method is stated to take the Object type, but since Object is the superclass of all subtypes we can use any class to substitute.

41
Q

How does dynamic binding work?

A

If an object invokes a method, Java will first search the most specific subclass, and then go up the hierarchy of more general classes(parents and parents of parents, etc) until it finds a class that defines the method.

42
Q

Does Java support Multiple Inheritance?

A

Java doesn’t support traditional Multiple Inheritance, as attempting to extend more than one class will cause a compile error.

Multiple Inheritance can be achieved through Interfaces.

43
Q

A java Class will implement interfaces, but what is it called when an interface is trying to use another interface?

A

Interface extends other interfaces. Note that an interface can extend multiple interfaces.

44
Q

What does a Collection mean? and provide examples

A

A collection is a container object that holds
a group of objects, often referred to as
elements.

Sets
Lists(Arraylist, LinkedList)
Maps(Key value pairs)
Stacks(First in, Last out)
Queues(First in, first out)
PriorityQueues

45
Q

What are some of the methods of the Collection interface?

A

clear() - removes all elements from this collection
add() - adds a new element to collection
addAll() - adds all elements from a collection to this one
remove() - remove element from collection
removeAll() - remove all elements that are in that collection from current collection
retainAll() - Only keep elements that are common in both collections

Note: AbstractCollection is an abstract class that partially implements Collection

46
Q

Given Collection A and Collection B,
how would you:
a. Remove all elements from A?

b. Add all elements from B to A?

c. Remove all elements in A from B

d. Only keep common elements that both A and B have

A

a. A.clear()

b. A.addAll(B)

c. B.removeAll(A)

d. A.retainAll(B)

47
Q

What are the classes of the List interface? and common methods?

A

ArrayList and LinkedList

Common Methods:
add(index, element)
addAll(index, collection)
remove(index)
set(index, element) - changes value of index(removes old value)
indexOf(element)
lastIndexOf(element)
subList(fromIndex, toIndex) - creates another list based on indexes, inclusive.

48
Q

Given the following:
List< String> list
Assuming it has values how would you output all elements of the list traversing forward?

A

ListIterator< String> iterator = list.listIterator();

while(iterator.hasNext()){
System.out.println(iterator.next());
}

49
Q

Given the following:
List< String> list
Assuming it has values
How to traverse the list backwards using ListIterator?

A

ListIterator< String> iterator = list.listIterator(list.size());

while(iterator.hasPrevious()){
System.out.println(iterator.previous());
}

50
Q

Given two versions of the same class

A. public class Circle{
double radius
public boolean equals(Circle c){
return this.radius == c.radius
}
}

B. public class Circle{
double radius
public boolean equals(Object o){
return this.radius == ((Circle)o.radius)
}
}

If this is the main:
Object o1 = new Circle();
Object o2 = new Circle ();
System.out.println(o1.equals(o2))

What would output of versions A and B be?

A

A. would be False
In the main o2 is defined as Object class, so with polymorphism it can still be passed in as a Circle, when being compared using == , without being casted using (Circle) the == operator returns false

B. would be True
since the o2 being passed into the equals method is casted as Circle, the comparison would be between two of the same classes.

51
Q

What is an inner class?

A

A class in a class, the inner class can use private fields/methods of the class it exists in.

It is compiled like so: OuterClass$InnerClass.class.

An inner class can be declared static

52
Q

True or False?
If a method is declared private in the superclass, you may declare the method protected in the subclass.

A

True,
the methods would be unrelated to each other, not overridden. The private modifier would prevent the subclass from inheriting it, allowing the subclass to create it’s own protected version of the method.