21) What is static method?
- A static method belongs to the class rather than object of a class.
- A static method can be invoked without the need for creating an instance of a class.
- static method can access static data member and can change the value of it.
22) Why main method is static?
because object is not required to call static method if It were non-static method,jvm creats object first then call main() method that will lead to the problem of extra memory allocation.23) What is static block?
- Is used to initialize the static data member.
- It is executed before main method at the time of class loading.
24) Can we execute a program without main() method?
Ans) Yes, one of the way is static block25) What if the static modifier is removed from the signature of the main method?
Program compiles. But at runtime throws an error "NoSuchMethodError".26) What is difference between static (class) method and instance method?
static or class
method
|
instance method
|
1)A method i.e. declared as static is known as static
method.
|
A method i.e. not declared as static is known as instance
method.
|
2)Object is not required to call static method.
|
Object is required to call instance methods.
|
3)Non-static (instance) members cannot be accessed in
static context (static method, static block and static nested class)
directly.
|
static and non-static variables both can be accessed in
instance methods.
|
4)For example: public static int cube(int n){ return
n*n*n;}
|
For example: public void msg(){...}.
|
Core Java - OOPs Concepts: Inheritance Interview Questions
27) What is this in java?
It is a keyword that that refers to the current object.28)What is Inheritance?
Inheritance is a mechanism in which one object acquires all the properties and behaviour of another object of another class. It represents IS-A relationship. It is used for Code Resusability and Method Overriding.29) Which class is the superclass for every class.
Object class.30) Why multiple inheritance is not supported in java?
- To reduce the complexity and simplify the language, multiple inheritance is not supported in java in case of class.Page 1 2 3 4 5 6 7 8
- Page 5 OOPS concepts Java Interview questions
No comments:
Post a Comment