24 Java Interview Questions and Answers

Compiled By Unknown - No Comments
124. Question: You can create an abstract class that contains only abstract methods. On the other hand, you can create an interface that declares the same methods. So can you use abstract classes instead of interfaces?
Answer : Sometimes. But your class may be a descendent of another class and in this case the interface is your only option

125. Question: What comes to mind when you hear about a young generation in Java?
Answer : Garbage collection

126. Question: What comes to mind when someone mentions a shallow copy in Java?
Answer : Object cloning

127. Question: If you're overriding the method equals() of an object, which other method you might also consider?
Answer : hashCode()

128. Question: You are planning to do an indexed search in a list of objects. Which of the two Java collections should you use: ArrayList or LinkedList?
Answer : ArrayList

129. Question: How would you make a copy of an entire Java object with its state?
Answer : Have this class implement Cloneable interface and call its method clone()

130. Question: How can you minimize the need of garbage collection and make the memory use more effective?
Answer : Use object pooling and weak object references

131. Question: There are two classes: A and B. The class B need to inform a class A when some important event has happened. What Java technique would you use to implement it?
Answer : If these classes are threads I'd consider notify() or notifyAll(). For regular classes you can use the Observer interface

132. Question: What access level do you need to specify in the class declaration to ensure that only classes from the same directory can access it?
Answer : You do not need to specify any access level, and Java will use a default package access level

133. Question: When you declare a method as abstract method ?
Answer : When i want child class to implement the behavior of the method

134. Question: Can I call a abstract method from a non abstract method ?
Answer : Yes, We can call a abstract method from a Non abstract method in a Java abstract class

135. Question: What is the difference between an Abstract class and Interface in Java ? or can you explain when you use Abstract classes ?
Answer : Abstract classes let you define some behaviors; they force your subclasses to provide others. These abstract classes will provide the basic functionality of your application, child class which inherited this class will provide the functionality of the abstract methods in abstract class. When base class calls this method, Java calls the method defined by the child class. An Interface can only declare constants and instance methods, but cannot implement default behavior. Interfaces provide a form of multiple inheritances. A class can extend only one other class. Interfaces are limited to public methods and constants with no implementation. Abstract classes can have a partial implementation, protected parts, static methods, etc. A Class may implement several interfaces. But in case of abstract class, a class may extend only one abstract class. Interfaces are slow as it requires extra indirection to find corresponding method in the actual class. Abstract classes are fast

136. Question: What is user-defined exception in java ?
Answer : User-defined expectations are the exceptions defined by the application developer which are errors related to specific application. Application Developer can define the user defined exception by inherite the Exception class as shown below. Using this class we can throw new exceptions. Java Example : public class noFundException extends Exception { } Throw an exception using a throw statement: public class Fund { ...
public Object getFunds() throws noFundException { if (Empty()) throw new noFundException(); ... } }
User-defined exceptions should usually be checked

137. Question: What is the difference between checked and Unchecked Exceptions in Java ?
Answer : All predefined exceptions in Java are either a checked exception or an unchecked exception. Checked exceptions must be caught using try .. catch() block or we should throw the exception using throws clause. If you dont, compilation of program will fail. Java Exception Hierarchy +--------+ | Object | +--------+ | | +-----------+ | Throwable | +-----------+ / \ / \ +-------+ +-----------+ | Error | | Exception | +-------+ +-----------+ / | \ / | \ \________/ \______/ \ +------------------+ unchecked checked | RuntimeException | +------------------+ / | | \ \_________________/ unchecked

138. Question: Explain garbage collection ?
Answer : Garbage collection is an important part of Java's security strategy. Garbage collection is also called automatic memory management as JVM automatically removes the unused variables/objects from the memory. The name "garbage collection" implies that objects that are no longer needed by the program are "garbage" and can be thrown away. A more accurate and up-to-date metaphor might be "memory recycling." When an object is no longer referenced by the program, the heap space it occupies must be recycled so that the space is available for subsequent new objects. The garbage collector must somehow determine which objects are no longer referenced by the program and make available the heap space occupied by such unreferenced objects. In the process of freeing unreferenced objects, the garbage collector must run any finalizers of objects being freed

139. Question: How you can force the garbage collection ?
Answer : Garbage collection automatic process and can't be forced. We can call garbage collector in Java by calling System.gc() and Runtime.gc(), JVM tries to recycle the unused objects, but there is no guarantee when all the objects will garbage collected

140. Question: What are the field/method access levels (specifiers) and class access levels ?
Answer : Each field and method has an access level: private: accessible only in this class (package): accessible only in this package protected: accessible only in this package and in all subclasses of this class public: accessible everywhere this class is available Similarly, each class has one of two possible access levels: (package): class objects can only be declared and manipulated by code in this package public: class objects can be declared and manipulated by code in any package

141. Question: What are the static fields & static Methods ?
Answer : If a field or method defined as a static, there is only one copy for entire class, rather than one copy for each instance of class. static method cannot accecss non-static field or call non-static method
Example Java Code
static int counter = 0;
A public static field or method can be accessed from outside the class using either the usual notation:
Java-class-object.field-or-method-name
or using the class name instead of the name of the class object:
Java- class-name.field-or-method-name

142. Question: What are the Final fields & Final Methods ?
Answer : Fields and methods can also be declared final. A final method cannot be overridden in a subclass. A final field is like a constant: once it has been given a value, it cannot be assigned to again Java Code
private static final int MAXATTEMPTS = 10;

143. Question: Describe the wrapper classes in Java ?
Answer : Wrapper class is wrapper around a primitive data type. An instance of a wrapper class contains, or wraps, a primitive value of the corresponding type
Following table lists the primitive types and the corresponding wrapper classes: Primitive Wrapper boolean java.lang.Boolean byte java.lang.Byte char java.lang.Character double java.lang.Double float java.lang.Float int java.lang.Integer long java.lang.Long short java.lang.Short void java.lang.Void

144. Question: What are different types of inner classes ?
Answer : Inner classes nest within other classes. A normal class is a direct member of a package. Inner classes, which became available with Java 1.1, are four types Static member classes Member classes Local classes Anonymous classes Static member classes - a static member class is a static member of a class. Like any other static method, a static member class has access to all static methods of the parent, or top-level, class
Member Classes - a member class is also defined as a member of a class. Unlike the static variety, the member class is instance specific and has access to any and all methods and members, even the parent's this reference
Local Classes - Local Classes declared within a block of code and these classes are visible only within the block
Anonymous Classes - These type of classes does not have any name and its like a local class Java Anonymous Class Example public class SomeGUI extends JFrame { ... button member declarations ... protected void buildGUI() { button1 = new JButton(); button2 = new JButton(); ... button1.addActionListener( new java.awt.event.ActionListener() <------ Anonymous Class { public void actionPerformed(java.awt.event.ActionEvent e) { // do something } } );

145. Question: What are the uses of Serialization?
Answer : In some types of applications you have to write the code to serialize objects, but in many cases serialization is performed behind the scenes by various server-side containers. These are some of the typical uses of serialization: To persist data for future use. To send data to a remote computer using such client/server Java technologies as RMI or socket programming. To "flatten" an object into array of bytes in memory. To exchange data between applets and servlets. To store user session in Web applications. To activate/passivate enterprise java beans. To send objects between the servers in a cluster.

146. Question: what is a collection ?
Answer : Collection is a group of objects. java.util package provides important types of collections. There are two fundamental types of collections they are Collection and Map. Collection types hold a group of objects, Eg. Lists and Sets where as Map types hold group of objects as key, value pairs Eg. HashMap and Hashtable

147. Question: For concatenation of strings, which method is good, StringBuffer or String ?
Answer : StringBuffer is faster than String for concatenation. Question: What is Runnable interface ? Are there any other ways to make a java program as multithred java program? There are two ways to create new kinds of threads: - Define a new class that extends the Thread class - Define a new class that implements the Runnable interface, and pass an object of that class to a Thread's constructor. - An advantage of the second approach is that the new class can be a subclass of any class, not just of the Thread class. Here is a very simple example just to illustrate how to use the second approach to creating threads: class myThread implements Runnable { public void run() { System.out.println("I'm running!"); } } public class tstRunnable { public static void main(String[] args) { myThread my1 = new myThread(); myThread my2 = new myThread(); new Thread(my1).start(); new Thread(my2).start(); }



Tags:

No Comment to " 24 Java Interview Questions and Answers "