COMP213 - Advanced Object-Oriented Programming Flashcards

1
Q

Define ‘immutable’

A

An object whose state cannot be modified after the object is created

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

What is an object?

A

An allocated region of storage.

In procedural programming it can contain data or instructions but not both, in object-oriented programming it may contain data and instructions for that data.

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

What is an array?

A

A container object that holds a fixed number of values of a single type.

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

What is an element?

A

An item in an array

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

What is a class?

A

A template/blueprint that will be used in constructing objects. It may contain fields that describe its state or methods that determine its behaviour

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

What is public/private data?

A
Public data can be accessed from any class or method (e.g. card.name to access the public 'name' variable in the card class)
Private data can only be accessed within its class
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are access modifiers?

A

A command word that specifies whether a variable is public, private or secret

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

What is API?

A

Application Program Interface (e.g. Javadoc)

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

What tags are available in Javadoc?

A
Any HTML tags as well as:
@author your name
@param the parameters of the method (on separate lines)
@return what the method holds
@throws what exceptions can be thrown
@version number or date of code
@since when this part has first existed
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are local variables?

A

Variables declared within a method

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

What are instance variables?

A

Variables declared within a class, not within a method

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

What does the keyword ‘this’ do?

A

References the current object (e.g. this.name is the instance variable ‘name’, as opposed to the local variable ‘name’)

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

What is the initial value of an integer?

A

0

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

What is the initial value of a float?

A

0.0

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

What is the initial value of a boolean?

A

false

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

What is the initial value of a reference variable?

A

null

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

What does a class constructor do?

A

Creates objects from the class blueprint

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

How are identical methods distinguished?

A

By the types of their parameters

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

What is a nested class?

A

A class declared inside another class (not a subclass or extension of a class)

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

What are the two types of nested class?

A

Static nested classes with the modifier ‘static’

Non-static nested classes called ‘Inner’

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

What are the motivations for implementing nested classes?

A
logical grouping: if a class is only useful to one other class, it is logical to nest it within
encapsulation: if a nested class is private within its top-level enclosing it is invisible to the outside world
more readable code: nesting small classes within top-level classes places the code closer to where it is used
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What is a queue?

A

A first-in-first-out (FIFO) data structure

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

What is a java interface?

A

An abstract type that is used to specify a behaviour that classes must implement (e.g. a queue specifies that methods are needed to (a) take element from the head and (b) add an element to the back)

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

What is inheritance?

A

The process where one class gets non-private properties (methods and fields, but not constructors) of another.

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

What is a child class?

A

The class that inherits the properties of another (e.g. an extended class, a subclass)

26
Q

What is a parent class?

A

A class whose properties are inherited (e.g. base class, superclass)

27
Q

The keyword ‘extends’…

A

Makes a child class an extension of a parent class

28
Q

The keyword ‘super’…

A

Refers to the parent class to avoid confusion for classes with the same name or to invoke constructors of the parent class

29
Q

What is polymorphism?

A

The ability to process objects of various types and classes through a single, uniform interface

30
Q

How is static polymorphism achieved?

A

Through method overloading: several methods having the same name but different types/order/number of parameters (note: it’s not enough just to change the return type)

31
Q

How is dynamic polymorphism achieved?

A

Method overriding: a child class overrides a method with the same signature in the parent (e.g. both classes have a void method that prints something, the child class’s method will run instead of the parent class’s)

32
Q

The keyword ‘final’…

A
Restricts the user in the context of variables, parameters, methods, and classes (but not constructors)
A class declared 'final' can't be extended
A method declared 'final' in a parent class is still inherited to child classes, but cannot be overridden
A variable/parameter declared 'final' has a fixed value and becomes a constant
33
Q

Can a constructor be declared private?

A

Yes, but only if other constructors (i.e. with different parameters) are not private

34
Q

What are generic methods/classes?

A

Methods or classes that have type (template)
parameters, which can be replaced by actual
types, so one code works for many types

35
Q

What is an abstract method?

A
A method (with the keyword abstract) that has only a declaration, but no code.
A class containing an abstract method is also abstract and should be extended before objects can be created
36
Q

What is the point of abstract classes?

A

Help save time and effort by implementing essential methods that should be shared by several types of data

37
Q

What is an interface in java?

A

An abstract class with three types of methods: abstract, static and default

38
Q

How is an interface declared?

A

public interface Player {
….
}
Can either be public or have no modifier,
All methods in an interface are implicitly public
All fields in an interface are implicitly public static final (constants)

39
Q

The keyword ‘implements’….

A
Allows a class to implement all abstract methods of an interface
(e.g. public class Human implements Player{})
All methods in a class implementing an interface should be declared public
40
Q

What are default methods?

A

Methods declared default in an interface are to be inherited by a class implementing the interface or by another interface extending the interface. Interface, interface interface. Interface.

41
Q

What is a local class?

A

A class defined within a method (e.g. in an if statement or for loop), they are non-static and can’t contain static declarations (exception for constants (public static final))

42
Q

What is an anonymous class?

A

A one-use-only, nameless class that is declared and initiated at the same time (typically used for initialising local variables)

43
Q

What is encapsulation?

A

All values of a data object are hidden in a class so that unauthorised parties cannot access them

44
Q

What does concurrency mean?

A

That software can handle many transactions “simultaneously” even on a single processing unit

45
Q

What is a thread?

A

A lightweight process that acts as a path to be followed during program execution

46
Q

What is an exception (in programming)?

A

An event which occurs during the execution of the program that disrupts the normal flow of the program’s instructions.

47
Q

What is an exception (in Java)?

A

An object that wraps an error event that occurred within a method. It has information about the error, a state of the code when the error occurred, an potentially other custom information

48
Q

What is the call stack?

A

A stack of information about the active subroutines of a program

49
Q

What does it mean to ‘throw’ an exception?

A

When an error occurs in a method, the method creates an exception object and hands it to the runtime system, which then attempts to find a block of code (exception handler) to handle the exception - it searches through the call stack in reverse order, and terminates if there is no suitable handler whose type matches the exception

50
Q

What does it mean to ‘throw’ an exception?

A

When an error occurs in a method, the method creates an exception object and hands it to the runtime system, which then tries to find a block of code (exception handler) to handle the exception - the search begins in the method where the error occurred and goes backwards through the call stack. The program terminates if no handler is found

51
Q

Advantages for using exceptions

A

Error handling is separate from regular code
Cleaner algorithms, less clutter
Exceptions propagate errors up the call stack (more reliable, less work)
Exception classes group and differentiate error types

52
Q

The keywords ‘try’ and ‘catch’…

A

Separate code from error handling, by placing risky code in the try block and exception handling in the catch block (multiple catch blocks can be used for different exceptions)

53
Q

The keyword ‘finally’….

A

Denotes a block of code that is to be executed regardless of the exceptions in the try…catch preceding it

54
Q

The keyword ‘throws’….

A
declares and propagates an exception up the call stack,
is followed by class of an exception,
is used with the method signature after the parameter list,
can declare multiple exceptions.
55
Q

What does it mean to ‘rethrow’ an exception?

A

To throw an exception already caught in the catch block to pass it to a higher level function
E.g.
try{…}
catch ArithmeticException e{throw e}

56
Q

Of the keywords ‘throw’ and ‘throws’, which is used to explicitly throw an exception, and which to declare and propagate an exception up the call stack?

A

Throw explicitly throws an exception

Throws declares and propagates an exception up the call stack

57
Q

What is a ‘checked’ exception?

A

An exception that is checked at compile time, and the method in which it was thrown must either handle the exception or specify a handler with the ‘throw’ keyword

58
Q

The keywords ‘try’ and ‘catch’

A

By putting risky code in the ‘try’ block, we can handle any exceptions with the ‘catch’ block.

59
Q

The keyword ‘finally’…

A

Denotes code that will be executed regardless of exceptions thrown in the ‘try’ block (it’s a block that goes after the catch)

60
Q

The keyword ‘throws’…

A
is used to declare and propagate an exception up the call stack
is followed by class of an exception
is used with the method signature after the name with the parameter list
can be used to declare multiple exceptions
can be used for checked or unchecked exceptions