Registered users can read the full content for free
Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.
Page
1
(This page has no text content)
Page
2
(This page has no text content)
Page
3
Grokking The Java Developer Interview More Than 200 Questions To Crack The Java, Spring, SpringBoot & Hibernate Interview JATIN ARORA
Page
4
GROKKING THE JAVA DEVELOPER INTERVIEW COPYRIGHT © 2020 BY JATIN ARORA. All rights reserved. No part of this book may be reproduced, distributed, or transmitted in any form or by any means, including photocopying, recording or other electronic or mechanical methods, without the prior written permission of the author, except in the case of brief quotations embodied in critical reviews and certain other non-commercial uses permitted by copyright law. For permission or any other information, contact me at: jatinarora208@gmail.com
Page
5
Preface Grokking The Java Developer Interview helps you to crack a Java, Spring & Hibernate interview. This book covers enough details of a Java/Spring topic that will surely help the absolute beginners and if you are already familiar with Java & Spring, then this book will help you to brush-up on the concepts. The book has more than 200 questions that are frequently asked during an interview for Java, Spring, SpringBoot & Hibernate profile. Some of the important topics like Multi- threading, Collection framework, Singleton Pattern, SpringBoot Annotations and many more are covered in detail. Most of these topics are explained with code examples that will help you to quickly grasp the concept. Who is this book for? This book is for you if you are either preparing for an interview, planning to move into this field in future, brushing up your Java & Spring skills or just want to get an in-depth overview of the field. This book provides the most important and frequently asked questions along with their solutions. Who is this book NOT for? This book is not for you if you are looking for an in-depth study of Java or Spring. The objective of this book is not to discuss the ongoing research or challenges in this industry or serve as a substitute for a course book. If you follow this book diligently, you would be better equipped to face any Java interview. Whether you are a beginner or an intermediate level expert in Java, this book has enough juice for you.
Page
6
This book contains a lot of code examples, most of the code snippets are displayed in image format that you can zoom-in and out of. However, if some image is not clearly visible on your device, you can also refer to the GitHub repository for this book. The GitHub repo contains all of the snippets shown in this book and it can be found at: https://github.com/reachjatin/Grokking-The-Java- Developer-Interview Contact Information I would love to hear from you. For feedback or queries, you can contact me at jatinarora208@gmail.com Your valuable suggestions to improve the book are always welcomed. I wish you all the best and I am confident that this book will help you in making that job switch that you are looking for. Happy Learning, Cheers :) -Jatin Arora
Page
7
Acknowledgements I thank my brother, Sumit Kumar, for encouraging me to take up this project. From reading early drafts to advising me on the structure of the book to making sure that it was written in a manner that was coherent, organized and engaging at the same time, his role has been pivotal in the making of this book. To elaborate a little on his background, Sumit is an UChicago Alum, and presently working for Amazon, Seattle as a Research Scientist at Alexa AI. He has also worked with Samsung, India as a Lead Engineer. He is an active blogger and regularly post articles on Data structures and Algorithms, System Design, NLP and many more interesting topics. You can find those at: https://medium.com/@sumit.arora https://blog.reachsumit.com To Sumit, for his patience in reading through various preliminary versions of this book, and his vital suggestions. Thank you brother for never saying no to my numerous feedback requests and providing all the constructive criticism. I dedicate this book to him. I would like to express my sincere gratitude to my family, friends, and everyone who motivated me throughout. Thanks for the support, the honest feedback, and for everything else that has helped to make this book possible in its current form. -Jatin Arora
Page
8
Table of Content s Preface Acknowledgements Question 1: What are the 4 pillars of OOPS? Question 2: What is an abstract class? Question 3: Does Abstract class have constructor? Question 4: What is an Interface? Question 5: Difference between abstract class and interface Question 6: What to choose – interface or abstract class Question 7: Why Java 8 has introduced default methods? Question 8: Why Java 8 has introduced static methods? Question 9: Why Java does not allow multiple inheritance? Question 10: What are the rules for Method Overloading and Method Overriding? Question 11: Can we override final methods? Question 12: Can constructors and private methods be overridden? Question 13: What is final keyword and where it can be used? Question 14: What is exception and exception handling? Question 15: Difference between error and exception Question 16: What are the different types of exceptions? Question 17: How exception handling is done in java? Question 18: Can we write a try block without catch block? Question 19: How to handle multiple exceptions together? Question 20: When finally block will not get executed
Page
9
Question 21: Difference between throw and throws keyword. And discuss Exception Propagation Question 22: Exception handling w.r.t. method overriding Question 23: Programs related to Exception handling and return keyword Question 24: How to make your own custom exception class? Question 25: How to make custom checked / unchecked exception? Question 26: What happens when you throw an exception from finally block? Question 27: What will be Output of below program related to try-catch-finally? Question 28: Explain try-with-resources Question 29: Why String is Immutable? Question 30: What does the equals() method of String class do? Question 31: Explain StringBuffer and StringBuilder Question 32: Explain the output of below program related to equals() method of StringBuilder Question 33: When to use String/StringBuffer/StringBuilder Question 34: Explain equals and hashcode contract Question 35: What is Marker Interface? Question 36: Can you write your own custom Marker interface? Question 37: What is Comparable and Comparator? Question 38: How to compare a list of Employees based on name and age such that if name of the employee is same then sorting should be based on age
Page
10
Question 39: Difference between Comparable and Comparator Question 40: Different methods of Object class Question 41: What type of arguments are allowed in System.out.println() method? Question 42: Explain System.out.println() statement Question 43: Explain Auto-boxing and Un-boxing Question 44: Find the output of below program Question 45: Can you pass primitive long value in switch statement? Question 46: Explain static keyword in Java Question 47: What is an Inner Class in Java, how it can be instantiated and what are the types of Inner Classes? Question 48: What is Constructor Chaining in java? Question 49: What is init block? Question 50: What is called first, constructor or init block? Question 51: What is Variable shadowing and Variable hiding in Java? Question 52: What is a constant and how we create constants in Java? Question 53: Explain enum Question 54: What is Cloneable? Question 55: What is Shallow Copy and Deep Copy? Question 56: What is Serialization and De-serialization? Question 57: What is SerialVersionUID? Question 58: Serialization scenarios with Inheritance Question 59: Stopping Serialization and De-serialization Question 60: What is Externalizable Interface?
Page
11
Question 61: Externalizable with Inheritance Question 62: Difference between Serializable and Externalizable Question 63: How to make a class Immutable? Question 64: Explain Class loaders in Java Question 65: What is Singleton Design Pattern and how it can be implemented? Question 66: What are the different ways in which a Singleton Design pattern can break and how to prevent that from happening? Question 67: What are the different design patterns you have used in your projects? Question 68: Explain Volatile keyword in java Question 69: What is Garbage Collection in Java, how it works and what are the different types of Garbage Collectors? Question 70: Explain Generics in Java Question 71: What is Multi-threading? Question 72: How to create a thread in Java? Question 73: Which way of creating threads is better: Thread class or Runnable interface Question 74: What will happen if I directly call the run() method and not the start() method to execute a thread Question 75: Once a thread has been started can it be started again Question 76: Why wait, notify and notifyAll methods are defined in the Object class instead of Thread class Question 77: Why wait(), notify(), notifyAll() methods must be called from synchronized block Question 78: difference between wait() and sleep() method
Page
12
Question 79: join() method Question 80: yield() method Question 81: Tell something about synchronized keyword Question 82: What is static synchronization? Question 83: What will be output of below program where one synchronized method is calling another synchronized method? Question 84: Programs related to synchronized and static synchronized methods Question 85: What is Callable Interface? Question 86: How to convert a Runnable to Callable Question 87: Difference between Runnable and Callable Question 88: What is Executor Framework in Java, its different types and how to create these executors? Question 89: Tell something about awaitTermination() method in executor Question 90: Difference between shutdown() and shutdownNow() methods of executor Question 91: What is Count down latch in Java? Question 92: What is Cyclic Barrier? Question 93: Atomic classes Question 94: What is Collection Framework? Question 95: What is Collections? Question 96: What is ArrayList? Question 97: What is default size of ArrayList? Question 98: Which data structure is used internally in an ArrayList? Question 99: How add() method works internally or How the ArrayList grows at runtime
Page
13
Question 100: How to make an ArrayList as Immutable Question 101: What is LinkedList? Question 102: When to use ArrayList / LinkedList Question 103: What is HashMap? Question 104: Explain the internal working of put() and get() methods of HashMap class and discuss HashMap collisions Question 105: equals and hashCode method scenarios in HashMap when the key is a custom class Question 106: How to make a HashMap synchronized? Question 107: What is Concurrent HashMap? Question 108: What is HashSet class and how it works internally? Question 109: Explain Java’s TreeMap Question 110: Explain Java’s TreeSet Question 111: Difference between fail-safe and fail-fast iterators Question 112: Difference between Iterator and ListIterator Question 113: Difference between Iterator.remove and Collection.remove() Question 114: What is the difference between a Monolith and Micro-service architecture? Question 115: What is Dependency Injection in Spring? Question 116: What are the different types of Dependency Injection? Question 117: Difference between Constructor and Setter injection Question 118: What is @Autowired annotation? Question 119: What is the difference between BeanFactory and ApplicationContext?
Page
14
Question 120: Explain the life-cycle of a Spring Bean Question 121: What are the different scopes of a Bean? Question 122: What is the Default scope of a bean? Question 123: What happens when we inject a prototype scope bean in a singleton scope bean? Question 124: How to inject a prototype scope bean in a singleton scope bean? Question 125: Explain Spring MVC flow Question 126: What is the difference between <context:annotation-config> and <context:component- scan>? Question 127: What is the difference between Spring and SpringBoot? Question 128: What is auto-configuration in SpringBoot? Question 129: What are SpringBoot starters? Question 130: What is @SpringBootApplication Annotation? Question 131: Where does a Spring Boot application start from? Question 132: How to remove certain classes from getting auto-configured in SpringBoot? Question 133: How to autowire a class which is in a package other than SpringBoot application class’s package or any of its sub-packages Question 134: What is application.properties file in a SpringBoot application? Question 135: How to configure the port number of a SpringBoot application? Question 136: Which jar builds our springboot application automatically whenever we change some code just like a node.js application?
Page
15
Question 137: What default embedded server is given in spring boot web starter and how we can change the default embedded server to the one of our choice Question 138: Where should we put our html and javascript files in a spring boot application? Question 139: What are the different stereotype annotations? Question 140: Difference between @Component, @Controller, @Service, @Repository annotations? Question 141: Difference between @Controller and @RestController annotation Question 142: What is @RequestMapping and @RequestParam annotation? Question 143: How to define a Get or Post endpoint? Question 144: Which annotation is used for binding the incoming json request to the defined pojo class? Question 145: What is @Qualifier annotation? Question 146: What is @Transactional annotation? Question 147: What is @ControllerAdvice annotation? Question 148: What is @Bean annotation? Question 149: Difference between @Component and @Bean Question 150: How to do profiling in a SpringBoot application Question 151: What is RestTemplate? Question 152: What is Spring Data JPA? Question 153: What is the difference between JPARepository, CRUDRepository, PagingAndSortingRepository and which one you have used in your applications? Question 154: What is Spring AOP?
Page
16
Question 155: Have you used Spring Security in your application Question 156: What do you know about Spring Batch framework? Question 157: Difference between SOAP and REST Question 158: What is Restful api? Question 159: What is a stateless object? Question 160: What is the difference between Web Server and Application Server? Question 161: What do you know about CommandLineRunner and ApplicationRunner? Question 162: What do you know about Eureka Naming Server? Question 163: What do you know about Zuul? Question 164: What do you know about Zipkin? Question 165: What do you know about Hysterix? Question 166: What is JPA? Question 167: What is Hibernate? Question 168: Difference between JPA and Hibernate Question 169: What is @Entity? Question 170: How to give a name to a table in JPA? Question 171: What is @Id, @GeneratedValue? Question 172: How to use a custom database sequence in Hibernate to generate primary key values? Question 173: How to give names to the columns of a JPA Entity Question 174: How to define a @OneToMany relationship between entities
Page
17
Question 175: Why annotations should be imported from JPA and not from Hibernate? Question 176: What is the difference between get() and load() method of Hibernate Session? Question 177: What is the difference between save(), saveOrUpdate() and persist() method of Hibernate Session? Question 178: What is Session and SessionFactory in Hibernate? Question 179: What is First Level and Second Level Cache in Hibernate? Question 180: What is session.flush() method in Hibernate? Question 181: How can we see the SQL query that gets generated by Hibernate? Question 182: What is Hibernate Dialect and why we need to configure it? Question 183: What do you know about hibernate.hbm2ddl.auto property in Hibernate? Question 184: What is Maven? Question 185: What is pom.xml? Question 186: What is local repo and central repo? Question 187: Where we define our local repo path? Question 188: Where do we define proxies so that maven can download jars from the internet in a corporate environment? Question 189: Explain Maven build life-cycle Question 190: What do you know about SQL Joins? Question 191: Difference between TRUNCATE & DELETE statements Question 192: Difference between Function and Stored Procedure
Page
18
Question 193: What is DDL, DML statements? Question 194: How to find the nth highest salary from Employee table Question 195: Difference between UNION and UNION ALL commands in SQL Question 196: Difference between Unique Key and Primary Key in SQL Question 197: What is the difference between Primary and Foreign key in SQL? Question 198: What is the difference between clustered and non-clustered index? Question 199: What is the difference between WHERE and HAVING clause in SQL Question 200: How to change the gender column value from Male to Female and Female to Male using single Update statement Question 201: Find first 3 largest numbers in an array Question 202: Move all negative numbers at the beginning of an array and all positive numbers at the end About the Author
Page
19
Question 1: What are the 4 pillars of OOPS? Answer: 4 pillars of OOPS are: 1. Abstraction 2. Encapsulation 3. Inheritance 4. Polymorphism Let’s take a look at them: 1. Abstraction : Abstraction is a process of hiding the implementation details and showing only functionality to the user. Real world examples: - TV remote: To start the TV, you have to press the power button, you don’t have to know about the internal circuit operations like how infrared waves are passing. - Car gears: We know what happens when we change the gear. But we don’t know how changing gear works under the hood, that information is irrelevant to us, so it is abstracted. In java, Abstraction can be achieved in two ways: - Abstract classes - Interfaces 2. Encapsulation : Encapsulation is a process of Binding data and methods within a class . Think of it like showing the essential details of a class by using the access control modifiers (public, private, protected ). So, we can say that Encapsulation leads to the desired level of Abstraction.
Page
20
Example: Java Bean, where all data members are made private and you define certain public methods to the outside world to access them. 3. Inheritance : Using inheritance means defining a parent-child relationship between classes, by doing so, you can reuse the code that is already defined in the parent class. Code reusability is the biggest advantage of Inheritance. Java does not allow multiple inheritance through classes but it allows it through interfaces. 4. Polymorphism : Poly means many and Morph means forms. Polymorphism is the process in which an object or function takes different forms. There are 2 types of Polymorphism : - Compile Time Polymorphism (Method Overloading) - Run Time Polymorphism (Method Overriding) In Method overloading, two or more methods in one class have the same method name but different arguments. It is called as Compile time polymorphism because it is decided at compile time which overloaded method will be called. Overriding means when we have two methods with same name and same parameters in parent and child class. Through overriding, child class can provide specific implementation for the method which is already defined in the parent class. Question 2: What is an abstract class?
The above is a preview of the first 20 pages. Register to read the complete e-book.
AI Reading Assistant
Whole-book reading guide from stratified index samples; jump to passages in the text
AI guide
【One-Line Pitch】
A question-driven cram guide that walks Java developers through the exact topics interviewers probe—core language semantics, collections internals, concurrency, and Spring wiring—so you can rehearse crisp, correct answers instead of re-reading tutorials. Best for junior-to-mid Java candidates preparing for technical screens and for developers who want a fast refresher on "why does this behave that way" questions.
【Book Arc】
- **Opening (~0%–15%)**: Establishes the Q&A format and starts with OOP fundamentals—the four pillars, interfaces vs. abstract classes, overriding rules, and exception-handling edge cases—solving the "can I explain the basics precisely?" problem.
- **Early (~15%–35%)**: Moves into language mechanics that trip people up: marker interfaces, Comparable/Comparator, static and inner classes, cloning, serialization/Externalizable, and Singleton (including how it breaks via reflection/serialization and how to defend it).
- **Middle (~35%–60%)**: Shifts to runtime and concurrency: JVM heap generations and GC pauses, thread lifecycle (start vs. run, restart rules), wait/notify placement in Object, join ordering, synchronized/re-entrant locking, and thread pools.
- **Late (~55%–75%)**: Dives into collection internals—ArrayList growth and immutability options, LinkedList structure, HashMap put/get, hashing, collisions, equals/hashCode with custom keys, ConcurrentHashMap, HashSet/TreeMap/TreeSet, and fail-fast vs. fail-safe iterators.
- **Ending (~75%–100%)**: Bridges to the Spring ecosystem—monolith vs. microservices, dependency injection types, @Autowired, and BeanFactory vs. ApplicationContext—closing the loop from language to framework. (Excerpts do not cover the Hibernate/Spring Boot portions in depth.)
【Key Takeaways】
- **Precision beats breadth in Java interviews** (Opening): The book drills exact rules—covariant returns, narrower checked exceptions, unreachable code after return—because interviewers test edge cases, not definitions.
- **Interfaces are contracts, abstract classes are partial implementations** (Opening): Java 8 default/static methods soften the old divide, but the "what vs. how" framing remains the cleanest answer.
- **Marker interfaces are just instanceof checks** (Early): Nothing magical happens in the JVM; understanding this lets you write your own markers and explain Cloneable/Serializable correctly.
- **Singleton is fragile by default** (Early): Reflection, serialization, and cloning can each create a second instance; readResolve(), enum singletons, and Bill Pugh's holder idiom are the standard defenses.
- **GC generations explain latency** (Middle): Young/Old/Perm (metaspace-era) structure, minor vs. major collections, and "Stop the World" pauses are the vocabulary interviewers expect for performance questions.
- **Thread semantics hinge on start(), not run()** (Middle): Calling run() directly executes on the caller thread; a thread can start only once; wait/notify live in Object because locks belong to objects, not threads.
- **HashMap internals are a favorite probe** (Late): Default capacity 16, load factor 0.75, hash spreading via `h ^ (h >>> 16)`, Node buckets, and collision handling—plus why equals/hashCode must agree for custom keys.
- **Choose collections by contract, not habit** (Late): ArrayList vs. LinkedList trade-offs, synchronized vs. ConcurrentHashMap, fail-fast vs. fail-safe iterators—answers should name the trade-off, not just the class.
【Reading Tips】
- **Use it as a drill book, not a tutorial**: Read a question, answer aloud, then check. The value is in retrieval practice, not passive reading.
- **Deep-read the internals chapters** (HashMap, concurrency, GC): These are where follow-up questions cascade; skim the definitional OOP questions if you're already solid.
- **Type and run the small programs**: Several answers hinge on output prediction (finally blocks, synchronized scenarios, serialVersionUID mismatches)—seeing them run cements the rule.
- **Build a one-page cheat sheet** of rules you keep missing (exception-in-override rules, iterator types, injection trade-offs) for the final review pass.
- **Pair with hands-on Spring/Hibernate practice**: The framework section is question-oriented; supplement with real project experience for behavioral follow-ups.
【Coverage Limits】
This guide is based on stratified excerpts that emphasize core Java, collections, concurrency, and early Spring DI questions; the Hibernate and Spring Boot material promised by the title is only lightly represented, so treat those areas as needing outside preparation.
Passage locations
Page 13
ion 108: What is HashSet class and how it works internally? Question 109: Explain Java’s TreeMap Question 110: Explain Java’s TreeSet Question 111: Differenc...
View in text
Excerpt 2
alizable interface then NotSerializableException is thrown. Question 36: Can you write your own custom Marker interface? Answer: Yes. As you already know tha...
View in text
Excerpt 3
sent in the class, let’s see what will happen in that case. Let’s change the Employee class constructor and getter method: Employee.java: Test.java: Output:...
View in text
Excerpt 4
s that are calling 2 different static synchronized methods. Here, these 2 threads will block each other, as only one lock per class exists. So, these 2 stati...
View in text
Recommended for You
{{#thumbnailUrl}}
{{/thumbnailUrl}}
{{^thumbnailUrl}}
{{/thumbnailUrl}}
Loading recommended books...
Failed to load, please try again later
Tip the Site
Scan the WeChat Pay or Alipay code to tip. No login required.
WeChat Pay
Alipay