Sep 22, 2008

Core Java Inteview Questions Part -V

111 Q How does a try statement determine which catch clause should be used to handle an exception?

When an exception is thrown , the catch block of the try statement are examined in the order in which they appear. The first catch block that is capable of handling the exception is executed. The remaining catch blocks are ignored

112 Q How parameters are passed to methods in java program ?

All java method parameters in java are passed by value only. Obviously primitives are passed by value. In case of objects a copy of the reference is passed and so all the changes made in the method will persist.

113 Q If a class doesn't have any constructors, what will happen?

If a class doesn't have a constructor, the JVM will provide a default constructor for the class.

114 Q What will happen if a thread cannot acquire a lock on an object?

It enters to the waiting state until lock becomes available.

115 Q How does multithreading occurring on a computer with a single CPU?

The task scheduler of OS allocates an execution time for multiple tasks. By switching between different executing tasks, it creates the impression that tasks execute sequentially. But actually there is only one task is executed at a time.

116 Q What will happen if you are invoking a thread's interrupt method while the thread is waiting or sleeping?

When the task enters to the running state, it will throw an InterruptedException.

117 Q What are the different ways in which a thread can enter into waiting state?

There are three ways for a thread to enter into waiting state. By invoking its sleep() method, by blocking on I/O, by unsuccessfully attempting to acquire an object's lock, or by invoking an object's wait() method.

118 Q What are the the different ways for creating a thread?


A thread can be created by subclassing Thread, or by implementing the Runnable interface.

119 Q What is the difference between creating a thread by extending Thread class and by implementing Runnable interface? Which one should prefer?

When creating a thread by extending the Thread class, it is not mandatory to override the run method (If we are not overriding the run method , it is useless), because Thread class have already given a default implementation for run method. But if we are implementing Runnable , it is mandatory to override the run method. The preferred way to create a thread is by implementing Runnable interface, because it give loose coupling.

120 Q What is coupling?


Coupling is the dependency between different components of a system

121 Q How is an interface?

An interface is a collection of method declarations and constants. In java interfaces are used to achieve multiple inheritance. It sets a behavioral protocol to all implementing classes.

122 Q What is an abstract class?

An abstract class is an incomplete class. An abstract class is defined with the keyword abstract . We cannot create an object of the abstract class because it is not complete. It sets a behavioral protocol for all its child classes.

123 Q How will you define an interface?

An interface is defined with the keyword interface. Eg:
public interface MyInterface { }

124 Q How will you define an abstract class?

An abstract class is defined with the keyword abstract Eg:
public abstract class MyClass { }

125 Q What is any an anonymous class?

A An anonymous class is a local class with no name.

126 Q What is a JVM heap?

The heap is the runtime data area from which memory for all class instances and arrays is allocated. The heap may be of a fixed size or may be expanded. The heap is created on virtual machine start-up.

127 Q What is difference between string and StringTokenizer?


StringTokenizer as its name suggests tokenizes a String supplied to it as an argument to its constructor and the character based on which tokens of that string are to be made. The default tokenizing character is space " ".

128 Q What is the difference between array and ArrayList ?

Array is collection of same data type. Array size is fixed, It cannot be expanded. But ArrayList is a growable collection of objects. ArrayList is a part of Collections Framework and can work with only objects.

129 Q What is difference between java.lang .Class and java.lang.ClassLoader? What is the hierarchy of ClassLoader ?


Class 'java.lang.Class' represent classes and interfaces in a running Java application. JVM construct 'Class' object when class in loaded. Where as a ClassLoader is also a class which loads the class files into memory in order for the Java programs to execute properly. The hierarchy of ClassLoaders is:

Bootstrap ClassLoaders
Extensive ClassLoaders
System Classpath ClassLoaders
Application ClassLoaders

130 Q What is daemon thread?

Theards which are running on the background are called deamon threads. daemon thread is a thread which doesn't give any chance to run other threads once it enters into the run state it doesn't give any chance to run other threads. Normally it will run forever, but when all other non-daemon threads are dead, daemon thread will be killed by JVM

131 Q What is a green thread?

Native threads can switch between threads preemptively. Green threads switch only when control is explicitly given up by a thread ( Thread.yield(), Object.wait(), etc.) or a thread performs a blocking operation (read(), etc.). On multi-CPU machines, native threads can run more than one thread simultaneously by assigning different threads to different CPUs. Green threads run on only one CPU. Native threads create the appearance that many Java processes are running: each thread takes up its own entry in the process table. One clue that these are all threads of the same process is that the memory size is identical for all the threads - they are all using the same memory. The process table is not infinitely large, and processes can only create a limited number of threads before running out of system resources or hitting configured limits.

132 Q What is volatile variable?

A volatile variable is not allowed to have a local copy of a variable that is different from the value currently held in "main" memory. Volatile modifier requests the JVM to always access the shared copy of the variable so the its most current value is always read.

133 Q Why java does not support multiple inheritance?

Because the multiple inheritance causes the redundancy. Also we cannot solve diamond problem.

134 Q What is diamond problem?

The diamond problem is an ambiguity that can occur when a class multiply inherits from two classes that both descend from a common super class

135 Q How many JVM's we can run in a system?

Any number of JVMs can run in a system. Whenever we issue the command 'java' a new JVM will start.

136 Q Why Java is not 100% pure object oriented language?

Because java uses primitives.

137 Q Why ArrayList is faster than Vector?

Because Vector is synchronized. Synchronization reduces the performance.

138 Q What is the security mechnaism used in java?

Java uses sand box security model.

139 Q What is sandbox?

A sandbox is a security mechanism for safely running programs. The sandbox typically provides a tightly-controlled set of resources for guest programs to run in, such as scratch space on disk and memory.

140 Q What is phantom memory?

Phantom memory is the memory that does not exist in reality.

141 Q What is reflection?

Reflection is the process of finding out the different features of a class dynamically.

142 Q What are the differences between JIT and HotSpot?

The Hotspot VM is a collection of techniques, the most important of which is called adaptive optimization. The original JVMs interpreted byte codes one at a time. Second-generation JVMs added a JIT compiler, which compiles each method to native code upon first execution, then executes the native code. Thereafter, whenever the method is called, the native code is executed. The adaptive optimization technique used by Hotspot is a hybrid approach, one that combines byte code interpretation and run-time compilation to native code. Hotspot, unlike a regular JIT compiling VM, doesn't do "premature optimization"

143 Q What are the advantages and disadvantages of reference counting in garbage collection?

An advantage of this scheme is that it can run in small chunks of time closely linked with the execution of the program. These characteristic makes it particularly suitable for real-time environments where the program can't be interrupted for very long time. A disadvantage of reference counting is that it does not detect cycles. A cycle is two or more objects that refer to one another. Another disadvantage is the overhead of incrementing and decrementing the reference count each time. Because of these disadvantages, reference counting currently is out of favor.

144 Q How would you implement a thread pool?


The ThreadPool class is a generic implementation of a thread pool, which takes the following input Size of the pool to be constructed and name of the class which implements Runnable (which has a visible default constructor) and constructs a thread pool with active threads that are waiting for activation. once the threads have finished processing they come back and wait once again in the pool.

145 Q What is the difference between throw and throws clause?

throw is used to throw an exception manually, where as throws is used in the case of checked exceptions, to tell the compiler that we haven't handled the exception, so that the exception will be handled by the calling function.

146 Q What is JAR file?

A JAR file (short for Java Archive) is a ZIP file used to distribute a set of Java classes. It is used to store compiled Java classes and associated metadata that can constitute a program

147 Q What is a classloader?

A class loader is an object that is responsible for loading classes.

148 Q What is the difference between Comparable and Comparator ?

The Comparable is for natural ordering and Comparator is for custom ordering. But we can override the compareTo method of comparable interface to give a custom ordering.

149 Q What is the difference between List, Set and Map?

A Set is a collection that has no duplicate elements. A List is a collection that has an order associated with its elements. A map is a way of storing key/value pairs. The way of storing a Map is similar to two-column table.

150 Q What is the difference between Exception and Error ?

Error is unrecoverable.

No comments: