Oracle Java Interview Questions & Answers Set-5

«« Previous
Next »»

81) Can an exception be rethrown?
Ans: Yes.

82) Can subclass overriding method declare an exception if parent class method doesn't throw an exception ?
Ans: Yes but only unchecked exception not checked.

83) What is exception propagation ?
Ans: Forwarding the exception object to the invoking method is known as exception propagation.

Core Java: String Handling Interview Questions

84) What is the meaning of immutable in terms of String?
Ans: The simple meaning of immutable is unmodifiable or unchangeable. Once string object has been created, its value can't be changed.

85) Why string objects are immutable in java?
Ans: Because java uses the concept of string literal. Suppose there are 5 reference variables,all referes to one object "sachin".If one reference variable changes the value of the object, it will be affected to all the reference variables. That is why string objects are immutable in java.

86) How many ways we can create the string object?
Ans: There are two ways to create the string object, by string literal and by new keyword.

87) How many objects will be created in the following code?
String s1="Welcome";
String s2="Welcome";
String s3="Welcome";
Only one object.

88) Why java uses the concept of string literal?
Ans: To make Java more memory efficient (because no new objects are created if it exists already in string constant pool).

89)How many objects will be created in the following code?
Ans:  String s = new String("Welcome");
Two objects, one in string constant pool and other in non-pool(heap).

90) What is the basic difference between string and stringbuffer object?
Ans: String is an immutable object. StringBuffer is a mutable object.

91) What is the difference between StringBuffer and StringBuilder ?
Ans: StringBuffer is synchronized whereas StringBuilder is not synchronized.

92) How can we create immutable class in java ?
Ans: We can create immutable class as the String class by defining final class.

93) What is the purpose of toString() method in java ?
Ans: The toString() method returns the string representation of any object. If you print any object, java compiler internally invokes the toString() method on the object. So overriding the toString() method, returns the desired output, it can be the state of an object etc. depends on your implementation.

Core Java : Nested classes and Interfaces Interview Questions

94)What is nested class?
Ans: A class which is declared inside another class is known as nested class. There are 4 types of nested class member inner class, local inner class, annonymous inner class and static nested class.

95) Is there any difference between nested classes and inner classes?
Ans: Yes, inner classes are non-static nested classes i.e. inner classes are the part of nested classes.

96) Can we access the non-final local variable, inside the local inner class?
Ans: No, local variable must be constant if you want to access it in local inner class.

97) What is nested interface ?
Ans: Any interface i.e. declared inside the interface or class, is known as nested interface. It is static by default.

98) Can a class have an interface?
Ans: Yes, it is known as nested interface.

99) Can an Interface have a class?
Ans: Yes, they are static implicitely.

100. What are different types of inner classes ?
Ans: Simple Inner Class, Local Inner Class, Anonymous Inner Class , Static Nested Inner Class.

«« Previous
Next »»