Tech-Notes

Interview Questions

Core Java:

Question Answer
What is JDK, JRE, JVM? JDK=JRE+devTools, JRE=JVM+lib, JVM=Interpreter
What is Bytecode? intermediate code generated by the Java compiler(JDK)
What are types of comments in Java? Single-line (//), Multi-line (/* */), Documentation (/** */).
What is Identifier? name for variables, methods, classes, or other entities.
What is Literal? value for variables
What is an Expression? combination of variables, operators, and literal
What are Keywords in Java? Reserved words
Write Java input syntax. Scanner inp = new Scanner(System.in);String a = inp.nextLine();
Write Java output syntax. System.out.println("Hi")
What are Datatypes and its types? Define the type of data
Java is not 100% OOPs language, why? becz it supports primitive data types
What are Variables and its types? name for the datatype
What is Operator and its types? perform math operations
What is Array? collection of similar datatype, fixed-size
What is String? sequence of characters
Why is String immutable? Becz of string pool
What is StringBuffer? Thread-safe, mutable strings
What is StringBuilder? Non-thread-safe, faster mutable strings.
What are types of String comparisons? == (reference), .equals() (content), .compareTo() (lexicographical).
What is String Tokenizer? split strings into tokens (e.g., words).
What is Static Variable & Class? create only one time, called without reference
What is Static Block? A block of code executed when the class is loaded into memory.
What is Final Variable, Method & Class? assigned only once, cannot modify once it assigned
What is Final, Finalize, Finally?  
What is Constants? Immutable. Declare as final static
What is Enum? A special class to define constants
What is Package and Import? Groups related classes and interfaces
What is Wrapper Class? wrap around primitive datatype & give object appearence
What is Autoboxing & Unboxing? Autoboxing: Primitive → Object. Unboxing: Object → Primitive
What is Typecasting? Converting one data type to another
What is Generics? type-safe to class
What is Exception Handling? Mechanism to handle runtime errors
What are Checked and Unchecked Exceptions? Compile-time (e.g., IOException). Runtime (e.g., NullPointerException).
What are throw and throws?  
When do ClassNotFoundException? when a class is not found
When do NoClassDefFoundError occur? when a compiled class is unavailable.
What is Serialization? Converting an object to a byte stream.

OOPS:

Question Answer
What is constructor and its types? Special method to initialize objects
What is lambda expression? A concise way to write functions using ()->{} syntax.
What is interface? and its types. A contract for classes; types: Functional, Marker, Normal.
What is marker interface and example. Interface with no methods; Example: Serializable.
What are the types of access specifier? Public, Private, Protected, Default.
What are the seven methods of Object class? toString(), hashCode(), equals(), clone(), getClass(), wait(), notify().
What are the four ways to create object? new, Reflection, Cloning, Deserialization.
What is object cloning? Creating a copy of an object using the clone() method.
When garbage collection gets triggered? When JVM runs out of memory or explicitly via System.gc().
What is method and vararg? Method: Block of code; Vararg: Variable-length arguments (...).
What are the types and subtypes of encapsulation? Types: Data Encapsulation, Behavioral Encapsulation.
What are inheritance and its types? Acquiring properties; types: Single, Multiple (via interface), Multilevel, Hierarchical.
What is super and super()? super: Access parent class members; super(): Call parent constructor.
What is polymorphism and its types? Single entity with many forms; types: Compile-time, Runtime.
What is abstract class? Class with abstract methods, cannot be instantiated.
What is interface and its types? A contract for classes; types: Functional, Marker, Normal.
What is IS-A and HAS-A relationship? IS-A: Inheritance; HAS-A: Composition.
What is shallow and deep copy? Shallow: Copies references; Deep: Copies entire objects.

MultiThreading:

Question Answer
What is multithreading? Executing multiple threads concurrently.
What is process and thread? Process: Independent program; Thread: Lightweight process within a process.
What are the lifecycle in Thread? New, Runnable, Running, Blocked/Waiting, Terminated.
What is thread priority? and its values. Determines execution priority; Values: MIN(1), NORM(5), MAX(10).
What is synchronized method? Ensures only one thread executes the method at a time.
What is synchronized block? Limits synchronization to a specific block of code.
What is static synchronized? Synchronizes static methods, locking the class object.
What are the Thread class methods? start(), run(), sleep(), join(), interrupt().
Explain wait, notify, notifyAll. wait(): Pauses thread; notify(): Wakes one thread; notifyAll(): Wakes all threads.
What is deadlock, how to avoid? Circular waiting of threads; Avoid by using proper locking order or timeout.
What is race condition? Incorrect results due to concurrent access to shared resources.
What are the methods available in Thread Object? wait(), notify(), notifyAll(), join(), sleep(), yield().
What is volatile keyword? Ensures visibility of changes to variables across threads.
How do you stop the thread in java? Use flags or interruption (Thread.interrupt()); stop() is deprecated.
Explain about ExecutorService? A framework for managing thread pools and task execution.
What is atomic variables? Variables providing atomic updates to avoid race conditions.
List and explain thread-safe collections. ConcurrentHashMap, CopyOnWriteArrayList, CopyOnWriteArraySet: Support safe multi-threaded operations.

Collection:

Question Answer
How to calculate new ArrayList size? and default size. Default size: 10; New size: (oldCapacity * 3/2) + 1.
How does CopyOnWriteArrayList handle modifications? Creates a new copy of the list on each modification.
How to convert a collection to iterator? Use collection.iterator().
Why String is popular HashMap key in Java? Immutable, efficient hashCode() and equals() implementations.
What is the difference between ArrayList & LinkedList? ArrayList: Dynamic array, faster random access; LinkedList: Doubly linked list, better for insert/delete.
What is Comparable & Comparator interface? Comparable: Single sort order (compareTo()); Comparator: Custom sort order (compare()).
What is the difference between HashMap and Hashtable? HashMap: Non-synchronized, allows nulls; Hashtable: Synchronized, no nulls.
What is Iterator and ListIterator? Iterator: Traverse in one direction; ListIterator: Bidirectional traversal.
What is the difference between Collection and Collections? Collection: Interface; Collections: Utility class for collection operations.
How HashMap handles collision? Uses chaining (linked lists) in buckets.
What is the difference between poll() and remove() in Queue? poll(): Returns null if queue is empty; remove(): Throws exception.
What is the difference between fail-fast and fail-safe iterator? Fail-fast: Throws ConcurrentModificationException; Fail-safe: Works on a copy, no exception.
S. No Question Answer
1 How to calculate new ArrayList size? and default size. Default size: 10; New size: (oldCapacity * 3/2) + 1.
2 How does CopyOnWriteArrayList handle modifications? Creates a new copy of the list on each modification.
3 How to convert a collection to iterator? Use collection.iterator().
4 Why String is popular HashMap key in Java? Immutable, efficient hashCode() and equals() implementations.
5 What is the difference between ArrayList & LinkedList? ArrayList: Dynamic array, faster random access; LinkedList: Doubly linked list, better for insert/delete.
6 What is Comparable & Comparator interface? Comparable: Single sort order (compareTo()); Comparator: Custom sort order (compare()).
7 What is the difference between HashMap and Hashtable? HashMap: Non-synchronized, allows nulls; Hashtable: Synchronized, no nulls.
8 What is Iterator and ListIterator? Iterator: Traverse in one direction; ListIterator: Bidirectional traversal.
9 What is the difference between Collection and Collections? Collection: Interface; Collections: Utility class for collection operations.
10 How HashMap handles collision? Uses chaining (linked lists) in buckets.
11 What is the difference between poll() and remove() in Queue? poll(): Returns null if queue is empty; remove(): Throws exception.
12 What is the difference between fail-fast and fail-safe iterator? Fail-fast: Throws ConcurrentModificationException; Fail-safe: Works on a copy, no exception.

Miscellenous questions:

  1. Why we overrider hashcode and equals?
  2. Why we implements cloneable?
  3. What is static and dynamic classLoading? class.forName(String class_name)
  4. Can you have virtual functions in Java? Y, all non static functions

Tricky Questions:

Question Answer
Is Empty .java file name a valid source file name? Yes
If I don’t provide any arguments on the command line arg will be Empty
What if I write static public void instead of public static void? Valid
Is constructor inherited? No
Can you make a constructor final/static? No, final not allowed
Can you make an abstract method static? Not allowed
Default constructor won’t work if I give any one constructor explicitly? Yes
Can we override the private and static methods? No
Arrange the order of execution blocks. Static, instance, constructor, main
Can we execute a program without main() method? Yes, using static block
Can we override/overload main method? override No, Overload No
What if static modifier is removed from main method? NoSuchMethodError
Can we declare static variables and methods in abstract class? Yes
Is method overloading possible with return type? Why? No, it doesn’t distinguish methods
Can we use abstract and final in the same method? No
Can we use static and abstract at the same time for a method? No
Can we declare an interface method as static? No
Can an interface be final? No
Can we use private/protected for members of an interface? No
What is readonly and writeonly class? Not applicable in Java
Do I need to import java.lang package? No, it’s default
Is it necessary that each try block must be followed by catch block? No, finally can follow
Can finally block be used without catch? Yes
Does multiple finally block allowed? No
Is there any case where the finally block will not be executed? Yes, System.exit()
Can exception be rethrown? Yes, only unchecked exceptions
How many objects are created in String a = new String(“ra”);? Two
How can we make an immutable class in Java? Use final members and class
Can a class have an interface? Yes, as a nested interface
Can an interface have a class? Yes, static implicitly
What is System.gc()? Suggests garbage collection
What are the 3 ways objects can be unreferenced? Nullify reference, reassign, isolate
Is it possible to start a thread twice? No
Can we call the run() method instead of start()? Yes, but thread won’t start
Does each thread have its stack in multithreaded programming? Yes
In which way of object creation constructor is not called? Cloning
Can we import the same package/class two times? Yes
Can there be an abstract method without an abstract class? Yes, in an interface
Can we have multiple static blocks in a class? Yes, executes sequentially
Does Functional interface can extend another interface ? Yes, but parent should have one method
Is null case-sensitive? What does null == null return? Null is not case-sensitive; null == null returns true.

Output questions:

  1. System.out.println(10 + 20 + “Javatpoint”); //30Javatpoint
  2. System.out.println(“Javatpoint” + 10 + 20); //Javatpoint1020
  3. System.out.println(10 * 20 + “Javatpoint”); //200Javatpoint
  4. System.out.println(“Javatpoint” + 10 * 20); //Javatpoint200
  5. for(int i=0; 0; i++) //compile time error
  6. Will it work, throw 90;catch(int e)? N, it is not throwable
  7. Write doubly linked list in java.

Design patterns:

  1. What is factory and abstract factory method?

IO Serialization questions:

  1. What is Transient?
  2. What is bufferInputStream and bufferOutputStream?
  3. What is FileInputStream and FileOutputStream?
  4. What is Serializable and Externalizable interface?