KEMBAR78
Java Interview VSDark Themed Sample | PDF
0% found this document useful (0 votes)
4 views1 page

Java Interview VSDark Themed Sample

The document provides an overview of Java concepts such as HashMap and encapsulation. It explains how HashMap uses a hash table to store key-value pairs and describes encapsulation as a method to hide internal details of an object while exposing necessary parts through public methods. Real-life examples, such as a school locker system and an ATM, are used to illustrate these concepts.

Uploaded by

Jana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views1 page

Java Interview VSDark Themed Sample

The document provides an overview of Java concepts such as HashMap and encapsulation. It explains how HashMap uses a hash table to store key-value pairs and describes encapsulation as a method to hide internal details of an object while exposing necessary parts through public methods. Real-life examples, such as a school locker system and an ATM, are used to illustrate these concepts.

Uploaded by

Jana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Java Interview Preparation

1. How does HashMap work internally?

Answer:
HashMap in Java stores key-value pairs using a hash table. It calculates the hash of the key and places the value in the
corresponding bucket index. If two keys have the same hash, a linked list or tree (Java 8+) is used.
Map<String, String> map = new HashMap<>();
map.put("key1", "value1");
map.put("key2", "value2");
System.out.println(map.get("key1"));

Real-life Example:
Like a school locker system: Each student has a locker (bucket) based on an ID (hash).

2. Encapsulation with real time explanation

Answer:
Encapsulation hides internal object details and exposes only necessary parts via public methods. Variables are kept
private and accessed via getters/setters.
public class Employee {
private String name;

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}
}

Real-life Example:
ATM: Users only see interface (screen/buttons), not the internal wiring or logic.

You might also like