{"id":2288,"date":"2026-08-12T12:43:40","date_gmt":"2026-08-12T12:43:40","guid":{"rendered":"http:\/\/www.wscubetech.com\/blog\/?p=2288"},"modified":"2026-08-12T12:57:02","modified_gmt":"2026-08-12T12:57:02","slug":"java-interview-questions-answers","status":"publish","type":"post","link":"https:\/\/www.wscubetech.com\/blog\/java-interview-questions-answers\/","title":{"rendered":"155+\u00a0Java\u00a0Interview Questions and Answers (2026 Updated)"},"content":{"rendered":"\n<p>Java&nbsp;remains&nbsp;one of the most widely used&nbsp;<a href=\"https:\/\/www.wscubetech.com\/blog\/best-programming-languages\/\" target=\"_blank\" rel=\"noreferrer noopener\">programming languages<\/a>&nbsp;for building web, mobile, and enterprise applications. Its platform independence, reliability, and strong community support make it a preferred choice for developers worldwide.&nbsp;<\/p>\n\n\n\n<p>With companies hiring Java developers across backend, Android, and cloud roles, a clear understanding of core concepts and coding skills matters more than ever.&nbsp;Practising&nbsp;Java interview questions helps you revise important topics such as OOP, collections, exception handling, multithreading, and modern <a href=\"https:\/\/www.wscubetech.com\/resources\/java\/features\" target=\"_blank\" rel=\"noreferrer noopener\">Java features<\/a>. It also builds confidence and sharpens your problem solving.&nbsp;<\/p>\n\n\n\n<p>In this blog, you will find the&nbsp;most commonly asked&nbsp;questions, expert answers, and useful tips. Everything is grouped by topic and experience level, so freshers and experienced developers can both find what they need and prepare with ease.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Java&nbsp;Interview Questions for Freshers &amp; Beginners<\/h2>\n\n\n\n<p>Below are some commonly asked&nbsp;Java&nbsp;interview questions and answers for freshers that cover the basic concepts of&nbsp;Java. These questions can help you strengthen your fundamentals and prepare effectively for technical interviews.&nbsp;<\/p>\n\n\n\n<p>If you have recently completed a&nbsp;<a href=\"https:\/\/courses.wscubetech.com\/courses\/Java-In-Depth-Become-a-Complete-Java-Engineer?utm_source=WsBlog&amp;utm_medium=BlogInterlink&amp;utm_campaign=seo\" target=\"_blank\" rel=\"noreferrer noopener\">Java course<\/a>&nbsp;or are looking for your first Java developer role, reviewing the questions below will help you gain confidence and improve your interview readiness. You can also revise the basics with our&nbsp;complete <a href=\"https:\/\/www.wscubetech.com\/resources\/java\" target=\"_blank\" rel=\"noreferrer noopener\">Java tutorial<\/a>&nbsp;before you begin.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. What is&nbsp;Java? Explain its meaning and definition.<\/h3>\n\n\n\n<p><a href=\"https:\/\/www.wscubetech.com\/resources\/java\" target=\"_blank\" rel=\"noreferrer noopener\">Java<\/a>&nbsp;is a high-level, object-oriented programming language used to develop web applications, mobile apps, desktop software, games, and enterprise applications. It is known for its platform independence, which means&nbsp;<a href=\"https:\/\/www.wscubetech.com\/resources\/java\/programs\" target=\"_blank\" rel=\"noreferrer noopener\">Java&nbsp;programs<\/a> can run on any system that has a&nbsp;<a href=\"https:\/\/www.wscubetech.com\/resources\/java\/jvm\" target=\"_blank\" rel=\"noreferrer noopener\">Java&nbsp;Virtual Machine (JVM)<\/a>.&nbsp;<\/p>\n\n\n\n<p>Java was created by James Gosling and his team at&nbsp;Sun Microsystems, and&nbsp;was&nbsp;publicly released&nbsp;in 1995. It is secure, reliable, and easy to learn. Its &#8220;Write Once, Run Anywhere&#8221; (WORA) capability and extensive library support make it one of the most popular programming languages in the world.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. The syntax of&nbsp;Java&nbsp;is based on which programming language?<\/h3>\n\n\n\n<p><a href=\"https:\/\/www.wscubetech.com\/resources\/c-programming\" target=\"_blank\" rel=\"noreferrer noopener\">C<\/a>&nbsp;and&nbsp;<a href=\"https:\/\/www.wscubetech.com\/resources\/cpp\" target=\"_blank\" rel=\"noreferrer noopener\">C++<\/a>&nbsp;programming languages form the basis of&nbsp;Java&nbsp;syntax.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. How is&nbsp;Java&nbsp;platform independent?<\/h3>\n\n\n\n<p>This is one of the most common&nbsp;Java&nbsp;interview questions asked in technical interviews. To answer it correctly, you should understand how&nbsp;Java&nbsp;programs can run on different operating systems without requiring any modifications.&nbsp;<\/p>\n\n\n\n<p>Platform independent means that a&nbsp;Java&nbsp;program can be written on one machine and executed on different operating systems without any changes. This is possible because&nbsp;Java&nbsp;code is compiled into bytecode, which is executed by the&nbsp;Java&nbsp;Virtual Machine (JVM). Since each platform has its own JVM, the same bytecode can run anywhere without hardware compatibility issues.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4. When was&nbsp;Java&nbsp;developed?<\/h3>\n\n\n\n<p>Java&nbsp;development began in&nbsp;1991,&nbsp;and&nbsp;first&nbsp;public release was 1995.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5. Who developed the&nbsp;Java&nbsp;programming language?<\/h3>\n\n\n\n<p>Java&nbsp;was developed by&nbsp;<a href=\"https:\/\/www.linkedin.com\/in\/jamesgosling\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">James Gosling<\/a>, a Canadian computer scientist, while working at&nbsp;<a href=\"https:\/\/www.sun.com\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Sun Microsystems<\/a>. He is widely known as the founder of&nbsp;Java. Later, Sun Microsystems was&nbsp;acquired&nbsp;by&nbsp;<a href=\"https:\/\/www.oracle.com\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Oracle Corporation<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">6. What does \u2018write once run anywhere\u2019 mean in&nbsp;Java?<\/h3>\n\n\n\n<p>Write once, run anywhere, or WORA in&nbsp;Java&nbsp;means that it is a coding language where you write a program for some purpose only once and then use it or run it across multiple operating systems. For instance, you can write a program and run it on Windows, macOS, Android, Linux, etc.&nbsp;&nbsp;<\/p>\n\n\n\n<p>\u201cJava\u2019s write once, run anywhere\u201d term was first&nbsp;initiated&nbsp;by Sun Microsystems, where the founder of this language used to work. This characteristic makes&nbsp;Java&nbsp;a portable programming language.&nbsp;<\/p>\n\n\n\n<p><strong>Also Read:<\/strong>&nbsp;<a href=\"https:\/\/www.wscubetech.com\/blog\/html-interview-questions-answers\/\" target=\"_blank\" rel=\"noreferrer noopener\">Top 70+ HTML Interview Questions and Answers<\/a>&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">7. What is&nbsp;Java&nbsp;programming used for? Explain its primary applications.<\/h3>\n\n\n\n<p><strong>There is a wide range of use cases of&nbsp;Java&nbsp;programming&nbsp;language. Below are its main applications:&nbsp;<\/strong><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">a) Mobile App Development<\/h4>\n\n\n\n<p>Despite the introduction of Kotlin,&nbsp;Java&nbsp;is still used as a reliable programming language for Android app development. This coding language has&nbsp;the software&nbsp;development kits (SDKs) and libraries that are&nbsp;required&nbsp;to develop mobile apps.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">b) Chatbot Development<\/h4>\n\n\n\n<p>Another use of&nbsp;Java&nbsp;is in chatbot development. Smart chatbots that use natural language processing (NLP) can be built using this programming language.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">c) Development of Games<\/h4>\n\n\n\n<p>One of the most important applications of&nbsp;Java&nbsp;is in&nbsp;building games or gaming apps. Some of the world-famous games like Minecraft, Spiral Knights, SimCity, Saints Row 2, Asphalt 3, FIFA 11,&nbsp;Wakfu, Tokyo City Nights and many more are built on&nbsp;Java.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">d) Cloud Computing<\/h4>\n\n\n\n<p>The &#8220;Write Once, Run Anywhere&#8221; characteristic of&nbsp;Java&nbsp;makes it highly suitable for cloud applications. Many cloud platforms and distributed systems rely on&nbsp;Java&nbsp;because of&nbsp;its&nbsp;portability, scalability, and reliability.&nbsp;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">e) Big Data<\/h4>\n\n\n\n<p>Big data platforms or tools heavily depend on&nbsp;Java, and it is considered a language on which the future of big data relies. This is because of its features that enable faster processing of large sets of data.&nbsp;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">f) Enterprise-grade Web Applications<\/h4>\n\n\n\n<p>Java&nbsp;is widely used to develop enterprise-grade web applications. Its high performance, scalability, security, and support for a wide range of server-side technologies make it a popular choice for large-scale software development.&nbsp;<\/p>\n\n\n\n<p>Many organizations use&nbsp;Java&nbsp;for enterprise applications, and popular platforms such as LinkedIn and IRCTC are built using&nbsp;Java&nbsp;technologies.&nbsp;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">g) Internet of Things (IoT)<\/h4>\n\n\n\n<p>In&nbsp;IoT technology, sensors and smart devices collect and process data to perform different tasks. Many of these devices run applications developed using the&nbsp;Java&nbsp;programming language because of its reliability and platform independence.&nbsp;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">h) Artificial Intelligence (AI)<\/h4>\n\n\n\n<p>As one of the most suitable programming languages for <a href=\"https:\/\/www.wscubetech.com\/blog\/what-is-artificial-intelligence\/\" target=\"_blank\" rel=\"noreferrer noopener\">artificial intelligence (AI)<\/a> projects,&nbsp;Java&nbsp;can be used to develop intelligent solutions. For instance, it is great for building search algorithms, neural networks, ML-based services, deep learning applications, etc.&nbsp;&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">8. What is&nbsp;Java&nbsp;Virtual Machine (JVM)?<\/h3>\n\n\n\n<p>The&nbsp;Java&nbsp;Virtual Machine (JVM) is a software environment that executes&nbsp;Java&nbsp;bytecode and enables&nbsp;Java&nbsp;programs to run on different operating systems. It acts as a bridge between the&nbsp;Java&nbsp;program and the underlying hardware, making&nbsp;Java&nbsp;platform independent.<\/p>\n\n\n\n<iframe loading=\"lazy\" width=\"640\" height=\"350\" src=\"https:\/\/www.youtube.com\/embed\/5BDwuxhCFnY?si=5xTQ8TrpwzvJcx-U\" title=\"YouTube video player\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n\n\n\n<h3 class=\"wp-block-heading\">9. What is&nbsp;Java&nbsp;Runtime Environment (JRE)?<\/h3>\n\n\n\n<p>The <a href=\"https:\/\/www.wscubetech.com\/resources\/java\/jre\" target=\"_blank\" rel=\"noreferrer noopener\">Java Runtime Environment (JRE)<\/a> is the environment&nbsp;required&nbsp;to&nbsp;run&nbsp;Java applications. It sits between the operating system and the Java program.&nbsp;<\/p>\n\n\n\n<p><strong>The JRE&nbsp;contains:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The Java Virtual Machine (JVM)&nbsp;<\/li>\n\n\n\n<li>The core Java class libraries (java.lang,&nbsp;java.util,&nbsp;java.io, and others)&nbsp;<\/li>\n\n\n\n<li>Supporting files and configuration needed at runtime&nbsp;<\/li>\n<\/ul>\n\n\n\n<p><strong>Important point&nbsp;for modern interviews:&nbsp;<\/strong>Oracle stopped distributing a separate, standalone JRE from&nbsp;Java 11&nbsp;onwards. Instead, applications ship with the JDK, or a custom, trimmed-down runtime image is created using the&nbsp;jlink&nbsp;tool. The&nbsp;Java Plug-in&nbsp;(the browser plugin) and&nbsp;Java Web Start&nbsp;were also removed in Java 11.&nbsp;<\/p>\n\n\n\n<p><strong>Relationship to remember:<\/strong>&nbsp;JRE = JVM + class libraries, and&nbsp;JDK = JRE + development tools.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">10. What is&nbsp;Java&nbsp;SE (Standard Edition)?<\/h3>\n\n\n\n<p>Java&nbsp;Standard Edition, abbreviated as&nbsp;Java&nbsp;SE, is a computing platform on which programmers and developers build and deploy&nbsp;Java-based projects. This platform comes with plenty of&nbsp;Java&nbsp;libraries and APIs, including&nbsp;Java.util,&nbsp;Java.net,&nbsp;Java.math,&nbsp;Java.io, and many more.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">11. What are operators in&nbsp;Java?<\/h3>\n\n\n\n<p>This is also one of the basic questions for&nbsp;Java&nbsp;interviews that tests your understanding of fundamental programming concepts.&nbsp;<\/p>\n\n\n\n<p><a href=\"https:\/\/www.wscubetech.com\/resources\/java\/operators\" target=\"_blank\" rel=\"noreferrer noopener\">Java&nbsp;operators<\/a> are symbols used to perform different operations on variables and values. Each operator has a specific purpose or functionality.&nbsp;<\/p>\n\n\n\n<p>For example, the + operator is used for addition, &#8211; for subtraction, * for multiplication, and \/&nbsp;for&nbsp;division.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">12. What are the&nbsp;different types&nbsp;of&nbsp;Java&nbsp;operators?<\/h3>\n\n\n\n<p><strong>You can classify the operators in&nbsp;Java&nbsp;into five categories, as mentioned below:&nbsp;<\/strong><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">a)<em>&nbsp;<\/em>Java Arithmetic operators<\/h4>\n\n\n\n<p><a href=\"https:\/\/www.wscubetech.com\/resources\/java\/arithmetic-operators\" target=\"_blank\" rel=\"noreferrer noopener\">Arithmetic operators<\/a> are used to perform mathematical calculations on numeric values.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>+&nbsp;<\/td><td>Addition&nbsp;<\/td><\/tr><tr><td>&#8211;&nbsp;<\/td><td>Subtraction&nbsp;<\/td><\/tr><tr><td>*&nbsp;<\/td><td>Multiplication&nbsp;<\/td><\/tr><tr><td>\/&nbsp;<\/td><td>Division&nbsp;<\/td><\/tr><tr><td>%&nbsp;<\/td><td>Modulo&nbsp;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">b)<em>&nbsp;<\/em>Java Unary operators<\/h4>\n\n\n\n<p><a href=\"https:\/\/www.wscubetech.com\/resources\/java\/unary-operators\" target=\"_blank\" rel=\"noreferrer noopener\">Unary operators<\/a> are used to perform operations on a single operand, such as increasing or decreasing its value.&nbsp;&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>Unary minus (-)&nbsp;<\/td><td>To make a value negative&nbsp;<\/td><\/tr><tr><td>Unary plus (+)&nbsp;<\/td><td>Generally&nbsp;not used as values are positive by default&nbsp;<\/td><\/tr><tr><td>Increment (++)&nbsp;<\/td><td>To increase the value by 1&nbsp;<\/td><\/tr><tr><td>Decrement (&#8211;)&nbsp;<\/td><td>To decrease the value by 1&nbsp;<\/td><\/tr><tr><td>Inverting (!)&nbsp;<\/td><td>To invert the value&nbsp;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">c)&nbsp;Java Assignment operators<\/h4>\n\n\n\n<p><a href=\"https:\/\/www.wscubetech.com\/resources\/java\/assignment-operators\" target=\"_blank\" rel=\"noreferrer noopener\">Assignment operators<\/a> are used to assign values to variables and perform operations while updating the&nbsp;variable&#8217;s&nbsp;value.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>Operator<\/strong>&nbsp;<\/td><td><strong>Presentation<\/strong>&nbsp;<\/td><td><strong>Meaning<\/strong>&nbsp;<\/td><\/tr><tr><td>=&nbsp;<\/td><td>X = Y&nbsp;<\/td><td>X = Y&nbsp;<\/td><\/tr><tr><td>+=&nbsp;<\/td><td>X += Y&nbsp;<\/td><td>X = X + Y&nbsp;<\/td><\/tr><tr><td>-=&nbsp;<\/td><td>X -= Y&nbsp;<\/td><td>X = X &#8211; Y&nbsp;<\/td><\/tr><tr><td>*=&nbsp;<\/td><td>X *= Y&nbsp;<\/td><td>X = X * Y&nbsp;<\/td><\/tr><tr><td>%=&nbsp;<\/td><td>X %=&nbsp;<\/td><td>X = X % Y&nbsp;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">d)&nbsp;Java Relational or comparison operators<\/h4>\n\n\n\n<p><a href=\"https:\/\/www.wscubetech.com\/resources\/java\/relational-operators\" target=\"_blank\" rel=\"noreferrer noopener\">Relational<\/a> or comparison operators are used to compare two values in&nbsp;Java.&nbsp;They check conditions such as equality, inequality, greater than, less than, greater than or equal to, and less than or equal to.&nbsp;&nbsp;<\/p>\n\n\n\n<p>These operators return a&nbsp;boolean&nbsp;value (true or false) based on the comparison result.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>Operator<\/strong>&nbsp;<\/td><td><strong>Meaning<\/strong>&nbsp;<\/td><\/tr><tr><td>==&nbsp;<\/td><td>Is equal to&nbsp;<\/td><\/tr><tr><td>!=&nbsp;<\/td><td>Is not equal to&nbsp;<\/td><\/tr><tr><td>&gt;&nbsp;<\/td><td>Is greater than&nbsp;<\/td><\/tr><tr><td>&lt;&nbsp;<\/td><td>Is lesser than&nbsp;<\/td><\/tr><tr><td>&gt;=&nbsp;<\/td><td>Is greater than or equal to&nbsp;<\/td><\/tr><tr><td>&lt;=&nbsp;<\/td><td>Is less than or equal to&nbsp;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">e)&nbsp;Java&nbsp;Logical&nbsp;operators<\/h4>\n\n\n\n<p><a href=\"https:\/\/www.wscubetech.com\/resources\/java\/logical-operators\" target=\"_blank\" rel=\"noreferrer noopener\">Logical operators<\/a> are used to combine or&nbsp;modify&nbsp;conditions in&nbsp;Java. They help make decisions based on multiple conditions and return a&nbsp;boolean&nbsp;value (true or false).&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>Operator<\/strong>&nbsp;<\/td><td><strong>Meaning<\/strong>&nbsp;<\/td><\/tr><tr><td>&amp;&amp;&nbsp;<\/td><td>Logical AND&nbsp;<\/td><\/tr><tr><td>||&nbsp;<\/td><td>Logical OR&nbsp;<\/td><\/tr><tr><td>!&nbsp;<\/td><td>Logical NOT&nbsp;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">f)&nbsp;Java Bitwise operators<\/h4>\n\n\n\n<p><a href=\"https:\/\/www.wscubetech.com\/resources\/java\/bitwise-operators\" target=\"_blank\" rel=\"noreferrer noopener\">Bitwise operators<\/a> work on individual bits of integer values.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>Operator<\/strong>&nbsp;<\/td><td><strong>Meaning<\/strong>&nbsp;<\/td><\/tr><tr><td>&amp;&nbsp;<\/td><td>Bitwise AND&nbsp;<\/td><\/tr><tr><td>|&nbsp;<\/td><td>Bitwise OR&nbsp;<\/td><\/tr><tr><td>^&nbsp;<\/td><td>Bitwise XOR&nbsp;<\/td><\/tr><tr><td>~&nbsp;<\/td><td>Bitwise complement (inverts every bit)&nbsp;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">g) Java Shift operators<\/h4>\n\n\n\n<p>Shift operators move the bits of a number left or right.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>Operator<\/strong>&nbsp;<\/td><td><strong>Meaning<\/strong>&nbsp;<\/td><\/tr><tr><td>&lt;&lt;&nbsp;<\/td><td>Left shift&nbsp;<\/td><\/tr><tr><td>&gt;&gt;&nbsp;<\/td><td>Signed right shift, preserves the sign bit&nbsp;<\/td><\/tr><tr><td>&gt;&gt;&gt;&nbsp;<\/td><td>Unsigned right shift, always fills with 0&nbsp;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">h)&nbsp;Java Ternary operator<\/h4>\n\n\n\n<p>The <a href=\"https:\/\/www.wscubetech.com\/resources\/java\/ternary-operator\" target=\"_blank\" rel=\"noreferrer noopener\">ternary&nbsp;operator<\/a>&nbsp;?&nbsp;:&nbsp;is a shorthand for an if-else statement.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>java \n\nint max = (a &gt; b) ? a : b;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">i)&nbsp;instanceof&nbsp;operator&nbsp;<\/h4>\n\n\n\n<p>The&nbsp;instanceof&nbsp;operator checks whether an object is an instance of a particular class or interface and returns a&nbsp;boolean.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>java \n\nif (obj instanceof String) { \n    \/\/ obj is a String \n}<\/code><\/pre>\n\n\n\n<p>Since Java 16, pattern matching for&nbsp;instanceof&nbsp;allows the check and the cast in a single step:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>java \n\nif (obj instanceof String s) { \n    System.out.println(s.length()); \n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">13. What is JIT compiler in&nbsp;Java?<\/h3>\n\n\n\n<p>The JIT (Just-In-Time) compiler is a&nbsp;component&nbsp;of the&nbsp;JVM. It converts&nbsp;frequently&nbsp;executed bytecode into native machine code at runtime, so that the JVM can run the compiled code directly instead of interpreting the same bytecode repeatedly.&nbsp;<\/p>\n\n\n\n<p><strong>How it fits into execution:<\/strong><\/p>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li>javac&nbsp;compiles the source code into bytecode (.class&nbsp;files).&nbsp;<\/li>\n\n\n\n<li>The JVM loads the bytecode and begins executing it through the interpreter.&nbsp;<\/li>\n\n\n\n<li>The JIT compiler&nbsp;identifies&nbsp;&#8220;hot&#8221; methods, the ones being called repeatedly, and compiles them to native machine code.&nbsp;<\/li>\n\n\n\n<li>Subsequent&nbsp;calls run the compiled native code, which significantly improves performance.&nbsp;<\/li>\n<\/ol>\n\n\n\n<p>This is why Java applications typically get faster after they have been running for a while, a&nbsp;behaviour&nbsp;often called &#8220;warming up&#8221;.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">14. What is a&nbsp;Java&nbsp;class?<\/h3>\n\n\n\n<p>A&nbsp;<a href=\"https:\/\/www.wscubetech.com\/resources\/java\/classes-objects\" target=\"_blank\" rel=\"noreferrer noopener\">class in&nbsp;Java<\/a>&nbsp;is a blueprint or template used to create objects. It defines the properties (data members) and behaviors (methods) that objects of that class will have.&nbsp;<\/p>\n\n\n\n<p>In simple terms, a class&nbsp;represents&nbsp;a category, while objects are the individual items that belong to that category. All objects created from the same class share similar characteristics and behaviors.&nbsp;<\/p>\n\n\n\n<p>For example, in an online shopping store, &#8220;Mobile&#8221; can be a class, while different smartphones are objects. Although&nbsp;the smartphones&nbsp;may vary in brand and model, they all have common features such as a camera, RAM, calling functionality, and messaging capabilities.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">15. What is a package in&nbsp;Java?<\/h3>\n\n\n\n<p>A&nbsp;<a href=\"https:\/\/www.wscubetech.com\/resources\/java\/packages\" target=\"_blank\" rel=\"noreferrer noopener\">package in&nbsp;Java<\/a>&nbsp;is a collection of related classes, interfaces, and sub-packages that are grouped together based on their functionality. It helps organize code in a structured manner and makes large applications easier to manage.&nbsp;<\/p>\n\n\n\n<p>The main purpose of using packages is to improve code organization, prevent naming conflicts, and provide access control for classes and interfaces.&nbsp;<\/p>\n\n\n\n<p><strong>There are two types of packages in&nbsp;Java:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Built-in packages&nbsp;<\/li>\n\n\n\n<li>User-defined packages&nbsp;<\/li>\n<\/ul>\n\n\n\n<p>Built-in packages, such as&nbsp;Java.lang,&nbsp;Java.util,&nbsp;Java.io, and&nbsp;Java.net, are provided by the&nbsp;Java&nbsp;API and&nbsp;contain&nbsp;commonly used classes and interfaces. Developers can also create their own packages, known as user-defined packages, to organize application-specific code.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">16. What are keywords in&nbsp;Java?<\/h3>\n\n\n\n<p><a href=\"https:\/\/www.wscubetech.com\/resources\/java\/keywords\" target=\"_blank\" rel=\"noreferrer noopener\">Keywords in&nbsp;Java&nbsp;programming<\/a>&nbsp;are&nbsp;actually some&nbsp;predefined words in syntax that a programmer&nbsp;can\u2019t&nbsp;use in the form of classes, methods, identifiers, or variables. These are also known as reserved words in&nbsp;Java.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">17. How many keywords are there in&nbsp;Java?<\/h3>\n\n\n\n<p>Java&nbsp;has 52 reserved <a href=\"https:\/\/www.wscubetech.com\/blog\/seo-keywords\/\" target=\"_blank\" rel=\"noreferrer noopener\">keywords<\/a>&nbsp;that have predefined meanings and cannot be used as identifiers such as variable names, class names, or method names.&nbsp;<\/p>\n\n\n\n<p><strong>Keywords in&nbsp;Java:<\/strong><\/p>\n\n\n\n<p>abstract, assert,&nbsp;boolean, break, byte, case, catch, char, class, const&nbsp;(unused), continue, default, do, double, else,&nbsp;enum, extends, final, finally, float, for,&nbsp;goto&nbsp;(unused), if, implements, import,&nbsp;instanceof, int, interface, long, native, new, package, private, protected, public, return, short, static,&nbsp;strictfp, super, switch, synchronized, this, throw, throws, transient, try, void, volatile, and while.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">18. What are the key features of&nbsp;Java?<\/h3>\n\n\n\n<p>It is one of the basic&nbsp;Java&nbsp;interview questions for freshers, and sometimes for experienced professionals as well.<\/p>\n\n\n\n<p><strong>Here are the top 10&nbsp;features of&nbsp;Java&nbsp;that you must know:<\/strong><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">a) Simple, Clean, Easy to Learn<\/h4>\n\n\n\n<p>One of the best things about&nbsp;Java&nbsp;is that it is easy to learn and understand, even for beginners. Its syntax is simple as it is based on basic languages like C++. The code written in&nbsp;Java&nbsp;is also clean and easy to run.&nbsp;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">b) Object-oriented Programming Language<\/h4>\n\n\n\n<p>Java&nbsp;is an object-oriented programming language because it is based on classes and objects. It supports key <a href=\"https:\/\/www.wscubetech.com\/resources\/java\/oops\" target=\"_blank\" rel=\"noreferrer noopener\">OOP concepts<\/a> such as encapsulation, inheritance, polymorphism, and abstraction.&nbsp;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">c)&nbsp;Java&nbsp;is Both Compiled and Interpreted<\/h4>\n\n\n\n<p>Java&nbsp;is both a compiled and interpreted language. The source code is first compiled into bytecode, which is then interpreted and executed by the&nbsp;Java&nbsp;Virtual Machine (JVM).&nbsp;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">d)&nbsp;Java&nbsp;is Platform Independent<\/h4>\n\n\n\n<p>This is one of the top features of&nbsp;Java&nbsp;programming. The meaning of platform-independent here is that you can write a&nbsp;Java&nbsp;program on one machine and execute it on other machines or platforms. It has become possible because of the BYTE code.&nbsp;&nbsp;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">e) Portability<\/h4>\n\n\n\n<p>Java&nbsp;is portable because of its platform independence and architecture-neutral design.&nbsp;Java&nbsp;programs are compiled into bytecode, which can run on any system that has a&nbsp;Java&nbsp;Virtual Machine (JVM). This allows the same code to be executed on different hardware and operating systems without modification.&nbsp;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">f) Robust Programming Language<\/h4>\n\n\n\n<p>The abilities of&nbsp;Java, like garbage collection and exception handling, make it a solid programming language.&nbsp;&nbsp;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">g) Highly Secure<\/h4>\n\n\n\n<p>Java&nbsp;is known for its strong security features, which help developers build reliable and secure applications. Features such as bytecode verification, automatic memory management, and runtime security checks reduce common security risks, making&nbsp;Java&nbsp;a popular choice for enterprise-grade application development.&nbsp;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">h)&nbsp;Java&nbsp;Multithreading Features<\/h4>\n\n\n\n<p>The multithreading feature helps in writing code that can perform multiple tasks simultaneously. Moreover,&nbsp;the thread&nbsp;tasks consume less processing power and memory.&nbsp;&nbsp;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">i) Easy Interpretation<\/h4>\n\n\n\n<p>Regardless of the computer architecture,&nbsp;Java&nbsp;programs can be run and interpreted on any type of machine. You can call it&nbsp;architecture-neutral language.&nbsp;&nbsp;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">j) High Performance<\/h4>\n\n\n\n<p>Java&nbsp;offers high performance through features such as the Just-In-Time (JIT) compiler, which converts bytecode into native machine code at runtime. This helps&nbsp;Java&nbsp;applications run faster and more efficiently.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">19. What is an object in&nbsp;Java?<\/h3>\n\n\n\n<p>Since&nbsp;Java&nbsp;is an object-oriented programming language, classes and objects are among its most important concepts. Understanding objects is essential for answering many&nbsp;Java&nbsp;interview questions.&nbsp;<\/p>\n\n\n\n<p>An <a href=\"https:\/\/www.wscubetech.com\/resources\/java\/classes-objects\" target=\"_blank\" rel=\"noreferrer noopener\">object<\/a> is an instance of a class in&nbsp;Java. It is created from a class using the new keyword.&nbsp;Every object has its own identity, state (attributes), and behavior (methods), similar to real-world entities.&nbsp;<\/p>\n\n\n\n<p><strong>Example:&nbsp;<\/strong>If Car is a class, then a specific car such as a Honda City can be considered an object. The car&#8217;s color and model represent its state, while actions like starting, stopping, and accelerating represent its behavior.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">20. What is the difference between&nbsp;Java&nbsp;and&nbsp;JavaScript?<\/h3>\n\n\n\n<p>There are several differences between&nbsp;Java&nbsp;and&nbsp;JavaScript. Whether you are a fresher or an experienced professional, this is among the top&nbsp;Java&nbsp;interview questions for you. Below, we have provided a tabular comparison of&nbsp;<a href=\"https:\/\/www.wscubetech.com\/blog\/difference-java-vs-javascript\/\" target=\"_blank\" rel=\"noreferrer noopener\">Java&nbsp;vs&nbsp;JavaScript<\/a>&nbsp;so that it becomes easier for you to understand the main differences.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>Java<\/strong>&nbsp;<\/td><td><strong>JavaScript<\/strong>&nbsp;<\/td><\/tr><tr><td>Object-oriented programming language.&nbsp;<\/td><td>Multi-paradigm scripting language with prototype-based objects.&nbsp;<\/td><\/tr><tr><td>Statically typed. Variables must be declared with a type.&nbsp;<\/td><td>Dynamically typed. Types are resolved at runtime.&nbsp;<\/td><\/tr><tr><td>Strongly typed.&nbsp;<\/td><td>Loosely typed, with implicit&nbsp;type&nbsp;coercion.&nbsp;<\/td><\/tr><tr><td>Compiled to bytecode, then executed by the JVM.&nbsp;<\/td><td>Interpreted and JIT-compiled by a JavaScript engine such as V8.&nbsp;<\/td><\/tr><tr><td>Runs on the JVM. Needs the JDK to compile and&nbsp;a runtime&nbsp;to execute.&nbsp;<\/td><td>Runs in a browser engine or in a&nbsp;server&nbsp;runtime such as Node.js.&nbsp;<\/td><\/tr><tr><td>Supports multithreading through the&nbsp;Thread&nbsp;class and the Executor framework.&nbsp;<\/td><td>Single-threaded event loop model. Concurrency is handled through async callbacks, Promises, and Web Workers.&nbsp;<\/td><\/tr><tr><td>Objects are created from classes.&nbsp;<\/td><td>Objects are prototype-based, though&nbsp;class&nbsp;syntax has been available since ES6.&nbsp;<\/td><\/tr><tr><td>Higher memory consumption.&nbsp;<\/td><td>Lower memory consumption.&nbsp;<\/td><\/tr><tr><td>Files use the&nbsp;.java&nbsp;extension.&nbsp;<\/td><td>Files use the&nbsp;.js&nbsp;extension.&nbsp;<\/td><\/tr><tr><td>Mainly used for backend, enterprise, and Android development.&nbsp;<\/td><td>Used for both&nbsp;frontend&nbsp;and backend development.&nbsp;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n    <!-- LOTTIE SCRIPT -->\n    <script src=\"https:\/\/unpkg.com\/@lottiefiles\/lottie-player@latest\/dist\/lottie-player.js\"><\/script>\n\n    <section class=\"wscube-courses mb-2\">\n        <div class=\"container\">\n            <h3 class=\"mb-4\">Recommended Professional <\/br> Certificates<\/h3>\n\n            <div class=\"owl-carousel courseOwl\">\n\n                \n                    \n                    <div class=\"course-card card-r rounded-4\">\n\n                        <!-- \u2705 SINGLE MEDIA DIV (FIXED) -->\n                        <div class=\"course-media\"\n                             data-lottie=\"https:\/\/www.wscubetech.com\/uploads\/images\/courses\/json-images\/web-devlopment.json\"\n                             data-fallback=\"https:\/\/www.wscubetech.com\/blog\/wp-content\/themes\/newwscube\/assets\/imges\/Image.png\">\n                        <\/div>\n\n                        <div class=\"card-body\">\n                            <h5>Full Stack Development Course with AI Engineering<\/h5>\n\n                            <p class=\"rating\">\n                                4.9 \u2605\u2605\u2605\u2605\u2605\n                                (24922)\n                            <\/p>\n\n                            <ul class=\"course-meta\">\n                                <li>\ud83d\udc64 27000 Learners<\/li>\n                                <li>\u23f1 20 Weeks<\/li>\n                            <\/ul>\n\n                            <div class=\"mt-3\">\n                                                                    <a href=\"https:\/\/www.wscubetech.com\/full-stack-developer-course?utm_source=WsBlog&#038;utm_medium=blog_course_slider&#038;utm_campaign=SEO\"\n                                       target=\"_blank\"\n                                       class=\"btn view-btn btn-sm\">\n                                        View Brochure\n                                    <\/a>\n                                \n                                <a target=\"_blank\"\n                                   href=\"https:\/\/www.wscubetech.com\/full-stack-developer-course?utm_source=WsBlog&#038;utm_medium=blog_course_slider&#038;utm_campaign=SEO\"\n                                   class=\"btn btn-outline-secondary btn-sm\">\n                                    Learn More\n                                <\/a>\n                            <\/div>\n                        <\/div>\n                    <\/div>\n\n                \n                    \n                    <div class=\"course-card card-r rounded-4\">\n\n                        <!-- \u2705 SINGLE MEDIA DIV (FIXED) -->\n                        <div class=\"course-media\"\n                             data-lottie=\"https:\/\/www.wscubetech.com\/uploads\/images\/courses\/json-images\/wordpress-v2.json\"\n                             data-fallback=\"https:\/\/www.wscubetech.com\/blog\/wp-content\/themes\/newwscube\/assets\/imges\/Image.png\">\n                        <\/div>\n\n                        <div class=\"card-body\">\n                            <h5>WordPress Bootcamp<\/h5>\n\n                            <p class=\"rating\">\n                                4.9 \u2605\u2605\u2605\u2605\u2605\n                                (9406)\n                            <\/p>\n\n                            <ul class=\"course-meta\">\n                                <li>\ud83d\udc64 16000 Learners<\/li>\n                                <li>\u23f1 2 Months<\/li>\n                            <\/ul>\n\n                            <div class=\"mt-3\">\n                                                                    <a href=\"https:\/\/www.wscubetech.com\/wordpress-course?utm_source=WsBlog&#038;utm_medium=blog_course_slider&#038;utm_campaign=SEO\"\n                                       target=\"_blank\"\n                                       class=\"btn view-btn btn-sm\">\n                                        View Brochure\n                                    <\/a>\n                                \n                                <a target=\"_blank\"\n                                   href=\"https:\/\/www.wscubetech.com\/wordpress-course?utm_source=WsBlog&#038;utm_medium=blog_course_slider&#038;utm_campaign=SEO\"\n                                   class=\"btn btn-outline-secondary btn-sm\">\n                                    Learn More\n                                <\/a>\n                            <\/div>\n                        <\/div>\n                    <\/div>\n\n                \n            <\/div>\n        <\/div>\n    <\/section>\n\n    \n\n\n\n<h3 class=\"wp-block-heading\">21. Which&nbsp;Java&nbsp;class is considered a superclass of all other classes?<\/h3>\n\n\n\n<p>The Object class is the superclass of all classes in&nbsp;Java. Every&nbsp;Java&nbsp;class directly or indirectly inherits from the Object class.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">22. Is it possible for a class to extend itself?<\/h3>\n\n\n\n<p>No, a class cannot extend itself in&nbsp;Java. Allowing a class to inherit from itself would create a cyclic inheritance relationship, which is not&nbsp;permitted&nbsp;by the&nbsp;<a href=\"https:\/\/www.wscubetech.com\/resources\/java\/compiler\" target=\"_blank\" rel=\"noreferrer noopener\">Java&nbsp;compiler<\/a>.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">23. What is the difference between&nbsp;Java&nbsp;and C++ programming?<\/h3>\n\n\n\n<p>Another important topic among the questions asked in&nbsp;Java&nbsp;interviews is the difference between&nbsp;Java&nbsp;and C++ programming languages. Understanding these differences can help you answer technical interview questions more confidently. To make it easier to understand, we have provided the following comparison between&nbsp;Java&nbsp;and C++.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>Java<\/strong>&nbsp;<\/td><td><strong>C++<\/strong>&nbsp;<\/td><\/tr><tr><td>Platform independent&nbsp;<\/td><td>Platform dependent&nbsp;<\/td><\/tr><tr><td>Uses both a compiler and JVM interpreter&nbsp;<\/td><td>Uses a compiler&nbsp;<\/td><\/tr><tr><td>Automatic memory management through Garbage Collection&nbsp;<\/td><td>Manual memory management&nbsp;<\/td><\/tr><tr><td>Supports object-oriented programming&nbsp;<\/td><td>Supports both procedural and object-oriented programming&nbsp;<\/td><\/tr><tr><td>Does not support pointers directly&nbsp;<\/td><td>Supports pointers&nbsp;<\/td><\/tr><tr><td>Does not support the&nbsp;goto&nbsp;statement&nbsp;<\/td><td>Supports the&nbsp;goto&nbsp;statement&nbsp;<\/td><\/tr><tr><td>Developed by James Gosling&nbsp;<\/td><td>Developed by Bjarne Stroustrup&nbsp;<\/td><\/tr><tr><td>Used for web, mobile, desktop, and enterprise applications&nbsp;<\/td><td>Commonly used for system software, game development, and high-performance applications&nbsp;<\/td><\/tr><tr><td>Rich standard library and APIs&nbsp;<\/td><td>Large collection&nbsp;of standard libraries&nbsp;<\/td><\/tr><tr><td>Follows the &#8220;Write Once, Run Anywhere&#8221; principle&nbsp;<\/td><td>Requires separate compilation for different platforms&nbsp;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">24.&nbsp;Explain the difference between JDK, JRE, and JVM<\/h3>\n\n\n\n<p>One of the most common&nbsp;Java&nbsp;basic interview questions for freshers is understanding the&nbsp;<a href=\"https:\/\/www.wscubetech.com\/resources\/java\/difference-between-jdk-jre-jvm\" target=\"_blank\" rel=\"noreferrer noopener\">difference between JDK, JRE, and JVM<\/a>.&nbsp;The table below provides a simple comparison of their features and functions:&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>JDK<\/strong>&nbsp;<\/td><td><strong>JRE<\/strong>&nbsp;<\/td><td><strong>JVM<\/strong>&nbsp;<\/td><\/tr><tr><td>Java&nbsp;Development Kit&nbsp;<\/td><td>Java&nbsp;Runtime Environment&nbsp;<\/td><td>Java&nbsp;Virtual Machine&nbsp;<\/td><\/tr><tr><td>A software development kit used to develop, compile, and debug&nbsp;Java&nbsp;applications.&nbsp;<\/td><td>A runtime environment used to run&nbsp;Java&nbsp;applications.&nbsp;<\/td><td>A virtual machine that executes&nbsp;Java&nbsp;bytecode and enables platform independence.&nbsp;<\/td><\/tr><tr><td>Contains&nbsp;development tools such as the compiler, debugger, and documentation tools.&nbsp;<\/td><td>Contains&nbsp;JVM and class libraries&nbsp;required&nbsp;to run&nbsp;Java&nbsp;programs.&nbsp;<\/td><td>Responsible for loading, verifying, and executing&nbsp;Java&nbsp;bytecode.&nbsp;<\/td><\/tr><tr><td>Platform-dependent.&nbsp;<\/td><td>Platform-dependent.&nbsp;<\/td><td>Platform-independent.&nbsp;<\/td><\/tr><tr><td>Primarily used during application development.&nbsp;<\/td><td>Primarily used for running&nbsp;Java&nbsp;applications.&nbsp;<\/td><td>Provides the execution engine for&nbsp;Java&nbsp;programs.&nbsp;<\/td><\/tr><tr><td>JDK = JRE + Development Tools&nbsp;<\/td><td>JRE = JVM + Class Libraries&nbsp;<\/td><td>Part of the JRE that executes&nbsp;Java&nbsp;bytecode.&nbsp;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">25. Is it possible to assign a superclass to a subclass in&nbsp;Java?<\/h3>\n\n\n\n<p>No, a superclass object cannot be directly assigned to a subclass reference in&nbsp;Java&nbsp;because a superclass does not&nbsp;contain&nbsp;all the properties and methods of its subclass.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">26. How to print text in&nbsp;Java?<\/h3>\n\n\n\n<p>The&nbsp;println() and&nbsp;print() methods are used to display text in&nbsp;Java.&nbsp;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">a)&nbsp;println() Example<\/h4>\n\n\n\n<p>The&nbsp;println() method prints the text and moves the cursor to the next line.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Main { \n    public static void main(String&#091;] args) { \n        System.out.println(\"Hi There!\"); \n        System.out.println(\"Welcome to Java!\"); \n        System.out.println(\"Let's learn Java Interview Questions!\"); \n    } \n}<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Output:<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>Hi There! \nWelcome to Java! \nLet's learn Java Interview Questions!<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">b)&nbsp;print() Example<\/h4>\n\n\n\n<p>The&nbsp;print() method prints the text without moving to a new line.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Main { \n    public static void main(String&#091;] args) { \n        System.out.print(\"Hi There! \"); \n        System.out.print(\"I am learning Java.\"); \n    } \n}<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Output:<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>Hi There! I am learning Java.<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">27. What is the&nbsp;main() method in&nbsp;Java?<\/h3>\n\n\n\n<p>The&nbsp;main() method is the entry point of a&nbsp;Java&nbsp;program. The&nbsp;Java&nbsp;Virtual Machine (JVM) starts program execution from the&nbsp;main() method. Without it, a standalone&nbsp;Java&nbsp;application cannot&nbsp;run.&nbsp;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Syntax:<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>public static void main(String&#091;] args)<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>public&nbsp;\u2013 <\/strong>Accessible from anywhere.&nbsp;<\/li>\n\n\n\n<li><strong>static&nbsp;\u2013 <\/strong>Can be called without creating an object.&nbsp;<\/li>\n\n\n\n<li><strong>void&nbsp;\u2013 <\/strong>Does not return any value.&nbsp;<\/li>\n\n\n\n<li><strong>String[]&nbsp;args&nbsp;\u2013 <\/strong>Stores command-line arguments passed to the program.&nbsp;<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">28. What are&nbsp;variables&nbsp;in&nbsp;Java?<\/h3>\n\n\n\n<p><a href=\"https:\/\/www.wscubetech.com\/resources\/java\/variables\" target=\"_blank\" rel=\"noreferrer noopener\">Variables in&nbsp;Java<\/a>&nbsp;are named memory locations used to store data values. They allow a program to store,&nbsp;modify, and retrieve information during execution. Each variable must be declared with a specific data type, such as int, float, char, or String.&nbsp;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Example:<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>int age = 25; \nString name = \"John\";<\/code><\/pre>\n\n\n\n<p>In this example, age and name are variables that store an integer value and a string value, respectively.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">29. What are the different&nbsp;data types&nbsp;in&nbsp;Java?<\/h3>\n\n\n\n<p><strong><a href=\"https:\/\/www.wscubetech.com\/resources\/java\/data-types\" target=\"_blank\" rel=\"noreferrer noopener\">Data types in&nbsp;Java<\/a>&nbsp;specify&nbsp;the type of data that a variable can store.&nbsp;Java&nbsp;data types are broadly divided into two categories:<\/strong><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">1. Primitive Data Types<\/h4>\n\n\n\n<p>These are predefined data types provided by&nbsp;Java.&nbsp;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>byte&nbsp;<\/li>\n\n\n\n<li>short&nbsp;<\/li>\n\n\n\n<li>int&nbsp;<\/li>\n\n\n\n<li>long&nbsp;<\/li>\n\n\n\n<li>float&nbsp;<\/li>\n\n\n\n<li>double&nbsp;<\/li>\n\n\n\n<li>char&nbsp;<\/li>\n\n\n\n<li>boolean&nbsp;<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">2. Non-Primitive Data Types<\/h4>\n\n\n\n<p>These data types are created by the programmer and can store multiple values or complex data.&nbsp;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>String&nbsp;<\/li>\n\n\n\n<li>Arrays&nbsp;<\/li>\n\n\n\n<li>Classes&nbsp;<\/li>\n\n\n\n<li>Interfaces&nbsp;<\/li>\n\n\n\n<li>Objects&nbsp;<\/li>\n<\/ul>\n\n\n\n<p>Java&nbsp;uses these data types to define the kind of value a variable can&nbsp;hold&nbsp;and the operations that can be performed on it.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">30.&nbsp;What is&nbsp;type&nbsp;casting in&nbsp;Java?<\/h3>\n\n\n\n<p><a href=\"https:\/\/www.wscubetech.com\/resources\/java\/type-conversion-casting\" target=\"_blank\" rel=\"noreferrer noopener\">Type casting in&nbsp;Java<\/a>&nbsp;is&nbsp;the process of converting a value from one data type to another. It is commonly used when working with different numeric data types.<\/p>\n\n\n\n<p><strong>There are two types of type casting in&nbsp;Java:<\/strong><\/p>\n\n\n\n<p><strong>1. Widening Casting (Implicit)&nbsp;\u2013 <\/strong>Converts a smaller data type to a larger data type automatically.&nbsp;<\/p>\n\n\n\n<p><strong>Example:<\/strong>&nbsp;int to long, float to double&nbsp;<\/p>\n\n\n\n<p><strong>2. Narrowing Casting (Explicit)&nbsp;\u2013 <\/strong>Converts a larger data type to a smaller data type manually using a cast operator.&nbsp;<\/p>\n\n\n\n<p><strong>Example:&nbsp;<\/strong>double to int, long to short&nbsp;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Example:<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>double num = 10.5; \nint value = (int) num;<\/code><\/pre>\n\n\n\n<p>In this example, the double value 10.5 is converted to the int value 10.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">31. What is the difference between primitive and non-primitive data types in&nbsp;Java?<\/h3>\n\n\n\n<p><strong><a href=\"https:\/\/www.wscubetech.com\/resources\/java\/data-types\" target=\"_blank\" rel=\"noreferrer noopener\">Primitive and non-primitive data types<\/a> differ in terms of how they store data and their functionality. The table below highlights the main differences:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>Primitive Data Types<\/strong>&nbsp;<\/td><td><strong>Non-Primitive Data Types<\/strong>&nbsp;<\/td><\/tr><tr><td>Store simple values directly.&nbsp;<\/td><td>Store references to objects.&nbsp;<\/td><\/tr><tr><td>Predefined by&nbsp;Java.&nbsp;<\/td><td>Created by programmers or provided by&nbsp;Java&nbsp;libraries.&nbsp;<\/td><\/tr><tr><td>Have a fixed size.&nbsp;<\/td><td>Size can vary depending on the data.&nbsp;<\/td><\/tr><tr><td>Cannot call methods.&nbsp;<\/td><td>Can have methods and properties.&nbsp;<\/td><\/tr><tr><td>Faster and use less memory.&nbsp;<\/td><td>Generally&nbsp;require&nbsp;more memory.&nbsp;<\/td><\/tr><tr><td><strong>Examples:&nbsp;<\/strong>int, char,&nbsp;boolean, double.&nbsp;<\/td><td><strong>Examples:<\/strong>&nbsp;String, Arrays, Classes, Interfaces, Objects.&nbsp;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">32. What is the difference between a class and an object in&nbsp;Java?<\/h3>\n\n\n\n<p>A class is a blueprint or template used to define the properties and behaviors of objects,&nbsp;whereas&nbsp;an object is an instance of a class that&nbsp;contains&nbsp;actual data and can perform actions defined by the class. The table below clearly shows the differences between a class and an object:&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>Class<\/strong>&nbsp;<\/td><td><strong>Object<\/strong>&nbsp;<\/td><\/tr><tr><td>A blueprint or template for creating objects.&nbsp;<\/td><td>An instance of a class.&nbsp;<\/td><\/tr><tr><td>Defines attributes and methods.&nbsp;<\/td><td>Contains&nbsp;actual values for attributes.&nbsp;<\/td><\/tr><tr><td>Does not occupy memory until objects are created.&nbsp;<\/td><td>Occupies memory when created.&nbsp;<\/td><\/tr><tr><td>Used to create multiple objects.&nbsp;<\/td><td>Represents&nbsp;a real-world entity.&nbsp;<\/td><\/tr><tr><td><strong>Example:<\/strong>&nbsp;Car&nbsp;<\/td><td><strong>Example:<\/strong>&nbsp;Honda City, Hyundai i20&nbsp;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Example:&nbsp;<\/strong>If Car is a class, then a specific car such as Honda City is an object created from that class.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">33. What are comments in&nbsp;Java?<\/h3>\n\n\n\n<p><a href=\"https:\/\/www.wscubetech.com\/resources\/java\/comments\" target=\"_blank\" rel=\"noreferrer noopener\">Comments in&nbsp;Java<\/a>&nbsp;are non-executable lines of text used to explain code, improve readability, and make programs easier to understand. The&nbsp;Java&nbsp;compiler ignores comments during program execution.&nbsp;<\/p>\n\n\n\n<p><strong>Java&nbsp;supports three types of comments:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Single-line comment:<\/strong>&nbsp;\/\/ This is a comment&nbsp;<\/li>\n\n\n\n<li><strong>Multi-line comment:<\/strong>&nbsp;\/* This is a multi-line comment *\/&nbsp;<\/li>\n\n\n\n<li><strong>Documentation comment:<\/strong>&nbsp;\/** This is a documentation comment *\/&nbsp;<\/li>\n<\/ul>\n\n\n\n<p>Comments help developers understand the purpose and functionality of the code without affecting its execution.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">34. What is the use of the static keyword in&nbsp;Java?<\/h3>\n\n\n\n<p>This is one of the most asked&nbsp;Java&nbsp;interview questions because the <a href=\"https:\/\/www.wscubetech.com\/resources\/java\/static-keyword\" target=\"_blank\" rel=\"noreferrer noopener\">static keyword<\/a> is a fundamental concept used in class-level programming.&nbsp;<\/p>\n\n\n\n<p>The static keyword is used to declare class-level variables, methods, blocks, and nested classes. A static member belongs to the class rather than an object, which means it can be accessed without creating an instance of the class. This helps save memory and allows shared access across all objects of the class.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">35. What&nbsp;are&nbsp;command-line arguments in&nbsp;Java?<\/h3>\n\n\n\n<p>Command-line arguments are values passed to a&nbsp;Java&nbsp;program when it is executed from the command line. These arguments are stored in the&nbsp;String[]&nbsp;args&nbsp;parameter of the&nbsp;main() method and can be used to provide input to the program at runtime.&nbsp;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Example:<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Main { \n    public static void main(String&#091;] args) { \n        System.out.println(\"Hello \" + args&#091;0]); \n    } \n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">If the program is run as:<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>Java&nbsp;Main John<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Output:<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>Hello John<\/code><\/pre>\n\n\n\n<p>In this example, John is passed as a command-line argument and stored in&nbsp;args[0].&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">36. What is the use of the new keyword in&nbsp;Java?<\/h3>\n\n\n\n<p>The new keyword in&nbsp;Java&nbsp;is used to create objects&nbsp;of&nbsp;a class. When an object is created using new, memory is&nbsp;allocated&nbsp;for the object, and its constructor is called to initialize it.&nbsp;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Example:<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>Car car = new Car();<\/code><\/pre>\n\n\n\n<p>In this example, the new keyword creates an object of the Car class and assigns its reference to the car variable.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">37. Is Java a pure object-oriented language?<\/h3>\n\n\n\n<p>No, Java is&nbsp;not&nbsp;a pure object-oriented language. The main reason is that Java supports eight primitive data types (byte,&nbsp;short,&nbsp;int,&nbsp;long,&nbsp;float,&nbsp;double,&nbsp;char,&nbsp;boolean) which are not objects.&nbsp;<\/p>\n\n\n\n<p><strong>Other reasons commonly cited:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Static methods and variables belong to the class, not to any object, and can be used without creating an object.&nbsp;<\/li>\n\n\n\n<li>Wrapper classes exist precisely because primitives are not objects.&nbsp;<\/li>\n<\/ul>\n\n\n\n<p>Languages such as Smalltalk and Ruby, where everything is an object, are considered pure object-oriented.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">38. Is Java pass by value or pass by reference?<\/h3>\n\n\n\n<p>Java is&nbsp;always pass by value. There is no pass by reference in Java.&nbsp;<\/p>\n\n\n\n<p><strong>The confusion arises with objects. When an object is passed to a method, a&nbsp;copy of the reference&nbsp;is passed, not the object itself. This means:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Modifying the object&#8217;s&nbsp;contents&nbsp;inside the method&nbsp;does&nbsp;affect the original object, because both references point to the same object.&nbsp;<\/li>\n\n\n\n<li>Reassigning the parameter to a new object inside the method&nbsp;does not&nbsp;affect the original, because only the copy of the reference was changed.&nbsp;<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>static void modify(StringBuilder sb) { \n    sb.append(\" World\");              \/\/ affects the original \n    sb = new StringBuilder(\"New\");    \/\/ does NOT affect the original \n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">39. Why does Java not support pointers?<\/h3>\n\n\n\n<p><strong>Java deliberately omits explicit pointers for three reasons:&nbsp;<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Security.<\/strong>&nbsp;Pointers allow direct memory access, which would let code read or&nbsp;modify&nbsp;memory it should not have access to.&nbsp;<\/li>\n\n\n\n<li><strong>Simplicity.<\/strong>&nbsp;Pointer arithmetic is a common source of bugs and is difficult for beginners.&nbsp;<\/li>\n\n\n\n<li><strong>Garbage collection.<\/strong>&nbsp;The garbage collector needs to track all references reliably. Arbitrary pointer arithmetic would make this impossible.&nbsp;<\/li>\n<\/ul>\n\n\n\n<p>Java uses references instead, which point to objects but cannot be manipulated arithmetically.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">40. What is the difference between instance variables, local variables, and class variables?<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>Instance Variable<\/strong>&nbsp;<\/td><td><strong>Local Variable<\/strong>&nbsp;<\/td><td><strong>Class (Static) Variable<\/strong>&nbsp;<\/td><\/tr><tr><td>Declared inside a class but outside any method.&nbsp;<\/td><td>Declared inside a method, constructor, or block.&nbsp;<\/td><td>Declared inside a class with the&nbsp;static&nbsp;keyword.&nbsp;<\/td><\/tr><tr><td>Each object has its own copy.&nbsp;<\/td><td>Exists&nbsp;only while the method&nbsp;executes.&nbsp;<\/td><td>One copy shared by all objects.&nbsp;<\/td><\/tr><tr><td>Gets a default value automatically.&nbsp;<\/td><td>Has no default value, must be&nbsp;initialised&nbsp;before use.&nbsp;<\/td><td>Gets a default value automatically.&nbsp;<\/td><\/tr><tr><td>Stored in the heap with the object.&nbsp;<\/td><td>Stored on the&nbsp;thread&#8217;s&nbsp;stack.&nbsp;<\/td><td>Stored in the method area,&nbsp;allocated&nbsp;when the class loads.&nbsp;<\/td><\/tr><tr><td>Accessed using an object reference.&nbsp;<\/td><td>Accessible only within its block.&nbsp;<\/td><td>Accessed using the class name.&nbsp;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">41. What are the default values of variables in Java?<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>Type<\/strong>&nbsp;<\/td><td><strong>Default Value<\/strong>&nbsp;<\/td><\/tr><tr><td>byte,&nbsp;short,&nbsp;int,&nbsp;long&nbsp;<\/td><td>0&nbsp;<\/td><\/tr><tr><td>float,&nbsp;double&nbsp;<\/td><td>0.0&nbsp;<\/td><\/tr><tr><td>char&nbsp;<\/td><td>&#8216;\\u0000&#8217;&nbsp;(null character)&nbsp;<\/td><\/tr><tr><td>boolean&nbsp;<\/td><td>false&nbsp;<\/td><\/tr><tr><td>Any object reference&nbsp;<\/td><td>null&nbsp;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>An important qualifier:<\/strong> these defaults apply only to&nbsp;instance and static variables.&nbsp;Local variables have no default value&nbsp;and cause a compile-time error if used before being assigned.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">42. Can we override static, private, and final methods in Java?<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Static methods: no.<\/strong>&nbsp;A subclass can declare a static method with the same signature, but this is&nbsp;method hiding, not overriding. Which method runs is decided at compile time based on the reference type, not at runtime based on the object.&nbsp;<\/li>\n\n\n\n<li><strong>Private methods: no.<\/strong>&nbsp;Private methods are not inherited and are not visible to the subclass, so a method with the same name in the subclass is simply a new, unrelated method.&nbsp;<\/li>\n\n\n\n<li><strong>Final methods: no.<\/strong>&nbsp;The&nbsp;final&nbsp;keyword explicitly prevents overriding.&nbsp;<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">43. What is a marker interface in Java?<\/h3>\n\n\n\n<p>A <a href=\"https:\/\/www.wscubetech.com\/resources\/java\/interfaces\" target=\"_blank\" rel=\"noreferrer noopener\">marker interface<\/a> is an interface with&nbsp;no methods and no fields. Its only purpose is to mark or tag a class so that the JVM or a framework can treat it differently at runtime.&nbsp;<\/p>\n\n\n\n<p><strong>Common examples:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Serializable&nbsp;\u2014 <\/strong>marks a class as eligible for&nbsp;serialisation&nbsp;<\/li>\n\n\n\n<li><strong>Cloneable&nbsp;\u2014 <\/strong>marks a class as safe to clone&nbsp;<\/li>\n\n\n\n<li><strong>Remote&nbsp;\u2014 <\/strong>marks a class for RMI&nbsp;<\/li>\n<\/ul>\n\n\n\n<p>In modern Java, annotations are&nbsp;generally preferred&nbsp;over marker interfaces, because they carry metadata and do not affect the&nbsp;type&nbsp;hierarchy.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">44. What are&nbsp;varargs&nbsp;in Java?<\/h3>\n\n\n\n<p>Varargs, short for variable arguments, allow a method to accept any number of arguments of a given type. It was introduced in Java 5 and uses three dots after the type.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public int sum(int... numbers) { \n    int total = 0; \n    for (int n : numbers) total += n; \n    return total; \n} \n \nsum();              \/\/ valid \nsum(1, 2);          \/\/ valid \nsum(1, 2, 3, 4);    \/\/ valid<\/code><\/pre>\n\n\n\n<p class=\"has-white-color has-vivid-cyan-blue-background-color has-text-color has-background has-link-color wp-elements-1b38e15a93896ee90f7f72f90c08a592\"><em><strong>Rules:<\/strong>&nbsp;a method can have only one&nbsp;varargs&nbsp;parameter, and it must be the last parameter in the list. Internally,&nbsp;varargs&nbsp;are treated as an array.<\/em><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">45. What is a covariant return type?<\/h3>\n\n\n\n<p>A covariant return type allows an overriding method in a subclass to return a&nbsp;more specific type&nbsp;than the method it overrides. This was introduced in Java 5.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Animal { \n    Animal reproduce() { return new Animal(); } \n} \n \nclass Dog extends Animal { \n    @Override \n    Dog reproduce() { return new Dog(); }   \/\/ returns Dog, not Animal \n}<\/code><\/pre>\n\n\n\n<p>It removes the need for the caller to downcast the result, which makes the code cleaner and&nbsp;type-safe.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">46. What is a singleton class and how do you implement it?<\/h3>\n\n\n\n<p>A <a href=\"https:\/\/www.wscubetech.com\/resources\/java\/singleton-class\" target=\"_blank\" rel=\"noreferrer noopener\">singleton class<\/a> is one that allows only a single instance to exist for the entire application. It is used for shared resources such as&nbsp;configuration&nbsp;objects, logging, and connection pools.&nbsp;<\/p>\n\n\n\n<p><strong>Thread-safe implementation using double-checked locking:<\/strong>&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Singleton { \n    private static volatile Singleton instance; \n    private Singleton() {} \n \n    public static Singleton getInstance() { \n        if (instance == null) { \n            synchronized (Singleton.class) { \n                if (instance == null) { \n                    instance = new Singleton(); \n                } \n            } \n        } \n        return instance; \n    } \n}<\/code><\/pre>\n\n\n\n<p>The&nbsp;volatile&nbsp;keyword is essential here, otherwise a partially constructed object could be visible to another thread.&nbsp;<\/p>\n\n\n\n<p><strong>Simplest and safest implementation, using an&nbsp;enum:<\/strong>&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>java \n\nenum Singleton { \n    INSTANCE; \n    public void doSomething() { } \n}<\/code><\/pre>\n\n\n\n<p>The&nbsp;enum&nbsp;approach is recommended because it is thread-safe by default and is protected against reflection and&nbsp;serialisation&nbsp;attacks.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">47. What is the order of execution of static blocks, instance blocks, and constructors?<\/h3>\n\n\n\n<p><strong>This appears&nbsp;frequently&nbsp;as an output-based interview question. The order is:<\/strong><\/p>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li>Static variables and static blocks, in the order they appear,&nbsp;<strong>once<\/strong>&nbsp;when the class is first loaded&nbsp;<\/li>\n\n\n\n<li>Instance variables and instance blocks, in the order they appear&nbsp;<\/li>\n\n\n\n<li>Constructor&nbsp;<\/li>\n<\/ol>\n\n\n\n<p><strong>Steps 2 and 3&nbsp;repeat&nbsp;for every object created. Step 1 runs only once.<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>java \n\nclass Demo { \n    static { System.out.println(\"1. Static block\"); } \n    { System.out.println(\"2. Instance block\"); } \n    Demo() { System.out.println(\"3. Constructor\"); } \n \n    public static void main(String&#091;] args) { \n        new Demo(); \n        new Demo(); \n    } \n} \n\/\/ Output: 1, 2, 3, 2, 3<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Java String Interview Questions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. What is the String Pool in Java?<\/h3>\n\n\n\n<p>The String Pool, also called the String Constant Pool, is a special storage area inside heap memory where Java stores string literals. Its purpose is to save memory by reusing string objects instead of creating duplicates.&nbsp;<\/p>\n\n\n\n<p>When you create a string using a literal, Java first checks whether an identical string already exists in the pool. If it does, the existing reference is returned. If it does not, a new string is created in the pool.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>String s1 = \"Java\"; \nString s2 = \"Java\"; \nSystem.out.println(s1 == s2);   \/\/ true, both point to the same pooled object<\/code><\/pre>\n\n\n\n<p class=\"has-white-color has-vivid-cyan-blue-background-color has-text-color has-background has-link-color wp-elements-d2e6acece8d5a7776bb12b1ecc868c89\"><em><strong>Note:<\/strong>&nbsp;the&nbsp;String Pool was moved from&nbsp;PermGen&nbsp;to the heap in Java 7, which allowed it to&nbsp;be garbage&nbsp;collected and made much larger.<\/em><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. Why is String immutable in Java?<\/h3>\n\n\n\n<p>This is one of the most&nbsp;frequently&nbsp;asked Java interview questions for experienced developers. String is immutable, meaning once a String object is created its value cannot be changed. There are four main reasons for this design.&nbsp;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>String Pool:<\/strong>&nbsp;Pooling only works if strings cannot change. If one reference could&nbsp;modify&nbsp;a shared string, every other reference pointing to it would be affected.&nbsp;<\/li>\n\n\n\n<li><strong>Security:<\/strong>&nbsp;Strings are used for usernames, database URLs, file paths, and network connections. If they were mutable, a value could be changed after a security check but before it is used.&nbsp;<\/li>\n\n\n\n<li><strong>Thread safety:<\/strong>&nbsp;Immutable objects are inherently&nbsp;thread-safe. A String can be shared across threads with no&nbsp;synchronisation.&nbsp;<\/li>\n\n\n\n<li><strong>Hashcode&nbsp;caching:<\/strong>&nbsp;String caches its&nbsp;hashcode&nbsp;after the first calculation. This makes String extremely efficient as a HashMap key, which would not be possible if the value could change.&nbsp;<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">3. What is the difference between creating a String using a literal and using the&nbsp;new&nbsp;keyword?<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>String Literal<\/strong>&nbsp;<\/td><td><strong>new&nbsp;String()<\/strong>&nbsp;<\/td><\/tr><tr><td>String s = &#8220;Java&#8221;;&nbsp;<\/td><td>String s = new String(&#8220;Java&#8221;);&nbsp;<\/td><\/tr><tr><td>Stored in the String Pool.&nbsp;<\/td><td>Always&nbsp;creates&nbsp;a new object in heap memory.&nbsp;<\/td><\/tr><tr><td>Reused if the same value already exists in the pool.&nbsp;<\/td><td>A new object is created even if an identical string already exists.&nbsp;<\/td><\/tr><tr><td>More memory&nbsp;efficient.&nbsp;<\/td><td>Uses more memory.&nbsp;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>String s1 = \"Java\"; \nString s2 = \"Java\"; \nString s3 = new String(\"Java\"); \n \nSystem.out.println(s1 == s2);        \/\/ true, same pooled object \nSystem.out.println(s1 == s3);        \/\/ false, different memory locations \nSystem.out.println(s1.equals(s3));   \/\/ true, same content<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4. What does the&nbsp;intern()&nbsp;method do in Java?<\/h3>\n\n\n\n<p>The&nbsp;intern()&nbsp;method moves a string into the String&nbsp;Pool, or&nbsp;returns the existing pooled reference if a string with the same value is already there.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>String s1 = new String(\"Java\"); \nString s2 = s1.intern(); \nString s3 = \"Java\"; \n \nSystem.out.println(s1 == s3);   \/\/ false \nSystem.out.println(s2 == s3);   \/\/ true<\/code><\/pre>\n\n\n\n<p>It is useful when working with a large number of duplicate strings, for example strings read from a file or database, because interning them reduces memory usage.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5. Why is a character array preferred over String for storing passwords?<\/h3>\n\n\n\n<p>Because String is immutable, a password stored in a&nbsp;String stays&nbsp;in memory until the garbage collector removes it, and you have no control over when that happens. If a memory dump is taken during that window, the password is visible in plain text.&nbsp;<\/p>\n\n\n\n<p>A character array can be explicitly overwritten as soon as it is no longer needed.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>java \n\nchar&#091;] password = getPassword(); \n\/\/ use the password \nArrays.fill(password, ' ');   \/\/ wiped immediately <\/code><\/pre>\n\n\n\n<p>This is also why JPasswordField.getPassword() returns a char[] and not a String.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">6. What is the difference between&nbsp;==&nbsp;and&nbsp;equals()&nbsp;when comparing strings?<\/h3>\n\n\n\n<p>The&nbsp;==&nbsp;operator compares references, meaning it checks whether two variables point to the same object in memory. The&nbsp;equals()&nbsp;method compares the actual character content of the strings.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>String s1 = new String(\"Java\"); \nString s2 = new String(\"Java\"); \n \nSystem.out.println(s1 == s2);        \/\/ false, different objects \nSystem.out.println(s1.equals(s2));   \/\/ true, same content<\/code><\/pre>\n\n\n\n<p>For comparing string values, always use&nbsp;equals(). Use&nbsp;equalsIgnoreCase()&nbsp;when case should not matter.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">7. Why does&nbsp;String&nbsp;override&nbsp;equals()&nbsp;and&nbsp;hashCode()?<\/h3>\n\n\n\n<p>The default&nbsp;equals()&nbsp;inherited from&nbsp;Object&nbsp;compares references, which would make two strings with identical content compare as unequal. String overrides&nbsp;equals()&nbsp;to compare content instead.&nbsp;<\/p>\n\n\n\n<p>It also overrides&nbsp;hashCode()&nbsp;because the equals and&nbsp;hashCode&nbsp;contract&nbsp;requires&nbsp;that two objects considered equal must return the same&nbsp;hashcode. Without this, strings would not work correctly as keys in&nbsp;HashMap&nbsp;or&nbsp;HashSet.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">8. Which is faster for string&nbsp;concatenation,&nbsp;String,&nbsp;StringBuilder, or&nbsp;StringBuffer?<\/h3>\n\n\n\n<p>StringBuilder&nbsp;is fastest, followed by&nbsp;StringBuffer, with&nbsp;String&nbsp;being by far the slowest for repeated modification.&nbsp;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Concatenating with&nbsp;String&nbsp;in a loop creates a new object on every iteration, which is&nbsp;very expensive.&nbsp;<\/li>\n\n\n\n<li>StringBuilder&nbsp;modifies the same internal character array and is not&nbsp;synchronised, so it has no locking overhead.&nbsp;<\/li>\n\n\n\n<li>StringBuffer&nbsp;does the same thing but is&nbsp;synchronised, which makes it thread-safe but slower.&nbsp;<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Avoid this \nString result = \"\"; \nfor (int i = 0; i &lt; 1000; i++) { \n    result += i;    \/\/ creates 1000 objects \n} \n \n\/\/ Use this \nStringBuilder sb = new StringBuilder(); \nfor (int i = 0; i &lt; 1000; i++) { \n    sb.append(i);   \/\/ modifies one object \n}<\/code><\/pre>\n\n\n\n<p>Use&nbsp;StringBuilder&nbsp;in single-threaded code, and&nbsp;StringBuffer&nbsp;only when the same object is genuinely shared across threads.&nbsp;<\/p>\n\n\n\n<p><em><strong>Note:&nbsp;<\/strong>String handling questions often come with a small code snippet, so it helps to&nbsp;practise&nbsp;running them yourself. You can test&nbsp;all of&nbsp;these examples in&nbsp;our&nbsp;<a href=\"https:\/\/www.wscubetech.com\/resources\/java\/compiler\" target=\"_blank\" rel=\"noreferrer noopener\">online Java compiler<\/a>&nbsp;without setting anything up locally.<\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Java Collections Framework Interview Questions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. How does HashMap work internally in Java?<\/h3>\n\n\n\n<p>This is one of the&nbsp;most commonly asked&nbsp;advanced Java interview questions.&nbsp;A HashMap stores data as key-value pairs using an array of buckets.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">How a value is stored:&nbsp;<\/h4>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li>The&nbsp;hashCode()&nbsp;of the key is calculated.&nbsp;<\/li>\n\n\n\n<li>The hash is spread further to reduce collisions, using&nbsp;h ^ (h &gt;&gt;&gt; 16).&nbsp;<\/li>\n\n\n\n<li>The bucket index is calculated as&nbsp;(n &#8211; 1) &amp; hash, where&nbsp;n&nbsp;is the array length.&nbsp;<\/li>\n\n\n\n<li>The entry is stored in that bucket as a&nbsp;Node&nbsp;containing&nbsp;the hash, key, value, and a reference to the next node.&nbsp;<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">How collisions are handled:<\/h4>\n\n\n\n<p>When two keys map to the same bucket, the entries are chained together in a linked list. From Java 8 onwards, if a single bucket grows to&nbsp;8 or more nodes&nbsp;and the table capacity is at least&nbsp;<strong>64<\/strong>, that bucket is converted into a&nbsp;red-black tree. This improves worst-case lookup from O(n) to&nbsp;O(log n). The bucket reverts to a linked list if it shrinks back to 6 nodes.&nbsp;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Default values to remember:<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Default initial capacity:&nbsp;<\/strong>16&nbsp;<\/li>\n\n\n\n<li><strong>Default load factor:<\/strong>&nbsp;0.75&nbsp;<\/li>\n\n\n\n<li><strong>Resize trigger: <\/strong>when size exceeds capacity multiplied by load factor, the table capacity is&nbsp;doubled&nbsp;and all entries are rehashed.&nbsp;<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">How a value is retrieved:<\/h4>\n\n\n\n<p>The same hash and index calculation is performed, then the bucket is searched.&nbsp;equals()&nbsp;is&nbsp;used to&nbsp;identify&nbsp;the correct key within the bucket.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. What is the contract between&nbsp;equals()&nbsp;and&nbsp;hashCode()?<\/h3>\n\n\n\n<p><strong>The contract has three rules:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If two objects are equal according to&nbsp;equals(), they&nbsp;must&nbsp;return the same&nbsp;hashCode().&nbsp;<\/li>\n\n\n\n<li>If two objects have the same&nbsp;hashCode(), they are&nbsp;not&nbsp;necessarily equal. This is a hash collision and is normal.&nbsp;<\/li>\n\n\n\n<li>The&nbsp;hashCode()&nbsp;of an object must return the same value consistently, as long as the fields used in&nbsp;equals()&nbsp;have not changed.&nbsp;<\/li>\n<\/ul>\n\n\n\n<p><strong>Why it matters:<\/strong>&nbsp;if you override&nbsp;equals()&nbsp;without overriding&nbsp;hashCode(), objects that are logically equal will land in different buckets in a&nbsp;HashMap&nbsp;or&nbsp;HashSet. The result is that you can put an object into a map and then&nbsp;fail to&nbsp;retrieve it with an equal key.&nbsp;<\/p>\n\n\n\n<p>This is why&nbsp;equals()&nbsp;and&nbsp;hashCode()&nbsp;must always be overridden together.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. What is the difference between HashMap,&nbsp;Hashtable, and&nbsp;ConcurrentHashMap?<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>HashMap<\/strong><\/td><td><strong>Hashtable<\/strong><\/td><td><strong>ConcurrentHashMap<\/strong><\/td><\/tr><tr><td>Not&nbsp;synchronised.&nbsp;<\/td><td>Synchronised&nbsp;on every method.&nbsp;<\/td><td>Thread-safe using fine-grained locking.&nbsp;<\/td><\/tr><tr><td>Fastest in single-threaded use.&nbsp;<\/td><td>Slowest, because the entire map is locked.&nbsp;<\/td><td>Fast in multi-threaded use.&nbsp;<\/td><\/tr><tr><td>Allows one null key and multiple null values.&nbsp;<\/td><td>Does not allow null keys or values.&nbsp;<\/td><td>Does not allow null keys or values.&nbsp;<\/td><\/tr><tr><td>Introduced in Java 1.2.&nbsp;<\/td><td>Legacy class from Java 1.0.&nbsp;<\/td><td>Introduced in Java 5.&nbsp;<\/td><\/tr><tr><td>Iterators are fail-fast.&nbsp;<\/td><td>Enumerations are not fail-fast.&nbsp;<\/td><td>Iterators are weakly&nbsp;consistent,&nbsp;they do not throw&nbsp;ConcurrentModificationException.&nbsp;<\/td><\/tr><tr><td>Use in&nbsp;single-threaded code.&nbsp;<\/td><td>Effectively obsolete,&nbsp;avoid in&nbsp;new code.&nbsp;<\/td><td>Use in multi-threaded code.&nbsp;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Point worth&nbsp;adding:<\/strong>&nbsp;in Java 7,&nbsp;ConcurrentHashMap&nbsp;used segment-level locking with a default concurrency level of 16. From Java 8 it uses CAS operations and&nbsp;synchronises&nbsp;only on the head node of an individual bucket, which&nbsp;allows&nbsp;far higher concurrency.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4. What is the difference between fail-fast and fail-safe iterators?<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>Fail-Fast<\/strong>&nbsp;<\/td><td><strong>Fail-Safe<\/strong>&nbsp;<\/td><\/tr><tr><td>Throws&nbsp;ConcurrentModificationException&nbsp;if the collection is structurally&nbsp;modified&nbsp;during iteration.&nbsp;<\/td><td>Does not throw an exception.&nbsp;<\/td><\/tr><tr><td>Iterates over the original collection directly.&nbsp;<\/td><td>Iterates over a copy or a snapshot of the collection.&nbsp;<\/td><\/tr><tr><td>Detects changes using an internal&nbsp;modCount&nbsp;field.&nbsp;<\/td><td>Changes made during iteration are not reflected.&nbsp;<\/td><\/tr><tr><td>Used by&nbsp;ArrayList,&nbsp;HashMap,&nbsp;HashSet,&nbsp;Vector.&nbsp;<\/td><td>Used by&nbsp;CopyOnWriteArrayList,&nbsp;ConcurrentHashMap.&nbsp;<\/td><\/tr><tr><td>No extra memory overhead.&nbsp;<\/td><td>Higher memory&nbsp;use, because a copy may be&nbsp;maintained.&nbsp;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>List&lt;String&gt; list = new ArrayList&lt;&gt;(List.of(\"a\", \"b\", \"c\")); \nfor (String s : list) { \n    if (s.equals(\"b\")) { \n        list.remove(s);   \/\/ throws ConcurrentModificationException \n    } \n}<\/code><\/pre>\n\n\n\n<p>To remove safely during iteration, use the iterator&#8217;s own&nbsp;remove()&nbsp;method or&nbsp;removeIf().&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5. How does an&nbsp;ArrayList&nbsp;grow dynamically?<\/h3>\n\n\n\n<p>An&nbsp;ArrayList&nbsp;is backed by an internal array that is replaced with a larger one when it becomes full.&nbsp;<\/p>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li>The default capacity is&nbsp;10,&nbsp;allocated&nbsp;lazily on the first&nbsp;add()&nbsp;call.&nbsp;<\/li>\n\n\n\n<li>When the array is full, a new array is created with a capacity of&nbsp;<strong>1.5 times<\/strong>&nbsp;the old capacity, calculated as&nbsp;oldCapacity&nbsp;+ (oldCapacity&nbsp;&gt;&gt; 1).&nbsp;<\/li>\n\n\n\n<li>All existing elements are copied into the new array.&nbsp;<\/li>\n\n\n\n<li>The new element&nbsp;is&nbsp;added.&nbsp;<\/li>\n<\/ol>\n\n\n\n<p>So&nbsp;the capacity grows 10, 15, 22, 33, and so on. Because resizing is infrequent, the cost of&nbsp;add()&nbsp;is described as&nbsp;amortised&nbsp;constant time.&nbsp;<\/p>\n\n\n\n<p>If you know&nbsp;roughly how&nbsp;many elements you will store, passing&nbsp;an initial&nbsp;capacity to the constructor avoids repeated resizing.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">6. What is the difference between&nbsp;Iterator&nbsp;and&nbsp;ListIterator?<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong><a href=\"https:\/\/www.wscubetech.com\/resources\/java\/iterator-listIterator\" target=\"_blank\" rel=\"noreferrer noopener\">Iterator<\/a><\/strong><\/td><td><strong><a href=\"https:\/\/www.wscubetech.com\/resources\/java\/iterator-listIterator\" target=\"_blank\" rel=\"noreferrer noopener\">ListIterator<\/a><\/strong><\/td><\/tr><tr><td>Traverses in the forward direction only.&nbsp;<\/td><td>Traverses both forward and backward.&nbsp;<\/td><\/tr><tr><td>Can be used with List, Set, and Map.&nbsp;<\/td><td>Can be used with List only.&nbsp;<\/td><\/tr><tr><td>Cannot obtain the index of an element.&nbsp;<\/td><td>Provides&nbsp;nextIndex()&nbsp;and&nbsp;previousIndex().&nbsp;<\/td><\/tr><tr><td>Can only remove elements.&nbsp;<\/td><td>Can add, remove, and replace elements using&nbsp;add()&nbsp;and&nbsp;set().&nbsp;<\/td><\/tr><tr><td>Methods:&nbsp;hasNext(),&nbsp;next(),&nbsp;remove().&nbsp;<\/td><td>Adds&nbsp;hasPrevious(),&nbsp;previous(),&nbsp;add(),&nbsp;set().&nbsp;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">7. What is the difference between Collection and Collections?<\/h3>\n\n\n\n<p>Collection&nbsp;is an&nbsp;interface&nbsp;at the root of the&nbsp;collections&nbsp;hierarchy.&nbsp;List,&nbsp;Set, and&nbsp;Queue&nbsp;all extend it, and it defines core methods such as&nbsp;add(),&nbsp;remove(), and&nbsp;size().<\/p>\n\n\n\n<p><a href=\"https:\/\/www.wscubetech.com\/resources\/java\/collections\" target=\"_blank\" rel=\"noreferrer noopener\">Collections<\/a>&nbsp;is a&nbsp;utility class&nbsp;in&nbsp;java.util&nbsp;that&nbsp;contains&nbsp;only static helper methods for working with collections, such as&nbsp;sort(),&nbsp;reverse(),&nbsp;shuffle(),&nbsp;min(),&nbsp;max(),&nbsp;unmodifiableList(), and&nbsp;synchronizedList().<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">8. What is the difference between a Queue and a Deque?<\/h3>\n\n\n\n<p>A&nbsp;<a href=\"https:\/\/www.wscubetech.com\/resources\/java\/queue\" target=\"_blank\" rel=\"noreferrer noopener\">Queue<\/a>&nbsp;follows FIFO (First&nbsp;In&nbsp;First Out). Elements are added&nbsp;at&nbsp;the tail and removed from the head. Common implementations are&nbsp;LinkedList,&nbsp;PriorityQueue, and&nbsp;ArrayDeque.<\/p>\n\n\n\n<p>A\u00a0Deque, short for double-ended queue, allows insertion and removal at\u00a0both ends. It can therefore be used as either a queue (FIFO) or a stack (LIFO).\u00a0ArrayDeque\u00a0is the standard implementation and is the recommended replacement for the legacy\u00a0Stack\u00a0class.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Deque&lt;Integer&gt; deque = new ArrayDeque&lt;&gt;(); \ndeque.addFirst(1); \ndeque.addLast(2); \ndeque.pollFirst(); \ndeque.pollLast();<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">9. How can you make a collection thread-safe or read-only in Java?<\/h3>\n\n\n\n<p><strong>Thread-safe:<\/strong>&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>java \n\nList&lt;String&gt; syncList = Collections.synchronizedList(new ArrayList&lt;&gt;()); \nMap&lt;String, String&gt; syncMap = Collections.synchronizedMap(new HashMap&lt;&gt;()); \n \n\/\/ Preferred for concurrent use \nList&lt;String&gt; cowList = new CopyOnWriteArrayList&lt;&gt;(); \nMap&lt;String, String&gt; concurrentMap = new ConcurrentHashMap&lt;&gt;();<\/code><\/pre>\n\n\n\n<p><strong>Read-only:<\/strong>&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>java \n\nList&lt;String&gt; readOnly = Collections.unmodifiableList(list); \nList&lt;String&gt; immutable = List.of(\"a\", \"b\", \"c\");   \/\/ Java 9 and later \n\nAttempting to modify a read-only collection throws UnsupportedOperationException.<\/code><\/pre>\n\n\n\n<p><strong><em>Expand the existing Comparable vs Comparator answer with this code<\/em><\/strong>&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>java \n\n\/\/ Comparable, natural ordering, defined inside the class \nclass Employee implements Comparable&lt;Employee&gt; { \n    int id; \n    String name; \n \n    public int compareTo(Employee other) { \n        return this.id - other.id; \n    } \n} \nCollections.sort(employeeList); \n \n\/\/ Comparator, custom ordering, defined outside the class \nComparator&lt;Employee&gt; byName = (e1, e2) -&gt; e1.name.compareTo(e2.name); \nCollections.sort(employeeList, byName); \n \n\/\/ Java 8 shorthand \nemployeeList.sort(Comparator.comparing(e -&gt; e.name));<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Intermediate&nbsp;Java&nbsp;Interview Questions for Experienced (2-5 Years)<\/h2>\n\n\n\n<p>If you have 2\u20135 years of experience in&nbsp;Java&nbsp;development and are preparing for a job change, it is important to have a strong understanding of core&nbsp;Java&nbsp;concepts and their practical applications.&nbsp;<\/p>\n\n\n\n<p>Below are some of the most asked core&nbsp;Java&nbsp;interview questions and answers for experienced professionals.&nbsp;These questions can help you refresh your knowledge and prepare confidently for technical interviews.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. What is multithreading in&nbsp;Java?<\/h3>\n\n\n\n<p><a href=\"https:\/\/www.wscubetech.com\/resources\/java\/multithreading\" target=\"_blank\" rel=\"noreferrer noopener\">Multithreading in&nbsp;Java<\/a>&nbsp;is the process of executing multiple threads concurrently within a single program. It allows&nbsp;different parts&nbsp;of a program to run simultaneously, improving&nbsp;performance&nbsp;and making better use of CPU resources.&nbsp;<\/p>\n\n\n\n<p>The main purpose of multithreading is to create lightweight threads that can perform multiple tasks at the same time, resulting in faster and more efficient program execution.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. Which class is used for multithreading in&nbsp;Java?<\/h3>\n\n\n\n<p><strong>Java&nbsp;provides&nbsp;several ways to create and manage threads. The main ones are:&nbsp;<\/strong><\/p>\n\n\n\n<p><strong>1. Extending the&nbsp;Thread&nbsp;class&nbsp;<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>java \n\nclass MyThread extends Thread { \n    public void run() { \n        System.out.println(\"Thread running\"); \n    } \n} \nnew MyThread().start();<\/code><\/pre>\n\n\n\n<p><strong>2. Implementing the&nbsp;Runnable&nbsp;interface&nbsp;(preferred)&nbsp;<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>java \n\nclass MyTask implements Runnable { \n    public void run() { \n        System.out.println(\"Thread running\"); \n    } \n} \nnew Thread(new MyTask()).start();<\/code><\/pre>\n\n\n\n<p><strong>3. Implementing&nbsp;<\/strong><strong>Callable<\/strong>&nbsp;when the task needs to return a result or throw a checked exception. It is used with an&nbsp;ExecutorService&nbsp;and returns a&nbsp;Future.&nbsp;<\/p>\n\n\n\n<p><strong>4. Using the&nbsp;<\/strong><strong>ExecutorService<\/strong>&nbsp;from&nbsp;java.util.concurrent, which manages a pool of threads instead of creating them manually. This is the recommended approach in&nbsp;production&nbsp;code.&nbsp;<\/p>\n\n\n\n<p>Runnable&nbsp;is&nbsp;generally preferred&nbsp;over extending&nbsp;Thread&nbsp;because a Java class can implement multiple interfaces but can extend only one class. Extending&nbsp;Thread&nbsp;uses up&nbsp;that one inheritance slot.&nbsp;<\/p>\n\n\n\n<p><strong>Suggested Reading:<\/strong>&nbsp;<a href=\"https:\/\/www.wscubetech.com\/blog\/reactjs-interview-questions-answers\/\" target=\"_blank\" rel=\"noreferrer noopener\">ReactJS Interview Questions and Answers<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. What is&nbsp;Java&nbsp;applet?<\/h3>\n\n\n\n<p>An applet was a small Java program embedded in a web page and executed inside a browser through a Java plugin. It was used to add dynamic content to web pages in the early years of Java.&nbsp;<\/p>\n\n\n\n<p><strong>Applets are obsolete and no longer part of Java.&nbsp;The timeline is:&nbsp;<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Java 9 (2017):<\/strong>&nbsp;the Applet API was deprecated (JEP 289).&nbsp;<\/li>\n\n\n\n<li><strong>Java 11 (2018):<\/strong>&nbsp;the&nbsp;appletviewer&nbsp;tool, the Java Plug-in, and Java Web Start were removed. Browsers had already dropped plugin support by then.&nbsp;<\/li>\n\n\n\n<li><strong>Java 17 (2021):<\/strong>&nbsp;the Applet API was deprecated for removal (JEP 398).&nbsp;<\/li>\n\n\n\n<li><strong>JDK 26 (2026):<\/strong>&nbsp;the entire&nbsp;java.applet&nbsp;package was removed (JEP 504). Java 25 is the last release that&nbsp;contains&nbsp;it.&nbsp;<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">4. What is garbage collection in&nbsp;Java?<\/h3>\n\n\n\n<p>Garbage collection is one of the most important and commonly discussed topics in&nbsp;Java&nbsp;interviews. It is the process of automatically managing memory in&nbsp;Java&nbsp;applications.&nbsp;<\/p>\n\n\n\n<p>The Garbage Collector (GC) automatically&nbsp;identifies&nbsp;and removes objects that are no longer being used by the program. This frees up memory in the heap and helps&nbsp;optimize&nbsp;memory usage, improving the overall performance of&nbsp;Java&nbsp;applications.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5. What if you use&nbsp;Java&nbsp;keywords as a variable or identifier?<\/h3>\n\n\n\n<p><a href=\"https:\/\/www.wscubetech.com\/resources\/java\/keywords\" target=\"_blank\" rel=\"noreferrer noopener\">Java&nbsp;keywords<\/a>&nbsp;are reserved words and cannot be used as variable names, class names, method names, or other identifiers. If a keyword is used as an identifier, the program will generate a compile-time error.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">6. What is inheritance in&nbsp;Java?<\/h3>\n\n\n\n<p><a href=\"https:\/\/www.wscubetech.com\/resources\/java\/inheritance\" target=\"_blank\" rel=\"noreferrer noopener\">Inheritance<\/a> is an object-oriented programming (OOP) concept in&nbsp;Java&nbsp;that allows one class to&nbsp;acquire&nbsp;the properties and methods of another class. The class that inherits is called the subclass (child class), and the class being inherited from is called the superclass (parent class).&nbsp;<\/p>\n\n\n\n<p>Inheritance promotes code reusability, reduces duplication, and helps create a hierarchical relationship between classes. In&nbsp;Java, inheritance is implemented using the extends keyword.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">7. What is polymorphism in&nbsp;Java?<\/h3>\n\n\n\n<p>This is yet another important concept in the list of top&nbsp;Java&nbsp;interview questions and answers related to OOPS.&nbsp;<\/p>\n\n\n\n<p><a href=\"https:\/\/www.wscubetech.com\/resources\/java\/polymorphism\" target=\"_blank\" rel=\"noreferrer noopener\">Polymorphism in Java<\/a>&nbsp;is an object-oriented programming (OOP) concept that means &#8220;many forms.&#8221; In&nbsp;Java, it refers to the ability of a method, object, or class to take different forms and perform different actions based on the context.&nbsp;<\/p>\n\n\n\n<p>Polymorphism allows the same method name to behave differently for different objects or parameters. For example, a method can display different messages or perform different operations depending on the values or objects passed to it. This helps improve code flexibility, reusability, and maintainability.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">8. What is encapsulation in&nbsp;Java?<\/h3>\n\n\n\n<p>If you are&nbsp;preparing for&nbsp;Java&nbsp;interview questions for 3&nbsp;years experience&nbsp;or more, encapsulation is one of the important concepts you should understand.&nbsp;<\/p>\n\n\n\n<p><a href=\"https:\/\/www.wscubetech.com\/resources\/java\/encapsulation\" target=\"_blank\" rel=\"noreferrer noopener\">Encapsulation in&nbsp;Java<\/a>&nbsp;is&nbsp;the process of combining data (variables) and the methods that&nbsp;operate&nbsp;on that data into a single unit, known as a class. It also helps protect the data by restricting direct access from outside the class and allowing controlled access through methods such as getters and setters.&nbsp;<\/p>\n\n\n\n<p>An important point&nbsp;to remember is that encapsulation helps achieve data hiding. Once variables are&nbsp;declared as&nbsp;private, they cannot be accessed directly by other classes, which improves security and&nbsp;maintains&nbsp;the integrity of the data.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">9. What is serialization in&nbsp;Java?<\/h3>\n\n\n\n<p><a href=\"https:\/\/www.wscubetech.com\/resources\/java\/serialization-deserialization\" target=\"_blank\" rel=\"noreferrer noopener\">Serialization in&nbsp;Java<\/a>&nbsp;is the process of converting an object into a byte stream so that it can be stored in a file, transferred over a network, or saved for later use.<\/p>\n\n\n\n<p>When needed, the byte stream can be converted back into an object through a process called deserialization. Serialization is commonly used for data storage, object persistence, and communication between applications.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">10. What is&nbsp;Java&nbsp;JDBC?<\/h3>\n\n\n\n<p>JDBC (Java&nbsp;Database Connectivity) is an API that enables&nbsp;Java&nbsp;applications to connect to and interact with databases. It allows developers to&nbsp;establish&nbsp;database connections, execute&nbsp;<a href=\"https:\/\/www.wscubetech.com\/resources\/sql\" target=\"_blank\" rel=\"noreferrer noopener\">SQL<\/a>&nbsp;queries, retrieve data, and update records directly from a&nbsp;Java&nbsp;program.&nbsp;<\/p>\n\n\n\n<p>JDBC acts as a communication bridge between a&nbsp;Java&nbsp;application and a database. With the&nbsp;appropriate JDBC&nbsp;driver, developers can connect to various databases such as MySQL, Oracle, PostgreSQL, and SQL Server. Because it is widely used in database-driven applications, JDBC is an important topic in core&nbsp;Java&nbsp;interview&nbsp;questions for experienced professionals.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">11. What is&nbsp;Java&nbsp;enum?<\/h3>\n\n\n\n<p><a href=\"https:\/\/www.wscubetech.com\/resources\/java\/enums\" target=\"_blank\" rel=\"noreferrer noopener\">Enum in&nbsp;Java<\/a>&nbsp;stands for enumeration. It is a data type that comes with a set of pre-defined constant values. These values are separated by a comma. The&nbsp;enum&nbsp;concept was brought to this programming as part of&nbsp;Java&nbsp;5. For declaring the&nbsp;enums, the&nbsp;enum&nbsp;keyword is used.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">12. What is constructor overloading in&nbsp;Java?<\/h3>\n\n\n\n<p>The role of <a href=\"https:\/\/www.wscubetech.com\/resources\/java\/constructors\" target=\"_blank\" rel=\"noreferrer noopener\">constructors in&nbsp;Java<\/a>&nbsp;is to initialize the state of an object. When a class&nbsp;contains&nbsp;multiple constructors with different parameter lists, it is called constructor overloading.&nbsp;<\/p>\n\n\n\n<p>Constructor overloading allows a class to create and initialize objects in&nbsp;different ways&nbsp;based on the arguments passed during object creation. As a result, a single class can have multiple constructors to handle different initialization requirements.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">13. What is&nbsp;copy&nbsp;constructor in&nbsp;Java?<\/h3>\n\n\n\n<p>A copy constructor is a constructor that creates a new object by copying the values of another object&nbsp;of&nbsp;the same class.&nbsp;Java&nbsp;does not provide built-in copy constructors.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">14. Which keyword in&nbsp;Java&nbsp;is used to inherit a class?&nbsp;<\/h3>\n\n\n\n<p>The&nbsp;extends&nbsp;keyword is used in&nbsp;Java&nbsp;to inherit a class. It allows a subclass to&nbsp;acquire&nbsp;the properties and methods of its parent class.&nbsp;<\/p>\n\n\n\n<p><strong>Also Read:<\/strong>&nbsp;<a href=\"https:\/\/www.wscubetech.com\/blog\/java-full-stack-developer-roadmap\/\" target=\"_blank\" rel=\"noreferrer noopener\">Java&nbsp;Full Stack Developer Roadmap: Full Learning Path<\/a>&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">15. What are the top benefits of inheritance in&nbsp;Java?<\/h3>\n\n\n\n<p><strong>The following are the main benefits of&nbsp;Java&nbsp;inheritance:<\/strong>&nbsp;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Code reusability&nbsp;<\/li>\n\n\n\n<li>Method overriding&nbsp;<\/li>\n\n\n\n<li>Ability to achieve runtime polymorphism&nbsp;<\/li>\n\n\n\n<li>Optimize duplicate code&nbsp;<\/li>\n\n\n\n<li>Improve the redundancy of the app&nbsp;<\/li>\n\n\n\n<li>Code flexibility so that it can be changed easily&nbsp;<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">16. What are the different memory areas assigned by JVM?<\/h3>\n\n\n\n<p><strong>The JVM&nbsp;allocates&nbsp;memory into five&nbsp;main areas&nbsp;to manage program execution efficiently:<\/strong>&nbsp;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Method Area (Class Area)&nbsp;\u2013<\/strong> Stores class metadata, methods, and static variables.&nbsp;<\/li>\n\n\n\n<li><strong>Heap Memory&nbsp;\u2013<\/strong> Stores objects and instance variables created during program execution.&nbsp;<\/li>\n\n\n\n<li><strong>Stack Memory&nbsp;\u2013 <\/strong>Stores local variables, method calls, and execution frames for each thread.&nbsp;<\/li>\n\n\n\n<li><strong>Program Counter (PC) Register&nbsp;\u2013<\/strong> Keeps track of the current instruction being executed by a thread.&nbsp;<\/li>\n\n\n\n<li><strong>Native Method Stack&nbsp;\u2013<\/strong> Supports the execution of native (non-Java) methods.&nbsp;<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">17. What are access modifiers in&nbsp;Java?<\/h3>\n\n\n\n<p>As a programming enthusiast, you should know about the access modifiers while&nbsp;preparing for&nbsp;the&nbsp;Java&nbsp;interview questions and answers.&nbsp;&nbsp;<\/p>\n\n\n\n<p>As the name suggests, the <a href=\"https:\/\/www.wscubetech.com\/resources\/java\/access-modifiers\" target=\"_blank\" rel=\"noreferrer noopener\">access modifiers in&nbsp;Java<\/a>&nbsp;are used to manage the access level for classes, variables, methods, constructors, etc. The access can be changed or specified using these access modifiers.<\/p>\n\n\n\n<p><strong>Access modifiers are of four types:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Public&nbsp;<\/li>\n\n\n\n<li>Private&nbsp;<\/li>\n\n\n\n<li>Default&nbsp;<\/li>\n\n\n\n<li>Protected&nbsp;&nbsp;<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">18. How many types of inheritance are there in&nbsp;Java?<\/h3>\n\n\n\n<p><strong>There are five types of&nbsp;Java&nbsp;inheritance:<\/strong>&nbsp;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Single-level inheritance&nbsp;<\/li>\n\n\n\n<li>Multi-level Inheritance&nbsp;<\/li>\n\n\n\n<li>Hierarchical Inheritance&nbsp;<\/li>\n\n\n\n<li>Multiple Inheritance&nbsp;<\/li>\n\n\n\n<li>Hybrid Inheritance&nbsp;<\/li>\n<\/ul>\n\n\n\n<p class=\"has-white-color has-vivid-cyan-blue-background-color has-text-color has-background has-link-color wp-elements-56443d1e9ad507657bade8a12c92b36f\"><em><strong>Note:<\/strong>&nbsp;Java&nbsp;supports Single, Multilevel, and Hierarchical Inheritance through classes. Multiple and Hybrid Inheritance can be achieved using interfaces.<\/em><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">19. Can you restrict an object from inheriting its subclass? If yes, how?<\/h3>\n\n\n\n<p>Yes, a class can be restricted from being inherited by using the final keyword. A class declared as final cannot be extended by any other class.&nbsp;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Example:<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>final class Vehicle { \n}<\/code><\/pre>\n\n\n\n<p>In this example, the Vehicle class cannot be inherited by another class.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">20. How can we remove duplicate elements from a list of numbers in&nbsp;Java&nbsp;8?<\/h3>\n\n\n\n<p>In&nbsp;Java&nbsp;8, duplicate elements can be removed from a list using the&nbsp;distinct() method of the Stream API. This method returns a stream&nbsp;containing&nbsp;only unique elements.&nbsp;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Example:<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>List&lt;Integer&gt; numbers = Arrays.asList(1, 2, 2, 3, 4, 4, 5); \nList&lt;Integer&gt; uniqueNumbers = numbers.stream() \n                                     .distinct() \n                                     .collect(Collectors.toList());<\/code><\/pre>\n\n\n\n<p>In this example, the&nbsp;distinct() method removes duplicate values and returns a list&nbsp;containing&nbsp;only unique elements.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">21. What is abstraction in&nbsp;Java?<\/h3>\n\n\n\n<p>Abstraction is an object-oriented programming (OOP) concept that hides implementation details and shows only the essential features of an object to the user.&nbsp;<\/p>\n\n\n\n<p>In&nbsp;Java, abstraction is achieved using abstract classes and interfaces. It helps reduce complexity, improve code maintainability, and focus on what an object does rather than how it does it.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">22. What is the difference between an abstract class and an interface in&nbsp;Java?<\/h3>\n\n\n\n<p>Both abstract classes and interfaces are used to achieve abstraction in&nbsp;Java, but they differ in several ways.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>Abstract Class<\/strong>&nbsp;<\/td><td><strong><a href=\"https:\/\/www.wscubetech.com\/resources\/java\/interfaces\" target=\"_blank\" rel=\"noreferrer noopener\">Interface<\/a><\/strong><\/td><\/tr><tr><td>Declared using the abstract keyword.&nbsp;<\/td><td>Declared using the interface keyword.&nbsp;<\/td><\/tr><tr><td>Can&nbsp;contain&nbsp;both abstract and concrete methods.&nbsp;<\/td><td>Primarily&nbsp;contains&nbsp;abstract methods (can also have default and static methods).&nbsp;<\/td><\/tr><tr><td>Can have instance variables and constructors.&nbsp;<\/td><td>Cannot have constructors and typically&nbsp;contains&nbsp;constants.&nbsp;<\/td><\/tr><tr><td>A class can extend only one abstract class.&nbsp;<\/td><td>A class can implement multiple interfaces.&nbsp;<\/td><\/tr><tr><td>Used when classes share common state and behavior.&nbsp;<\/td><td>Used to define a common contract or capability.&nbsp;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Abstract classes are suitable when related classes share common functionality, while interfaces are ideal for defining behaviors that multiple unrelated classes can implement.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">23. What is method overloading in&nbsp;Java?<\/h3>\n\n\n\n<p><a href=\"https:\/\/www.wscubetech.com\/resources\/java\/method-overloading\" target=\"_blank\" rel=\"noreferrer noopener\">Java Method overloading<\/a>&nbsp;is a feature&nbsp;that allows a class to have multiple methods with the same name but different parameter lists. The methods can differ in the number, type, or order of parameters.&nbsp;<\/p>\n\n\n\n<p>Method overloading helps improve code readability and flexibility by allowing the same method to perform similar operations with different inputs. It is an example of compile-time&nbsp;polymorphism in&nbsp;Java.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">24. What is&nbsp;method&nbsp;overriding in&nbsp;Java?<\/h3>\n\n\n\n<p><a href=\"https:\/\/www.wscubetech.com\/resources\/java\/method-overriding\" target=\"_blank\" rel=\"noreferrer noopener\">Java Method overriding<\/a>&nbsp;is&nbsp;a feature&nbsp;where a subclass provides its own implementation of a method that is already defined in its superclass. The overriding method must have the same name, return type, and parameters as the method in the parent class.&nbsp;<\/p>\n\n\n\n<p>Method overriding allows a subclass to&nbsp;modify&nbsp;or extend the behavior of an inherited method. It is an example of runtime polymorphism in&nbsp;Java.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">25. What is the difference between compile-time polymorphism and runtime polymorphism?<\/h3>\n\n\n\n<p>Compile-time polymorphism and runtime polymorphism are two types of&nbsp;polymorphism in&nbsp;Java. The table below highlights their key differences.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>Compile-Time Polymorphism<\/strong><\/td><td><strong>Runtime Polymorphism<\/strong><\/td><\/tr><tr><td>Achieved through&nbsp;method&nbsp;overloading.&nbsp;<\/td><td>Achieved through method overriding.&nbsp;<\/td><\/tr><tr><td>Resolved by the compiler during compilation.&nbsp;<\/td><td>Resolved by the JVM during program execution.&nbsp;<\/td><\/tr><tr><td>Also known as static binding or early binding.&nbsp;<\/td><td>Also known as dynamic binding or late binding.&nbsp;<\/td><\/tr><tr><td>Inheritance is not&nbsp;required.&nbsp;<\/td><td>Inheritance is&nbsp;required.&nbsp;<\/td><\/tr><tr><td>Faster execution because the&nbsp;method&nbsp;call is&nbsp;determined&nbsp;at compile time.&nbsp;<\/td><td>Slightly slower because the&nbsp;method&nbsp;call is&nbsp;determined&nbsp;at runtime.&nbsp;<\/td><\/tr><tr><td><strong>Example:<\/strong>&nbsp;Multiple methods with the same name but different parameters.&nbsp;<\/td><td><strong>Example:<\/strong>&nbsp;A subclass providing its own implementation of a parent class method.&nbsp;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">26. What is&nbsp;the this&nbsp;keyword in&nbsp;Java?<\/h3>\n\n\n\n<p>The&nbsp;<a href=\"https:\/\/www.wscubetech.com\/resources\/java\/this-keyword\" target=\"_blank\" rel=\"noreferrer noopener\">this keyword in&nbsp;Java<\/a>&nbsp;refers to the current object of a class.&nbsp;It is used to access the current object&#8217;s variables, methods, and constructors.&nbsp;<\/p>\n\n\n\n<p>The this&nbsp;keyword is commonly used to distinguish instance variables from local variables, call another constructor within the same class, and pass the current object as an argument to a method or constructor.<\/p>\n\n\n\n<iframe loading=\"lazy\" width=\"640\" height=\"350\" src=\"https:\/\/www.youtube.com\/embed\/pNXC9Mfp5T0?si=kYyEjgBl8km4G_QD\" title=\"YouTube video player\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n\n\n\n<h3 class=\"wp-block-heading\">27. What is&nbsp;exception&nbsp;handling in&nbsp;Java?<\/h3>\n\n\n\n<p><a href=\"https:\/\/www.wscubetech.com\/resources\/java\/exceptions-handling\" target=\"_blank\" rel=\"noreferrer noopener\">Exception handling in&nbsp;Java<\/a>&nbsp;is a mechanism used to handle runtime errors and prevent a program from&nbsp;terminating&nbsp;unexpectedly. It allows developers to detect, manage, and&nbsp;recover from&nbsp;exceptional situations during program execution.&nbsp;<\/p>\n\n\n\n<p>Java&nbsp;provides the try, catch, finally, throw, and throws keywords for exception handling. By handling exceptions properly, programs can continue running smoothly even when errors occur.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">28. What is the difference between checked and unchecked exceptions in&nbsp;Java?<\/h3>\n\n\n\n<p>Checked and unchecked exceptions differ in how they are handled by the&nbsp;Java&nbsp;compiler.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>Checked Exceptions<\/strong>&nbsp;<\/td><td><strong>Unchecked Exceptions<\/strong>&nbsp;<\/td><\/tr><tr><td>Checked at compile time.&nbsp;<\/td><td>Checked at runtime.&nbsp;<\/td><\/tr><tr><td>Must be handled using try-catch or declared with throws.&nbsp;<\/td><td>Handling is optional.&nbsp;<\/td><\/tr><tr><td>Subclasses of Exception (excluding&nbsp;RuntimeException).&nbsp;<\/td><td>Subclasses of&nbsp;RuntimeException.&nbsp;<\/td><\/tr><tr><td>Occur due to external factors such as file or database issues.&nbsp;<\/td><td>Usually&nbsp;occur&nbsp;because of programming errors.&nbsp;<\/td><\/tr><tr><td><strong>Examples:&nbsp;<\/strong>IOException,&nbsp;SQLException.&nbsp;<\/td><td><strong>Examples:<\/strong>&nbsp;NullPointerException,&nbsp;ArithmeticException.&nbsp;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Checked exceptions help ensure error handling during compilation, while unchecked exceptions&nbsp;indicate&nbsp;issues that are typically fixed in the program logic.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">29. What is the difference between Array and&nbsp;ArrayList&nbsp;in&nbsp;Java?<\/h3>\n\n\n\n<p>Arrays and&nbsp;ArrayLists&nbsp;are both used to store collections of elements, but they differ in several ways.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong><a href=\"https:\/\/www.wscubetech.com\/resources\/java\/arrays\" target=\"_blank\" rel=\"noreferrer noopener\">Array<\/a><\/strong><\/td><td><strong>ArrayList<\/strong><\/td><\/tr><tr><td>Has a fixed size once created.&nbsp;<\/td><td>Can grow or shrink dynamically.&nbsp;<\/td><\/tr><tr><td>Can store primitive data types and objects.&nbsp;<\/td><td>Stores objects only (wrapper classes for primitives).&nbsp;<\/td><\/tr><tr><td>Faster in&nbsp;performance due to fixed size.&nbsp;<\/td><td>Slightly slower because of dynamic resizing.&nbsp;<\/td><\/tr><tr><td>Length is accessed using the&nbsp;length&nbsp;property.&nbsp;<\/td><td>Size is accessed using the&nbsp;size() method.&nbsp;<\/td><\/tr><tr><td>Part of the core&nbsp;Java&nbsp;language.&nbsp;<\/td><td>Part of the&nbsp;Java&nbsp;Collections Framework.&nbsp;<\/td><\/tr><tr><td>Elements are accessed using indexes.&nbsp;<\/td><td>Elements are accessed using methods such as&nbsp;get() and&nbsp;set().&nbsp;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Arrays are suitable when the size is known in advance, while&nbsp;ArrayLists&nbsp;are preferred when the number of elements can change dynamically.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">30. What is the purpose of the try-catch-finally&nbsp;block in&nbsp;Java?<\/h3>\n\n\n\n<p>The try-catch-finally&nbsp;block in&nbsp;Java&nbsp;is used for exception handling. It helps a&nbsp;program handle runtime errors&nbsp;gracefully and continue execution without crashing.&nbsp;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>try&nbsp;\u2013<\/strong> Contains the code that may throw an exception.&nbsp;<\/li>\n\n\n\n<li><strong>catch&nbsp;\u2013<\/strong> Handles the exception if one occurs in the try block.&nbsp;<\/li>\n\n\n\n<li><strong>finally&nbsp;\u2013<\/strong> Contains code that is always executed, whether an exception occurs or not.&nbsp;<\/li>\n<\/ul>\n\n\n\n<p>Using <a href=\"https:\/\/www.wscubetech.com\/resources\/javascript\/try-catch-finally\" target=\"_blank\" rel=\"noreferrer noopener\">try-catch-finally<\/a> improves program reliability and ensures that important cleanup tasks, such as closing files or database connections, are performed properly.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Advanced&nbsp;Java&nbsp;Interview Questions and Answers for Experienced (5-10 Years)<\/h2>\n\n\n\n<p>If you have 5\u201310 years of experience in&nbsp;Java&nbsp;development, the questions below will help you prepare for advanced technical interviews. These commonly asked topics focus on deeper&nbsp;Java&nbsp;concepts and real-world application knowledge.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. What is the difference between heap memory and stack memory in&nbsp;Java?<\/h3>\n\n\n\n<p>Two types of memory are used in the&nbsp;Java&nbsp;Virtual Machine (JVM). One is&nbsp;heap&nbsp;memory, and another is&nbsp;stack&nbsp;memory.&nbsp;&nbsp;<\/p>\n\n\n\n<p>The primary difference between the two is that heap memory\u2019s role is to store objects,&nbsp;whereas&nbsp;stack memory stores local variables and the order of method execution. The following tabular comparison shows all the key differences between&nbsp;the both. While&nbsp;preparing for&nbsp;Java&nbsp;interview questions and answers, ensure to understand this concept well.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>Heap Memory<\/strong>&nbsp;<\/td><td><strong>Stack Memory<\/strong>&nbsp;<\/td><\/tr><tr><td>Stores objects and their instance variables.&nbsp;<\/td><td>Stores method frames, local variables, and reference variables.&nbsp;<\/td><\/tr><tr><td>Shared&nbsp;by&nbsp;all threads in the application.&nbsp;<\/td><td>Each thread gets its own separate stack.&nbsp;<\/td><\/tr><tr><td>Larger in size.&nbsp;<\/td><td>Much smaller in size.&nbsp;<\/td><\/tr><tr><td>Memory is&nbsp;allocated&nbsp;when an object is created and reclaimed automatically by the garbage collector.&nbsp;<\/td><td>Memory is&nbsp;allocated&nbsp;and released automatically as method frames are pushed and popped.&nbsp;<\/td><\/tr><tr><td>No fixed allocation order.&nbsp;<\/td><td>Follows LIFO (Last&nbsp;In&nbsp;First Out) order.&nbsp;<\/td><\/tr><tr><td>Access is comparatively slower.&nbsp;<\/td><td>Access is faster.&nbsp;<\/td><\/tr><tr><td>Objects stay in memory as long as they are reachable.&nbsp;<\/td><td>Local variables are destroyed as soon as the method returns.&nbsp;<\/td><\/tr><tr><td>Size is configured using&nbsp;-Xms&nbsp;and&nbsp;-Xmx.&nbsp;<\/td><td>Size is configured using&nbsp;-Xss.&nbsp;<\/td><\/tr><tr><td>Running out of space throws&nbsp;OutOfMemoryError: Java heap space.&nbsp;<\/td><td>Running out of space throws&nbsp;StackOverflowError, usually from deep or infinite recursion.&nbsp;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">2. Which are the most widely used Java IDEs and development tools?<\/h3>\n\n\n\n<p><strong>First, a clarification that interviewers appreciate:<\/strong> the actual Java compiler is&nbsp;javac, which ships with the JDK. The tools listed below are IDEs and development environments that use&nbsp;javac&nbsp;under the hood.&nbsp;<\/p>\n\n\n\n<p><strong>Most widely used Java IDEs:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>IntelliJ IDEA&nbsp;\u2014 <\/strong>the most popular Java IDE in professional use&nbsp;<\/li>\n\n\n\n<li><strong>Eclipse&nbsp;\u2014<\/strong> long-established open-source IDE with a large plugin ecosystem&nbsp;<\/li>\n\n\n\n<li><strong>Visual Studio Code&nbsp;\u2014<\/strong> with the Extension Pack for Java&nbsp;<\/li>\n\n\n\n<li><strong>NetBeans&nbsp;\u2014<\/strong> Apache-maintained open-source IDE&nbsp;<\/li>\n\n\n\n<li><strong>Android Studio&nbsp;\u2014 <\/strong>the official IDE for Android development, built on IntelliJ&nbsp;<\/li>\n\n\n\n<li><strong>JDeveloper&nbsp;\u2014 <\/strong>Oracle&#8217;s IDE, used&nbsp;mainly with&nbsp;Oracle enterprise products&nbsp;<\/li>\n<\/ul>\n\n\n\n<p><strong>Teaching-focused IDEs:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>BlueJ&nbsp;and&nbsp;jGRASP, both designed for students learning Java&nbsp;<\/li>\n<\/ul>\n\n\n\n<p><strong>Supporting build and productivity tools:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Maven&nbsp;and&nbsp;Gradle&nbsp;for build automation and dependency management&nbsp;<\/li>\n\n\n\n<li>JUnit&nbsp;for unit testing<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">3. What is the difference between&nbsp;equals() method and equality (==) operator in&nbsp;Java?<\/h3>\n\n\n\n<p>There are a number of key differences between the equals method and the equality operator in&nbsp;Java.&nbsp;The primary difference is that one is a method, and another is an operator.&nbsp;<\/p>\n\n\n\n<p>Such tricky concepts are usually asked when you have some experience in this field.&nbsp;So, you need to study the core&nbsp;Java&nbsp;interview questions and answers for experienced professionals&nbsp;really well.&nbsp;<\/p>\n\n\n\n<p>For this question, we have created a tabular comparison to help you understand the differences between the equals method and equality operator in&nbsp;Java.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>Equals() Method<\/strong>&nbsp;<\/td><td><strong>Equality Operator (==)<\/strong>&nbsp;<\/td><\/tr><tr><td>It is a method&nbsp;<\/td><td>It is an operator&nbsp;<\/td><\/tr><tr><td>Its role is for comparing the content of an object&nbsp;<\/td><td>Its role is for comparing the reference values and objects&nbsp;<\/td><\/tr><tr><td>It can be overridden&nbsp;<\/td><td>Can\u2019t&nbsp;be overridden&nbsp;<\/td><\/tr><tr><td>Can\u2019t&nbsp;be used with primitives&nbsp;<\/td><td>Can be used with primitives&nbsp;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">4. Can you inherit static members&nbsp;to&nbsp;a subclass?<\/h3>\n\n\n\n<p>Yes, static members are inherited by subclasses in&nbsp;Java. However, they belong to the class rather than an object and are typically accessed using the class name.&nbsp;<\/p>\n\n\n\n<p><strong>Example:<\/strong>&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Parent { \n    static int value = 10; \n} \n \nclass Child extends Parent { \n}<\/code><\/pre>\n\n\n\n<p>In this example, Child can access value because it inherits the static member from Parent.&nbsp;<\/p>\n\n\n\n<p><strong>Suggested Reading:<\/strong>&nbsp;<a href=\"https:\/\/www.wscubetech.com\/blog\/java-syllabus\/\" target=\"_blank\" rel=\"noreferrer noopener\">Java&nbsp;Syllabus (Curriculum): Full Course Outline<\/a>&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5. Can you override a final method in&nbsp;Java?<\/h3>\n\n\n\n<p>No, a final method cannot be overridden in&nbsp;Java. When a method is declared with the final keyword, subclasses are not allowed to provide their own implementation of that method.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">6. How to declare an infinite loop in&nbsp;Java?<\/h3>\n\n\n\n<p>An infinite loop is a loop that continues to execute indefinitely because its condition never becomes false. In&nbsp;Java, infinite loops can be created using while, for, and do-while loops.&nbsp;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">a) Infinite while Loop<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>while (true) { \n    \/\/ code \n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">b) Infinite for Loop<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>for (;;) { \n    \/\/ code \n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">c) Infinite do-while Loop<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>do { \n    \/\/ code \n} while (true);<\/code><\/pre>\n\n\n\n<p>All three loops run continuously until they are&nbsp;terminated&nbsp;using a break&nbsp;statement&nbsp;or the program is stopped externally.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">7. What are the roles of final,&nbsp;finally, and&nbsp;finalize&nbsp;keywords in&nbsp;Java?<\/h3>\n\n\n\n<p>There are several keywords in&nbsp;Java, and three commonly confused keywords are final, finally, and&nbsp;finalize. The table below highlights their differences:&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>final<\/strong>&nbsp;<\/td><td><strong>finally<\/strong>&nbsp;<\/td><td><strong>finalize<\/strong>&nbsp;<\/td><\/tr><tr><td>Used to apply restrictions on classes, methods, and variables.&nbsp;<\/td><td>Used in exception handling.&nbsp;<\/td><td>A method on Object that the garbage collector could call before reclaiming an object. Deprecated for removal.&nbsp;<\/td><\/tr><tr><td>Can be applied to classes, methods, and variables.&nbsp;<\/td><td>Used with try-catch blocks.&nbsp;<\/td><td>Associate&nbsp;Associated&nbsp;with objects, not with classes or variables.&nbsp;d with objects.&nbsp;<\/td><\/tr><tr><td>A final variable cannot be&nbsp;reassigned,&nbsp;a final method cannot be overridden, and a final class cannot be inherited.&nbsp;<\/td><td>Executes whether an exception occurs or not.&nbsp;<\/td><td>Was intended for cleanup before garbage collection, but execution was never guaranteed.&nbsp;<\/td><\/tr><tr><td>Applied when it is declared.&nbsp;<\/td><td>Executes after the try-catch block completes.&nbsp;<\/td><td>Deprecated since Java 9 and deprecated for removal in Java 18.&nbsp;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">8. When to use the super keyword in&nbsp;Java?<\/h3>\n\n\n\n<p>The&nbsp;<a href=\"https:\/\/www.wscubetech.com\/resources\/java\/super-keyword\" target=\"_blank\" rel=\"noreferrer noopener\">super keyword in&nbsp;Java<\/a>&nbsp;is used to refer to the immediate parent class of a subclass. It is commonly used to access parent class variables, invoke parent class methods, and call the parent class constructor.&nbsp;<\/p>\n\n\n\n<p>Using the super keyword helps a&nbsp;subclass access members of its superclass, especially when the parent and child classes have variables or methods with the same name.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">9. What is a&nbsp;ClassLoader&nbsp;in&nbsp;Java?<\/h3>\n\n\n\n<p>A&nbsp;Java&nbsp;ClassLoader&nbsp;is used to load the classes in JRE in a dynamic manner. It is&nbsp;an important component&nbsp;in the Runtime Environment that loads the class into the memory part of the JRE.&nbsp;<\/p>\n\n\n\n<p>It is&nbsp;because of&nbsp;ClassLoaders&nbsp;that the JRE&nbsp;doesn\u2019t&nbsp;have to have information about the files loaded to it.&nbsp;&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">10. What are the&nbsp;different types&nbsp;of&nbsp;ClassLoaders&nbsp;in&nbsp;Java?<\/h3>\n\n\n\n<p>Java uses three built-in&nbsp;ClassLoaders, and the middle one&nbsp;changed&nbsp;in Java 9.&nbsp;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">a) Bootstrap&nbsp;ClassLoader<\/h4>\n\n\n\n<p>Loads the core Java classes. In Java 8 and&nbsp;earlier&nbsp;it loaded classes from&nbsp;rt.jar. From Java 9 onward it loads the core platform modules such as&nbsp;java.base. It is written in native code and is the parent of all other&nbsp;ClassLoaders.&nbsp;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">b) Platform&nbsp;ClassLoader&nbsp;(called the Extension&nbsp;ClassLoader&nbsp;in Java 8 and earlier)<\/h4>\n\n\n\n<p>In Java 8 and earlier, the Extension&nbsp;ClassLoader&nbsp;loaded classes from the JDK extensions directory (jre\/lib\/ext). That directory and the Extension&nbsp;ClassLoader&nbsp;were&nbsp;<strong>removed in Java 9<\/strong>&nbsp;and replaced by the Platform&nbsp;ClassLoader, which loads the Java SE platform modules that are not loaded by the Bootstrap&nbsp;ClassLoader. It is a child of the Bootstrap&nbsp;ClassLoader.&nbsp;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">c) System&nbsp;ClassLoader&nbsp;(also called the Application&nbsp;ClassLoader)<\/h4>\n\n\n\n<p>Loads the application&#8217;s own classes from the&nbsp;classpath&nbsp;or module path. It is a child of the Platform&nbsp;ClassLoader&nbsp;and is the one that loads the code you write.&nbsp;<\/p>\n\n\n\n<p>ClassLoaders&nbsp;follow the&nbsp;delegation model: a request is passed up to the parent first, and only if the parent cannot find the class does the child attempt to load it.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">11. Is it possible to access the members of a subclass if you create a superclass\u2019 object?<\/h3>\n\n\n\n<p>No, it is not possible. A superclass object can access only the members defined in the superclass. Subclass-specific members cannot be accessed through it.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">12. How to define a functional interface in&nbsp;Java?<\/h3>\n\n\n\n<p>A&nbsp;<a href=\"https:\/\/www.wscubetech.com\/resources\/java\/functional-interface\" target=\"_blank\" rel=\"noreferrer noopener\">functional interface in&nbsp;Java<\/a>&nbsp;can be defined using the @FunctionalInterface annotation. Introduced in&nbsp;Java&nbsp;8, it ensures that the interface&nbsp;contains&nbsp;only one abstract method.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">13. How are lambda expressions and functional interfaces&nbsp;interrelated?<\/h3>\n\n\n\n<p>A&nbsp;functional interface&nbsp;is an interface that&nbsp;contains&nbsp;exactly one abstract method. It can be used as the target type for a <a href=\"https:\/\/www.wscubetech.com\/resources\/java\/lambda-expressions\" target=\"_blank\" rel=\"noreferrer noopener\">lambda expression<\/a> or a&nbsp;<a href=\"https:\/\/www.wscubetech.com\/resources\/java\/method-reference\" target=\"_blank\" rel=\"noreferrer noopener\">method&nbsp;reference<\/a>.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>java \n\n@FunctionalInterface \ninterface Calculator { \n    int operate(int a, int b); \n} \n \nCalculator add = (a, b) -&gt; a + b;<\/code><\/pre>\n\n\n\n<p>The&nbsp;@FunctionalInterface&nbsp;annotation is optional.&nbsp;Any interface with a single abstract method is a functional interface&nbsp;whether or not&nbsp;it is annotated. What the annotation does is instruct the compiler to&nbsp;enforce&nbsp;the rule, so if someone later adds a second abstract method, the code&nbsp;fails to&nbsp;compile. This makes it a safety measure rather than a requirement.&nbsp;<\/p>\n\n\n\n<p>Runnable,&nbsp;Comparator, and&nbsp;Callable&nbsp;are all functional interfaces that existed long before Java 8 introduced the annotation.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">14.&nbsp;How many abstract methods can a functional interface have in Java 8?<\/h3>\n\n\n\n<p>A functional interface must have&nbsp;exactly one <a href=\"https:\/\/www.wscubetech.com\/resources\/java\/abstract-class-method\" target=\"_blank\" rel=\"noreferrer noopener\">abstract method<\/a>. That single method defines what the interface does and is what a lambda expression implements.&nbsp;<\/p>\n\n\n\n<p><strong>Alongside that one abstract method, a functional interface may also&nbsp;contain:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Any number of&nbsp;default&nbsp;methods&nbsp;<\/li>\n\n\n\n<li>Any number of&nbsp;static&nbsp;methods&nbsp;<\/li>\n\n\n\n<li>Any number of&nbsp;private&nbsp;methods (Java 9 and later)&nbsp;<\/li>\n<\/ul>\n\n\n\n<p>None of&nbsp;these count&nbsp;towards the single-abstract-method&nbsp;rule, because&nbsp;they all have an implementation.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">15. What rules must be followed when defining a functional interface in Java 8?<\/h3>\n\n\n\n<p><strong>Programmers need to follow these guidelines:<\/strong>&nbsp;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The interface must&nbsp;contain&nbsp;exactly one abstract method. Adding a second one breaks the contract.&nbsp;<\/li>\n\n\n\n<li>The abstract method&nbsp;can have any name. There is no reserved name.&nbsp;<\/li>\n\n\n\n<li>Use the&nbsp;@FunctionalInterface&nbsp;annotation&nbsp;so the compiler enforces the rule. It is optional but strongly recommended.&nbsp;<\/li>\n\n\n\n<li>Default, static, and private methods are allowed and do not count as abstract methods.&nbsp;<\/li>\n\n\n\n<li>Redeclaring a public method of&nbsp;java.lang.Object, such as&nbsp;equals(),&nbsp;hashCode(), or&nbsp;toString(), does&nbsp;not&nbsp;count as an abstract method, because every implementing class already inherits an implementation from&nbsp;Object.&nbsp;<\/li>\n\n\n\n<li>A functional interface may&nbsp;extend&nbsp;another interface, but only if the combined result still has exactly one abstract method.&nbsp;<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">16. What are the&nbsp;different types&nbsp;of functional interfaces in&nbsp;Java&nbsp;8?<\/h3>\n\n\n\n<p><strong>These are the main types of functional interface:<\/strong>&nbsp;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Consumer&nbsp;<\/li>\n\n\n\n<li>Predicate&nbsp;<\/li>\n\n\n\n<li>Supplier&nbsp;<\/li>\n\n\n\n<li>Function (UnaryOperator&nbsp;and&nbsp;BinaryOperator)&nbsp;<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">17. State the biggest difference between Map and&nbsp;FlatMap.<\/h3>\n\n\n\n<p>Both&nbsp;map()&nbsp;and&nbsp;flatMap()&nbsp;transform the elements of a stream, but they differ in how they handle the result.&nbsp;<\/p>\n\n\n\n<p>map()&nbsp;is a one-to-one transformation.&nbsp;It applies a function to each element and produces a stream with the same number of elements. If the function itself&nbsp;returns&nbsp;a stream or a collection, the result becomes a stream of streams, such as&nbsp;Stream&lt;List&lt;String&gt;&gt;, which is usually not what you want.&nbsp;<\/p>\n\n\n\n<p>flatMap()&nbsp;is a one-to-many transformation.&nbsp;It maps each element to a stream and then flattens all those streams into a single stream.&nbsp;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Example:<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>List&lt;List&lt;String&gt;&gt; nested = Arrays.asList( \n    Arrays.asList(\"a\", \"b\"), \n    Arrays.asList(\"c\", \"d\") \n); \n \n\/\/ map produces a nested result \nStream&lt;Stream&lt;String&gt;&gt; mapped = nested.stream().map(List::stream); \n \n\/\/ flatMap produces a single flat stream: a, b, c, d \nList&lt;String&gt; flat = nested.stream() \n                          .flatMap(List::stream) \n                          .collect(Collectors.toList());<\/code><\/pre>\n\n\n\n<p><strong>In short:<\/strong> use&nbsp;map()&nbsp;when each element becomes exactly one new&nbsp;element, and&nbsp;use&nbsp;flatMap()&nbsp;when each element expands into multiple elements that need to be merged into one stream.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">18. What is the primary benefit for which one should use&nbsp;Metaspace&nbsp;over&nbsp;PermGen?<\/h3>\n\n\n\n<p>The main advantage of&nbsp;Metaspace&nbsp;over&nbsp;PermGen&nbsp;is where the memory lives and how it grows.&nbsp;<\/p>\n\n\n\n<p>PermGen&nbsp;was part of the Java heap and had a fixed maximum size set at JVM startup. If an application loaded more classes than that fixed space allowed, it would fail with&nbsp;java.lang.OutOfMemoryError:&nbsp;PermGen&nbsp;space, which was&nbsp;a common problem&nbsp;in applications that loaded many classes or redeployed&nbsp;frequently.&nbsp;<\/p>\n\n\n\n<p>Metaspace, introduced in Java 8, stores class metadata in&nbsp;native memory&nbsp;instead of the heap. It grows automatically as more classes are loaded, so the fixed-ceiling problem&nbsp;largely disappears.&nbsp;<\/p>\n\n\n\n<p>Metaspace&nbsp;is not unlimited, though.&nbsp;It is bounded by the native memory available to the process, and it can be explicitly capped using&nbsp;-XX:MaxMetaspaceSize.&nbsp;-XX:MetaspaceSize&nbsp;sets the&nbsp;initial&nbsp;threshold at which the first garbage collection of class metadata is triggered. If&nbsp;Metaspace&nbsp;is exhausted, the JVM throws&nbsp;OutOfMemoryError:&nbsp;Metaspace.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">19. What is the difference between composition and aggregation in&nbsp;Java?<\/h3>\n\n\n\n<p>Both composition and aggregation are associations in&nbsp;Java. The former is considered a strong association, while the latter is considered a weak association.&nbsp;Let\u2019s&nbsp;understand the differences between them with the below tabular comparison:&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>Aggregation<\/strong>&nbsp;<\/td><td><strong>Composition<\/strong>&nbsp;<\/td><\/tr><tr><td>Weak&nbsp;&nbsp;<\/td><td>Strong&nbsp;<\/td><\/tr><tr><td>There is a relationship between classes&nbsp;<\/td><td>A class belongs to another class&nbsp;<\/td><\/tr><tr><td>Interrelated classes can be independent&nbsp;<\/td><td>Classes are dependent on each other.&nbsp;<\/td><\/tr><tr><td>As the classes can be independent, it is great for reusing the code&nbsp;<\/td><td>As the classes are not independent, code reusability becomes difficult&nbsp;&nbsp;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">20. What is synchronization in&nbsp;Java?<\/h3>\n\n\n\n<p>Synchronization in&nbsp;Java&nbsp;is a mechanism used to control access to shared resources when multiple threads are running simultaneously. It ensures that only one thread can access a synchronized block or method at a time, preventing data inconsistency and thread interference.&nbsp;<\/p>\n\n\n\n<p>Synchronization helps maintain data integrity and enables safe communication between threads in multithreaded applications.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">21. What is the difference between a process and a thread in&nbsp;Java?<\/h3>\n\n\n\n<p>A process and a thread are both units of execution, but they differ in how they use system resources and memory.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>Process<\/strong>&nbsp;<\/td><td><strong>Thread<\/strong>&nbsp;<\/td><\/tr><tr><td>An independent program in execution.&nbsp;<\/td><td>A lightweight unit of execution within a process.&nbsp;<\/td><\/tr><tr><td>Has its own memory space and resources.&nbsp;<\/td><td>Shares memory and resources with other threads in the same process.&nbsp;<\/td><\/tr><tr><td>Creation and execution are&nbsp;relatively slower.&nbsp;<\/td><td>Creation and execution are faster.&nbsp;<\/td><\/tr><tr><td>Communication between processes is more complex.&nbsp;<\/td><td>Threads can communicate easily through shared memory.&nbsp;<\/td><\/tr><tr><td>More resource-intensive.&nbsp;<\/td><td>Less&nbsp;resource-intensive.&nbsp;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">22. What is the difference between String, StringBuilder, and&nbsp;StringBuffer&nbsp;in&nbsp;Java?<\/h3>\n\n\n\n<p><strong>Below are the key differences between String, StringBuilder, and&nbsp;StringBuffer&nbsp;in&nbsp;Java:&nbsp;<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>String<\/strong>&nbsp;<\/td><td><strong>StringBuilder<\/strong>&nbsp;<\/td><td><strong>StringBuffer<\/strong>&nbsp;<\/td><\/tr><tr><td>Immutable (cannot be&nbsp;modified&nbsp;after creation).&nbsp;<\/td><td>Mutable (can be&nbsp;modified).&nbsp;<\/td><td>Mutable (can be&nbsp;modified).&nbsp;<\/td><\/tr><tr><td>Slower when performing frequent string modifications.&nbsp;<\/td><td>Faster than String and&nbsp;StringBuffer.&nbsp;<\/td><td>Slower than StringBuilder due to synchronization.&nbsp;<\/td><\/tr><tr><td>Thread-safe because it is immutable.&nbsp;<\/td><td>Not thread-safe.&nbsp;<\/td><td>Thread-safe (synchronized).&nbsp;<\/td><\/tr><tr><td>Creates a new object for every modification.&nbsp;<\/td><td>Modifies the existing object.&nbsp;<\/td><td>Modifies the existing object.&nbsp;<\/td><\/tr><tr><td>Suitable for fixed string values.&nbsp;<\/td><td>Suitable for single-threaded applications.&nbsp;<\/td><td>Suitable for multi-threaded applications.&nbsp;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">23. What is reflection in&nbsp;Java?<\/h3>\n\n\n\n<p><a href=\"https:\/\/www.wscubetech.com\/resources\/java\/reflection\" target=\"_blank\" rel=\"noreferrer noopener\">Reflection in&nbsp;Java<\/a>&nbsp;is&nbsp;a feature that allows a program to inspect and manipulate classes, methods, fields, and constructors at runtime. It enables developers to examine object details and invoke methods dynamically without knowing their names at compile time.&nbsp;<\/p>\n\n\n\n<p>Reflection is commonly used in frameworks, debugging tools, testing libraries, and dependency injection mechanisms.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">24. What is the use of the transient keyword in&nbsp;Java?<\/h3>\n\n\n\n<p>The transient keyword in&nbsp;Java&nbsp;is used to exclude a variable from the serialization process. When an object is serialized, transient variables are not saved and are ignored by the JVM.&nbsp;<\/p>\n\n\n\n<p>This keyword is commonly used for sensitive or temporary data that should not be stored or transferred during serialization.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">25. What is the difference between Comparable and Comparator in&nbsp;Java?<\/h3>\n\n\n\n<p>Below are the key differences between Comparable and Comparator in&nbsp;Java:&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>Comparable<\/strong><\/td><td><strong>Comparator<\/strong><\/td><\/tr><tr><td>Used to define the natural ordering of objects.&nbsp;<\/td><td>Used to define custom ordering of objects.&nbsp;<\/td><\/tr><tr><td>Implemented using the Comparable interface.&nbsp;<\/td><td>Implemented using the Comparator interface.&nbsp;<\/td><\/tr><tr><td>Contains&nbsp;the&nbsp;compareTo() method.&nbsp;<\/td><td>Contains&nbsp;the&nbsp;compare() method.&nbsp;<\/td><\/tr><tr><td>Sorting logic is defined inside the class.&nbsp;<\/td><td>Sorting logic is defined in a separate class or lambda expression.&nbsp;<\/td><\/tr><tr><td>Allows only one sorting sequence.&nbsp;<\/td><td>Allows multiple sorting sequences.&nbsp;<\/td><\/tr><tr><td><strong>Example:<\/strong>&nbsp;Sort employees&nbsp;by&nbsp;ID.&nbsp;<\/td><td><strong>Example:&nbsp;<\/strong>Sort employees by name, salary, or age.&nbsp;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">26. What is the purpose of the volatile keyword in&nbsp;Java?<\/h3>\n\n\n\n<p>The volatile keyword ensures that a&nbsp;variable&#8217;s&nbsp;value is always read from and written to the main memory.&nbsp;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>It makes changes made by one thread&nbsp;immediately&nbsp;visible to other threads.&nbsp;<\/li>\n\n\n\n<li>It helps prevent data inconsistency and visibility issues in multithreaded applications.&nbsp;<\/li>\n\n\n\n<li>It is commonly used for shared variables accessed by multiple threads.&nbsp;<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">27. What is&nbsp;deadlock&nbsp;in&nbsp;Java?<\/h3>\n\n\n\n<p><a href=\"https:\/\/www.wscubetech.com\/resources\/java\/deadlock\" target=\"_blank\" rel=\"noreferrer noopener\">Deadlock in&nbsp;Java<\/a>&nbsp;is&nbsp;a situation where two or more threads are blocked indefinitely because each thread is waiting for a resource that is held by another thread.&nbsp;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Occurs when multiple threads wait for each other to release resources.&nbsp;<\/li>\n\n\n\n<li>Causes&nbsp;the program to stop making progress.&nbsp;<\/li>\n\n\n\n<li>Commonly happens in multithreaded applications with improper synchronization.&nbsp;<\/li>\n<\/ul>\n\n\n\n<p>Can be prevented by managing resource locks carefully and&nbsp;maintaining&nbsp;a consistent lock order.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">28. What is the difference between shallow copy and deep copy in&nbsp;Java?<\/h3>\n\n\n\n<p>The following table compares shallow copy and deep copy in&nbsp;Java:&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>Shallow Copy<\/strong>&nbsp;<\/td><td><strong>Deep Copy<\/strong>&nbsp;<\/td><\/tr><tr><td>Copies the&nbsp;object&#8217;s&nbsp;fields but shares references to nested objects.&nbsp;<\/td><td>Copies the object and all referenced objects recursively.&nbsp;<\/td><\/tr><tr><td>Changes to referenced objects affect both original and copied objects.&nbsp;<\/td><td>Changes to copied objects do not affect the original object.&nbsp;<\/td><\/tr><tr><td>Faster and&nbsp;uses&nbsp;less memory.&nbsp;<\/td><td>Slower and requires more memory.&nbsp;<\/td><\/tr><tr><td>Creates a partial copy of the object.&nbsp;<\/td><td>Creates a completely independent copy of the object.&nbsp;<\/td><\/tr><tr><td>Commonly implemented using&nbsp;clone() with default behavior.&nbsp;<\/td><td>Requires manual copying of referenced objects.&nbsp;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">29. What is the difference between throw and throws in&nbsp;Java?<\/h3>\n\n\n\n<p>This is also one of the most common&nbsp;Java&nbsp;advanced interview questions, as exception handling is a fundamental concept that every experienced&nbsp;Java&nbsp;developer should understand.&nbsp;<\/p>\n\n\n\n<p>The <a href=\"https:\/\/www.wscubetech.com\/resources\/java\/throw-throws\" target=\"_blank\" rel=\"noreferrer noopener\">throw keyword<\/a> is used to explicitly throw an exception within a method or block of code. On the other hand, the <a href=\"https:\/\/www.wscubetech.com\/resources\/java\/throw-throws\" target=\"_blank\" rel=\"noreferrer noopener\">throws keyword<\/a> is used in a method declaration to specify the exceptions that the method may throw during execution.&nbsp;<\/p>\n\n\n\n<p>In simple terms, throw is used to generate an exception,&nbsp;whereas&nbsp;throws&nbsp;is&nbsp;used to declare and pass the responsibility of handling an exception to the calling method.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">30. What are immutable objects in&nbsp;Java?<\/h3>\n\n\n\n<p>Immutable objects in&nbsp;Java&nbsp;are objects whose state cannot be&nbsp;modified&nbsp;after creation. Once initialized, their data&nbsp;remains&nbsp;unchanged throughout their lifetime, making them thread-safe, secure, and reliable. The String class is a common example of an immutable object.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">31. What is autoboxing and unboxing in&nbsp;Java?<\/h3>\n\n\n\n<p><a href=\"https:\/\/www.wscubetech.com\/resources\/java\/autoboxing-unboxing\" target=\"_blank\" rel=\"noreferrer noopener\">Autoboxing<\/a> is the automatic conversion of a primitive data type into its corresponding wrapper class object, while <a href=\"https:\/\/www.wscubetech.com\/resources\/java\/autoboxing-unboxing\" target=\"_blank\" rel=\"noreferrer noopener\">unboxing<\/a> is the automatic conversion of a wrapper class object back to its primitive data type. These features simplify data handling and reduce the need for manual conversions in&nbsp;Java.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">32. What is the difference between List, Set, and Map in&nbsp;Java?<\/h3>\n\n\n\n<p>The table below highlights the key differences between List, Set, and Map in&nbsp;Java:&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong><a href=\"https:\/\/www.wscubetech.com\/resources\/java\/list\" target=\"_blank\" rel=\"noreferrer noopener\">List<\/a><\/strong><\/td><td><strong><a href=\"https:\/\/www.wscubetech.com\/resources\/java\/set\" target=\"_blank\" rel=\"noreferrer noopener\">Set<\/a><\/strong><\/td><td><strong><a href=\"https:\/\/www.wscubetech.com\/resources\/java\/map\" target=\"_blank\" rel=\"noreferrer noopener\">Map<\/a><\/strong><\/td><\/tr><tr><td>Stores&nbsp;elements in an ordered sequence.&nbsp;<\/td><td>Stores&nbsp;unique elements only.&nbsp;<\/td><td>Stores data as&nbsp;key-value&nbsp;pairs.&nbsp;<\/td><\/tr><tr><td>Allows duplicate elements.&nbsp;<\/td><td>Does not allow duplicate elements.&nbsp;<\/td><td>Keys must be unique, but values can be duplicated.&nbsp;<\/td><\/tr><tr><td>Maintains insertion order.&nbsp;<\/td><td>May or may not&nbsp;maintain&nbsp;insertion order, depending on the implementation.&nbsp;<\/td><td>Stores&nbsp;mappings between keys and values.&nbsp;<\/td><\/tr><tr><td>Elements are accessed by index.&nbsp;<\/td><td>Elements are accessed through iteration.&nbsp;<\/td><td>Values are accessed using keys.&nbsp;<\/td><\/tr><tr><td><strong>Examples:&nbsp;<\/strong>ArrayList, LinkedList, Vector.&nbsp;<\/td><td><strong>Examples:&nbsp;<\/strong>HashSet,&nbsp;LinkedHashSet,&nbsp;TreeSet.&nbsp;<\/td><td><strong>Examples:<\/strong>&nbsp;HashMap,&nbsp;LinkedHashMap,&nbsp;TreeMap.&nbsp;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">33. What is the difference between&nbsp;ArrayList&nbsp;and LinkedList in&nbsp;Java?<\/h3>\n\n\n\n<p><strong>The table below highlights the key differences between&nbsp;ArrayList&nbsp;and LinkedList in&nbsp;Java:&nbsp;<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>ArrayList<\/strong><\/td><td><strong>LinkedList<\/strong><\/td><\/tr><tr><td>Uses a dynamic array to store elements.&nbsp;<\/td><td>Uses&nbsp;a doubly linked list to store elements.&nbsp;<\/td><\/tr><tr><td>Faster for accessing elements by index.&nbsp;<\/td><td>Slower for random access.&nbsp;<\/td><\/tr><tr><td>Insertion and deletion are slower, especially in the middle of the list.&nbsp;<\/td><td>Insertion and deletion are faster.&nbsp;<\/td><\/tr><tr><td>Requires less memory overhead.&nbsp;<\/td><td>Requires more memory to store node links.&nbsp;<\/td><\/tr><tr><td>Better for frequent read operations.&nbsp;<\/td><td>Better for frequent insert and&nbsp;delete&nbsp;operations.&nbsp;<\/td><\/tr><tr><td>Implements the List interface.&nbsp;<\/td><td>Implements both List and Deque interfaces.&nbsp;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">34. What is the difference between HashSet and&nbsp;TreeSet&nbsp;in&nbsp;Java?<\/h3>\n\n\n\n<p><strong>The table below highlights the key differences between HashSet and&nbsp;TreeSet&nbsp;in&nbsp;Java:&nbsp;<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>HashSet<\/strong><\/td><td><strong>TreeSet<\/strong><\/td><\/tr><tr><td>Stores&nbsp;elements using a hash table.&nbsp;<\/td><td>Stores&nbsp;elements using a tree structure (Red-Black Tree).&nbsp;<\/td><\/tr><tr><td>Does not&nbsp;maintain&nbsp;any order of elements.&nbsp;<\/td><td>Stores&nbsp;elements in sorted order.&nbsp;<\/td><\/tr><tr><td>Provides faster insertion, deletion, and search operations.&nbsp;<\/td><td>Generally slower&nbsp;due to sorting operations.&nbsp;<\/td><\/tr><tr><td>Allows one null element.&nbsp;<\/td><td>Does not allow null elements.&nbsp;<\/td><\/tr><tr><td>Suitable when ordering is not&nbsp;required.&nbsp;<\/td><td>Suitable when sorted data is&nbsp;required.&nbsp;<\/td><\/tr><tr><td>Implements the Set interface.&nbsp;<\/td><td>Implements the&nbsp;NavigableSet&nbsp;and&nbsp;SortedSet&nbsp;interfaces.&nbsp;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">36. What is the difference between Runnable interface and Thread class in&nbsp;Java?<\/h3>\n\n\n\n<p><strong>The table below highlights the key differences between the Runnable interface and the Thread class in&nbsp;Java:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>Runnable Interface<\/strong><\/td><td><strong>Thread Class<\/strong><\/td><\/tr><tr><td>Defined as an interface.&nbsp;<\/td><td>Defined as a class.&nbsp;<\/td><\/tr><tr><td>Requires implementing the&nbsp;run() method.&nbsp;<\/td><td>Requires extending the Thread class and overriding the&nbsp;run() method.&nbsp;<\/td><\/tr><tr><td>Supports multiple inheritance because a class can implement multiple interfaces.&nbsp;<\/td><td>Does not support multiple inheritance because&nbsp;Java&nbsp;classes can extend only one class.&nbsp;<\/td><\/tr><tr><td>Separates the task from the thread execution mechanism.&nbsp;<\/td><td>Combines both the task and thread execution mechanism.&nbsp;<\/td><\/tr><tr><td>Preferred for better code flexibility and reusability.&nbsp;<\/td><td>Less flexible due to class inheritance limitations.&nbsp;<\/td><\/tr><tr><td>Executed by passing the object to a Thread instance.&nbsp;<\/td><td>Executed by creating and starting a subclass of Thread.&nbsp;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">37. What is a wrapper class in&nbsp;Java?<\/h3>\n\n\n\n<p>A&nbsp;<a href=\"https:\/\/www.wscubetech.com\/resources\/java\/wrapper-class\" target=\"_blank\" rel=\"noreferrer noopener\">wrapper class in&nbsp;Java<\/a>&nbsp;is a class that converts a primitive data type into an object. Each primitive type has a corresponding wrapper class, such as Integer for int, Double for double, Character for char, and Boolean for&nbsp;boolean.&nbsp;<\/p>\n\n\n\n<p>Wrapper classes are useful when working with collections, generics, and APIs that&nbsp;require&nbsp;objects instead of primitive data types.&nbsp;<\/p>\n\n\n\n<p><em><strong>Note:<\/strong>&nbsp;Once you have gone through these advanced topics, it is worth checking how much you actually remember.&nbsp;Try&nbsp;our&nbsp;<a href=\"https:\/\/www.wscubetech.com\/resources\/java\/quiz\" target=\"_blank\" rel=\"noreferrer noopener\">Java quiz<\/a>&nbsp;to&nbsp;identify&nbsp;the areas that still need revision before your interview.<\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Java Coding Interview Questions<\/h2>\n\n\n\n<p>Almost every&nbsp;Java interview includes at least one coding round. The questions below are the ones that come up most often, along with the approach interviewers expect. For a wider set of practice problems, go through our collection of&nbsp;<a href=\"https:\/\/www.wscubetech.com\/resources\/java\/programs\" target=\"_blank\" rel=\"noreferrer noopener\">Java programs<\/a>&nbsp;covering strings, arrays, patterns, and collections.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. Write a Java program to reverse a string without using the reverse method<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public class ReverseString { \n    public static String reverse(String str) { \n        char&#091;] chars = str.toCharArray(); \n        int left = 0, right = chars.length - 1; \n        while (left &lt; right) { \n            char temp = chars&#091;left]; \n            chars&#091;left++] = chars&#091;right]; \n            chars&#091;right--] = temp; \n        } \n        return new String(chars); \n    } \n}<\/code><\/pre>\n\n\n\n<p>Time complexity O(n), space complexity O(n).&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. Write a Java program to check whether a string is a palindrome<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public static boolean isPalindrome(String str) { \n    int left = 0, right = str.length() - 1; \n    while (left &lt; right) { \n        if (str.charAt(left++) != str.charAt(right--)) { \n            return false; \n        } \n    } \n    return true; \n}<\/code><\/pre>\n\n\n\n<p>Using two pointers avoids creating a reversed copy, so space complexity is&nbsp;O(1).&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. Write a&nbsp;Java&nbsp;program to check whether two strings are anagrams<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public static boolean isAnagram(String a, String b) { \n    if (a.length() != b.length()) return false; \n    int&#091;] count = new int&#091;256]; \n    for (int i = 0; i &lt; a.length(); i++) { \n        count&#091;a.charAt(i)]++; \n        count&#091;b.charAt(i)]--; \n    } \n    for (int c : count) { \n        if (c != 0) return false; \n    } \n    return true; \n}<\/code><\/pre>\n\n\n\n<p>The counting approach runs in O(n). Sorting both strings also works but is&nbsp;O(n log n).&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4. Write a Java&nbsp;program&nbsp;to find the first non-repeating character in a string<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public static char firstNonRepeating(String str) { \n    Map&lt;Character, Integer&gt; counts = new LinkedHashMap&lt;&gt;(); \n    for (char c : str.toCharArray()) { \n        counts.put(c, counts.getOrDefault(c, 0) + 1); \n    } \n    for (Map.Entry&lt;Character, Integer&gt; e : counts.entrySet()) { \n        if (e.getValue() == 1) return e.getKey(); \n    } \n    return '\\0'; \n}<\/code><\/pre>\n\n\n\n<p>LinkedHashMap&nbsp;is used because insertion order must be preserved to find the&nbsp;<em>first<\/em>&nbsp;such character.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5. Write a Java program to find the missing number in an array from 1 to n<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public static int findMissing(int&#091;] arr) { \n    int n = arr.length + 1; \n    int expectedSum = n * (n + 1) \/ 2; \n    int actualSum = 0; \n    for (int num : arr) actualSum += num; \n    return expectedSum - actualSum; \n}<\/code><\/pre>\n\n\n\n<p>Runs in O(n) time and&nbsp;O(1) space, which is what&nbsp;the interviewer is checking for.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">6. Write a Java program to remove duplicates from a list<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Using Set \nList&lt;Integer&gt; unique = new ArrayList&lt;&gt;(new LinkedHashSet&lt;&gt;(list)); \n \n\/\/ Using Streams \nList&lt;Integer&gt; unique = list.stream() \n                           .distinct() \n                           .collect(Collectors.toList());<\/code><\/pre>\n\n\n\n<p>LinkedHashSet&nbsp;preserves insertion order,&nbsp;whereas&nbsp;HashSet&nbsp;does not.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">7. Write a Java program to count the occurrences of each character in a string<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Map&lt;Character, Long&gt; counts = str.chars() \n    .mapToObj(c -&gt; (char) c) \n    .collect(Collectors.groupingBy(c -&gt; c, Collectors.counting()));<\/code><\/pre>\n\n\n\n<p>Being able to give both a loop-based and a Streams-based solution is what distinguishes a strong candidate here.&nbsp;<\/p>\n\n\n\n<p><em><strong>Note:<\/strong>&nbsp;Solving problems under time pressure is a different skill from writing code at your own pace. Working through structured&nbsp;<a href=\"https:\/\/www.wscubetech.com\/challenges\/java\" target=\"_blank\" rel=\"noreferrer noopener\">Java coding challenges<\/a>&nbsp;is the closest practice you can get to an actual interview round.<\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">List of&nbsp;Java&nbsp;8 Interview Questions<\/h2>\n\n\n\n<p>Java&nbsp;8 is among the newest versions of this programming language.&nbsp;The&nbsp;<a href=\"https:\/\/www.wscubetech.com\/blog\/java-8-interview-questions\/\" target=\"_blank\" rel=\"noreferrer noopener\">Java&nbsp;8 interview questions<\/a>&nbsp;can be asked to a candidate with any experience level. Undoubtedly, the level of questions will be according to your experience range.&nbsp;<\/p>\n\n\n\n<p><strong>Here are some of the most common interview questions on&nbsp;Java&nbsp;8:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>What are the new features in&nbsp;Java&nbsp;SE 8?<\/li>\n\n\n\n<li>What are some of the main benefits of using&nbsp;Java&nbsp;8?<\/li>\n\n\n\n<li>Define optional&nbsp;class.&nbsp;<\/li>\n\n\n\n<li>What is a functional interface in&nbsp;Java&nbsp;8?&nbsp;<\/li>\n\n\n\n<li>Define&nbsp;MetaSpace.&nbsp;<\/li>\n\n\n\n<li>What is the meaning of the&nbsp;String::ValueOf&nbsp;expression?&nbsp;<\/li>\n\n\n\n<li>Explain the concept of streams in&nbsp;Java&nbsp;8.&nbsp;<\/li>\n\n\n\n<li>What is&nbsp;Nashorn&nbsp;in&nbsp;Java&nbsp;8?&nbsp;<\/li>\n\n\n\n<li>How is&nbsp;MetaSpace&nbsp;different from&nbsp;PermGen?&nbsp;<\/li>\n\n\n\n<li>What do you mean by method reference?&nbsp;<\/li>\n\n\n\n<li>Explain intermediate and terminal operations.&nbsp;<\/li>\n\n\n\n<li>Which are the most used terminal operations?&nbsp;<\/li>\n\n\n\n<li>Is it possible for a functional interface to inherit another interface?&nbsp;<\/li>\n\n\n\n<li>What is the difference between&nbsp;findFirst() and&nbsp;findAny()?&nbsp;<\/li>\n\n\n\n<li>Which are the key components of a&nbsp;Java&nbsp;stream?&nbsp;<\/li>\n\n\n\n<li>Which functional interfaces come pre-defined in&nbsp;Java&nbsp;8?&nbsp;<\/li>\n\n\n\n<li>What does&nbsp;type&nbsp;interface mean?&nbsp;<\/li>\n\n\n\n<li>State the syntax of a lambda expression.&nbsp;<\/li>\n\n\n\n<li>What is the difference between collection and stream?&nbsp;<\/li>\n\n\n\n<li>Explain the role of JJS in&nbsp;Java&nbsp;8.&nbsp;<\/li>\n<\/ul>\n\n\n\n<p>Find&nbsp;<a href=\"https:\/\/www.wscubetech.com\/blog\/java-8-interview-questions\/\" target=\"_blank\" rel=\"noreferrer noopener\">Java&nbsp;8 interview questions and answers<\/a>&nbsp;by clicking on the linked write-up&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Practice Java Programs for Interview Preparation<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.wscubetech.com\/resources\/java\/programs\/hello-world\" target=\"_blank\" rel=\"noreferrer noopener\">Java Hello World Program<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.wscubetech.com\/resources\/java\/programs\/print-integer\" target=\"_blank\" rel=\"noreferrer noopener\">Java Program to Print an Integer<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.wscubetech.com\/resources\/java\/programs\/add-two-numbers\" target=\"_blank\" rel=\"noreferrer noopener\">Java Program to Add Two Numbers<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.wscubetech.com\/resources\/java\/programs\/even-odd\" target=\"_blank\" rel=\"noreferrer noopener\">Even or Odd Program in Java<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.wscubetech.com\/resources\/java\/programs\/leap-year\" target=\"_blank\" rel=\"noreferrer noopener\">Java Program to Check Leap Year<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.wscubetech.com\/resources\/java\/programs\/multiplication-table\" target=\"_blank\" rel=\"noreferrer noopener\">Multiplication Table in Java<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.wscubetech.com\/resources\/java\/programs\/km-to-mile\" target=\"_blank\" rel=\"noreferrer noopener\">Java Program to Convert Kilometers to Miles<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.wscubetech.com\/resources\/java\/programs\/star-pattern\" target=\"_blank\" rel=\"noreferrer noopener\">Star Pattern in Java<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.wscubetech.com\/resources\/java\/programs\/palindrome\" target=\"_blank\" rel=\"noreferrer noopener\">Palindrome Program in Java<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.wscubetech.com\/resources\/java\/programs\/power-number\" target=\"_blank\" rel=\"noreferrer noopener\">Power of a Number in Java<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.wscubetech.com\/resources\/java\/programs\/square-root-number\" target=\"_blank\" rel=\"noreferrer noopener\">Java Program to Find Square Root of a Number<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.wscubetech.com\/resources\/java\/programs\/swap-two-numbers\" target=\"_blank\" rel=\"noreferrer noopener\">Swap Two Numbers in Java<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.wscubetech.com\/resources\/java\/programs\/ascii-value-character\" target=\"_blank\" rel=\"noreferrer noopener\">Java Program to Find ASCII Value of a character<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.wscubetech.com\/resources\/java\/programs\/alphabet\" target=\"_blank\" rel=\"noreferrer noopener\">Java Program to Check Character is Alphabet or Not<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.wscubetech.com\/resources\/java\/programs\/vowel-consonant\" target=\"_blank\" rel=\"noreferrer noopener\">Vowel or Consonant Program in Java<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.wscubetech.com\/resources\/java\/programs\/neon-number\" target=\"_blank\" rel=\"noreferrer noopener\">Neon Number in Java<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.wscubetech.com\/resources\/java\/programs\/lucky-number\" target=\"_blank\" rel=\"noreferrer noopener\">Lucky Number Program in Java<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.wscubetech.com\/resources\/java\/programs\/happy-number\" target=\"_blank\" rel=\"noreferrer noopener\">Happy Number in Java<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.wscubetech.com\/resources\/java\/programs\/unique-number\" target=\"_blank\" rel=\"noreferrer noopener\">Unique Number in Java<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.wscubetech.com\/resources\/java\/programs\/triangular-number\" target=\"_blank\" rel=\"noreferrer noopener\">Triangular Number in Java<\/a><\/li>\n<\/ul>\n\n\n\n<p>For more Java Programs to&nbsp;practice,&nbsp;visit our&nbsp;<a href=\"https:\/\/www.wscubetech.com\/resources\/java\/programs\" target=\"_blank\" rel=\"noreferrer noopener\">Java Programs<\/a>&nbsp;page.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java&nbsp;remains&nbsp;one of the most widely used&nbsp;programming languages&nbsp;for building web, mobile, and enterprise applications. Its platform independence, reliability, and strong community support make it a preferred choice for developers worldwide.&nbsp; With companies hiring Java developers across backend, Android, and cloud roles, a clear understanding of core concepts and coding skills matters more than ever.&nbsp;Practising&nbsp;Java interview questions [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":23160,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[226,68],"tags":[],"class_list":["post-2288","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-interview-questions","category-web-development"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>155+ Java Interview Questions and Answers (2026 Updated)<\/title>\n<meta name=\"description\" content=\"Master 155+ Java interview questions and answers for freshers &amp; experienced developers in 2026. Crack top Java interviews with confidence. Read now!\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wscubetech.com\/blog\/java-interview-questions-answers\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"155+ Java Interview Questions and Answers (2026 Updated)\" \/>\n<meta property=\"og:description\" content=\"Master 155+ Java interview questions and answers for freshers &amp; experienced developers in 2026. Crack top Java interviews with confidence. Read now!\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wscubetech.com\/blog\/java-interview-questions-answers\/\" \/>\n<meta property=\"og:site_name\" content=\"WsCube Tech Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/wscubetech.india\" \/>\n<meta property=\"article:published_time\" content=\"2026-08-12T12:43:40+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-08-12T12:57:02+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wscubetech.com\/blog\/wp-content\/uploads\/2026\/08\/java-interview-question-answers.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1654\" \/>\n\t<meta property=\"og:image:height\" content=\"800\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"Virendra Soni\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@wscube\" \/>\n<meta name=\"twitter:site\" content=\"@wscube\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Virendra Soni\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"59 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wscubetech.com\/blog\/java-interview-questions-answers\/\",\"url\":\"https:\/\/www.wscubetech.com\/blog\/java-interview-questions-answers\/\",\"name\":\"155+ Java Interview Questions and Answers (2026 Updated)\",\"isPartOf\":{\"@id\":\"https:\/\/www.wscubetech.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.wscubetech.com\/blog\/java-interview-questions-answers\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.wscubetech.com\/blog\/java-interview-questions-answers\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.wscubetech.com\/blog\/wp-content\/uploads\/2026\/08\/java-interview-question-answers.webp\",\"datePublished\":\"2026-08-12T12:43:40+00:00\",\"dateModified\":\"2026-08-12T12:57:02+00:00\",\"author\":{\"@id\":\"https:\/\/www.wscubetech.com\/blog\/#\/schema\/person\/86f20800856ef0446a922403e609345e\"},\"description\":\"Master 155+ Java interview questions and answers for freshers & experienced developers in 2026. Crack top Java interviews with confidence. Read now!\",\"breadcrumb\":{\"@id\":\"https:\/\/www.wscubetech.com\/blog\/java-interview-questions-answers\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wscubetech.com\/blog\/java-interview-questions-answers\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.wscubetech.com\/blog\/java-interview-questions-answers\/#primaryimage\",\"url\":\"https:\/\/www.wscubetech.com\/blog\/wp-content\/uploads\/2026\/08\/java-interview-question-answers.webp\",\"contentUrl\":\"https:\/\/www.wscubetech.com\/blog\/wp-content\/uploads\/2026\/08\/java-interview-question-answers.webp\",\"width\":1654,\"height\":800,\"caption\":\"155+ Java Interview Questions and Answers (Updated)\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wscubetech.com\/blog\/java-interview-questions-answers\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.wscubetech.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"155+\u00a0Java\u00a0Interview Questions and Answers (2026 Updated)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wscubetech.com\/blog\/#website\",\"url\":\"https:\/\/www.wscubetech.com\/blog\/\",\"name\":\"WsCube Tech Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wscubetech.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wscubetech.com\/blog\/#\/schema\/person\/86f20800856ef0446a922403e609345e\",\"name\":\"Virendra Soni\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.wscubetech.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/0b175c5ff6bbbe5689ba772754de747e391d35e730ac7d25b89036db59128d5b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/0b175c5ff6bbbe5689ba772754de747e391d35e730ac7d25b89036db59128d5b?s=96&d=mm&r=g\",\"caption\":\"Virendra Soni\"},\"description\":\"Virendra is an SEO Content Writer with 7+ years of experience in blogging, content marketing, SEO, and content editing. He holds a B.Tech. in Electronics and Communication Engineering (ECE) and has worked across the IT and EdTech industries. Virendra specializes in creating SEO-friendly, user-focused content that drives organic traffic and improves search rankings. His mantra is simple: keep it clear, make it memorable, and create content that both readers and search engines love.\",\"sameAs\":[\"https:\/\/www.linkedin.com\/in\/virendra-soni\/\"],\"url\":\"https:\/\/www.wscubetech.com\/blog\/author\/virendra\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"155+ Java Interview Questions and Answers (2026 Updated)","description":"Master 155+ Java interview questions and answers for freshers & experienced developers in 2026. Crack top Java interviews with confidence. Read now!","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wscubetech.com\/blog\/java-interview-questions-answers\/","og_locale":"en_US","og_type":"article","og_title":"155+ Java Interview Questions and Answers (2026 Updated)","og_description":"Master 155+ Java interview questions and answers for freshers & experienced developers in 2026. Crack top Java interviews with confidence. Read now!","og_url":"https:\/\/www.wscubetech.com\/blog\/java-interview-questions-answers\/","og_site_name":"WsCube Tech Blog","article_publisher":"https:\/\/www.facebook.com\/wscubetech.india","article_published_time":"2026-08-12T12:43:40+00:00","article_modified_time":"2026-08-12T12:57:02+00:00","og_image":[{"width":1654,"height":800,"url":"https:\/\/www.wscubetech.com\/blog\/wp-content\/uploads\/2026\/08\/java-interview-question-answers.webp","type":"image\/webp"}],"author":"Virendra Soni","twitter_card":"summary_large_image","twitter_creator":"@wscube","twitter_site":"@wscube","twitter_misc":{"Written by":"Virendra Soni","Est. reading time":"59 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wscubetech.com\/blog\/java-interview-questions-answers\/","url":"https:\/\/www.wscubetech.com\/blog\/java-interview-questions-answers\/","name":"155+ Java Interview Questions and Answers (2026 Updated)","isPartOf":{"@id":"https:\/\/www.wscubetech.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.wscubetech.com\/blog\/java-interview-questions-answers\/#primaryimage"},"image":{"@id":"https:\/\/www.wscubetech.com\/blog\/java-interview-questions-answers\/#primaryimage"},"thumbnailUrl":"https:\/\/www.wscubetech.com\/blog\/wp-content\/uploads\/2026\/08\/java-interview-question-answers.webp","datePublished":"2026-08-12T12:43:40+00:00","dateModified":"2026-08-12T12:57:02+00:00","author":{"@id":"https:\/\/www.wscubetech.com\/blog\/#\/schema\/person\/86f20800856ef0446a922403e609345e"},"description":"Master 155+ Java interview questions and answers for freshers & experienced developers in 2026. Crack top Java interviews with confidence. Read now!","breadcrumb":{"@id":"https:\/\/www.wscubetech.com\/blog\/java-interview-questions-answers\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wscubetech.com\/blog\/java-interview-questions-answers\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.wscubetech.com\/blog\/java-interview-questions-answers\/#primaryimage","url":"https:\/\/www.wscubetech.com\/blog\/wp-content\/uploads\/2026\/08\/java-interview-question-answers.webp","contentUrl":"https:\/\/www.wscubetech.com\/blog\/wp-content\/uploads\/2026\/08\/java-interview-question-answers.webp","width":1654,"height":800,"caption":"155+ Java Interview Questions and Answers (Updated)"},{"@type":"BreadcrumbList","@id":"https:\/\/www.wscubetech.com\/blog\/java-interview-questions-answers\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.wscubetech.com\/blog\/"},{"@type":"ListItem","position":2,"name":"155+\u00a0Java\u00a0Interview Questions and Answers (2026 Updated)"}]},{"@type":"WebSite","@id":"https:\/\/www.wscubetech.com\/blog\/#website","url":"https:\/\/www.wscubetech.com\/blog\/","name":"WsCube Tech Blog","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wscubetech.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.wscubetech.com\/blog\/#\/schema\/person\/86f20800856ef0446a922403e609345e","name":"Virendra Soni","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.wscubetech.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/0b175c5ff6bbbe5689ba772754de747e391d35e730ac7d25b89036db59128d5b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/0b175c5ff6bbbe5689ba772754de747e391d35e730ac7d25b89036db59128d5b?s=96&d=mm&r=g","caption":"Virendra Soni"},"description":"Virendra is an SEO Content Writer with 7+ years of experience in blogging, content marketing, SEO, and content editing. He holds a B.Tech. in Electronics and Communication Engineering (ECE) and has worked across the IT and EdTech industries. Virendra specializes in creating SEO-friendly, user-focused content that drives organic traffic and improves search rankings. His mantra is simple: keep it clear, make it memorable, and create content that both readers and search engines love.","sameAs":["https:\/\/www.linkedin.com\/in\/virendra-soni\/"],"url":"https:\/\/www.wscubetech.com\/blog\/author\/virendra\/"}]}},"_links":{"self":[{"href":"https:\/\/www.wscubetech.com\/blog\/wp-json\/wp\/v2\/posts\/2288","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wscubetech.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wscubetech.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wscubetech.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wscubetech.com\/blog\/wp-json\/wp\/v2\/comments?post=2288"}],"version-history":[{"count":68,"href":"https:\/\/www.wscubetech.com\/blog\/wp-json\/wp\/v2\/posts\/2288\/revisions"}],"predecessor-version":[{"id":23164,"href":"https:\/\/www.wscubetech.com\/blog\/wp-json\/wp\/v2\/posts\/2288\/revisions\/23164"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wscubetech.com\/blog\/wp-json\/wp\/v2\/media\/23160"}],"wp:attachment":[{"href":"https:\/\/www.wscubetech.com\/blog\/wp-json\/wp\/v2\/media?parent=2288"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wscubetech.com\/blog\/wp-json\/wp\/v2\/categories?post=2288"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wscubetech.com\/blog\/wp-json\/wp\/v2\/tags?post=2288"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}