Some of the Java Interview Questions for the fresher and Experienced ( Set - 3 )

1. What is package in Java? What is its advantage ?

Ans: This is the basic question might be asked in the interview. You just need to give the exact and proper definition in simple words.
" Package is the storage directory of java files. It is a way by which java classes get organized and placed in well structured directory. "

Advantages of packages:

1. It is going to organize the group of java classes in a well directory structure.
2. It reduces the naming conflict between the java classes.
3. It gives the hierarchy directory structure to store java classes.
4. It also increases the maintenance issue of java classes stored in different packages.
5. It increases the readability of java classes.

2. What is the difference between equals() method " == " operator ?

Ans: This is one of the commonly asked question in an interview.This can be answered with the following points:-

1. equals() is a method where as '==' is an operator.
2. equals() method can be used to compare the string literals where as '==' operator is used to compare the primitive types.
3. equals() is used to compare the content of the String object where as '==' is used to compare the memory location of the objects, whether that two object belong to same memory location or not.

3. What is the Garbage Collector in Java?

Ans: Garbage Collector in java is used to de-allocate the memory of the unreferenced object remain in the heap memory.

4. What do you understand by immutability in Java?

Ans: This is one of the famous and mostly asked question in an interview.  This can be explained in simple terms as " the object which cannot be modified once created is considered as immutable object in java , even if we try to modify the existing object then it will create another object rather modifying to the existing one ".

In java String is the immutable class.

5. What are Wrapper classes in Java?

Ans: In java for every primitive type there is a class which is called as the Wrapper classes. By the use of wrapper class we can represent the primitive types as an object.

6. What is Exception in Java ? How to handle the exception in Java?

Ans: Exception raised in the program will stops the normal flow of program unconditionally but it won't terminate the program that means exceptions can be handled.

There are two ways by which exception raised in the program :
1. By using the try-catch block.
2. Using the 'throws' keyword (let caller or JVM will handle )

7. What is error in Java? Tell some errors you have experienced while writing programs?

Ans: Error totally terminates the flow of execution of the program and hence control will never back to the program from where it get terminated. Error can't be handled and can't be recovered.

Interviewer may ask you about the error you have experienced while writing the codes if you going to an interview as a experienced guy. So you just need to mention some of the common errors we you have got like :
a) NoSuchDefinitionError
b) OutOfMemoryError....etc

8. What is the difference between Error and Exception in Java?

Ans: This is the common freshers interview question.
Error vs Exception:
1. Error terminates the normal flow of execution of program whereas it won't terminate the program.
2. Error can't be handled whereas Exception can be handled.

9. What is inheritance in Java? What are its types?

Ans: Inheritance is one of the important Oops features. Inheritance is the mechanism by which a new class is derived from existing class. By inheritance a relation is being developed between classes hierarchically.

According to Oops there are 5 types of inheritance :-

a) Simple Inheritance
b) Multiple Inheritance
c) Multi-level Inheritance
d) Hierarchical Inheritance
e) Hybrid Inheritance.

10. Is other overloaded main() method will be invoked by the JVM or not ?

Ans: No , other overloaded main() will not be invoked by JVM as it will only look for the main() method signature i.e public static void main(String[] arg) .
We are responsible to call all other overloaded method.

11. What do you understand by 'import' statement in Java?

Ans: 'import' is one of the keyword in java, which is used in java to import the packages. Import statement must be placed after package statement in java programs. Import statement will look alike:-

import java.lang.*; // one way ( will import all the classes within the java.lang package)
import java.lang.Object ; // another way ( importing specific class )

Note: second approach is the good practice always.

12. What is thread in Java?

Ans: This is one of the commonly and basic question to be asked in the interview. Thread is the smallest unit of process. It is the independent path of execution within a program. More than one thread can execute concurrently within a program which creates multi-threaded environment and hence enhance the performance.

13. Is constructor can be inherited in Java?

Ans: No , constructor cannot be inherited .

14. Is it possible to make main() as final?

Ans: Yes, it is very much possible to make main() as final.

15. Can you make methods inside an interface as static and why ?

Ans: No, method inside the interface can't be made static as because static method cannot be override in the sub-class.

16. What is finally block?

Ans: finally is a block in java to be executed at least once and it must be written either with try or with try-catch block. Finally block will have all the codes for releasing all the resources allocated while executing the java program.

17. What is singleton class in Java?

Ans: Singleton in java means the object for which will be created at the time of class loading and the same will be returned always, so singleton class will have only one object created and the same will be used every-time.

18. How many objects are created in below written codes?
String s="java";
String s1="java";

Ans: Only one object will be created.

19. Difference between StringBuilder and StringBuffer?
Ans: This is one of the most asked question in both fresher's and experienced interview.

StringBuilder :

a) All the methods in the StringBuilder are non-synchronized.
b) It is not a thread safe.

StringBuffer:

a) All the methods in the StringBuffer are synchronized.
b) It is thread safe.

20. What is the purpose of using toString() in Java?

Ans: toString() method in java will return the string representation of an object that is,

getClass().getName() + "@" + Integer.toHexString(hashCode());  

Like this toString() is overridden in different class and they have their own representation.

4 comments:

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

Online Members

Live Traffic