Java Interview Questions Flashcards

1
Q

Which memory areas does the JVM provide and what’s the difference between them?

A

Heap Area: represents the runtime data, where arrays and classes are allocated. It is created on the startup, doesn’t need to be continuous.

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

Where a primitive is stored in memory?

A

Stack area

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

How can you override a static method?

A

They can’t be overriden

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

Difference between StringBuffer and StringBuilder?

A

StringBuffer is not thread safe

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

What is JIT for?

A

JIT gets a block of code that is executed frequently and compiles it to native machine language to speed up process

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

Where are complex objects stored?

A

The heap

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

What do the stack store?

A

local variables like int,float

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

What is Metaspace? What types of variables is stored in the metaspace?

A

Metaspace is only for Java 8+.
static primitives are stored in metaspace.
static objects have the reference stored in the metaspace

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

Where is the String Pool stored?

A

In the Heap

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

When is an object eligible for GC?

A

any elements on the heap that are not reachable

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

Where is the Young and Old gen stored?

A

The heap

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

What’s the difference between Young and Old generation?

A

The Young gen. is way smaller. That’s why the GC on that part of the memory only takes a fraction of second. If it survives, it gets moved to the Old Gen.
GC on the Old gen are way slower, but they happen less frequently

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

What are the survivor spaces in the Young gen?

A

Eden: When it gets full, the GC happens

S0/S1: When GC happens, objects get moved from one to another, either one them will be empty

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

Explain JDK, JRE, JVM

A

JDK: Java development kit, it is the tool necessary to compile, document and package Java Programs. It contains JRE + development tools

JRE: Java Runtime Environment, refers to a runtime env. in which Java bytecode can be executed. It’s an implementation of JVM which physically exists.

JVM: Java virtual Machine, it’s an abstract machine. It is a specification that provides a runtime environment in which java bytecode can be executed

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

What is the difference between ArrayList and Vector?

A

ArrayList is not syncronized.

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

What is the difference between Stack and Heap?

A

Memory: Stack is only used for a single thread execution. Heap is used by all parts of the application

Access: stack memory can’t be accessed by other threads. Heap memory are globally accessed

Memory management: Stack Follows LIFO manner to free memory. Heap is based on the generation associated with each object

Lifetime: Stack, until the end of execution of a thread. Heap memory lives from the start of the application till the end.

Usage: Stack only contains local primitives and reference variables to objects on the heap.
Heap, whenever an object is created, it’s stored in the heap.

17
Q

What are the main concepts of OOP?

A

Inheritance, Encapsulation, Abstraction and Polymorphism

18
Q

What is the difference between compilation time polymorphism and runtime?

A

Compilation time polymorphism would be method overloading, where Runtime polymorphism is done by interfaces and abstractions

19
Q

In Java, what is association?

A

Relationship where objects have their own lifecycle and there is no owner. For example, teacher and students

20
Q

What do you mean by aggregation in java?

A

Is a specialized form of association where all objects have their own lifecycle but there is an owner and a child object cannot belong to another parent object. Ex, Department and Teacher, a single teacher cannot belong to multiple departments, but if you remove the department, you don’t have to remove the the teacher

21
Q

What is a composition in Java?

A

Is a specialized form of Aggregation and we can call this “death” relationship. Is a strong type of aggregation, child objects does not have their own lifecycle, and if the parent is removed, the child are as well. For ex. Houses and rooms

22
Q

What are the different types of GC in Java?

A
  • Parallel
  • Serial
  • CMS
  • G1 GC
23
Q

Is java Pass-by-reference or Pass-by-value?

A

Java is always pass-by-value.
However, when we pass the value of an object, we are actually passing its REFERENCE. Thats why its confusing.

Lets say you have:

Dog myDog;

myDog is not the OBJECT, it’s the pointer(reference)