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
Composition
Identify what varies and separate that from what stays the same. Create class that contains multiple fields, instead of multiple interfaces/subtypes.
26
Separation of Concerns
Keep things structured and clear. Classes should do one thing only.
27
Loose coupling
Achieved by design that promotes single-responsibility and separation of concerns. For example, interfaces.
28
Advantages of loose coupling
1. allow to replace and extend your code 2. can be consumed and tested independently of other classes
29
Getters
Enable read-access to private fields via its behavior (method).
30
Setters
Update state (field) of object via its behavior (method).
31
Interfaces
Common means for unrelated objects to communicate with each other. Only behavior, no implementation.
32
What can be in an interface?
An interface has abstract methods, default methods and constant definitions.
33
What is not possible with an interface?
An inteface has no constructors and instance variables.
34
Contracts
Maximum functionality a client can use, minimum functionality a server has to provide.
35
Goal of interfaces
Minimize dependencies between different parts by separating concern of implementation from concern of user.
36
How to use interface?
Implement it in a class and override its methods. "public class implements Interface1, Interface2, ..."
37
Subtype rule
If method/variable requires reference of type A, then subtype of A may be provided.
38
Static type
Determined by declaration of variable/field/parameter. At compile time.
39
Dynamic type
Determined during execution. Always a subtype of static type.
40
Default methods
Method in interface that can be overriden, but does not need to because the return is already defined.
41
Marker interface + why
Interface with no behavior, but useful to "mark" special types. Helpful if method should only be called on specific special classes.
42
Abstract classes
Classes that should not create objects. Incomplete classes that describe contracts.
43
What can an abstract class have?
Abstract classes can have fields and constructors (but cannot create objects!)
44
How to use abstract classes?
Extend it by making a constructor with super() (and other fields) and override methods to implement. "public class extends abstractClass"
45
Abstract classes vs. interfaces
- 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
(Object) Polymorphism
Object can take multiple forms or types, but only one dynamic type (which can be subtype of other types).
47
Downcasting
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
Why avoid downcasting?
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
instanceof
instanceof operator tests whether an object is an instance of the specified type.
50
Binding + types
Which method to call when objects/variables have multiple types. You have early and late binding.
51
Late binding
Call method on dynamic time decided at runtime. Method calls on this object.
52
Early binding
Use static type when calling method, decided at compile time. Method parameters.
53
Double dispatching
Mimicking late binding for parameters via Dynamic Dispatcher trick.
54
Static factory
Named constructor, static method that calls the constructor, but is hidden from the user.
55
Advantadges static factory
- hide logic from user - readable method to construct object (with lots of arguments)
56
Builder
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
Inheritance
Base class can be extended by a derived class.
58
Base class
Any class.
59
Derived class
Define subtypes, inherits fields and methods from base class. Could have additional fields and change overridden methods.
60
super()
Method that calls the constructor of superclass (needed for Derived classes).
61
super keyword
Keyword analogous to this, calling methods and fields from superclass.
62
final keyword
- final fields: constants - final methods: prevent overriding - final classes: prevent deriving
63
Method signature
methodName()
64
Overriding
Define method with same signature of the superclass. Can return any subtype.
65
Overloading
Define method with same name but different parameter types.
66
Hiding
Define variable (or method) with same name/signature.
67
Object class
Implicit base class for every class, automatically extended by Java.
68
Four methods in Object class
1. String toString() 2. boolean equals(Object other) 3. int hashCode() 4. Object clone()
69
What does x.equals(null) return?
returns False
70
Strong is-a relationship
Use inheritance.
71
Has-a relationship
Use composition.
72
Weak is-a relationship
Is-a-kind-of-relationship, use interfaces.
73
Mixin
When multiple inheritance is needed. Combination of a primary type + secondary type from an interface.
74
Runtime errors
Thrown exceptions.
75
Exceptions
An object that represent the kind of error.
76
How to throw an exception?
Create instance of appropiate exception and throw it. "throw new TheException()"
77
How to declare exceptions?
Methods must state the types of checked exceptions they may throw. "myMethod() throws TheException, OtherException"
78
Unchecked exceptions
Programming errors. Exceptions from exception classes and subclasses.
79
Checked exceptions
Everything except programming errors. For user or environment errors.
80
How to deal with checked exceptions?
- try-catch block - declare to throw exception in calling method
81
Generics
Placeholder for a type.
82
Collection interface
Interface that generalize a way to handle operations on different kinds of containers.
83
List
Elements are ordered (indexing) and can occur more than once.
84
Set
Elements not ordered (no index), no duplicates and can be (sometimes) sorted.
85
Iterator design pattern
Traverse elements of collection without exposing its underlying representation.
86
List interface
Extension of Collection, added methods to manipulate elements via indices.
87
ArrayList
Implements List. Store elements in array, no pre-defined size.
88
LinkedList
Implements List. Store elements in a recursive data structure. Has a head and a tail.
89
Perks and cons ArrayList
Perk: accessing elements is fact O(1) Con: inserting/deleting elements is expensive O(N)
90
Perks and cons LinkedList
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
==
Operator that checks equality of object-locations (not good for reference types).
92
getClass()
Returns runtime class of an object.
93
compareTo()
A method that check equality, but also whether it is smaller or bigger than the other.
94
How to implement compareTo()?
public class implements Comparable<>