Java Flashcards

1
Q

What are the three key features of OOP and their advantages?

A

Encapsulation = Robustness & Reliability. Inheritance = Reuse & Interface Extension. Polymorphism = Implementation Reuse.

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

What is Encapsulation and it’s advantage using examples?

A

Encapsulation is to bind related functionality (Methods) & Data (Variables) in a protective wrapper (Class) with required access modifiers (public, private, default & protected). The class then provides a well defined interface to a set of functions to access and manipulate the data. This practice allows for a robust and reliable system where unauthorized access is controlled and internal workings are hidden from it’s users making it easier to maintain.

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

What is Inheritance and it’s advantage using examples?

A

Inheritance allows us to derive new classes from existing classes. A subclass inherits all the members (fields, methods, and nested classes) from its superclass. The inherited methods can be overridden with a new implementation or new methods added. The advantage of inheritance is code reuse and interface extension which means that the fields and methods of the existing class can be used without having to re-write and re-debug them.

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

What is Polymorphism and it’s advantage using examples?

A

Polymorphism is the ability of an object to take on many forms. For example when a parent class reference is used to refer to a child class object. The advantage of polymorphism is implementation reuse because of the objects ability to morph.

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

What is an Abstract Class, provide an example?

A

The purpose of an abstract class is to function as a base for subclasses. It may include both abstract and non-abstract methods, and all types of variables. Abstract classes cannot be instantiated, meaning you cannot create new instances of an abstract class.

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

What is an Interface, provide an example?

A

An interface is similar to a class but the body of an interface can include only abstract methods and final fields (constants). Interfaces cannot be instantiated and a class must implement an interface by providing code for each method declared by the interface.

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

What is an Overloading, provide an example?

A

Overloading is to add a new method with the same name as an inherited method but with a different signature (Static binding at compile time).

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

What is an Overriding, provide an example?

A

Overriding is to redefine an inherited method which have the same signature (dynamic runtime at runtime).

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

What is composition and how does it differ from inheritance?

A

Composition is when instances of other classes and primitives are members of the class. This differs to inheritance which holds an “is a” relationship and composition holds an “has a” relationship.

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

What are the four types of access modifiers?

A

Public which can be accessed from everywhere in the program, Private from inside the same class, Package from anywhere inside the package and Protected from a subclass and same package that contains the protected member.

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

What is a checked exception?

A

Describes a problem that is out of our control (i.e. disk failure) and the compiler handles them at compile time.

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

What is an unchecked exception?

A

Describes problems in the programmers code and these are not checked by the compiler.

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

What is the difference between a checked and unchecked exception?

A

Checked exceptions must be handled whereas unchecked exceptions may be ignored.

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

What is parametric polymorphism?

A

This is another term for Generics.

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

What is a method signature?

A

It is the method name and parameter list.

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

What is auto-boxing and auto-unboxing?

A

Occurs when a primitive types are converted into their wrapper objects, or converted back into their primitive types. Auto refers to the implicit use of = new Object();

17
Q

What is a String, StringBuilder and StringBuffer and when is it used?

A

A String is immutable and using methods on it (i.e. toLowerCase()) returns a new string. Strings should be used for objects that will not change. On the other hand, a StringBuilder is mutable, changing the object rather than returning a new one when changed. StringBuilder should be used when objects will change. StringBuffer is similar to StringBuilder but supports multithreading.

18
Q

What are the key steps in writing ANY equals() method?

A
1 - Set parameter to Object obj
2 - Check if object is null
3 - Check if object is an instance of
4 - Cast object into correct class
5 - Check if object is the same instance ( this == obj)
6 - Perform comparisons
19
Q

What is the difference between Object == Object versus Object.equals(Object)?

A

Using != and == operator to compare two objects will compare the memory location not the objects’ data. The equals() can be overridden to compare the data…

20
Q

What is a cast expression?

A

A cast expression coerces the type into another.

21
Q

What does “this” refer to?

A

This refers to the current object (instance).

22
Q

What is a static method?

A

A static method is a class level method which requires no object to invoke.

23
Q

What is an abstract method?

A

An abstract method needs to be implemented in a subclass.

24
Q

What is generics and its benefits?

A

Generics enable types (classes and interfaces) to be parameters when defining classes, methods, and interfaces. This allows for greater code reuse, stronger type checks at compile time, and eliminates the need for type casting.

25
Q

What are the two types of generic type?

A

Box classes which are used to hold objects and generic methods.

26
Q

What are generic wildcards (unbounded)?

A

It allows for any class –> ArrayList

27
Q

What are generic upperbound wildcards (bounded)?

A

It must match the class or any subtypes –> ArrayList

28
Q

What are generic lowerbound wildcards (bounded)?

A

It must match the class or any super type –> ArrayList

29
Q

What are the three characteristics of a recursive method?

A

1 - A base case for which we have a value to return
2 - A way to get closer to the base case (tail, head or mutual recursion)
3 - A recursive call which passes a simpler problem

30
Q

What is Serializable?

A

Serializable is a special interface that specifies that class is serialiazable. It’s special in that unlike a normal interface it does not define any methods that must be implemented: it is simply marking the class as serializable. “Serializable” simply means converting an instance of a class (an object) into a format where it can be written to disk, or possibly transmitted over a network. You could for example save your object to disk and reload it later, with all the field values and internal state saved.