Java Interview Questions Flashcards
Which memory areas does the JVM provide and what’s the difference between them?
Heap Area: represents the runtime data, where arrays and classes are allocated. It is created on the startup, doesn’t need to be continuous.
Where a primitive is stored in memory?
Stack area
How can you override a static method?
They can’t be overriden
Difference between StringBuffer and StringBuilder?
StringBuffer is not thread safe
What is JIT for?
JIT gets a block of code that is executed frequently and compiles it to native machine language to speed up process
Where are complex objects stored?
The heap
What do the stack store?
local variables like int,float
What is Metaspace? What types of variables is stored in the metaspace?
Metaspace is only for Java 8+.
static primitives are stored in metaspace.
static objects have the reference stored in the metaspace
Where is the String Pool stored?
In the Heap
When is an object eligible for GC?
any elements on the heap that are not reachable
Where is the Young and Old gen stored?
The heap
What’s the difference between Young and Old generation?
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
What are the survivor spaces in the Young gen?
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
Explain JDK, JRE, JVM
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
What is the difference between ArrayList and Vector?
ArrayList is not syncronized.
What is the difference between Stack and Heap?
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.
What are the main concepts of OOP?
Inheritance, Encapsulation, Abstraction and Polymorphism
What is the difference between compilation time polymorphism and runtime?
Compilation time polymorphism would be method overloading, where Runtime polymorphism is done by interfaces and abstractions
In Java, what is association?
Relationship where objects have their own lifecycle and there is no owner. For example, teacher and students
What do you mean by aggregation in java?
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
What is a composition in Java?
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
What are the different types of GC in Java?
- Parallel
- Serial
- CMS
- G1 GC
Is java Pass-by-reference or Pass-by-value?
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)