Top 2020 Java Interview Questions: Core Concepts, Code Samples, and Answers

A comprehensive guide covering Java fundamentals, data types, OOP principles, JVM/JRE/JDK differences, collections, static and inner classes, exception handling, reflection, I/O streams, and common interview code snippets, providing clear explanations and practical examples for Java interview preparation.

Java Architect Essentials
Java Architect Essentials
Java Architect Essentials
Top 2020 Java Interview Questions: Core Concepts, Code Samples, and Answers

Java Basics Overview

This section introduces the core concepts of Java programming, including the definition of programming, the nature of Java as an object‑oriented language, its platform‑independent bytecode, and the role of the Java Virtual Machine (JVM) in executing code on any operating system.

JVM, JRE, and JDK

The JVM is the virtual machine that runs Java bytecode, providing cross‑platform execution. The JRE bundles the JVM with core class libraries needed to run Java applications. The JDK adds development tools such as the compiler ( javac) and packaging utility ( jar), allowing developers to create and compile Java programs.

Java source code --> javac --> .class (bytecode) --> JVM --> native machine code

Java Versions After JDK 1.5

Java SE (Standard Edition)

Java EE (Enterprise Edition, now Jakarta EE)

Java ME (Micro Edition)

Fundamental Language Features

Data Types

Java defines eight primitive types ( byte, short, int, long, float, double, char, boolean) and a set of reference types (classes, interfaces, arrays). Primitive variables hold raw values, while reference variables store object addresses.

Control Flow Statements

break

– exits the nearest loop. continue – skips the current iteration. return – exits a method and optionally returns a value.

Static vs. Instance

Static members belong to the class itself and are shared by all instances; they can be accessed without creating an object ( ClassName.method()). Instance members belong to a specific object and require an instance to be accessed.

Object Equality

==

compares object references (address equality). equals() can be overridden to compare logical content. For collections like HashSet, hashCode() must be consistent with equals() to avoid duplicate entries.

Object‑Oriented Concepts

Inheritance and Polymorphism

Java supports single inheritance for classes and multiple inheritance for interfaces. Polymorphism allows a superclass reference to point to a subclass object, enabling dynamic method dispatch at runtime.

Method Overloading vs. Overriding

Overloading: same method name, different parameter lists within the same class; return type is irrelevant.

Overriding: subclass provides its own implementation of a superclass method with the same signature; return type must be compatible.

Five OOP Principles

Single Responsibility (SRP)

Open/Closed (OCP)

Liskov Substitution (LSP)

Dependency Inversion (DIP)

Interface Segregation (ISP)

Special Language Features

Static Keyword

Static variables are created once per class and shared across all instances. Static blocks execute once when the class is loaded, useful for one‑time initialization.

Inner Classes

Java supports four inner‑class types:

Member inner class – non‑static, tied to an outer instance.

Static inner class – can be instantiated without an outer instance.

Local inner class – defined inside a method; can access final or effectively final local variables.

Anonymous inner class – no name, often used for callbacks; must extend a class or implement an interface.

Outer outer = new Outer();
Outer.Inner inner = outer.new Inner();
Outer.StaticInner staticInner = new Outer.StaticInner();

Final, Finally, and Finalize

final

– prevents subclassing, method overriding, or variable reassignment. finally – block that always executes after try / catch, typically for resource cleanup. finalize() – called by the garbage collector before object reclamation (rarely used).

Reflection

Reflection allows runtime inspection and manipulation of classes, fields, and methods. Common use cases include framework configuration, dependency injection, and dynamic proxy creation.

Class<?> cls = Class.forName("com.example.Student");
Field nameField = cls.getDeclaredField("name");
nameField.setAccessible(true);
Object value = nameField.get(studentInstance);

I/O Streams

Java I/O is organized into four abstract base classes: InputStream / OutputStream for byte streams and Reader / Writer for character streams. Streams can be categorized as node streams (directly connected to a data source) or processing streams (wrap other streams to add functionality).

BI​O, NIO, AIO

BIO – blocking, synchronous I/O; simple but limited scalability.

NIO – non‑blocking, selector‑based I/O; supports multiplexed channels.

AIO – asynchronous I/O (NIO‑2); uses callbacks and completion handlers.

Common APIs

String

Strings are immutable objects backed by a final char[] array. The JVM maintains a string constant pool to reuse identical literals, improving memory efficiency. length() – returns the number of characters. charAt(int), indexOf(String), substring(int,int), replace(), trim(), split(), toUpperCase(), toLowerCase().

Mutable alternatives: StringBuilder (non‑thread‑safe) and StringBuffer (thread‑safe).

StringBuilder sb = new StringBuilder();
sb.append("abcdefg");
System.out.println(sb.reverse()); // gfedcba

Wrapper Classes

Primitive types have corresponding wrapper classes (e.g., intInteger). Autoboxing converts between primitives and wrappers automatically. Values between –128 and 127 are cached, so Integer a = 127; Integer b = 127; a == b evaluates to true, while values outside this range are distinct objects.

Integer a = new Integer(3);
Integer b = 3; // autoboxed
int c = 3;
System.out.println(a == b); // false (different objects)
System.out.println(b == c); // true (unboxing)

Key Takeaways for Interview Preparation

Understand the lifecycle of Java programs: compilation → bytecode → JVM execution.

Be able to explain differences between JDK, JRE, and JVM.

Know primitive vs. reference types, default values, and scope rules.

Master OOP concepts: inheritance, polymorphism, encapsulation, abstraction, and the five SOLID principles.

Differentiate method overloading and overriding, and know when each applies.

Explain static initialization, static blocks, and when to use static members.

Describe inner‑class types and their access rules, especially the need for final when accessing local variables.

Articulate the purpose of hashCode() and equals() in collections.

Demonstrate basic reflection usage and its typical framework scenarios.

Compare BIO, NIO, and AIO models and choose the appropriate one for high‑concurrency servers.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaJVMinterviewOOPfundamentals
Java Architect Essentials
Written by

Java Architect Essentials

Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.