Oracle Java Interview Questions & Answers Set-2

«« Previous
Next »»

21) What is static method?
Ans: 

  • 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?
Ans: 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?
Ans: 
  • Is used to initialize the static data member.
  • It is excuted before main method at the time of classloading.

24) Can we execute a program without main() method?
Ans: Yes, one of the way is static block.


25) What if the static modifier is removed from the signature of the main method?
Ans: Program compiles. But at runtime throws an error "NoSuchMethodError".


26) What is difference between static (class) method and instance method?
Ans:


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?
Ans: It is a keyword that that refers to the current object.


28)What is Inheritance?
Ans: 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.
Ans: Object class.


30) Why multiple inheritance is not supported in java?
Ans: To reduce the complexity and simplify the language, multiple inheritance is not supported in java in case of class.


31) What is composition?
Ans: Holding the reference of the other class within some other class is known as composition.


32) What is difference between aggregation and composition?
Ans: Aggregation represents weak relationship whereas composition represents strong relationship. For example: bike has an indicator (aggregation) but bike has an engine (compostion).


33) Why Java does not support pointers?
Ans: Pointer is a variable that refers to the memory address. They are not used in java because they are unsafe(unsecured) and complex to understand.


34) What is super in java?
Ans: It is a keyword that refers to the immediate parent class object.


35) Can you use this() and super() both in a constructor?
Ans: No. Because super() or this() must be the first statement.


36)What is object cloning?
Ans: The object cloning is used to create the exact copy of an object.


Core Java - OOPs Concepts: Method Overloading Interview Questions



37) What is method overloading?
Ans: If a class have multiple methods by same name but different parameters, it is known as Method Overloading. It increases the readability of the program.


38) Why method overloading is not possible by changing the return type in java?
Ans: Becauseof ambiguity.


39) Can we overload main() method?
Ans: Yes, You can have many main() methods in a class by overloading the main method.


Core Java - OOPs Concepts: Method Overriding Interview Questions



40) What is method overriding:
Ans: If a subclass provides a specific implementation of a method that is already provided by its parent class, it is known as Method Overriding. It is used for runtime polymorphism and to provide the specific implementation of the method.

«« Previous
Next »»