Top 120+ Java Interview Questions & BCA Viva Exam Guide

This guide synthesizes the top 120+ most frequently asked Java interview and BCA viva questions into categorized, high-yield conceptual answers with technical depth expected by senior tech interviewers.

Study Tip for University & Placement Exams

Focus on the "Why" and "Under the hood" mechanics rather than simple dictionary definitions. Interviewers want to see that you understand JVM memory, concurrency guarantees, and algorithmic complexity.

Section 1: Core Fundamentals & Language Syntax

Q1. What is the fundamental difference between JDK, JRE, and JVM?

JVM (Java Virtual Machine) is the runtime engine that executes compiled bytecode. JRE (Java Runtime Environment) bundles the JVM with standard core runtime libraries (rt.jar / java.base). JDK (Java Development Kit) is the full superset containing the JRE plus development tools like the compiler (javac), debugger (jdb), and archiver (jar).

Q2. Why is Java strictly Pass-by-Value? Prove it.

In Java, variables storing objects actually hold a copy of the reference address. When passed into a method, that 32-bit/64-bit address value is copied into the local stack frame. Changing the attributes of the referenced object modifies the shared heap instance, but reassigning the parameter reference variable (ref = new Object()) leaves the caller's original reference completely untouched.

Q3. Why is the main() method in Java declared 'public static void main(String[] args)'?

  • public: Accessible globally by the JVM execution launcher outside the package.
  • static: Can be invoked without requiring the JVM to instantiate an object of the class first.
  • void: Does not return any exit status (exit codes are reported via System.exit(n)).
  • String[] args: Accepts command-line arguments passed at program invocation.

Section 2: Object-Oriented Programming (OOP)

Q4. What is the difference between Method Overloading and Method Overriding?

FeatureMethod Overloading (Compile-Time)Method Overriding (Runtime)
LocationOccurs within the same class (or across hierarchy)Occurs strictly between Superclass and Subclass
SignatureSame name, different parameter listExact same name and parameter list
Return TypeCan vary freelyMust be identical or covariant (subtype)
ResolutionStatic Binding resolved at compile timeDynamic Dispatch via Virtual Method Table (vtable)

Q5. Can we override a static method or private method in Java?

No. static methods belong to the class, not instances, and resolve at compile time via Method Hiding. private methods are not visible to subclasses, so defining a method with the same signature in a child class creates an entirely independent new method.

Q6. What is the Diamond Problem and how does Java 8 solve it for interfaces?

Multiple class inheritance causes ambiguity when two parent classes define the same method with different implementations. Java forbids multiple class inheritance. However, with Java 8 default methods in interfaces, if a class implements two interfaces sharing the identical default method signature, the compiler forces the implementing class to explicitly resolve the conflict via InterfaceName.super.methodName().

Section 3: Strings & Memory Management

Q7. Why are String objects immutable in Java?

  1. Security: Strings hold sensitive parameters (URLs, DB connections, passwords, file paths). Immutability prevents unauthorized mutation after security checks.
  2. String Constant Pool (SCP): Enables memory sharing across thousands of references to identical text literals.
  3. Thread Safety: Immutable instances can be freely shared across concurrent threads without locks.
  4. HashCode Caching: Because the string never changes, the hash is calculated once lazily and cached, providing maximum performance in HashMaps.

Q8. Difference between String, StringBuilder, and StringBuffer?

ClassMutabilityThread SafetyPerformance
StringImmutableThread-Safe (by immutability)Slow for repeated concatenations
StringBuilderMutableNot Thread-SafeFastest (best for single-thread loops)
StringBufferMutableThread-Safe (synchronized methods)Slower due to lock acquisition overhead

Section 4: Java Collections Framework

Q9. How does HashMap work internally in Java 8+?

HashMap stores entries in an array of Node<K,V> buckets. Key hashes are mapped to index via (n - 1) & hash. When collisions occur, entries are chained in a LinkedList. Java 8 Optimization: Once a bucket reaches 8 entries (TREEIFY_THRESHOLD) and total capacity >= 64, the linked list transforms into a balanced Red-Black Tree (TreeNode), reducing search complexity from $O(n)$ to $O(log n)$.

Q10. Difference between Comparable and Comparator?

  • Comparable<T>: Defined in java.lang. Implemented by the class itself via compareTo(T o) to establish the Natural Ordering (e.g., sorting Numbers or Strings).
  • Comparator<T>: Defined in java.util. External sorting strategy implemented via compare(T o1, T o2), permitting custom sorting on multiple criteria (e.g., Sort by Age, then by Name).

Section 5: Multithreading & Concurrency

Q11. What is the difference between wait() and sleep()?

Featurewait()Thread.sleep()
Source Classjava.lang.Objectjava.lang.Thread
Lock HoldingReleases the monitor lock during waitRetains all acquired monitor locks
Invocation ContextMust be inside a synchronized blockCan be invoked from anywhere
Wakeup ConditionVia notify(), notifyAll(), or timeoutStrictly when timer expires or interrupt

Q12. What does the volatile keyword guarantee in Java?

volatile guarantees Visibility and establishes a Happens-Before relationship. Writes to a volatile variable are immediately flushed to main RAM, and subsequent reads by other threads bypass CPU L1/L2 caches to fetch directly from main RAM. It prevents CPU instruction reordering, but does not guarantee atomicity for compound operations like count++.

BCA / University Viva Pearl

When asked: "Can an abstract class have a constructor if it cannot be instantiated with new?"
Answer: YES! An abstract class has constructors to initialize its own member fields. They are invoked by subclass constructors via super() during subclass instantiation.

Frequently Asked Questions (FAQ)

Q1. Why is Java not considered 100% pure Object-Oriented?
Java supports primitive data types (int, float, double, boolean, char, byte, short, long) which are not objects and do not inherit from java.lang.Object. In a 100% pure OOP language (like Smalltalk), everything including numbers and booleans must be an object.
Q2. Is Java strictly Pass-by-Value or Pass-by-Reference?
Java is STRICTLY Pass-by-Value in all circumstances. When an object is passed as a parameter to a method, the VALUE of the reference (the 64-bit memory address pointer) is copied to the method stack frame. Reassigning the parameter reference variable inside the method has zero effect on the caller reference.
Q3. What is the contract between equals() and hashCode()?
If two objects are equal according to equals(Object), they MUST produce the identical integer hashCode(). However, if two objects produce the same hashCode (a collision), they are not necessarily equal. Violating this contract breaks HashMap, HashSet, and Hashtable lookups.

Explore Related Java Tutorials

Accelerate your Java mastery by studying these tightly connected core tutorials:

Practice Drills & Dry Runs →

Code tracing and tricky output drills.

Quick Reference Cheat Sheet →

Syntax and keywords reference guide.

Collections Framework →

Core collection hierarchies and complexity.

JVM Internals →

Execution engine and classloader deep dive.

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!