Java Interview questions 2021

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

What is the main objective of garbage collection?

The main objective of this process is to free up the memory space occupied by the unnecessary and unreachable objects during the Java program execution by deleting those unreachable objects. This ensures that the memory resource is used efficiently, but it provides no guarantee that there would be sufficient memory for the program execution.

Can the static methods be overloaded?

Yes! There can be two or more static methods in a class with the same name but differing input parameters.

A single try block and multiple catch blocks can co-exist in a Java Program. Explain.

Yes, multiple catch blocks can exist but specific approaches should come prior to the general approach because only the first catch block satisfying the catch condition is executed.

Apart from the security aspect, what are the reasons behind making strings immutable in Java?

A String is made immutable due to the following reasons: String Pool: Designers of Java were aware of the fact that String data type is going to be majorly used by the programmers and developers. Thus, they wanted optimization from the beginning. They came up with the notion of using the String pool (a storage area in Java heap) to store the String literals. They intended to decrease the temporary String object with the help of sharing. An immutable class is needed to facilitate sharing. The sharing of the mutable structures between two unknown parties is not possible. Thus, immutable Java String helps in executing the concept of String Pool.

Do final, finally and finalize keywords have the same function?

All three keywords have their own utility while programming. Final: If any restriction is required for classes, variables, or methods, the final keyword comes in handy. Inheritance of a final class and overriding of a final method is restricted by the use of the final keyword. The variable value becomes fixed after incorporating the final keyword. Finally: It is the block present in a program where all the codes written inside it get executed irrespective of handling of exceptions. Finalize: Prior to the garbage collection of an object, the finalize method is called so that the clean-up activity is implemented.

Using relevant properties highlight the differences between interfaces and abstract classes.

Availability of methods: Only abstract methods are available in interfaces, whereas non-abstract methods can be present along with abstract methods in abstract classes. Variable types: Static and final variables can only be declared in the case of interfaces, whereas abstract classes can also have non-static and non-final variables. Inheritance: Multiple inheritances are facilitated by interfaces, whereas abstract classes do not promote multiple inheritances. Data member accessibility: By default, the class data members of interfaces are of the public- type. Conversely, the class members for an abstract class can be protected or private also. Implementation: With the help of an abstract class, the implementation of an interface is easily possible. However, the converse is not true;

Briefly explain the concept of constructor overloading

Constructor overloading is the process of creating multiple constructors in the class consisting of the same name with a difference in the constructor parameters. Depending upon the number of parameters and their corresponding types, distinguishing of the different types of constructors is done by the compiler.

What do you mean by data encapsulation?

Data Encapsulation is an Object-Oriented Programming concept of hiding the data attributes and their behaviors in a single unit. It helps developers to follow modularity while developing software by ensuring that each object is independent of other objects by having its own methods, attributes, and functionalities. It is used for the security of the private properties of an object and hence serves the purpose of data hiding.

What part of memory - Stack or Heap - is cleaned in garbage collection process?

Heap.

Comment on method overloading and overriding by citing relevant examples.

In Java, method overloading is made possible by introducing different methods in the same class consisting of the same name. Still, all the functions differ in the number or type of parameters. It takes place inside a class and enhances program readability. The only difference in the return type of the method does not promote method overloading. Method overriding is the concept in which two methods having the same method signature are present in two different classes in which an inheritance relationship is present. A particular method implementation (already present in the base class) is possible for the derived class by using method overriding.

Explain the use of final keyword in variable, method and class.

In Java, the final keyword is used as defining something as constant /final and represents the non-access modifier. final variable:When a variable is declared as final in Java, the value can't be modified once it has been assigned.If any value has not been assigned to that variable, then it can be assigned only by the constructor of the class. final method:A method declared as final cannot be overridden by its children's classes.A constructor cannot be marked as final because whenever a class is inherited, the constructors are not inherited. Hence, marking it final doesn't make sense. Java throws compilation error saying - modifier final not allowed here final class:No classes can be inherited from the class declared as final. But that final class can extend other classes for its usage.

What do you understand by an instance variable and a local variable?

Instance variables are those variables that are accessible by all the methods in the class. They are declared outside the methods and inside the class. These variables describe the properties of an object and remain bound to it at any cost. All the objects of the class will have their copy of the variables for utilization. If any modification is done on these variables, then only that instance will be impacted by it, and all other class instances continue to remain unaffected.

Tell us something about JIT compiler.

JIT stands for Just-In-Time and it is used for improving the performance during run time. It does the task of compiling parts of byte code having similar functionality at the same time thereby reducing the amount of compilation time for the code to run. The compiler is nothing but a translator of source code to machine-executable code. But what is special about the JIT compiler? Let us see how it works:First, the Java source code (.java) conversion to byte code (.class) occurs with the help of the javac compiler.Then, the .class files are loaded at run time by JVM and with the help of an interpreter, these are converted to machine understandable code.JIT compiler is a part of JVM. When the JIT compiler is enabled, the JVM analyzes the method calls in the .class files and compiles them to get more efficient and native code. It also ensures that the prioritized method calls are optimized.Once the above step is done, the JVM executes the optimized code directly instead of interpreting the code again. This increases the performance and speed of the execution.

Why is Java not a pure object oriented language?

Java supports primitive data types - byte, boolean, char, short, int, float, long, and double and hence it is not a pure object-oriented language.

Can the static methods be overridden?

No! Declaration of static methods having the same signature can be done in the subclass but run time polymorphism can not take place in such cases. Overriding or dynamic polymorphism occurs during the runtime, but the static methods are loaded and looked up at the compile time statically. Hence, these methods cant be overridden.

Pointers are used in C/ C++. Why does Java not make use of pointers?

Pointers are quite complicated and unsafe to use by beginner programmers. Java focuses on code simplicity, and the usage of pointers can make it challenging. Pointer utilization can also cause potential errors. Moreover, security is also compromised if pointers are used because the users can directly access memory with the help of pointers. Thus, a certain level of abstraction is furnished by not including pointers in Java. Moreover, the usage of pointers can make the procedure of garbage collection quite slow and erroneous. Java makes use of references as these cannot be manipulated, unlike pointers.

How would you differentiate between a String, StringBuffer, and a StringBuilder?

Storage area: In string, the String pool serves as the storage area. For StringBuilder and StringBuffer, heap memory is the storage area. Mutability: A String is immutable, whereas both the StringBuilder and StringBuffer are mutable. Efficiency: It is quite slow to work with a String. However, StringBuilder is the fastest in performing operations. The speed of a StringBuffer is more than a String and less than a StringBuilder. (For example appending a character is fastest in StringBuilder and very slow in String because a new memory is required for the new String with appended character.) Thread-safe: In the case of a threaded environment, StringBuilder and StringBuffer are used whereas a String is not used. However, StringBuilder is suitable for an environment with a single thread, and a StringBuffer is suitable for multiple threads.

Why is Java a platform independent language?

ava language was developed in such a way that it does not depend on any hardware or software due to the fact that the compiler compiles the code and then converts it to platform-independent byte code which can be run on multiple systems. The only condition to run that byte code is for the machine to have a runtime environment (JRE) installed in it.

Can you tell the difference between equals() method and equality operator (==) in Java?

equals() is a method defined in the Object class. This method is used for checking the equality of contents between two objects as per the specified business logic. == is a binary operator in Java. This operator is used for comparing addresses (or references), i.e checks if both the objects are pointing to the same memory location.

When can you use super keyword?

he super keyword is used to access hidden fields and overridden methods or attributes of the parent class. Following are the cases when this keyword can be used:Accessing data members of parent class when the member names of the class and its child subclasses are same.To call the default and parameterized constructor of the parent class inside the child class.Accessing the parent class methods when the child classes have overridden them. The following example demonstrates all 3 cases when a super keyword is used.


Kaugnay na mga set ng pag-aaral

Investment Vehicle Characteristics

View Set

Chapter 25 The Urinary System Exam

View Set

National Practice Exam Wrong Questions (4)

View Set