Java Interview Questions: 1

Created by
@TopCoding

Questions and Answers

Every quiz on Quizgecko comes with Questions, Flashcards and Study notes to help you learn optimally. Sign up free now.

How does a HashMap in Java store key-value pairs?

A HashMap in Java uses an array of nodes (buckets) and a linked list or a binary tree to store key-value pairs.

What is the purpose of a ClassLoader in Java?

A ClassLoader loads classes into memory.

What does the Java Memory Model (JMM) specify?

The JMM specifies how threads interact through memory and what behaviors are allowed in concurrent executions.

What is the difference between Comparable and Comparator interfaces?

<p><code>Comparable</code> is used for natural ordering and implemented by the class itself, while <code>Comparator</code> is used for custom ordering and implemented as a separate class.</p> Signup and view all the answers

What does the volatile keyword ensure in Java?

<p>The <code>volatile</code> keyword ensures that changes to a variable are visible to all threads.</p> Signup and view all the answers

What happens when multiple keys map to the same bucket in a HashMap?

<p>They are stored in a linked list or a binary tree within that bucket.</p> Signup and view all the answers

What is the purpose of the hashCode() method in Java?

<p>The <code>hashCode()</code> method is used to locate and retrieve entries efficiently in a <code>HashMap</code>.</p> Signup and view all the answers

How does the Bootstrap ClassLoader work?

<p>It loads core Java classes (<code>java.lang.*</code>, etc.).</p> Signup and view all the answers

What is the purpose of the synchronized keyword in Java?

<p>It is used to enforce ordering and visibility of changes to shared variables in concurrent executions.</p> Signup and view all the answers

What is the main difference between natural ordering and custom ordering in Java?

<p>Natural ordering is implemented by the class itself using <code>Comparable</code>, while custom ordering is implemented as a separate class using <code>Comparator</code>.</p> Signup and view all the answers

What is the Diamond Problem in Java and how does it occur?

<p>The Diamond Problem occurs when a class inherits from two classes with the same method.</p> Signup and view all the answers

What are the key benefits of using Spring Boot for microservices development?

<p>Simplified configuration, embedded servers, and Spring Cloud for distributed systems management.</p> Signup and view all the answers

What security services does Spring Security provide for Java applications?

<p>Authentication, authorization, and protection against common vulnerabilities.</p> Signup and view all the answers

What is the core concept of aspect-oriented programming (AOP) in Spring?

<p>Separation of cross-cutting concerns from the main business logic using aspects, advice, and pointcuts.</p> Signup and view all the answers

What is the purpose of annotations in Java, and how do they influence program behavior?

<p>Annotations provide metadata that can influence program behavior at compile-time or runtime.</p> Signup and view all the answers

What is the key difference between the Callable and Runnable interfaces in Java?

<p>Callable returns a result and can throw a checked exception, while Runnable does not return a result and cannot throw a checked exception.</p> Signup and view all the answers

How does JPA differ from Hibernate in terms of ORM in Java?

<p>JPA is a specification for ORM, while Hibernate is a JPA implementation.</p> Signup and view all the answers

What is the primary concern of reactive programming, and how is it implemented in Java?

<p>Reactive programming is concerned with data streams and propagation of change, implemented using frameworks like Reactor and RxJava.</p> Signup and view all the answers

What is Apache Kafka, and how can it be integrated with Java applications?

<p>Apache Kafka is a distributed streaming platform, integrated with Java applications using Kafka clients for producing and consuming messages.</p> Signup and view all the answers

What is the main difference between the Serial Garbage Collector and the Parallel Garbage Collector in Java?

<p>The Serial Garbage Collector uses a single thread, whereas the Parallel Garbage Collector uses multiple threads for garbage collection.</p> Signup and view all the answers

What is the main advantage of the G1 (Garbage-First) Garbage Collector in Java?

<p>The G1 Garbage Collector divides the heap into regions and collects regions with the most garbage first.</p> Signup and view all the answers

What is the purpose of the metaspace in Java?

<p>The <code>metaspace</code> is used to store class metadata and is not limited by the heap size.</p> Signup and view all the answers

How does the synchronized keyword work in Java?

<p>The <code>synchronized</code> keyword ensures that only one thread can execute a block of code or method at a time by locking the object or class.</p> Signup and view all the answers

What is the main difference between a PhantomReference and a WeakReference in Java?

<p>A <code>PhantomReference</code> is used to track objects that have been finalized but not yet collected by the garbage collector, whereas a <code>WeakReference</code> is collected when no strong references exist.</p> Signup and view all the answers

What is the purpose of default methods in interfaces in Java?

<p>Default methods in interfaces allow interfaces to provide methods with implementations to avoid breaking existing implementations.</p> Signup and view all the answers

How does the ForkJoinPool framework work in Java?

<p>The <code>ForkJoinPool</code> is designed for work that can be broken into smaller tasks and executed in parallel using the <code>ForkJoinTask</code> to recursively divide tasks and join the results.</p> Signup and view all the answers

What is the purpose of Reflection in Java?

<p>Reflection allows inspection and modification of classes, methods, and fields at runtime.</p> Signup and view all the answers

How does Spring manage the bean life cycle?

<p>Spring manages the bean life cycle through stages: instantiation, dependency injection, initialization, and destruction.</p> Signup and view all the answers

What is the main advantage of CompletableFuture over Future in Java?

<p>CompletableFuture extends Future with asynchronous methods, allowing non-blocking operations and combining multiple futures with functional-style callbacks.</p> Signup and view all the answers

Study Notes

Java Advanced Topics

HashMap Internal Working

  • A HashMap uses an array of nodes (buckets) and a linked list or binary tree to store key-value pairs.
  • The hash code of a key determines the bucket index, and collisions are stored in a linked list or binary tree.

ClassLoader

  • A ClassLoader is part of the Java Runtime Environment that loads classes into memory.
  • Types of ClassLoader include:
    • Bootstrap ClassLoader: Loads core Java classes (java.lang.*, etc.).
    • Extension ClassLoader: Loads classes from the java.ext.dirs directory.
    • System/Application ClassLoader: Loads classes from the classpath.

Java Memory Model

  • The Java Memory Model (JMM) defines how threads interact through memory and what behaviors are allowed in concurrent executions.
  • It specifies how and when different threads can see values written by other threads, and how to enforce ordering and visibility of changes to shared variables using volatile, synchronized, and final.

Comparable and Comparator

  • Comparable is used for natural ordering and is implemented by the class itself (compareTo method).
  • Comparator is used for custom ordering and is implemented as a separate class (compare method).

Volatile Keyword

  • The volatile keyword ensures that changes to a variable are visible to all threads.
  • It prevents the variable from being cached in CPU caches, so every read/write is done directly from/to main memory.

Garbage Collectors

  • Types of garbage collectors include:
    • Serial Garbage Collector: Uses a single thread.
    • Parallel Garbage Collector: Uses multiple threads for garbage collection.
    • CMS (Concurrent Mark-Sweep) Garbage Collector: Aims for low pause times.
    • G1 (Garbage-First) Garbage Collector: Divides heap into regions and collects regions with the most garbage first.

PermGen and Metaspace

  • PermGen (Permanent Generation) was used in Java 7 and earlier to store class metadata.
  • Metaspace was introduced in Java 8, which is not limited by the heap size and can grow dynamically.

Synchronized Keyword

  • Synchronized ensures that only one thread can execute a block of code or method at a time.
  • It locks the object or class, preventing other threads from accessing the synchronized block until the lock is released.

References

  • PhantomReference is used to track objects that have been finalized but not yet collected by the garbage collector.
  • WeakReference is collected when no strong references exist.
  • SoftReference is collected when memory is low.

Default and Static Methods in Interfaces

  • Default methods allow interfaces to provide methods with implementations to avoid breaking existing implementations.
  • Static methods can be called without an instance.

ForkJoinPool

  • The ForkJoinPool is designed for work that can be broken into smaller tasks and executed in parallel.
  • It uses the ForkJoinTask to recursively divide tasks and join the results.

Reflection

  • Reflection allows inspection and modification of classes, methods, and fields at runtime.
  • It is used for tasks like testing frameworks, dependency injection, and accessing private members.

Spring Framework

  • Spring manages the bean life cycle through stages: instantiation, dependency injection, initialization, and destruction.
  • Bean life cycle methods like @PostConstruct and @PreDestroy can be used for custom initialization and cleanup.

ExecutorService and ForkJoinPool

  • ExecutorService is designed for managing a pool of threads and submitting tasks.
  • ForkJoinPool is specifically designed for fork/join tasks that can be broken into smaller tasks.

CompletableFuture

  • CompletableFuture extends Future with asynchronous methods, allowing non-blocking operations and combining multiple futures with functional-style callbacks.

java.util.concurrent Package

  • The java.util.concurrent package provides utilities for concurrent programming, including thread-safe collections, synchronization aids, and executors for managing threads.

Spring Annotations

  • @Component is a generic stereotype for any Spring-managed component.
  • @Service indicates a service layer component.
  • @Repository indicates a data access layer component and adds persistence exception translation.
  • @Controller is used for Spring MVC controllers.

Hibernate and ORM

  • Hibernate is an ORM framework that maps Java objects to database tables, managing database operations and providing features like caching, lazy loading, and transaction management.

Lazy and Eager Initialization

  • Lazy Initialization defers the bean creation until it is needed.
  • Eager Initialization creates the beans at the startup.

Diamond Problem

  • The Diamond Problem occurs with multiple inheritance, where a class inherits from two classes with the same method.
  • Java handles it by using interfaces with default methods and avoiding multiple class inheritance.

Microservices and Spring Boot

  • Microservices architecture structures an application as a collection of loosely coupled services.
  • Spring Boot supports microservices with features like embedded servers, simplified configuration, and Spring Cloud for distributed systems management.

Spring Security

  • Spring Security provides comprehensive security services for Java applications, including authentication, authorization, and protection against common vulnerabilities.

Stream API

  • The Stream API is used for processing sequences of elements, providing operations like map, filter, and reduce, supporting functional-style programming.

Aspect-Oriented Programming (AOP)

  • AOP allows separation of cross-cutting concerns (like logging, security) from the main business logic using aspects, advice, and pointcuts.

Annotations

  • Annotations provide metadata for classes, methods, and fields.
  • They can influence program behavior at compile-time or runtime and are used extensively in frameworks like Spring and JPA.

Callable and Runnable Interfaces

  • Callable returns a result and can throw a checked exception.
  • Runnable does not return a result and cannot throw a checked exception.

JPA and Hibernate

  • JPA (Java Persistence API) is a specification for ORM in Java.
  • Hibernate is a JPA implementation that provides additional features.

Reactive Programming

  • Reactive Programming is an asynchronous programming paradigm concerned with data streams and propagation of change.
  • It is implemented in Java using frameworks like Reactor and RxJava, supporting non-blocking and event-driven applications.

Apache Kafka

  • Apache Kafka is a distributed streaming platform used for building real-time data pipelines and streaming applications.
  • It can be integrated with Java applications using Kafka clients for producing and consuming messages.

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

More Quizzes Like This

Use Quizgecko on...
Browser
Browser