Java Flashcards

1
Q

What are the 3 uses of the keyword final

A

variable
method
class

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

Can classes marked as final be extended?

A

no, that is the purpose of marking a class as final

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

Does a final class mean the objects of that class are immutable?

A

No, you can change the fields in.,

Dog dog = new Dog()
Dog.setName(“Hamish”)

You just can’t extend it

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

Can methods marked as final be overridden?

A

No

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

Can variables marked as final be reassigned?

A

No - constants

ie., MAX_VALUE

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

How to explain OOP to someone without a technical background

A

OOP stands for “Object-Oriented Programming,” and it’s a way for computers to understand how to do things.

Imagine you have a toy box with lots of different toys inside. Each toy is like an object, and each object has different things it can do.

For example, a toy car can move forward and backward, while a toy doll can move its arms and legs.

In OOP, we can tell the computer what each object can do and how it should behave when we interact with it.

So, it’s like teaching the computer to play with toys, but instead of just playing, the computer can use them to do all kinds of cool things!

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

Can you explain the basics of Javas memory model and how it affects multi threaded programming?

A

Java’s memory model states how memory actions (reads and writes) will appear to execute to the programmer

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

What is memory management?

A

Memory management is the process of removing unused objects to make space for new objects

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

Where do Java objects reside?

A

In the heap

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

When is the heap created?

A

The heap is created when the JVM starts up

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

What are the 3 main benefits of using generics?

A

reusable

type safe

flexible

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

What is a functional interface?

A

An interface that contains only a single abstract (unimplemented) method.

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

Describe 2 simalarities between abstract classes and interfaces

A
  • Cant instantiate either of them
  • Both can contain methods
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Name 3 scenarios when an interface is a good choice?

A
  1. Multiple inheritance and different class hierarchies will be used
  2. Unrelated classes implementing the interface ie., Shape - rectangle, square
  3. A contract, but don’t care who impmements
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Give an example of an interface?

A

interface Sender {
send ()
}

ImageSender implements Sender {
send() {
// image send code
}

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

Name 4 scenarios you would use an abstract class?

A

When you want too..

  • Define a template for related classes to implement
  • Enforce consistency across a class hierarchy
  • Provide default behaviour to inherited methods
  • Encapsulate implementation details
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is an example of an Abstract class

A

public abstract class vehicle {
public abstract void turnOn();
}

MotorCycle extends Vehicle {
public void turnOn(){ .. }
}

Car extends Vehicle {
public void turnOn(){..}
}

If we tried to use inheritance instead of an abstract class, Vehicle could be instantiated and this may mean we get behaviour we didn’t expect

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

What is the difference between an interface and an abstract class in Java?

A

An abstract class can have a state, but an interface can’t

For example, if we define

protected String colour;

in an abstract class it can only be accessed by subclasses of our abstract class

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

When should you use an interface instead of an abstract class

A

You should use an interface when you want multiple unrelated classes to have the same functionality

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

What does immutable mean?

A

An object’s state can’t be changed after it has been created

So it will behave the same way its whole lifetime

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

Is a String immutable?

A

Yes so if you checked if two Strings “Hello” were the same, they aren’t because they actually are objects with a different memory location

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

Are variables mutable by default?

A

Yes we can change the value

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

What are the benefits of immutable objects in Java?

A
  • can share it safely between multiple threads
  • are side effects free
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What is the difference between polymorphism and inheritance?

A

Inheritance allows you to create new classes based on existing classes

Polymorphism allows objects to take on multiple forms through method overriding and overloading

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

What is method overriding?

A

Method overriding allows a subclass to provide its own implementation of a method that is already defined in its superclass

It must have the same name, return type, and parameter list as the method in the superclass

26
Q

What is method overloading?

A

Method overloading is a feature that allows a class to have two or more methods with the same name, but with different parameters.

The Java compiler is able to tell the difference between the methods based on the number and/or types of parameters.

27
Q

Describe inheritance?

A

Inheritance is a mechanism in object-oriented programming that allows a class to inherit properties and behaviors from a parent class.

In Java, a class uses “extends” keyword to inherit from another class

Allows you to create a new class that is a modified version of an existing class, without having to rewrite all of the code in the new class.

28
Q

Describe polymorphism?

A

Polymorphism is the ability of an object to take on multiple forms. In Java, polymorphism is achieved through method overriding and method overloading.

29
Q

What are 3 cache problems?

A

Cache penetration
Cache avalanche
Cache breakdown

30
Q

What is a load balancer?

A

A load balancer is an algorithm that routes client requests (ie., database write, cache queries) across all servers

Like a traffic warden directing traffic

31
Q

What is the Java Memory Model made up of?

A

Thread Stacks and a heap area

32
Q

What is a thread stack?

A

An area of memory that contains variables, method calls etc

Can be fixed or variable size

If stack size exceeds the limit it will throw a Stack Overflow error

33
Q

What is the heap?

A

Contains objects created during the application lifecycle

34
Q

Will the heap change in size?

A

The heap size might change when the application runs

35
Q

What happens when the heap is full?

A

Garbage is collected

36
Q

What are key features of Java?

A
  • Complied (converted to bytecode - run on platform with jvm)
  • developed for class based and object oriented paradigms
  • time effort to keep code readable
  • strongly typed
  • faster types assigned during compilation
37
Q

Give an example of an interface

A

Ie., interface Shape
double area
Rectangle implements Shape
implementation of double area

38
Q

What are 6 key concepts in Java?

A

Classes
Objects
Abstraction

Polymorphism
Inheritance
Encapsulation

39
Q

What is a class?

A

a template for creating objects

has a variable, method, constructor

40
Q

What are objects?

A

An object is an instance of a class that has a state and behaviours

41
Q

What is abstraction

A

Abstraction allows us to hide the implementation from the user (in abstract classes / interfaces) and only show the user information needed to use the implementation

42
Q

What is polymorphism?

A

Polymorphism means many forms. It lets us process data differently depending on the input.

43
Q

What is inheritance?

A

Inheritance allows a new created class (child) to acquire the properties of an existing class (parent)
IS A relationship

44
Q

What is encapsulation?

A

Encapsulation lets us hide the data and functions of a class . This helps protect it from being misused

45
Q

Describe 4 benefits of using Inheritance?

A

Reduce the amount of duplicate code

Only need to change code in one place

Can override methods of the parent class

Keeps data private - derived class can’t alter it

46
Q

Give 2 examples of what interfaces are useful for?

A

decoupling code
implementing polymorphism

47
Q

Describe generics in Java

A

Generics allows you to write code that can work with a range of different data types in a type-safe manner

Classes, interfaces, and methods can operate on a parameterized type which is determined at compile time.

Generics use type parameters, which are enclosed in angle brackets (“<” and “>”).

48
Q

Give an example of generics in Java

A
49
Q

Why do we overwrite public boolean equals(Object o)

A

To define what it means to be equal

50
Q

Describe 4 things an inherited class will have?

A

extends

super() constructor

override toString()

protected attributes

51
Q

What are object arrays initally

A

null

52
Q

What is a variable array initially?

A

0

53
Q

What is instanceof used for?

A

instanceof is used to test if an object is of a given type

public class Round {}

public class Ring extends Round {}

@Test
void checkInstance(){
Ring ring = new Ring();
assertTrue(ring instanceof Round);
}

54
Q

How does the instanceof operator work?

A

The instanceof operator works on the principle of the is-a relationship.

The concept of an is-a relationship is based on class inheritance or interface implementation.

55
Q

Give an example of a violation of encapsulation?

A

public variables which allow anyone to change them directly

56
Q

What happens if we try to override a final variable, method or class?

A

We’ll see a compiler error

… is final and cannot be overwritten

57
Q

How can an abstract class in Java have a state?

A

An abstract class can have a state if it has variables and a constructor

Ie.,

public abstract class Shape {

protected String color;

public Shape(String color) {
this.color = color;
}

public abstract double getArea();

public abstract double getPerimeter();
}

public class Circle extends Shape {
private double radius;

public Circle(String color, double radius) {
super(color);
this.radius = radius;
}
}

58
Q

What contributes to the state of an object in Java?

A

Both the constructor and the instance variables contribute to the state of a Java object. The constructor is responsible for initializing the instance variables to their initial state, while the instance variables themselves hold the values that define the object’s state at any given time.

59
Q

What does state mean in OOP?

A

“state” typically refers to the set of values held in an object’s instance variables at any given time. An object’s state is defined by the values of its instance variables, and these values can change over time as the object interacts with the rest of the program.

60
Q

What is memoization?

A