Introduction Flashcards

1
Q

Java supports single an multilevel inheritance. What does that mean?

A

A class cannot extent from more than one class directly, but it can use a hierarchy

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

What is Polymorphism?

A

Polymorphism is the ability to process data differently depending on their types of inputs -> Same method name but with different parameters

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

What is dynamic Polymorphism?

A

Child class overrides the parent’s method

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

What is static Polymorphism?

A

Polymorphism which is resolved at compile time and thus does away with runtime virtual table lookups -> Multiple methods in one class have the same name

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

What are possible problems with Polymorphism?

A

Type identification during downcasting and fragile base class problem

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

When to use inheritance instead of composition?

A

Inheritance is used if classes fulfil the “is-a” condition and mainly provide additive functionality further down the class hierarchy

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

When to use composition instead of inheritance?

A

Composition allows to model objects that are made up of other objects -> the object(s) that compose or are contained by one object are destroyed too when that object is destroyed

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

What does the variable scope define?

A

The variable scope defines where variables can be seen

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

What method modifiers exist in java? And what is the default modifier?

A

public, protected, private, and package-private. Default is package-private

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

What is the problem with comparing two objects with the == operator?

A

Comparing two objects with the == operator returns false because it is not the values that get compared but the memory spaces -> Comparable and Comparator Interface

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

What are characteristics of abstract classes?

A
  • Abstract modifier
  • Abstract class can be subclassed, but it cannot be instantiated
  • Abstract class has at least one abstract method but can declare both abstract and concrete methods
  • A subclass derived from an abstract class must either implement ALL the abstract methods or be abstract itself
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are typical scenarios where we should prefer abstract classes over interfaces and concrete classes?

A
  • Encapsulate some common functionality in one place that multiple subclasses will share
  • Partially define an API that subclasses can easily extend and refine
  • Subclasses need to inherit one or more common methods or fields with protected access
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is an interface?

A

An interface is an abstract type that contains a collection of method and constant variables

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

Why are interfaces used?

A

Interfaces are used to achieve abstraction, polymorphism and multiple inheritances

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

What are characteristics of an interface?

A
  • We cannot instantiate interfaces directly
  • Interfaces can be empty -> No variables or methods
  • Interface methods cannot be protected or final
  • Interface’s variables are public, static, and final by default and we are not allowed to change their visibility
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What can we achieve by using interfaces?

A
  • Behavioral Functionality
  • Multiple Inheritances
  • Polymorphism
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What are the interface inheritance rules?

A
  • Interface extending another interface -> All abstract methods are inherited
  • Abstract class implementing an interface -> All abstract and default methods are inherited
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is the difference between a class and an interface?

A
  • Concrete class is a blueprint for object creation
  • Interface is a user-defined type which can have a collection of field constants and method signatures that will be overwritten by an implementing class
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What is a functional interface?

A

A functional interface is an interface that contains only one abstract method

20
Q

When to use an interface?

A
  • Using multiple inheritances and if there are different class hierarchies
  • When unrelated classes implement the interface
  • When application functionalities have to be defined as a contract, but not concerned about who implements the behavior
21
Q

When to use an abstract class?

A
  • When trying to provide a base class with methods that the subclasses override
  • If we have specified requirements and only partial implementation details
  • While classes that extend abstract classes have several common fields or methods (that require non-public modifiers)
  • If one wants to have non-final or non-static methods to modify the states of an object
22
Q

For what can the super keyword be used?

A
  • Access the parent class
  • Super() method can be used to call the parent default constructor
  • Accessing parent class variables
  • Accessing methods from the parent class
23
Q

For what can the this keyword be used?

A
  • Reference to the current object whose method is being called
  • Disambiguating field shadowing
  • Referencing constructors of the same class
  • Returning this to return the current class instance
  • Access the outer class and it’s variables from within the inner class
24
Q

What is field shadowing?

A

Example: If constructor parameters have the same name as instance fields

25
Q

What is a generic constructor?

A

A generic constructor is a constructor that has at least one parameter of a generic type

26
Q

What are anonymous classes?

A

Anonymous classes are inner classes with no name that you use to only instantiate one single object of that class ever -> You need to define that class and instantiate one object at the same time
Generally anonymous classes are just a particular case of nested classes

27
Q

What are use cases for anonymous classes?

A
  • Use inner classes in general use cases and anonymous classes in very specific ones in order to achieve a cleaner hierarchy
  • Are usually used if the implementations of some methods of some classes needs to be modified on the fly
  • Creating various event listeners in an application with graphical interface
28
Q

What are nested classes?

A

A nested class is a class that is declared inside another class or interface

29
Q

What is variable hiding?

A
  • Local variables shadow instance variables with the same name
  • When both the child and the parent classes have a variable with the same name, the child’s variable hides the one from the parent
30
Q

What is method hiding?

A

When a child class defines a static method with the same signature as a static method in the parent class, then the child method hides the one in the parent class

31
Q

What is an immutable object?

A

An immutable object is an object whose contents cannot be changed, so the internal state remains constant after it has been entirely created

32
Q

What are the benefits of immutable objects?

A
  • It can be safely shared among multiple threads
  • Immutable objects are side-effects free because none of the objects referencing it will notice any difference
33
Q

When are exceptions raised and what are possible reasons?

A

Exceptions are raised when the normal flow of the application is disrupted. Possible reasons are wrong input data, lose of a connection or an unexpected situation

34
Q

When is the throw keyword used?

A

The throw keyword is used to explicitly throw an exception. This exception must be a subclass of Throwable but it cannot throw multiple exceptions with a single throw

35
Q

When is the throws keyword used?

A

The throws keyword can be placed in the method declaration. It denotes which exceptions can be thrown from this method. These exceptions must be handles with a try-catch

36
Q

What are checked exceptions?

A

Checked exceptions are checked at compile time

37
Q

What are most common checked exceptions?

A

Most common checked exceptions are IOExceptions, FileNotFoundExceptions, ParseExceptions

38
Q

What are unchecked exceptions?

A

Unchecked exceptions are thrown during runtime

39
Q

What are most common unchecked exceptions?

A

Most common unchecked exceptions are ArrayIndexOutOfBoundsExceptions, IllegalArgumentException, NullPointerException

40
Q

What is a JVM?

A
  • Java Virtual Machine
  • Is an implementation of a virtual machine which executes a java program
  • It is an abstract computing machine with it’s own instruction set and manipulates various memory areas at runtime
41
Q

How does a JVM work?

A

First interprets the bytecode, then stores class information in the memory area, and finally it executes the bytecode generated by the java compiler

42
Q

What is a JRE?

A
  • Java Runtime Environment
  • Is a bundle of software components used to run java applications
43
Q

What are components of a JRE?

A
  • An implementation of a JVM
  • Classes required to run the java programs
  • Property files
44
Q

What are components of a JVM?

A
  • Class loaders
  • Runtime data areas
  • Execution engine
  • Java native interface
  • Native libraries
45
Q

What is a JDK?

A
  • Java Development Kit
  • Provides environment and tools for developing, compiling, debugging, and executing a java program
46
Q

What are components of a JDK?

A
  • JRE
  • Development tools