0% found this document useful (0 votes)
2 views13 pages

2-Java Basic

The document provides a comprehensive overview of key Java concepts relevant for interviews, including differences between == and .equals(), the String Pool, mutable vs immutable objects, and the use of constructors. It also covers exception handling, the Java Collection Framework, and the distinctions between various data structures like List, Set, and Map. Each section includes examples, common mistakes, and takeaways to help candidates prepare effectively for Java interviews.

Uploaded by

alt.tp-1ofxao0p
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)
2 views13 pages

2-Java Basic

The document provides a comprehensive overview of key Java concepts relevant for interviews, including differences between == and .equals(), the String Pool, mutable vs immutable objects, and the use of constructors. It also covers exception handling, the Java Collection Framework, and the distinctions between various data structures like List, Set, and Map. Each section includes examples, common mistakes, and takeaways to help candidates prepare effectively for Java interviews.

Uploaded by

alt.tp-1ofxao0p
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

JAVA INTERVIEW HANDBOOK — PART 2

26. Difference between == and .equals() in Java

== compares reference (memory address), while .equals() compares actual content


of objects.

Example

String a = new String("Java");

String b = new String("Java");

[Link](a == b); // false

[Link]([Link](b)); // true

Why Interviewers Ask This

To test understanding of memory, objects, and string comparison.

Common Mistakes

Assuming == compares values for objects.

Interview Takeaway
Always use .equals() for content comparison.

27. What is String Pool in Java?

String Pool is a special memory area in heap where String literals are stored to save
memory.

Example

String s1 = "Test";

String s2 = "Test";

[Link](s1 == s2); // true

Why Interviewers Ask This

To check memory optimization knowledge.

Common Mistakes

Thinking all Strings behave the same.

Interview Takeaway
String literals share memory, objects created using new do not.

© 2026 Ajit Marathe — Java Interview Handbook


Connect on LinkedIn for more interview content.
JAVA INTERVIEW HANDBOOK — PART 2

28. Difference between String, StringBuilder, and StringBuffer

Type Mutable Thread-Safe

String

StringBuilder

StringBuffer

Example

StringBuilder sb = new StringBuilder("Hi");

[Link](" Java");

[Link](sb);

Why Interviewers Ask This

To evaluate performance and concurrency awareness.

Common Mistakes

Using String in loops.

Interview Takeaway
Use StringBuilder for performance in single-threaded apps.

29. What is Immutable Object?

An object whose state cannot be changed after creation.

Example

String s = "Java";

[Link](" World");

[Link](s); // Java

Why Interviewers Ask This

To test thread-safety and design concepts.

Common Mistakes

Assuming methods modify original object.


Interview Takeaway
Immutable objects are thread-safe by default.

© 2026 Ajit Marathe — Java Interview Handbook


Connect on LinkedIn for more interview content.
JAVA INTERVIEW HANDBOOK — PART 2

30. What is Constructor in Java?

A constructor initializes objects when they are created.

Example

class User {

User() {

[Link]("Constructor called");

Why Interviewers Ask This

To check object lifecycle understanding.

Common Mistakes

Thinking constructor has return type.

Interview Takeaway
Constructor name must match class name.

31. Can we override a constructor?

No. Constructors cannot be overridden.

Why Interviewers Ask This

To test inheritance fundamentals.

Common Mistakes

Confusing constructor overloading with overriding.

Interview Takeaway
Constructors can be overloaded, not overridden.

© 2026 Ajit Marathe — Java Interview Handbook


Connect on LinkedIn for more interview content.
JAVA INTERVIEW HANDBOOK — PART 2

32. What is this keyword?

Refers to the current object.

Example

class Test {

int x;

Test(int x) {

this.x = x;

Why Interviewers Ask This

To test scope clarity.

Common Mistakes

Not using this when variable names clash.

Interview Takeaway
this avoids ambiguity between instance and local variables.

33. What is super keyword?

Used to access parent class members.

Example

[Link]();

Why Interviewers Ask This

To validate inheritance knowledge.

Common Mistakes

Using super without inheritance.

Interview Takeaway
super refers to immediate parent.

© 2026 Ajit Marathe — Java Interview Handbook


Connect on LinkedIn for more interview content.
JAVA INTERVIEW HANDBOOK — PART 2

34. What is Method Overloading?

Multiple methods with same name but different parameters.

Example

void add(int a, int b) {}

void add(double a, double b) {}

Why Interviewers Ask This

To test compile-time polymorphism.

Common Mistakes

Changing return type only.

Interview Takeaway
Method signature must differ.

35. What is Method Overriding?

Child class providing its own implementation of parent method.

Example

@Override

void run() {}

Why Interviewers Ask This

To test runtime polymorphism.

Common Mistakes

Reducing access modifier.

Interview Takeaway
Method signature must match exactly.

© 2026 Ajit Marathe — Java Interview Handbook


Connect on LinkedIn for more interview content.
JAVA INTERVIEW HANDBOOK — PART 2

36. Difference between Abstract Class and Interface

Abstract Interface

Can have constructor Cannot

Can have method body Default methods only

Supports inheritance Multiple inheritance

Why Interviewers Ask This

To test design decision making.

Interview Takeaway
Use interface for capability, abstract class for base behavior.

37. Can Interface have methods with body?

Yes, using default keyword.

Example

default void log() {

[Link]("Log");

Interview Takeaway
Default methods were added in Java 8.

38. What is Static keyword?

Belongs to class, not object.

Example

static int count;

Interview Takeaway
Static members are shared across objects.

© 2026 Ajit Marathe — Java Interview Handbook


Connect on LinkedIn for more interview content.
JAVA INTERVIEW HANDBOOK — PART 2

39. Static vs Instance Variables in Java

Static variables belong to the class, while instance variables belong to individual
objects.

Static variables share a single copy across all objects.

Example

class Counter {

static int count = 0;

int instanceCount = 0;

Counter() {

count++;

instanceCount++;

Why Interviewers Ask This

To check understanding of memory allocation and shared data.

Common Mistakes

Using static variables where instance-level data is required.

Interview Takeaway
Use static variables for shared state, instance variables for object-specific data.

© 2026 Ajit Marathe — Java Interview Handbook


Connect on LinkedIn for more interview content.
JAVA INTERVIEW HANDBOOK — PART 2

40. Final Keyword in Java

The final keyword is used to restrict modification.


It can be applied to variables, methods, and classes.

Example

final int TIMEOUT = 10;

Why Interviewers Ask This

To test immutability and design safety concepts.

Common Mistakes

Not using final for constants.

Interview Takeaway
Final improves safety, clarity, and prevents accidental changes.

41. What is an Exception in Java?

An exception is an unwanted event that disrupts the normal flow of a program.

Example

int a = 10 / 0; // ArithmeticException

Why Interviewers Ask This

To evaluate error-handling awareness.

Common Mistakes

Ignoring exception handling in automation code.

Interview Takeaway
Exceptions must be handled to build stable automation frameworks.

© 2026 Ajit Marathe — Java Interview Handbook


Connect on LinkedIn for more interview content.
JAVA INTERVIEW HANDBOOK — PART 2

42. Checked vs Unchecked Exceptions

Checked exceptions are validated at compile time, while unchecked exceptions occur
at runtime.

Example

// Checked

FileInputStream fis = new FileInputStream("[Link]");

// Unchecked

int a = 5 / 0;

Why Interviewers Ask This

To test JVM and compiler behavior understanding.

Common Mistakes

Catching generic Exception everywhere.

Interview Takeaway
Handle checked exceptions explicitly and prevent unchecked ones via clean code.

43. try-catch-finally Block

Used to handle exceptions and ensure cleanup logic is executed.

Example

try {

[Link](url);

} catch (Exception e) {

[Link]();

} finally {

[Link]();

Why Interviewers Ask This

To test resource management skills.

© 2026 Ajit Marathe — Java Interview Handbook


Connect on LinkedIn for more interview content.
JAVA INTERVIEW HANDBOOK — PART 2

Common Mistakes

Forgetting to close resources in finally.

Interview Takeaway
Always release resources using finally.

44. Difference between throw and throws

throw is used to explicitly throw an exception, while throws declares it.

Example

throw new Exception("Invalid data");

Why Interviewers Ask This

To test exception propagation understanding.

Common Mistakes

Using throw and throws interchangeably.

Interview Takeaway
throw = action, throws = declaration.

45. Can finally Block Be Skipped?

Yes, the finally block may not execute if JVM crashes or [Link]() is called.

Example

[Link](0);

Why Interviewers Ask This

To test JVM-level knowledge.

Common Mistakes

Assuming finally always executes.

Interview Takeaway
Finally executes in most cases, but not all JVM termination scenarios.

© 2026 Ajit Marathe — Java Interview Handbook


Connect on LinkedIn for more interview content.
JAVA INTERVIEW HANDBOOK — PART 2

46. Java Collection Framework

The Collection Framework provides classes and interfaces to store and manipulate data
efficiently.

Example

List<String> list = new ArrayList<>();

Why Interviewers Ask This

To check data-structure usage in real projects.

Common Mistakes

Using arrays instead of collections.

Interview Takeaway
Collections make code flexible and scalable.

47. Difference Between List, Set, and Map

Each collection type stores data differently.

Example

Map<String, String> data = new HashMap<>();

Why Interviewers Ask This

To test selection of correct data structure.

Common Mistakes

Not understanding uniqueness and key-value mapping.

Interview Takeaway
Choose collections based on data behavior.

© 2026 Ajit Marathe — Java Interview Handbook


Connect on LinkedIn for more interview content.
JAVA INTERVIEW HANDBOOK — PART 2

48. Array vs ArrayList

Arrays have fixed size, while ArrayLists grow dynamically.

Example

ArrayList<String> names = new ArrayList<>();

Why Interviewers Ask This

To test flexibility and performance understanding.

Common Mistakes

Using arrays when size is dynamic.

Interview Takeaway
Prefer ArrayList for dynamic data.

49. What is HashMap in Java?

HashMap stores data as key-value pairs using hashing.

Example

HashMap<String, String> map = new HashMap<>();

Why Interviewers Ask This

To test real-world data handling.

Common Mistakes

Assuming insertion order is preserved.

Interview Takeaway
HashMap is fast and commonly used in automation.

© 2026 Ajit Marathe — Java Interview Handbook


Connect on LinkedIn for more interview content.
JAVA INTERVIEW HANDBOOK — PART 2

50. Difference Between HashMap and Hashtable

HashMap is not synchronized, while Hashtable is synchronized.

Example

HashMap<String, String> map = new HashMap<>();

Why Interviewers Ask This

To test thread-safety awareness.

Common Mistakes

Using Hashtable in modern applications.

Interview Takeaway
Use HashMap with proper synchronization when needed.

© 2026 Ajit Marathe — Java Interview Handbook


Connect on LinkedIn for more interview content.

You might also like