Lecture 1 Flashcards

1
Q

What are primitive types (mention some)?

A

Types that are already defined by the program (int, float, double, char, boolean)

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

What are non-primitive types (mention some)?

A

Types that the programmer defines for the program (arrays, strings, classes)

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

Primitive types:
Describe whole numbers and decimal numbers

A

Whole numbers: long, byte, int, short
Decimal numbers: float, double (default)

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

What is a value literal?

A

The letter at the end of a variable initialisation:
float 1.2f (without f, it will be interpreted as double)
double 1.6 or 1.6d (interpreted as double, since it’s default (d optional))

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

What is a local variable?

A

Variables that are declared inside the body of a method

class Guru99 {
static int a = 1; //static variable
int data = 99; //instance variable
void method() {
int b = 90; //local variable
}
}

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

What is an instance variable?

A

Defined without the static keyword. They are defined outside a method declaration and are object specific

class Guru99 {
static int a = 1; //static variable
int data = 99; //instance variable
void method() {
int b = 90; //local variable
}
}

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

What is a static variable?

A

Initialised only once, at the start of the program execution. They should be initialised first (before any instance variables)

class Guru99 {
static int a = 1; //static variable
int data = 99; //instance variable
void method() {
int b = 90; //local variable
}
}

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

What does null reference mean?

A

null (used for non-primitive types) will reference to nowhere, since it has not been set to target a proper element

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

Write the skeleton of an “if” statement.

A

boolean condition = true; // has to be true or false
// …

if (condition) {
// … happens if true (then block, mandatory)
} else {
// … happens if false (else block, optional)
}

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

Write the skeleton of a “while” statement.

A

boolean condition = true;

while (condition) {
// Perform operations repeatedly
// Modify condition to eventually end loop
}

(if the condition is initially false, it will not execute, and will repeat as long as the condition holds)

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

Write the skeleton of a “for” loop statement. What does the (initialiser, condition, increment/decrement) (IN THAT ORDER) do?

A

e.g.:
for (int i = 0; i < 10; i++) {
// Perform operation ten times
}

Initialiser: one time before the first loop
Condition: checked every time before loop
Increment/Decrement: every time
after loop

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

Write the skeleton of a “for each” loop

A

for (type variableName : arrayName) {
// code block to be executed
}

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

Can you use “==” (equality operator) on Strings?

A

NO. Instead use “equals()”.
Example:
if (message.equals(“Hello World”)){
//…
}

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

What is Class-based OOP and what does it contain?

A

You have a Class, that Class contains Data and Actions on that Data. Then you have an Object that is an Instance of the whole Class

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

What is a Class?

A

Represents many objects that belong to the Class. Will group data and actions on that data for data which logically belong closely together (high coherence)

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

What is an object (instance of a class)?vend tilbage

A

Is the property of a specific element. A class is the super, and an object is inside of the class.

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

How do you create an new object/instance of a class. Give an example:

A

You use the “new” keyword and reference to the object with a new variable name.

class Car {
// …
}

class CarProgram {
public static void main(String[] args){
Car myCar = new Car();
(my car is the new object you’re creating from the Car class)

18
Q

What is an attribute?vend tilbage til

A

A data element in a class, sometimes known as field (DATA)

19
Q

What is a method ?

A

a function on data, sometimes known as operation (ACTION ON DATA)

20
Q

What is a member?

A

Umbrella term for attributes and methods (ATTRIBUTE, MEMBER, CONSTRUCTOR)

21
Q

How to write the “main()”?

A

public static void main(String[] args)
(public static is mandatory)

22
Q

Describe attributes.

A

They’re declared within a class.
Consists of modifiers (optional), type, name.

23
Q

Give an example of attributes.

A

class Car {
private String licensePlate;
private double gasLevel;
}
(modifier, type, name)

24
Q

What is in a method.

A

Signature:
- Visibility modifiers (optional)
- Return type (possibly void)
- Name
- Parameter(s) (optional) Name and Type

Body:
- Code
- return statement (at least one if not void)

25
Q

Give an example of methods.

A

class Car {
double getGasLevel() {
return gasLevel;
}
public void refuel(double amount) {
// …
}
}

26
Q

How do you make comments?

A

single line comment = //
multi line comment = /* and */
java documentation = /** and */

27
Q

What does the visibility modifier public do?

A

public (+) = Everyone can see this class, attribute, method, constructor, and it’s therefore accessible by any other class.

28
Q

What does the visibility modifier private do?

A

private (-) = Can be used for subclasses (only, not a super), attributes, methods and constructors, making them only accessible within the declared class.

29
Q

What does the visibility modifier protected do?

A

protected (#) = Can be used for subclasses (only, not a super), attributes, methods and constructors, making them accessible in the same package and subclasses.

30
Q

What is static?vend tilbage til

A

It’s a non-access modifier used for methods and attributes. Static methods/attributes can be accessed without creating an object of a class.

31
Q

What is void?

A

The void keyword specifies that a method should not have a return value.

32
Q

Why is code duplication bad?

A

It gives you more to maintain, and one modification means you have to change everything else. Inheritance can solve this problem.

33
Q

What is inheritance?

A

A sub-class inherits all the members of a super class (given the visibility modifier)

34
Q

How do you use inheritance? Give an example.

A

You use the “extends” keyword, like this:
class Car {
String licensePlate;
void drive() {
// …
}
}

class GasolineCar extends Car {
double gasLevel;
// …
}

class ElectricCar extends Car {
double batteryLevel;
// …
}

GasolineCar extends Car and GasolineCar inherits from Car.
Car is the super class of GasolineCar and GasolineCar is a subclass of Car

35
Q

What are packages?

A

A logically grouping of coherent classes. Packages allow for different classes to have the same name since they’re in different packages. They can be nested.

36
Q

Give an example of packages, and an example in code.

A

Vehicles: Car, motorbike, gasoline car, electric car
Biology (nested):
Animals: Mammal, reptile, bird
Plants: Flower, bush, tree

package vehicles;
import vehicles.Car;
class GasolineCar extends Car {
// …
}

37
Q

What is encapsulation?

A

It regulates access to members from the outside. We put the class in a capsule to protect it. We use visibility modifiers for this.

38
Q

Is encapsulation/visibility modifiers a security mechanism?

A

NO, they’re a design mechanism, as people with bad intentions can still get around this fact.

39
Q

How do you achieve encapsulation?

A

Declare class variables/attributes as private and by providing public get and set methods to access and update the value of a private variable. By using get and set methods, we can make it accessible.

40
Q

What are the benefits of OOP?

A

Abstraction, encapsulation, inheritance, polymorphism

41
Q

What is required from a variable to be made in a class?

A

They must be declared and they must have a type.