Java is one of the most widely used programming languages across the globe. It is estimated that there are over 3 billion devices across the world that are using the Java programming language. As technology and the world of science are expanding each day, it is very important to have knowledge of at least one programming language. Java is one of the best languages that can be easily learned. Here in this article are some of the most commonly asked interview questions that are asked in a Java interview. Candidates who are preparing for the Java interviews and other technical interviews are requested to go through the article in detail to get an idea regarding the language and the type of questions asked.
Java Interview Questions
The given below are some of the important interview questions that are associated with Java programming language and candidates who are preparing for the interviews are requested to go through the data in detail.
- Explain public static void main(String args) in Java.
main() in Java is the entry point for any Java program. It is always written as public static void main(String args).
- public: Public is an access modifier, which is used to specify who can access this method. Public means that this Method will be accessible by any Class.
- static: It is a keyword in java which identifies it is class-based. main() is made static in Java so that it can be accessed without creating the instance of a Class. In case, main is not made static then the compiler will throw an error as main() is called by the JVM before any objects are made and only static methods can be directly invoked via the class.
- void: It is the return type of the method. Void defines the method which will not return any value.
- main: It is the name of the method which is searched by JVM as a starting point for an application with a particular signature only. It is the method where the main execution occurs.
- String args: It is the parameter passed to the main method.
- Why Java is platform-independent?
Java is called platform independent because of its byte codes which can run on any system irrespective of its underlying operating system.
- Why Java is not 100% Object-oriented?
Java is not 100% Object-oriented because it makes use of eight primitive data types such as boolean, byte, char, int, float, double, long, short which are not objects.
- What are wrapper classes in Java?
Wrapper classes convert the Java primitives into the reference types (objects). Every primitive data type has a class dedicated to it. These are known as wrapper classes because they “wrap” the primitive data type into an object of that class. Refer to the below image which displays the different primitive types, wrapper classes, and constructor arguments.
- What are constructors in Java?
In Java, a constructor refers to a block of code that is used to initialize an object. It must have the same name as that of the class. Also, it has no return type and it is automatically called when an object is created.
There are two types of constructors:
- Default Constructor: In Java, a default constructor is the one that does not take any inputs. In other words, default constructors are the no argument constructors which will be created by default in case you no other constructor is defined by the user. Its main purpose is to initialize the instance variables with the default values. Also, it is majorly used for object creation.
- Parameterized Constructor: The parameterized constructor in Java, is the constructor which is capable of initializing the instance variables with the provided values. In other words, the constructors which take the arguments are called parameterized constructors.
- What is a singleton class in Java and how can we make a class singleton?
Singleton class is a class whose only one instance can be created at any given time, in one JVM. A class can be made singleton by making its constructor private.
- What is the difference between Array list and vector in Java?
|Array List is not synchronized.
|Vector is synchronized.
|Array List is fast as it’s non-synchronized.
|Vector is slow as it is thread safe.
|If an element is inserted into the Array List, it increases its Array size by 50%.
|Vector defaults to doubling size of its array.
|Array List does not define the increment size.
|Vector defines the increment size.
|Array List can only use Iterator for traversing an Array List.
|Vector can use both Enumeration and Iterator for traversing.
- What is the difference between equals() and == in Java?
Equals() method is defined in Object class in Java and used for checking the equality of two objects defined by business logic. “==” or equality operator in Java is a binary operator provided by Java programming language and used to compare primitives and objects. public boolean equals(Object o) is the method provided by the Object class. The default implementation uses == operator to compare two objects.
- What is the importance of reflection in Java?
Reflection is a runtime API for inspecting and changing the behavior of methods, classes, and interfaces. Java Reflection is a powerful tool that can be really beneficial. Java Reflection allows you to analyze classes, interfaces, fields, and methods during runtime without knowing what they are called at compile time. Reflection can also be used to create new objects, call methods, and get/set field values. External, user-defined classes can be used by creating instances of extensibility objects with their fully-qualified names. Debuggers can also use reflection to examine private members of classes.
- How to not allow serialization of attributes of a class in Java?
The NonSerialized attribute can be used to prevent member variables from being serialized.
You should also make an object that potentially contains security-sensitive data nonserializable if possible. Apply the NonSerialized attribute to certain fields that store sensitive data if the object must be serialized. If you don’t exclude these fields from serialisation, the data they store will be visible to any programmes with serialization permission.
- Can you call a constructor of a class inside another constructor?
Yes, we can call a constructor of a class inside another constructor. This is also called as constructor chaining. Constructor chaining can be done in 2 ways-
- Within the same class: For constructors in the same class, the this() keyword can be used.
- From the base class: The super() keyword is used to call the constructor from the base class.
The constructor chaining follows the process of inheritance. The constructor of the sub class first calls the constructor of the super class. Due to this, the creation of sub class’s object starts with the initialization of the data members of the super class. The constructor chaining works similarly with any number of classes. Every constructor keeps calling the chain till the top of the chain.
- Contiguous memory locations are usually used for storing actual values in an array but not in ArrayList. Explain.
An array generally contains elements of the primitive data types such as int, float, etc. In such cases, the array directly stores these elements at contiguous memory locations. While an ArrayList does not contain primitive data types. An arrayList contains the reference of the objects at different memory locations instead of the object itself. That is why the objects are not stored at contiguous memory locations.
- How is the creation of a String using new() different from that of a literal?
When we create a string using new(), a new object is created. Whereas, if we create a string using the string literal syntax, it may return an already existing object with the same name.
- Explain the term “Double Brace Initialisation” in Java?
Double Brace Initialization is a Java term that refers to the combination of two independent processes. There are two braces used in this. The first brace creates an anonymous inner class. The second brace is an initialization block. When these both are used together, it is known as Double Brace Initialisation. The inner class has a reference to the enclosing outer class, genertally using the ‘this’ pointer. It is used to do both creation and initialization in a single statement. It is generally used to initialize collections. It reduces the code and also makes it more readable.
- What is constructor chaining in Java?
In Java, constructor chaining is the process of calling one constructor from another with respect to the current object. Constructor chaining is possible only through legacy where a subclass constructor is responsible for invoking the superclass’ constructor first. There could be any number of classes in the constructor chain. Constructor chaining can be achieved in two ways:
- Within the same class using this()
- From base class using super()
- What is a Map in Java?
In Java, Map is an interface of Util package which maps unique keys to values. The Map interface is not a subset of the main Collection interface and thus it behaves little different from the other collection types. Below are a few of the characteristics of Map interface:
- Map doesn’t contain duplicate keys.
- Each key can map at max one value.
- Differentiate between the constructors and methods in Java?
|1. Used to represent the behavior of an object
|1. Used to initialize the state of an object
|2. Must have a return type
|2. Do not have any return type
|3. Needs to be invoked explicitly
|3. Is invoked implicitly
|4. No default method is provided by the compiler
|4. A default constructor is provided by the compiler if the class has none
|5. Method name may or may not be same as class name
|5. Constructor name must always be the same as the class name
Candidates who wish to apply for the examination are requested to go through the complete syllabus and use the best materials for their preparation. Start your preparations for your dream government job with Entri App. We provide a wide range of courses over different government exams. Sign Up for Entri classes and ace the preparation for the government job examinations and bank examinations today itself. Entri helps you with thousands of questions. Attempt mock tests, analyze yourself and improve your success rate. We wish you all the success in your preparations.