{"id":10057,"date":"2023-04-29T11:43:54","date_gmt":"2023-04-29T11:43:54","guid":{"rendered":"https:\/\/www.scmgalaxy.com\/tutorials\/?p=10057"},"modified":"2023-06-19T13:30:24","modified_gmt":"2023-06-19T13:30:24","slug":"top-50-java-interview-questions-with-answers","status":"publish","type":"post","link":"https:\/\/www.scmgalaxy.com\/tutorials\/top-50-java-interview-questions-with-answers\/","title":{"rendered":"Top 50 Java Interview Questions with Answers"},"content":{"rendered":"<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.scmgalaxy.com\/tutorials\/wp-content\/uploads\/2023\/04\/image-17.png\" alt=\"\" class=\"wp-image-10058\" width=\"716\" height=\"358\" srcset=\"https:\/\/www.scmgalaxy.com\/tutorials\/wp-content\/uploads\/2023\/04\/image-17.png 800w, https:\/\/www.scmgalaxy.com\/tutorials\/wp-content\/uploads\/2023\/04\/image-17-300x150.png 300w, https:\/\/www.scmgalaxy.com\/tutorials\/wp-content\/uploads\/2023\/04\/image-17-768x384.png 768w\" sizes=\"auto, (max-width: 716px) 100vw, 716px\" \/><figcaption class=\"wp-element-caption\"><strong><em>Java Interview Questions with Answers<\/em><\/strong><\/figcaption><\/figure>\n<\/div>\n\n\n<h3 class=\"wp-block-heading\">1. What is the output of the following code?<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nint x = 5;\nSystem.out.println(++x);\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">a) 5<br>b) 6<br>c) Compilation error<br>d) Runtime error<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: b) 6<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">2. Which keyword is used to prevent a member variable from being serialized?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">a) transient<br>b) static<br>c) final<br>d) volatile<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: a) transient<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">3. What is the output of the following code?<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nString str1 = &quot;hello&quot;;\nString str2 = &quot;hello&quot;;\nSystem.out.println(str1 == str2);\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">a) true<br>b) false<br>c) Compilation error<br>d) Runtime error<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: a) true<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">4. What is an abstract class?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><br>a) A class that cannot be instantiated<br>b) A class that can only be instantiated<br>c) A class that is final<br>d) A class with no constructor<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: a) A class that cannot be instantiated<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">5. What is polymorphism?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">a) The ability of an object to take on many forms<br>b) The ability of a method to be defined in multiple classes<br>c) The ability of a variable to be of multiple types<br>d) The ability of a class to be inherited by multiple classes<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: a) The ability of an object to take on many forms<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">6. Which keyword is used to create a new instance of a class?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">a) new<br>b) this<br>c) super<br>d) instance<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: a) new<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">7. What is the output of the following code?<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nint&#x5B;] nums = {1, 2, 3};\nfor (int i = 0; i &amp;lt; nums.length; i++) {\nSystem.out.println(nums&#x5B;i]);\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">a) 1 2 3<br>b) 1<br>2<br>3<br>c) Compilation error<br>d) Runtime error<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: b) 1 2 3<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">8. Which of the following is not a primitive data type in Java?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">a) int<br>b) float<br>c) char<br>d) String<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: d) String<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">9. What is inheritance?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">a) The ability of a class to implement an interface<br>b) The ability of a class to contain an object of another class<br>c) The ability of a class to be derived from another class<br>d) The ability of a class to have multiple constructors<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: c) The ability of a class to be derived from another class<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">10. What is the output of the following code?<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nString str = &quot;hello&quot;;\nSystem.out.println(str.substring(1, 4));\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">a) h<br>b) el<br>c) ell<br>d) llo<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: c) ell<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">11. Which keyword is used to exit a for loop?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">a) exit<br>b) break<br>c) continue<br>d) return<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: b) break<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">12. What is the output of the following code?<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nString str = &quot;hello&quot;;\nfor (int i = str.length() - 1; i &amp;gt;= 0; i--) {\nSystem.out.print(str.charAt(i));\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">a) hello<br>b) olleh<br>c) hlo<br>d) Compilation error<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: b) olleh<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">13. Which keyword is used to create an interface?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">a) interface<br>b) extends<br>c) implements<br>d) abstract<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: a) interface<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">14. What is encapsulation?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">a) The ability of a class to be extended by another class<br>b) The ability of a class to implement an interface<br>c) The ability of a class to contain an object of another class<br>d) The ability of a class to hide its implementation details<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: d) The ability of a class to hide its implementation details<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">15. What is the output of the following code?<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nint x = 6;\nif (x &amp;lt; 5) {\nSystem.out.println(&quot;Less than 5&quot;);\n} else if (x &amp;lt; 10) {\nSystem.out.println(&quot;Between 5 and 10&quot;);\n} else {\nSystem.out.println(&quot;Greater than or equal to 10&quot;);\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">a) Less than 5<br>b) Between 5 and 10<br>c) Greater than or equal to 10<br>d) Compilation error<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: b) Between 5 and 10<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">16. Which keyword is used to access a static method or variable?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">a) static<br>b) private<br>c) public<br>d) this<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: a) static<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">17. What is a constructor?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">a) A method used to create an object<br>b) A method used to destroy an object<br>c) A method used to access an object&#8217;s properties<br>d) A method used to declare an object&#8217;s class<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: a) A method used to create an object<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">18. What is the output of the following code?<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nint x = 2;\nswitch(x) {\ncase 1:\nSystem.out.println(&quot;One&quot;);\nbreak;\ncase 2:\nSystem.out.println(&quot;Two&quot;);\nbreak;\ndefault:\nSystem.out.println(&quot;Default&quot;);\nbreak;\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">a) One<br>b) Two<br>c) Default<br>d) Compilation error<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: b) Two<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">19. Which keyword is used to access a parent class&#8217;s method or variable?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">a) extends<br>b) super<br>c) this<br>d) implements<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: b) super<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">20. What is the output of the following code?<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nint x = 2;\nwhile (x &amp;lt; 5) {\nSystem.out.println(x);\nx++;\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">a) 2 3 4<br>b) 2 3 4 5<br>c) 3 4<br>d) 3 4 5<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: a) 2 3 4<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">21. Which keyword is used to declare an abstract method?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">a) abstract<br>b) final<br>c) static<br>d) private<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: a) abstract<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">22. What is the output of the following code?<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nString str = &quot;hello&quot;;\nSystem.out.println(str.indexOf(&quot;l&quot;));\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">a) 2<br>b) 3<br>c) 4<br>d) -1<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: b) 3<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">23. Which keyword is used to create a constant variable?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">a) final<br>b) static<br>c) volatile<br>d) transient<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: a) final<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">24. What is the output of the following code?<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nint x = 5;\nif (x &amp;gt; 3 &amp;amp;&amp;amp; x &amp;lt; 7) {\nSystem.out.println(&quot;In range&quot;);\n} else {\nSystem.out.println(&quot;Out of range&quot;);\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">a) In range<br>b) Out of range<br>c) Compilation error<br>d) Runtime error<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: a) In range<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">25. What is a static method?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">a) A method that can be accessed without creating an object of the class<br>b) A method that can only be accessed by objects of the class<br>c) A method that is final<br>d) A method that is abstract<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: a) A method that can be accessed without creating an object of the class<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">26. What is the output of the following code?<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nint x = 10;\ndo {\nSystem.out.println(x);\nx--;\n} while (x &amp;gt; 5);\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">a) 10 9 8 7 6 5<br>b) 10 9 8 7 6<br>c) 9 8 7 6<br>d) Compilation error<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: b) 10 9 8 7 6<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">27. Which keyword is used to declare a synchronized method?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">a) synchronized<br>b) static<br>c) final<br>d) volatile<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: a) synchronized<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">28. What is the output of the following code?<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nint x = 2;\nSystem.out.println(x++);\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">a) 2<br>b) 3<br>c) Compilation error<br>d) Runtime error<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: a) 2<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">29. What is the difference between an interface and an abstract class?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">a) An interface cannot have any method implementations, while an abstract class can have some method implementations<br>b) An abstract class cannot have any method implementations, while an interface can have some method implementations<br>c) An abstract class can be instantiated, while an interface cannot be instantiated<br>d) An interface cannot be extended, while an abstract class can be extended<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: a) An interface cannot have any method implementations, while an abstract class can have some method implementations<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">30. What is a package in Java?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">a) A container for classes and other packages<br>b) A keyword that tells the compiler to import a class<br>c) A keyword that indicates the start of a block of code<br>d) A keyword that indicates the end of a block of code<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: a) A container for classes and other packages<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">31. What is the output of the following code?<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nint x = 2;\nint y = x == 2 ? 5 : 7;\nSystem.out.println(y);\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">a) 5<br>b) 7<br>c) Compilation error<br>d) Runtime error<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: a) 5<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">32. Which keyword is used to specify a variable as a reference to an object?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">a) obj<br>b) ref<br>c) this<br>d) none of the above<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: d) none of the above<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">33. What is the output of the following code?<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nString str = &quot;hello&quot;;\nSystem.out.println(str.toUpperCase());\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">a) hello<br>b) HELLO<br>c) Compilation error<br>d) Runtime error<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: b) HELLO<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">34. Which keyword is used to declare a variable that cannot be changed?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">a) final<br>b) static<br>c) volatile<br>d) transient<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: a) final<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">35. What is a thread?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">a) A container for code and data<br>b) A unit of execution within a process<br>c) A keyword that indicates the start of a block of code<br>d) A keyword that indicates the end of a block of code<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: b) A unit of execution within a process<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">36. What is the output of the following code?<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nfor (int i = 0; i &amp;lt; 5; i) {\nSystem.out.println(i);\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">a) Compilation error<br>b) 0 1 2 3 4<br>c) Infinite loop<br>d) Runtime error<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: c) Infinite loop<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">37. Which keyword is used to declare a method that cannot be overridden?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">a) final<br>b) static<br>c) synchronized<br>d) abstract<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: a) final<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">38. What is the output of the following code?<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nint x = 1;\nint y = 2;\nSystem.out.println(x + y);\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">a) 1<br>b) 2<br>c) 3<br>d) Compilation error<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: c) 3<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">39. Which keyword is used to import a class into a Java program?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">a) import<br>b) package<br>c) class<br>d) none of the above<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: a) import<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">40. What is the output of the following code?<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nint x = 1;\nint y = 2;\nSystem.out.println(x &amp;gt; y ? x : y);\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">a) 1<br>b) 2<br>c) Compilation error<br>d) Runtime error<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: b) 2<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">41. Which keyword is used to declare an inner class?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">a) class<br>b) private<br>c) protected<br>d) none of the above<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: d) none of the above (an inner class is declared within another class)<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">42. What is the output of the following code?<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nString str = &quot;hello&quot;;\nSystem.out.println(str.length());\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">a) 5<br>b) 6<br>c) Compilation error<br>d) Runtime error<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: a) 5<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">43. Which keyword is used to create a new instance of an array?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">a) new<br>b) this<br>c) super<br>d) instance<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: a) new<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">44. What is the output of the following code?<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nint x = 5;\nif (x == 5) {\nint y = 2;\nSystem.out.println(y);\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">a) 2<br>b) Compilation error<br>c) Runtime error<br>d) No output<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: a) 2<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">45. What is the output of the following code?<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nString str1 = &quot;hello&quot;;\nString str2 = &quot;world&quot;;\nSystem.out.println(str1 + &quot; &quot; + str2);\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">a) helloworld<br>b) hello world<br>c) Compilation error<br>d) Runtime error<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: b) hello world<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">46. Which keyword is used to declare a variable that can be accessed by any object of the class?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">a) private<br>b) protected<br>c) public<br>d) none of the above<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: c) public<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">47. What is the output of the following code?<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nint&#x5B;] nums = {1, 2, 3};\nSystem.out.println(nums&#x5B;1]);\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">a) 1<br>b) 2<br>c) 3<br>d) Compilation error<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: b) 2<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">48. Which keyword is used to declare that a method can throw an exception?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">a) try<br>b) catch<br>c) throw<br>d) throws<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: d) throws<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">49. What is the output of the following code?<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nint x = 2;\nSystem.out.println(x--);\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">a) 2<br>b) 1<br>c) Compilation error<br>d) Runtime error<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: a) 2<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">50. What is the difference between a while loop and a do-while loop?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">a) A while loop checks the condition before executing the loop body, while a do-while loop checks the condition after executing the loop body<br>b) A do-while loop checks the condition before executing the loop body, while a while loop checks the condition after executing the loop body<br>c) A while loop can only be executed once, while a do-while loop can be executed multiple times<br>d) There is no difference between a while loop and a do-while loop<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: a) A while loop checks the condition before executing the loop body, while a do-while loop checks the condition after executing the loop body<\/h4>\n","protected":false},"excerpt":{"rendered":"<p>1. What is the output of the following code? a) 5b) 6c) Compilation errord) Runtime error Answer: b) 6 2. [&hellip;]<\/p>\n","protected":false},"author":33,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[1],"tags":[],"class_list":["post-10057","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Top 50 Java Interview Questions with Answers - scmGalaxy<\/title>\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.scmgalaxy.com\/tutorials\/top-50-java-interview-questions-with-answers\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Top 50 Java Interview Questions with Answers - scmGalaxy\" \/>\n<meta property=\"og:description\" content=\"1. What is the output of the following code? a) 5b) 6c) Compilation errord) Runtime error Answer: b) 6 2. [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.scmgalaxy.com\/tutorials\/top-50-java-interview-questions-with-answers\/\" \/>\n<meta property=\"og:site_name\" content=\"scmGalaxy\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/profile.php?id=100003935402658\" \/>\n<meta property=\"article:published_time\" content=\"2023-04-29T11:43:54+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-06-19T13:30:24+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.scmgalaxy.com\/tutorials\/wp-content\/uploads\/2023\/04\/image-17.png\" \/>\n<meta name=\"author\" content=\"Ashwani Kumar\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ashwani Kumar\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/top-50-java-interview-questions-with-answers\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/top-50-java-interview-questions-with-answers\\\/\"},\"author\":{\"name\":\"Ashwani Kumar\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#\\\/schema\\\/person\\\/19f28f8f969c1679bfb37d4dad6adc31\"},\"headline\":\"Top 50 Java Interview Questions with Answers\",\"datePublished\":\"2023-04-29T11:43:54+00:00\",\"dateModified\":\"2023-06-19T13:30:24+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/top-50-java-interview-questions-with-answers\\\/\"},\"wordCount\":1392,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/top-50-java-interview-questions-with-answers\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/wp-content\\\/uploads\\\/2023\\\/04\\\/image-17.png\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/top-50-java-interview-questions-with-answers\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/top-50-java-interview-questions-with-answers\\\/\",\"url\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/top-50-java-interview-questions-with-answers\\\/\",\"name\":\"Top 50 Java Interview Questions with Answers - scmGalaxy\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/top-50-java-interview-questions-with-answers\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/top-50-java-interview-questions-with-answers\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/wp-content\\\/uploads\\\/2023\\\/04\\\/image-17.png\",\"datePublished\":\"2023-04-29T11:43:54+00:00\",\"dateModified\":\"2023-06-19T13:30:24+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#\\\/schema\\\/person\\\/19f28f8f969c1679bfb37d4dad6adc31\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/top-50-java-interview-questions-with-answers\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/top-50-java-interview-questions-with-answers\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/top-50-java-interview-questions-with-answers\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/wp-content\\\/uploads\\\/2023\\\/04\\\/image-17.png\",\"contentUrl\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/wp-content\\\/uploads\\\/2023\\\/04\\\/image-17.png\",\"width\":800,\"height\":400},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/top-50-java-interview-questions-with-answers\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Top 50 Java Interview Questions with Answers\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#website\",\"url\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/\",\"name\":\"scmGalaxy\",\"description\":\"A Platform for Software Engineers\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#\\\/schema\\\/person\\\/19f28f8f969c1679bfb37d4dad6adc31\",\"name\":\"Ashwani Kumar\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b0fe597f4b8473400f95ce54bee0bdd62df08a319a198ad229b279aaab4f5410?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b0fe597f4b8473400f95ce54bee0bdd62df08a319a198ad229b279aaab4f5410?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b0fe597f4b8473400f95ce54bee0bdd62df08a319a198ad229b279aaab4f5410?s=96&d=mm&r=g\",\"caption\":\"Ashwani Kumar\"},\"description\":\"There is no end to education. It is not that you read a book, pass an examination, and finish with education. The whole of life, from the moment you are born to the moment you die, is a process of learning.\",\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/profile.php?id=100003935402658\",\"https:\\\/\\\/www.instagram.com\\\/adarshashwani\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/adarsh-ashwani-a75b23130\\\/\"],\"url\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/author\\\/ashwani\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Top 50 Java Interview Questions with Answers - scmGalaxy","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.scmgalaxy.com\/tutorials\/top-50-java-interview-questions-with-answers\/","og_locale":"en_US","og_type":"article","og_title":"Top 50 Java Interview Questions with Answers - scmGalaxy","og_description":"1. What is the output of the following code? a) 5b) 6c) Compilation errord) Runtime error Answer: b) 6 2. [&hellip;]","og_url":"https:\/\/www.scmgalaxy.com\/tutorials\/top-50-java-interview-questions-with-answers\/","og_site_name":"scmGalaxy","article_author":"https:\/\/www.facebook.com\/profile.php?id=100003935402658","article_published_time":"2023-04-29T11:43:54+00:00","article_modified_time":"2023-06-19T13:30:24+00:00","og_image":[{"url":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-content\/uploads\/2023\/04\/image-17.png","type":"","width":"","height":""}],"author":"Ashwani Kumar","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Ashwani Kumar","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/top-50-java-interview-questions-with-answers\/#article","isPartOf":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/top-50-java-interview-questions-with-answers\/"},"author":{"name":"Ashwani Kumar","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#\/schema\/person\/19f28f8f969c1679bfb37d4dad6adc31"},"headline":"Top 50 Java Interview Questions with Answers","datePublished":"2023-04-29T11:43:54+00:00","dateModified":"2023-06-19T13:30:24+00:00","mainEntityOfPage":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/top-50-java-interview-questions-with-answers\/"},"wordCount":1392,"commentCount":0,"image":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/top-50-java-interview-questions-with-answers\/#primaryimage"},"thumbnailUrl":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-content\/uploads\/2023\/04\/image-17.png","inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.scmgalaxy.com\/tutorials\/top-50-java-interview-questions-with-answers\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/top-50-java-interview-questions-with-answers\/","url":"https:\/\/www.scmgalaxy.com\/tutorials\/top-50-java-interview-questions-with-answers\/","name":"Top 50 Java Interview Questions with Answers - scmGalaxy","isPartOf":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/top-50-java-interview-questions-with-answers\/#primaryimage"},"image":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/top-50-java-interview-questions-with-answers\/#primaryimage"},"thumbnailUrl":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-content\/uploads\/2023\/04\/image-17.png","datePublished":"2023-04-29T11:43:54+00:00","dateModified":"2023-06-19T13:30:24+00:00","author":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#\/schema\/person\/19f28f8f969c1679bfb37d4dad6adc31"},"breadcrumb":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/top-50-java-interview-questions-with-answers\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.scmgalaxy.com\/tutorials\/top-50-java-interview-questions-with-answers\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/top-50-java-interview-questions-with-answers\/#primaryimage","url":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-content\/uploads\/2023\/04\/image-17.png","contentUrl":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-content\/uploads\/2023\/04\/image-17.png","width":800,"height":400},{"@type":"BreadcrumbList","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/top-50-java-interview-questions-with-answers\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.scmgalaxy.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Top 50 Java Interview Questions with Answers"}]},{"@type":"WebSite","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#website","url":"https:\/\/www.scmgalaxy.com\/tutorials\/","name":"scmGalaxy","description":"A Platform for Software Engineers","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.scmgalaxy.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#\/schema\/person\/19f28f8f969c1679bfb37d4dad6adc31","name":"Ashwani Kumar","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/b0fe597f4b8473400f95ce54bee0bdd62df08a319a198ad229b279aaab4f5410?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/b0fe597f4b8473400f95ce54bee0bdd62df08a319a198ad229b279aaab4f5410?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/b0fe597f4b8473400f95ce54bee0bdd62df08a319a198ad229b279aaab4f5410?s=96&d=mm&r=g","caption":"Ashwani Kumar"},"description":"There is no end to education. It is not that you read a book, pass an examination, and finish with education. The whole of life, from the moment you are born to the moment you die, is a process of learning.","sameAs":["https:\/\/www.facebook.com\/profile.php?id=100003935402658","https:\/\/www.instagram.com\/adarshashwani\/","https:\/\/www.linkedin.com\/in\/adarsh-ashwani-a75b23130\/"],"url":"https:\/\/www.scmgalaxy.com\/tutorials\/author\/ashwani\/"}]}},"_links":{"self":[{"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/posts\/10057","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/users\/33"}],"replies":[{"embeddable":true,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/comments?post=10057"}],"version-history":[{"count":3,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/posts\/10057\/revisions"}],"predecessor-version":[{"id":10533,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/posts\/10057\/revisions\/10533"}],"wp:attachment":[{"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/media?parent=10057"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/categories?post=10057"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/tags?post=10057"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}