Core Java - Interview Questions and Answers

71. What is meant by Inheritance and what are its advantages?

Inheritance is the process of inheriting all the features from a class. The advantages of inheritance are reusability of code and accessibility of variables and methods of the super class by subclasses.

72. What is the difference between this() and super()?

Ans: this() can be used to invoke a constructor of the same class whereas super() can be used to invoke a super class constructor.

73. What is the difference between superclass and subclass?

A super class is a class that is inherited whereas sub class is a class that does the inheriting.

74. What modifiers may be used with top-level class?

Ans: public, abstract and final can be used for top-level class.

75. What are inner class and anonymous class?

Inner class : classes defined in other classes, including those defined in methods are called inner classes.
An inner class can have any accessibility including private.

Anonymous class : Anonymous class is a class defined inside a method without a name and is instantiated and declared in the same place and cannot have explicit constructors.

76. What is a reflection package?

Ans: java.lang.reflect package has the ability to analyze itself in runtime.

77. What is interface and its use?

Ans: Interface is similar to a class which may contain method's signature only but not bodies and it is a formal set of method and constant declarations that must be defined by the class that implements it. Interfaces are useful for:

a) Declaring methods that one or more classes are expected to implement
b) Capturing similarities between unrelated classes without forcing a class relationship.
c) Determining an object's programming interface without revealing the actual body of the class.

78. What is the difference between Integer and int?

a) Integer is a class defined in the java.lang package, whereas int is a primitive data type defined in the Java language itself. Java does not automatically convert from one to the other.

b) Integer can be used as an argument for a method that requires an object, whereas int can be used for calculations.

79. Can you have an inner class inside a method and what variables can you access?

Yes, we can have an inner class inside a method and final variables can be accessed.

80. What is the difference between exception and error?

Ans: The exception class defines mild error conditions that your program encounters.
Ex: Arithmetic Exception, FilenotFound exception
Exceptions can occur when
-- try to open the file, which does not exist
-- the network connection is disrupted
-- operands being manipulated are out of prescribed ranges
-- the class file you are interested in loading is missing

The error class defines serious error conditions that you should not attempt to recover from. In most cases it is advisable to let the program terminate when such an error is encountered.
Ex: Running out of memory error, Stack overflow error.


Post a Comment

Mới hơn Cũ hơn