Java Interview Questions with Answers –
Q1. What is the difference between “==” and “equals( )” in comparing Java String class objects?
equals method always checks the contents of both the strings whereas comparator operator(==) always checks references of both the strings. equals() returns true if the contents of both strings are the same. Comparator operator returns true when both references or strings points to the same object.
Q2. How are Strings interned in Java?
By using the intern() method of the Java String class. It creates an exact copy of the heap string object in the String Constant Pool. Using intern() we can save a lot of memory consumed by duplicate string objects. A string is duplicated if it contains the same content as another string but it can be stored in different locations in the memory.
Q3. Why is the String class immutable in Java?
Java uses the concept of string literal and when we create object using string literal method that object is created inside SCP(String Constant Pool area).
While storing these literals object inside SCP Jvm first check whether string object content which we define in the program is already present in the SCP area or not.
If it is already present in the SCP then no new object will be created and JVM simply returns reference of that original object else it creates the new object and assigns reference variable to that obj.
Again when new string literal object is created by the programmer again JVM Checks that this string objects content is already there or not if it is present then it return the ref of second object into the first object and so on.
This way JVM create only one object and assign the ref of the other entire object to the same object which it creates first. But if we try to make any change into one of reference variable then that changes may affects all the other object/reference var.
So to avoid this java makes string as immutable objects.
For Free, Demo classes Call: 8237077325
Registration Link: Click Here!
Q4. Explain key words final, finally, and finalize ?
final is the keyword and access modifier in java which is used to apply restrictions on a class, method and variable.
If we declare variables as a final then we cannot change the value of that final variable. It remains constant throughout the program
If we declare a method as a final then we cannot override that method in child class.
If we declare a class as a final then we cannot inherit that class in child class
finally is the block or keyword in Java Exception Handling in which we can write the code that we want to get executed always whether the exception occurs or not.
finalize is the method in Java which is used to perform clean up activity just before an object is garbage collected by the garbage collector of the JVM.
Q5. Explain “method overloading” versus “method overriding”?
When you have multiple methods having the same name but with different parameter lists, and that all methods are defined inside the same class the case is called Overloading.
On the other hand, Overriding refers to a situation where we have multiple methods with the same name and with , but that method is defined in a parent and child class respectively.
Q6. Are overriding & polymorphism applicable static methods as well?
No, we cannot override static methods because method overriding is based runtime polymorphism and the static methods are binded using static binding at compile time. So, we cannot override static methods.
Q7. What is GC? How does it works in the JVM?
Garbage Collection is the process destroying the unused objects from the memory.Java Garbage Collection is the process by which Java programs perform automatic memory management by removing unused or unrefered objects . After compilation, the compiler generates bytecode that can be run on a Java Virtual Machine (JVM).
When Java programs run on the JVM, objects of that class can be created inside the heap, which is nothing but a portion of memory allocated to the program.
Over the lifetime of a Java program, new objects are created and destroyed. Eventually, some objects are no longer needed. So that every time , the heap memory consists of two types of objects:
- Live – these objects are being used and referenced from some of the part of program
- Dead – these objects are not used or referenced from anywhere
- The garbage collector searches these unused objects and deletes them from the memory.
Q9. Which class declaration is correct if A and B are classes and C and D are interfaces? a) class Z extends A implements C, D{} b) class Z extends A,B implements D {} c) class Z extends C implements
class Z extends A implements C, D{}
Q10. Will the program run if we write static public void main?
Yes, because we can change the order of modifiers in java
For Free, Demo classes Call: 8237077325
Registration Link: Click Here!
Q11. What is a Marker Interface?
The interface which does NOT consist of any single abstract method,that interface is called Marker interface. Marker interface is also called a blank or empty interface.
Q12.What is Object Cloning?
Object cloning is the process by which we can create an exact copy of one object into another .To do this process we have a clone() method of Object class.
We need to implement the java.lang.Cloneable interface with the class whose object we want to clone. If we don’t implement a Cloneable interface, clone() method generates a CloneNotSupportedException.
Q13. Why is Java not completely object-oriented?
Java is not a fully object oriented programming language because it supports primitive data types of c like int,short,byte,long,float,double etc.,which are not objects.
Q14. Define Wrapper Classes in Java.
The wrapper class in Java provides the mechanism to convert primitive types/values into object and object into primitive.
The process of converting primitive types/values into objects is called boxing and since J2SE 5.0, this process can be done automatically. That’s why it is called autoboxing.
The process of converting objects back into the primitive values is called as unboxing and since J2SE 5.0, this process can be done automatically that’s why it is called as auto-unboxing
For Free, Demo classes Call: 8237077325
Registration Link: Click Here!
Q15. Define package in Java.
A java package is a collection or group of similar types of classes, interfaces and sub-packages.
Package in java can be divided into two types , built-in package and user-defined package. To used the classes from other program without actually copying them into program then this can be achieved in java by using Packages
Q16. Can you implement pointers in a Java Program?
Java doesn’t support pointers, but has references instead. These are essentially the same thing, with the difference being that you’re dealing with pointers to objects in java and not pointers to memory locations. What this means is that you can’t perform operations like incrementing a pointer and getting a reference from anywhere else. Pointer arithmetic isn’t supported.
Q17. Explain Java String Pool.
String pool is Separate place in the heap memory where the value of the String or string object which is defined in the program gets stored.
So, when we use the first way(by string literal) to create a string object, the String is stored inside SCP by the JVM. Whenever you create a string by using literal, the JVM checks the “string constant pool” first. If the string already exists in the pool, it does not create a new string, it simply returns the new reference to that already existing string.
If the string doesn’t exist in the pool, a new string object is created in the pool. Thereby no memory gets wasted for repeated content of object so to avoid memory wastage and increase reusability of object, java has the concept of String literal.SCP is used for reusability purposes in java.
Q18.What is an Exception?
An exception (or exceptional event) is a situation that occurs during the execution of a program. When an Exception occurs the normal flow of the program is disrupted and the program/Application terminates abnormally and that is not recommended hence these exceptions need to be handled .
Based on these, we have two categories of Exceptions
- checked exception/compile time exception
A checked exception is an exception that is checked by the compiler at a time of compilation of a program, these are also called as compile time exceptions.
These exceptions cannot simply be ignored by the JVM, the programmer should handle these exceptions where they occur in the program
Example: SQLException,ClassNotFoundException,IOException,FileNotFoundException
- Unchecked exceptions
An unchecked exception is an exception that occurs at the time of execution of the program. These are also called Runtime Exceptions. These include the improper use of an API or programming bugs, such as logic errors or. Runtime exceptions are ignored at the time of compilation/Java’s compiler does not check for this error during compilation.
Example : ArithmeticException,ArrayIndexOutOfBoundsException,NullPointerException,NumberFormatException,StringIndexOutOfBoundsException
Q 19 . Why do we need to handle exceptions in the program?
Because, if an exception will get occured in our program, then due to these exceptions our rest of the code will not get executed even if it is correct and will not get proper output. So to maintain the proper flow of our prog execution, we need to handle the exception
For Free, Demo classes Call: 8237077325
Registration Link: Click Here!
Q20. What is Type casting in Java?
Casting is nothing but conversion of one datatype to another datatype
Type casting is assigning a value of one datatype to another datatype
There are 2 type of Casting :
1.Upcasting/widening casting
Upcasting means conversion of smaller data type to larger data type also known as widening(extend).It performs automatically by java compiler
2.Downcasting/narrowing casting
Downcasting means conversion of larger datatype to smaller data type also known as narrowing(short). It performs manually by using the cast operator
Author:-
Pooja Nandode-Bhavsar
SevenMentor Pvt Ltd
Call the Trainer and Book your free demo Class for now!!!
call icon
© Copyright 202! | Sevenmentor Pvt Ltd.