Commonly Asked Java Programming Interview Questions | Set 1
Last Updated :
16 Jun, 2024
Why is Java called the ‘Platform Independent Programming Language’?
Platform independence means that execution of your program does not dependent on type of operating system(it could be any : Linux, windows, Mac ..etc). So compile code only once and run it on any System (In C/C++, we need to compile the code for every machine on which we run it). Java is both compiler(javac) and interpreter(jvm) based language. Your java source code is first compiled into byte code using javac compiler. This byte code can be easily converted to equivalent machine code using JVM. JVM(Java Virtual Machine) is available in all operating systems we install. Hence, byte code generated by javac is universal and can be converted to machine code on any operating system, this is the reason why java is platform independent.
Explain Final keyword in java?
Final keyword in java is used to restrict usage of variable, class and method. Variable: Value of Final variable is constant, you can not change it. Method: you can’t override a Final method. Class: you can’t inherit from Final class. Refer this for details.
When is the super keyword used?
Super keyword is used to refer:
- immediate parent class constructor,
- immediate parent class variable,
- immediate parent class method.
Refer this for details.
What is the difference between StringBuffer and String?
String is an Immutable class, i.e. you can not modify its content once created. While StringBuffer is a mutable class, means you can change its content later. Whenever we alter content of String object, it creates a new string and refer to that,it does not modify the existing one. This is the reason that the performance with StringBuffer is better than with String. Refer this for details.
Why multiple inheritance is not supported in java?
Java supports multiple inheritance but not through classes, it supports only through its interfaces. The reason for not supporting multiple inheritance is to avoid the conflict and complexity arises due to it and keep Java a Simple Object Oriented Language. If we recall this in C++, there is a special case of multiple inheritance (diamond problem) where you have a multiple inheritance with two classes which have methods in conflicts. So, Java developers decided to avoid such conflicts and didn’t allow multiple inheritance through classes at all.
Can a top level class be private or protected?
Top level classes in java can’t be private or protected, but inner classes in java can. The reason for not making a top level class as private is very obvious, because nobody can see a private class and thus they can not use it. Declaring a class as protected also doesn’t make any sense. The only difference between default visibility and protected visibility is that we can use it in any package by inheriting it. Since in java there is no such concept of package inheritance, defining a class as protected is no different from default.
What is the difference between ‘throw’ and ‘throws’ in Java Exception Handling?
Following are the differences between two:
- throw keyword is used to throw Exception from any method or static block whereas throws is used to indicate that which Exception can possibly be thrown by this method
- If any method throws checked Exception, then caller can either handle this exception(using try catch block )or can re throw it by declaring another ‘throws’ clause in method declaration.
- throw clause can be used in any part of code where you feel a specific exception needs to be thrown to the calling method
E.g. throw throw new Exception(“You have some exception”) throw new IOException(“Connection failed!!”) throws throws IOException, NullPointerException, ArithmeticException
What is finalize() method?
Unlike c++ , we don’t need to destroy objects explicitly in Java. ‘Garbage Collector‘ does that automatically for us. Garbage Collector checks if no references to an object exist, that object is assumed to be no longer required, and the memory occupied by the object can be freed. Sometimes an object can hold non-java resources such as file handle or database connection, then you want to make sure these resources are also released before object is destroyed. To perform such operation Java provide protected void finalize() in object class. You can override this method in your class and do the required tasks. Right before an object is freed, the java run time calls the finalize() method on that object. Refer this for more details.
Difference in Set and List interface?
Set and List both are child interface of Collection interface. There are following two main differences between them
- List can hold duplicate values but Set doesn’t allow this.
- In List interface data is present in the order you inserted but in the case of Set insertion order is not preserved.
What will happen if you put System.exit(0) on try or catch block? Will finally block execute?
By Calling System.exit(0) in try or catch block, we can skip the finally block. System.exit(int) method can throw a SecurityException. If System.exit(0) exits the JVM without throwing that exception then finally block will not execute. But, if System.exit(0) does throw security exception then finally block will be executed.
This article is compiled by Dharmesh Singh.
You may like to see the following:
Commonly Asked C Programming Interview Questions | Set 1
Commonly Asked C Programming Interview Questions | Set 2
Amazon’s most asked interview questions
Microsoft’s most asked interview questions
Accenture’s most asked Interview Questions
Commonly Asked OOP Interview Questions
Commonly Asked C++ Interview Questions
Commonly asked DBMS interview questions | Set 1
Commonly asked DBMS interview questions | Set 2
Commonly Asked Operating Systems Interview Questions | Set 1
Commonly Asked Data Structure Interview Questions.
Commonly Asked Algorithm Interview Questions
Commonly asked Computer Networks Interview Questions
Top 10 algorithms in Interview Questions
Similar Reads
Commonly Asked Java Programming Interview Questions | Set 2
In this article, some of the most important Java Interview Questions and Answers are discussed, to give you the cutting edge in your interviews. Java is one of the most popular and widely used programming language and platform. Java is fast, reliable and secure. From desktop to web applications, sci
10 min read
10 Most asked Questions from Java Programmers
Java is one of the most widely used programming languages, powering everything from web apps to big business systems. If you're a Java programmer or learning Java, knowing the most commonly asked questions can be really helpful. In this most-asked question from a Java programmer, weâll go over key q
2 min read
Microsoftâs most frequently asked interview questions | Set 2
Microsoft's most asked interview questions | Set 1Level - Easy Celebrity problem - Practice herePrint numbers in given range of bst - Practice hereRoof to leaf path sum - Practice hereLevel order traversal - Practice hereTransform to sum tree - Practice hereDelete middle of linked list - Practice he
1 min read
Amazonâs most frequently asked interview questions | Set 2
Amazon's Most Frequently Asked Questions | Set 1 Level - Easy Get minimum element from the stack - Practice hereSerialize and deserialize a binary tree - Practice herePrint a binary tree in a vertical order - Practice hereCelebrity problem - Practice hereLevel order traversalSwap the kth element fro
2 min read
Java Interview Questions and Answers
Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Top 30 Plus Advanced Java Interview Questions and Answers 2024
Java is a leading programming language essential for developing enterprise solutions, mobile apps, and web platforms. This article provides a comprehensive set of Advanced Java Interview Questions that are critical for Java Backend Developers to master. Covering key areas such as Arrays, Multithread
15+ min read
Top 30 Java 8 Interview Questions and Answers for 2024
Java 8 introduced a host of powerful features that have significantly enhanced the Java programming language. Introducing new features such as Lambda Expressions, Stream API, Functional Interfaces, the new Date and Time API, and more. As a result, Java 8 skills are highly sought after by employers i
15+ min read
ION Group Interview Questions and Answers for Technical Profiles
ION Group is a global technology leader, providing innovative solutions for digital transformation and delivering cutting-edge services for businesses optimizing their online operations. ION Group offers a variety of technical roles in areas such as software engineering, data science, and IT infrast
12 min read
Core Java Interview Questions For Freshers
For the latest Java Interview Questions Refer to the Following Article â Java Interview Questions â Fresher and Experienced (2025) Java is one of the most popular and widely used programming languages and a platform that was developed by James Gosling in the year 1995. It is based on the concept of
15 min read
Top 25 Frequently Asked Interview Questions in Technical Rounds
Here is the collection of the TOP 25 frequently asked questions based on the experience of interviews in multiple companies. 1Lowest common Ancestor2An unsorted array of integers is given; you must find the max product formed by multiplying three numbers. (You cannot sort the array, watch out when t
1 min read