Final exam Flashcards

1
Q

4 phases running a Java program

A
  1. edit: store program in .java file
  2. compile: use java compiler (Javac) to create bytecodes from source code to .class file
  3. load class: read bytecodes in .class
  4. execute: Java Virtual Machine (JVM) translates bytecodes into machine language
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Fields

A

State or properties of object.

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

Methods

A

Behavior or actions of an object. May model concrete things, abstract things or tasks.

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

Classes

A

Classification of objects, describes commonality of sets of similar objects.

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

Object

A

Instance of a class.

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

Static fields

A

Independent of object, exists once per class and shared by all objects of class.

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

Constructor

A

Speical method, to create object of class (via new).

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

Static methods

A

Independent of any object, cannot access fields or methods. Connected to class.

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

Void methods

A

Type of method that returns nothing.

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

Array

A

Ordered list of variables of the same type. Size of array is predefined.

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

Program

A

Collection of interacting objects (from classes).

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

How to access fields?

A

objectName.fieldName

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

How to invoke methods?

A

objectName.methodName()

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

Which types are there + examples?

A
  1. primitive type: int, long, short, boolean, double, float, char (support operations +, -, *)
  2. reference type: classes
    String is special type: support concatenation (+)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

A variable ___ to an object, and ___ an address.

A

A variable refers to an object, and contains an address.

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

Which type has an address?

A

Reference type, stored as objects.

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

What does ‘ ==’ check between two variables?

A

Whether they have the same address.

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

What does ‘ c1.equals(c2)’ check between two variables?

A

Check the equivalence in fields.

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

Aliasing

A

Two different variables for the same object (address), with ‘ c1 = c2’

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

Garbage collection

A

Removes unused addresses on its own.

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

this Object

A

Default calling object. Inside class passed implicitly.

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

What are the Object Oriented fundamentals?

A

Encapsulation, Separation of Concerns and Loose Coupling.

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

Encapsulation

A

Information hiding from user, to keep people from breaking your code.

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

3 methods for encapsulation + explain

A
  1. change state only via behavior (getters and setters)
  2. hide implementation types (private methods)
  3. composition = identify what varies and separate that from what stays the same
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

Composition

A

Identify what varies and separate that from what stays the same. Create class that contains multiple fields, instead of multiple interfaces/subtypes.

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

Separation of Concerns

A

Keep things structured and clear. Classes should do one thing only.

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

Loose coupling

A

Achieved by design that promotes single-responsibility and separation of concerns. For example, interfaces.

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

Advantages of loose coupling

A
  1. allow to replace and extend your code
  2. can be consumed and tested independently of other classes
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

Getters

A

Enable read-access to private fields via its behavior (method).

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

Setters

A

Update state (field) of object via its behavior (method).

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

Interfaces

A

Common means for unrelated objects to communicate with each other. Only behavior, no implementation.

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

What can be in an interface?

A

An interface has abstract methods, default methods and constant definitions.

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

What is not possible with an interface?

A

An inteface has no constructors and instance variables.

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

Contracts

A

Maximum functionality a client can use, minimum functionality a server has to provide.

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

Goal of interfaces

A

Minimize dependencies between different parts by separating concern of implementation from concern of user.

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

How to use interface?

A

Implement it in a class and override its methods.
“public class <className> implements Interface1, Interface2, ..."</className>

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

Subtype rule

A

If method/variable requires reference of type A, then subtype of A may be provided.

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

Static type

A

Determined by declaration of variable/field/parameter. At compile time.

39
Q

Dynamic type

A

Determined during execution. Always a subtype of static type.

40
Q

Default methods

A

Method in interface that can be overriden, but does not need to because the return is already defined.

41
Q

Marker interface + why

A

Interface with no behavior, but useful to “mark” special types. Helpful if method should only be called on specific special classes.

42
Q

Abstract classes

A

Classes that should not create objects. Incomplete classes that describe contracts.

43
Q

What can an abstract class have?

A

Abstract classes can have fields and constructors (but cannot create objects!)

44
Q

How to use abstract classes?

A

Extend it by making a constructor with super() (and other fields) and override methods to implement.
“public class <className> extends abstractClass"</className>

45
Q

Abstract classes vs. interfaces

A
  • abstract classes can have fields, constructors and private methods
  • class can implement multiple interfaces, but extend only one abstract class
  • cannot create objects of abstract classes or interfaces
46
Q

(Object) Polymorphism

A

Object can take multiple forms or types, but only one dynamic type (which can be subtype of other types).

47
Q

Downcasting

A

Going down the hierarchy in types. Telling the compiler you are sure an object of a supertype is also an other (sub)type. Add new type between brackets in front of variable.

48
Q

Why avoid downcasting?

A

You can wrongly downcast, but no errors during compiling, because it trusts the programmer. Therefore these bugs can only show up during runtime, which you want to avoid.

49
Q

instanceof

A

instanceof operator tests whether an object is an instance of the specified type.

50
Q

Binding + types

A

Which method to call when objects/variables have multiple types. You have early and late binding.

51
Q

Late binding

A

Call method on dynamic time decided at runtime. Method calls on this object.

52
Q

Early binding

A

Use static type when calling method, decided at compile time. Method parameters.

53
Q

Double dispatching

A

Mimicking late binding for parameters via Dynamic Dispatcher trick.

54
Q

Static factory

A

Named constructor, static method that calls the constructor, but is hidden from the user.

55
Q

Advantadges static factory

A
  • hide logic from user
  • readable method to construct object (with lots of arguments)
56
Q

Builder

A

Class with all final private fields. Nested static Builder class with only required fields final, constructor with only required fields and the rest with setters, it has also a public build() method that will return an instance of the class. Class has private constructor with parameter Builder.

57
Q

Inheritance

A

Base class can be extended by a derived class.

58
Q

Base class

A

Any class.

59
Q

Derived class

A

Define subtypes, inherits fields and methods from base class. Could have additional fields and change overridden methods.

60
Q

super()

A

Method that calls the constructor of superclass (needed for Derived classes).

61
Q

super keyword

A

Keyword analogous to this, calling methods and fields from superclass.

62
Q

final keyword

A
  • final fields: constants
  • final methods: prevent overriding
  • final classes: prevent deriving
63
Q

Method signature

A

methodName()

64
Q

Overriding

A

Define method with same signature of the superclass. Can return any subtype.

65
Q

Overloading

A

Define method with same name but different parameter types.

66
Q

Hiding

A

Define variable (or method) with same name/signature.

67
Q

Object class

A

Implicit base class for every class, automatically extended by Java.

68
Q

Four methods in Object class

A
  1. String toString()
  2. boolean equals(Object other)
  3. int hashCode()
  4. Object clone()
69
Q

What does x.equals(null) return?

A

returns False

70
Q

Strong is-a relationship

A

Use inheritance.

71
Q

Has-a relationship

A

Use composition.

72
Q

Weak is-a relationship

A

Is-a-kind-of-relationship, use interfaces.

73
Q

Mixin

A

When multiple inheritance is needed. Combination of a primary type + secondary type from an interface.

74
Q

Runtime errors

A

Thrown exceptions.

75
Q

Exceptions

A

An object that represent the kind of error.

76
Q

How to throw an exception?

A

Create instance of appropiate exception and throw it.
“throw new TheException()”

77
Q

How to declare exceptions?

A

Methods must state the types of checked exceptions they may throw. “myMethod() throws TheException, OtherException”

78
Q

Unchecked exceptions

A

Programming errors. Exceptions from exception classes and subclasses.

79
Q

Checked exceptions

A

Everything except programming errors. For user or environment errors.

80
Q

How to deal with checked exceptions?

A
  • try-catch block
  • declare to throw exception in calling method
81
Q

Generics

A

Placeholder for a type.

82
Q

Collection interface

A

Interface that generalize a way to handle operations on different kinds of containers.

83
Q

List

A

Elements are ordered (indexing) and can occur more than once.

84
Q

Set

A

Elements not ordered (no index), no duplicates and can be (sometimes) sorted.

85
Q

Iterator design pattern

A

Traverse elements of collection without exposing its underlying representation.

86
Q

List interface

A

Extension of Collection, added methods to manipulate elements via indices.

87
Q

ArrayList

A

Implements List. Store elements in array, no pre-defined size.

88
Q

LinkedList

A

Implements List. Store elements in a recursive data structure. Has a head and a tail.

89
Q

Perks and cons ArrayList

A

Perk: accessing elements is fact O(1)
Con: inserting/deleting elements is expensive O(N)

90
Q

Perks and cons LinkedList

A

Perk: adding elements at begin and end fast O(1)
Con: Finding spot to add and remove at a certain index is O(i)

91
Q

==

A

Operator that checks equality of object-locations (not good for reference types).

92
Q

getClass()

A

Returns runtime class of an object.

93
Q

compareTo()

A

A method that check equality, but also whether it is smaller or bigger than the other.

94
Q

How to implement compareTo()?

A

public class <ClassName> implements Comparable<<ClassName>></ClassName></ClassName>