Can You Tell The Difference Between Equals Method And Equality Operator In Java
|This is a method defined in the Object class.
|It is a binary operator in Java.
|This method is used for checking the equality of contents between two objects as per the specified business logic.
|This operator is used for comparing addresses , i.e checks if both the objects are pointing to the same memory location.
- In the cases where the equals method is not overridden in a class, then the class uses the default implementation of the equals method that is closest to the parent class.
- Object class is considered as the parent class of all the java classes. The implementation of the equals method in the Object class uses the == operator to compare two objects. This default implementation can be overridden as per the business logic.
What Is Garbage Collection In Java
Garbage collection is the process of identifying used and unused objects on java heap and removing unused object from the heap.A live object means object is still being referred to some part of program. Unused object means object is not being referred by any part of program and is eligible for garbage collection.Programmer does not have to do manual garbage collection like C or C++. Java takes care of
Explain In Brief The History Of Java
In the year 1991, a small group of engineers called Green Team led by James Gosling, worked a lot and introduced a new programming language called Java. This language is created in such a way that it is going to revolutionize the world.
In todays World, Java is not only invading the internet, but also it is an invisible force behind many of the operations, devices, and applications.
Also Check: What Do You Need For A Job Interview
What Is Android And The Latest Version Of Android
Android is an operating system that is built basically for Mobile phones. It is based on the Linux Kernel and other open-source software and is developed by Google. It is used for touchscreen mobile devices such as smartphones and tablets. But nowadays these are used in Android Auto cars, TV, watches, cameras, etc. It has been one of the best-selling OS for smartphones. Android OS was developed by Android Inc.
The latest version of Android is Android 12 and the initial stable release date is October 4, 2021.
To read more, refer to the article: Introduction to Android
Question 1: Find Minimum Number Of Platforms Required For Railway Station
You are given arrival and departure time of trains reaching to a particular station. You need to find minimum number of platforms required to accommodate the trains at any point of time.
|arrival=departure=No.of platforms required inabove scenario=4
Don’t Miss: How To Conduct A Phone Screen Interview
Q What Is The Purpose Of The Runtime Class And System Class
Runtime Class: The purpose of the Runtime class is to provide access to the Java runtime system. The runtime information like memory availability, invoking the garbage collector, etc.
System Class: The purpose of the System class is to provide access to system resources. It contains accessibility to standard input, standart output, error output streams, current time in millis, terminating the application, etc.
What Will Happen In Case Of Below Program
class A synchronized void m2 }
There are two threads T1 and T2. T1 is accessing m1 method. Will T2 be able to access m2 method on the same instance at the same time?
Yes, T2 will be able to access m2 as it requires object level lock to access m2 method and T1 thread has taken class level lock.You can read more about Object level lock vs Class level lock.
Read Also: How To Do An Online Interview
Can You Tell Us The Difference Between The Fail
This is an advanced Java question for experienced professionals. To answer this question, the difference between the Fail-fast iterator and fail-safe iterator in Java are-
fail-fast iterator- It fails as soon as they realize that the structure of the collection is changed since the beginning of the iteration. The structural changes here are removing, adding, or updating any element from the collection when one thread is iterating over that other collection. The fail-fast can be implemented by maintaining a modification count. When the iteration thread will realize the change in the count, it throws ConcurrentModificationException.
fail-safe iterator in Java- It does not throw an Exception when the collection is modified in a structural manner while one iteration is working over it. It works on the clone collection instead of the original collection.
Can The Main Method Be Overloaded
Yes, It is possible to overload the main method. We can create as many overloaded main methods we want. However, JVM has a predefined calling method that JVM will only call the main method with the definition of –
Consider the below code snippets:
You May Like: List Of References For Job Interview
Q What Are The Differences Between Throw And Throws
Throw keyword is used in the method body to throw an exception, while throws is used in method signature to declare the exceptions that can occur in the statements present in the method.
publicclassThrowExample publicstaticvoidmain }
Exception in thread "main" java.lang.ArithmeticException: Not Eligible for votingat Example1.checkAgeat Example1.main
publicclassThrowsExample publicstaticvoidmain catch } }
Can We Modify The Throws Clause Of The Superclass Method While Overriding It In The Subclass
Yes, we can modify the throws clause of the superclass method while overriding it in the subclass. However, there are some rules which are to be followed while overriding in case of exception handling.
- If the superclass method does not declare an exception, subclass overridden method cannot declare the checked exception, but it can declare the unchecked exception.
- If the superclass method declares an exception, subclass overridden method can declare same, subclass exception or no exception but cannot declare parent exception.
Recommended Reading: How To Answer Interview Questions For Manager Position
In The Below Java Program How Many Objects Are Eligible For Garbage Collection
In the above program, a total of 7 objects will be eligible for garbage collection. Lets visually understand what’s happening in the code.
In the above figure on line 3, we can see that on each array index we are declaring a new array so the reference will be of that new array on all the 3 indexes. So the old array will be pointed to by none. So these three are eligible for garbage collection. And on line 4, we are creating a new array object on the older reference. So that will point to a new array and older multidimensional objects will become eligible for garbage collection.
You Cannot Use Extra Space The First Index Of Occurrence And The Last Index Of Occurrence Will Be
We will keep three variables. The 2 variables firstIndex and lastIndex will be initialized to -1. The third will be a boolean type variable found which will be initially false. If the element is found, we will make it true, and store the current index in firstIndex and lastIndex variables. If the element is found further, only the lastIndex variable will change. The fact that the number has already been found in the array will be denoted by the found boolean variable.
Java Program to find the First and Last Occurrence of an element in the Array
import java.util.* classMainint target = scn.nextInt int fIndex = -1, lIndex = -1 boolean found = false for else } }if else }}
Input:5123252Output: First Index = 1 Last Index = 3
When the element does not exist
Input:5123252Output:The element does not exist in the array.
- Corner Cases, You might Miss: The corner case of elements not present in the array is something that should be taken care of separately. In our code, we use the boolean variable found to do so. Otherwise, we can directly see if the values of fIndex and lIndex variables are -1 or not.
- Time Complexity: O as we are traversing the array.
- Auxiliary Space: O as we have not used any extra space to solve the problem.
You May Like: How To Do A Phone Screen Interview
What Is Jdk Mention The Variants Of Jdk
JDK stands for Java Development Kit, a package containing developer tools and JRE. JDK is used to develop applets, applications, and components of Java using the Java programming language. It also contains numerous tools that are used for developmental work. These tools include debuggers, compilers, etc.
There are some variants of JDK as follows:
- JDK Standard Edition: This edition of JDK is the minimum requirement to run a java application as it provides the base to run applications.
- JDK Enterprise Edition: JDK Enterprise Edition is developed by extending JDK Standard Edition with specifications that help developers create applications.
- JDK Micro Edition: The micro edition of JDK or ME is used to develop applications and their deployment where the portable java code is embedded in mobile devices.
What Do We Get In The Jdk File
- JDK– For making java programs, we need some tools that are provided by JDK . JDK is the package that contains various tools, Compiler, Java Runtime Environment, etc.
- JRE – To execute the java program we need an environment. JRE contains a library of Java classes + JVM. What are JAVA Classes? It contains some predefined methods that help Java programs to use that feature, build and execute. For example – there is a system class in java that contains the print-stream method, and with the help of this, we can print something on the console.
- JVM – JVM is a part of JRE that executes the Java program at the end. Actually, it is part of JRE, but it is software that converts bytecode into machine-executable code to execute on hardware.
You May Like: What Questions To Ask An Employer At Interview
What Are The Important Benefits Of Using Hibernate Framework
Some of the important benefits of using hibernate framework are:
Overall hibernate is the best choice in current market for ORM tool, it contains all the features that you will ever need in an ORM tool.
Q What Is A Memory Leak
The standard definition of a memory leak is a scenario that occurs when objects are no longer being used by the application, but the Garbage Collector is unable to remove them from working memory â because they’re still being referenced. As a result, the application consumes more and more resources â which eventually leads to a fatal OutOfMemoryError.
Some tools that do memory management to identifies useless objects or memeory leaks like:
Read Also: How Do You Ace A Job Interview
Question : Edit Distance Problem In Java
Given two strings string1 and string2, String1 is to be converted into String2 with the given operations available in the minimum number of steps. Using any one of the given operations contributes to the increment of steps by one.
Allowed Operations are : Remove : This operation allows the Removal any one character from String. Insert : This operation allows the Insertion of one character at any spot in the String. Replace : This operation allows the replacement of any one character in the string withany other character.
Solution: Edit distance problem in java.
What Are Various Access Specifiers Present In Java
There are four access specifiers present in Java, and they are:
Also Check: How To Prepare For Google Technical Interview
What Are The Different Types Of Thread Priorities In Java And What Is The Default Priority Of A Thread Assigned By Jvm
There are a total of 3 different types of priority available in Java.
MIN_PRIORITY: It has an integer value assigned with 1.MAX_PRIORITY: It has an integer value assigned with 10.NORM_PRIORITY: It has an integer value assigned with 5.
In Java, Thread with MAX_PRIORITY gets the first chance to execute. But the default priority for any thread is NORM_PRIORITY assigned by JVM.
What Are The Main Uses Of This Keyword
There are the following uses of this keyword.
- this can be used to refer to the current class instance variable.
- this can be used to invoke current class method
- this can be used to invoke the current class constructor.
- this can be passed as an argument in the method call.
- this can be passed as an argument in the constructor call.
- this can be used to return the current class instance from the method.
Also Check: Rpa Solution Architect Interview Questions
What Are The Different Types Of Variables In Java
There are mainly three different types of variables available in Java, and they are:
- Static Variables
- Local Variables
- Instance Variables
Static Variables: A variable that is declared with the static keyword is called a static variable. A static variable cannot be a local variable, and the memory is allocated only once for these variables.
Local Variables: A variable that is declared inside the body of the method within the class is called a local variable. A local variable cannot be declared using the static keyword.
Instance Variables: The variable declared inside the class but outside the body of the method is called the instance variable. This variable cannot be declared as static and its value is instance-specific and cannot be shared among others.
class A }//end of class
Why Is The Main Method Static In Java
The main method is always static because static members are those methods that belong to the classes, not to an individual object. So if the main method will not be static then for every object, It is available. And that is not acceptable by JVM. JVM calls the main method based on the class name itself. Not by creating the object.
Because there must be only 1 main method in the java program as the execution starts from the main method. So for this reason the main method is static.
Also Check: How To Wow In An Interview
Can We Overload The Methods By Making Them Static
No, We cannot overload the methods by just applying the static keyword to them. Consider the following example.
Animal.java:7: error: method consume is already defined in class Animal static void consume ^Animal.java:15: error: non-static method consume cannot be referenced from a static context Animal.consume ^2 errors
Write A Program In Java To Show Multiple Inheritance
Multiple inheritance is not possible in Java. So, we can use Interfaces in Java to create a scenario of multiple inheritance. In our example below, we have a class called Phone and a class called SmartPhone. We know that a SmartPhone is a Phone, however, it has various other features as well. For instance, a SmartPhone has a camera, a music player, etc. Notice that a SmartPhone is a Phone and has a camera and has a Music Player. So, there is one is-A relationship and multiple has-A relationships. The is-A relationship denotes extending the features and the has-A relationship denotes implementing the features. This means that a SmartPhone is a Phone i.e. it extends the features of a Phone however, it just implements the features of a Music Player and a Camera. It itself is not a music player or a camera. Following is the code for the above discussion.
Java Code for Multiple Inheritance
Video callCallMessagePicture clickRecord VideoPlay musicPause MusicStop music
Read Also: What Interview Questions To Prepare For
What Is The Difference Between Throws And Throws In Java
The throw is used to actually throw an instance of java.lang.Throwable class, which means you can throw both Error and Exception using the throw keyword e.g.
throw new IllegalArgumentException
On the other hand, throws are used as part of method declaration and signals which kind of exceptions are thrown by this method so that its caller can handle them. Its mandatory to declare any unhandled checked exception in the throws clause in Java. Like the previous question, this is another frequently asked Java interview question from errors and exception topics but too easy to answer.
What Do You Understand By Object Cloning And How Do You Achieve It In Java
- It is the process of creating an exact copy of any object. In order to support this, a java class has to implement the Cloneable interface of java.lang package and override the clone method provided by the Object class the syntax of which is:
protected Object clonethrows CloneNotSupportedException
- In case the Cloneable interface is not implemented and just the method is overridden, it results in CloneNotSupportedException in Java.
Read Also: What Are My Weaknesses Job Interview