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
What is method overriding?
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
What is method overloading?
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
Describe inheritance?
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
Describe polymorphism?
Polymorphism is the ability of an object to take on multiple forms. In Java, polymorphism is achieved through method overriding and method overloading.
29
What are 3 cache problems?
Cache penetration Cache avalanche Cache breakdown
30
What is a load balancer?
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
What is the Java Memory Model made up of?
Thread Stacks and a heap area
32
What is a thread stack?
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
What is the heap?
Contains objects created during the application lifecycle
34
Will the heap change in size?
The heap size might change when the application runs
35
What happens when the heap is full?
Garbage is collected
36
What are key features of Java?
- 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
Give an example of an interface
Ie., interface Shape double area Rectangle implements Shape implementation of double area
38
What are 6 key concepts in Java?
Classes Objects Abstraction Polymorphism Inheritance Encapsulation
39
What is a class?
a template for creating objects has a variable, method, constructor
40
What are objects?
An object is an instance of a class that has a state and behaviours
41
What is abstraction
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
What is polymorphism?
Polymorphism means many forms. It lets us process data differently depending on the input.
43
What is inheritance?
Inheritance allows a new created class (child) to acquire the properties of an existing class (parent) IS A relationship
44
What is encapsulation?
Encapsulation lets us hide the data and functions of a class . This helps protect it from being misused
45
Describe 4 benefits of using Inheritance?
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
Give 2 examples of what interfaces are useful for?
decoupling code implementing polymorphism
47
Describe generics in Java
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
Give an example of generics in Java
49
Why do we overwrite public boolean equals(Object o)
To define what it means to be equal
50
Describe 4 things an inherited class will have?
extends super() constructor override toString() protected attributes
51
What are object arrays initally
null
52
What is a variable array initially?
0
53
What is instanceof used for?
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
How does the instanceof operator work?
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
Give an example of a violation of encapsulation?
public variables which allow anyone to change them directly
56
What happens if we try to override a final variable, method or class?
We'll see a compiler error ... is final and cannot be overwritten
57
How can an abstract class in Java have a state?
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
What contributes to the state of an object in Java?
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
What does state mean in OOP?
"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
What is memoization?