Some of the java Interview Questions for the fresher and Experienced ( Set - 2 )

1. What will happen if we try to modify the value of a final variable ?

Ans: Final variable can't be modified , if we try to do so then a compile time exception will be thrown i.e java.lang.IllegalAccessException.

2. What is Oops Concept ?

Ans: Oops is considered as Object Oriented Feature. As java is Object- Oriented language so it supports the following Oops features:-

a) Encapsulation
b) Inheritance
c) Polymorphism
d) Message Passing
e) Data Abstraction
f) Data Hiding

3. What is static variable ?where it is useful?

Ans: Static variable is a class variable which is at the class scope and not at the object scope. It is declared using the 'static' keyword with the variable name..........Click here to know more 


Memory allocation for the static are done only once at the time of class loading , so it is useful only if there is a common information which will be shared among all the object created for that class. 

4. What is instance variable ?

Ans: Instance variable are at the object level which means which is at the Object level scope. Each object can have a their own copy of instance variable. It is initialized by the constructor calling i.e while creating the object. 

5. What is polymorphism? How many types of Polymorphism is there in Java?

Ans: Polymorphism means 'many forms'. In java Polymorphism defines as the 'more than one methods having same name but perform different functions' . 

Basically there are two types of polymorphism in java :- 

a) Dynamic Polymorphism
b) Static Polymorphism

6. What is the difference between static method and instance method?

Ans: Static method :
a) It can be called with the class name.
b) It has only stack. 

Instance method :
a) It will be called with the Object only.
b) Separate stack will be created for each object calling instance method.  

7. Is it possible to access static content from non-static area? why ?

Ans: No, its not possible to access static content from non-static content. It is because non-static content can be accessible only if we are creating object and there are not guarantee for always creating the object. 

8. Difference between class level variable and local variable?

Ans: 
Class level variable :-

a) It is at the class scope or Object scope.

b) It can be static or instance. 
c) Memory will be allocated at the time of class loading if class level variable is static.
d) Memory will be allocated at the time of creating the object if the class level variable is instance.
e) It consume the space in heap memory. 

Local Variable:-

a) It is at the method scope,
b) It can't be static. 
c) Memory will be allocated only if method will be called.
d) It consumes the space in stack. 

9. Difference between dynamic and static Polymorphism ?

Ans: 
Dynamic Polymorphism:

a) It is otherwise called as overriding. 
b) Method to be called will be decided at the time of execution. 
c) It will be achieved by dynamic dispatch
d) It will have one method at the super class and another at the sub-class.

Static Polymorphism:

a) It is otherwise called as overloading.
b) Method to be called will be decided at the compile time only.
c) It will be achieved by calling respective overloaded method by passing requirement information.
d) It will have all the methods at the same class.

10. Why main() method is declared as static?

Ans: main() method is declared as static resulting there will not be any need to creating object for the JVM to call main() method. 

11. What is Dynamic Dispatch?

Ans: Dynamic dispatch means assigning sub-class object to the super-class reference variable. By this we can approach for dynamic polymorphism.

For ex: 

A--> super class
B----> Sub-class

A a = new B() ;---> Dynamic dispatch 


12. What is an Abstract class?
Ans:  A class declared with the abstract keyword is called as Abstract class. 
It has following features:-

a) An abstract class cannot be instantiated but can be subclassed. 
b) It can have both instance and static variables.
c) It can also have both abstract and non-abstract methods.
...............Click here to know more .

13. What is a marker interface and what is its usage?
Ans: Marker interface is an interface without any method declaration. Its is a empty interface like Serializable, Cloneable etc interfaces. 

It will be used just to indicate JVM that to perform for which that marker is designed.
for ex Cloneable marker interface has been designed to indicate JVM that there is a requirement of creating the clone of the object of the class implementing Cloneable interface.


14. What is the difference between Abstract and Interface?
Ans: Click here to know all the difference 

15. What is an anonymous class and its use?
Ans: An anonymous class is a class without any name and hence we can create only one object of an anonymous class.......Click here to know more about Anonymous class .

Use of Anonymous class:

a) It provide security to the application over network as we cannot create object of the anonymous explicitly. 
b) It can be used if we have to create only a single object to operate. 

16. Can we write constructor inside anonymous class and why ?
Ans: No , we cannot create/write constructor for the anonymous class as it has no name and constructor is same as the class name. 

17. What is the default package imported in Java class automatically ?
Ans: Default package which is going to be imported for every single java class is java.lang.* pakage. 

18. Name some of the methods from Object class?
Ans: Some of the common methods from Object class are:-
a) toString()
b) clone()
c) wait()
d) notify()
e) notifyAll()
e) hashCode()
f) equals()

g) finalize()
h) getClass()

19. What is hashcode?
Ans: HashCode is the integer representation of the object. Actual object get modified resulting some integer value is called as hashcode. Two different object can have same hashcode. 

20. What is cloning?
Ans: Cloning is process of creating proxy or dummy object of the actual object, which covers all the features of the actual/original object. Default cloning is done using clone() method. 





0 comments:

Post a Comment

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

Online Members

Live Traffic