Sunday, December 28, 2008

Core Java Interview Questions

1)What are Encapsulation, Inheritance Polymorphism?

- Encapsulation is the mechanism that binds together code and data it manipulates and keeps both safe from outside interference and misuse.

Inheritance is the process by which one object acquires the properties of another object.

1. class inheritance - create a new class as an extension of another class, primarily for the purposeof code reuse. That is, the derived class inherits the public methods and public data of thebase class. Java only allows a class to have one immediate base class, i.e., single classinheritance.

Polymorphism is a term that describes a situation where one name may refer to different methods. In java there are two type of polymorphism: overloading type and overriding type.

2)What are different types of access modifiers?
public: Any thing declared as public can be accessed from anywhere.
private: Any thing declared as private can’t be seen outside of its class.
protected: Any thing declared as protected can be accessed by classes in the same package and subclasses in the other packages.
default modifier : Can be accessed only to classes in the same package.

Modifier Class Package Subclass World
public ------Y--Y-------- Y-------- Y
protected---Y--Y-------- Y-------- N
default------Y--Y-------- N-------- N
private -----Y--N-------- N-------- N

3)What is the difference between Instance variable and Static Variables ?

Instance variable :: Copy belong to Particular class,and will be created with new key word
Class Variables (Static Variables) :: A class variable is any field declared with the static modifier; this tells the compiler that there is exactly one copy of this variable in existence, regardless of how many times the class has been instantiated. A field defining the number of gears for a particular kind of bicycle could be marked as static since conceptually the same number of gears will apply to all instances. The code static int numGears = 6; would create such a static field. Additionally, the keyword final could be added to indicate that the number of gears will never change.

4)What is the difference between checked and unchecked exceptions?

Checked: You have to catch or declare it. It's stuff that your program can expect to go wrong and should be able to deal with.
Unchecked: You don't have to catch (in fact, usually shouldn't) or declare it. It's stuff that you can't anticipate or can't really do anything about--like a bug in a method you're calling, or a problem in the VM.
RuntimeException and its children are Exceptions that are for program bugs. Error and its children are for VM problems.
You don’t need to declare unchecked Exceptions, those that derive from java.lang.Error and java.lang.RuntimeException, in your throws clauses. Checked Exceptions usually derive from java.lang.Exception.

5)What is difference between overloading and overriding?
a) In overloading, there is a relationship between methods available in the same class whereas in overriding, there is relationship between a superclass method and subclass method.
b) Overloading does not block inheritance from the superclass whereas overriding blocks inheritance from the superclass.
c) In overloading, separate methods share the same name whereas in overriding, subclass method replaces the superclass.
d) Overloading must have different method signatures whereas overriding must have same signature.

6)What is the difference between this() and super()?
this() can be used to invoke a constructor of the same class whereas super() can be used to invoke a super class constructor.

7)What is the difference between abstract class and interface?
a) All the methods declared inside an interface are abstract whereas abstract class must have at least one abstract method and others may be concrete or abstract. b) In abstract class, key word abstract must be used for the methods whereas interface we need not use that keyword for the methods. c) Abstract class must have subclasses whereas interface can’t have subclasses.

8)What is the difference between String and String Buffer?
a) String objects are constants and immutable whereas StringBuffer objects are not. b) String class supports constant strings whereas StringBuffer class supports growable and modifiable strings.

9)What is the difference between set and list?
- Set stores elements in an unordered way but does not contain duplicate elements, whereas list stores elements in an ordered way but may contain duplicate elements.

10)What is final, finalize() and finally?
- final : final keyword can be used for class, method and variables. A final class cannot be subclassed and it prevents other programmers from subclassing a secure class to invoke insecure methods. A final method can’t be overridden. A final variable can’t change from its initialized value.
finalize() : finalize() method is used just before an object is destroyed and can be called just prior to garbage collection.
finally : finally, a key word used in exception handling, creates a block of code that will be executed after a try/catch block has completed and before the code following the try/catch block. The finally block will execute whether or not an exception is thrown. For example, if a method opens a file upon exit, then you will not want the code that closes the file to be bypassed by the exception-handling mechanism. This finally keyword is designed to address this contingency.

11)What is Garbage Collection and how to call it explicitly?
- When an object is no longer referred to by any variable, java automatically reclaims memory used by that object. This is known as garbage collection. System. gc() method may be used to call it explicitly.

12)What are Transient and Volatile Modifiers?
Transient: The transient modifier applies to variables only and it is not stored as part of its object’s Persistent state. Transient variables are not serialized.
Volatile: Volatile modifier applies to variables only and it tells the compiler that the variable modified by volatile can be changed unexpectedly by other parts of the program.

13)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.

14)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.

15)What is the difference between process andthread?
Process is a program in execution whereas thread is a separate path of execution in a program.

16)What is multithreading and what are the methods for inter-thread communication and what is the class in which these methods are defined?
- Multithreading is the mechanism in which more than one thread run independent of each other within the process. wait (), notify () and notifyAll() methods can be used for inter-thread communication and these methods are in Object class.
wait() : When a thread executes a call to wait() method, it surrenders the object lock and enters into a waiting state.
notify() or notifyAll() : To remove a thread from the waiting state, some other thread must make a call to notify() or notifyAll() method on the same object.

17)What is the class and interface in java to create thread and which is the most advantageous method?
- Thread class and Runnable interface can be used to create threads and using Runnable interface is the most advantageous method to create threads because we need not extend thread class here.

18)What are the states associated in the thread?
- Thread contains ready, running, waiting and dead states.

19)What is synchronization?
- Synchronization is the mechanism that ensures that only one thread is accessed the resources at a time.

20)When you will synchronize a piece of your code?
- When you expect your code will be accessed by different threads and these threads may change a particular data causing data corruption.

21)What is deadlock?
- When two threads are waiting each other and can’t precede the program is said to be deadlock.

22)What is daemon thread and which method is used to create the daemon thread?
- Daemon thread is a low priority thread which runs intermittently in the back ground doing the garbage collection operation for the java runtime system. setDaemon method is used to create a daemon thread.

23)What are Vector, Hashtable, LinkedList and Enumeration?
- Vector : The Vector class provides the capability to implement a growable array of objects. Hashtable : The Hashtable class implements a Hashtable data structure. A Hashtable indexes and stores objects in a dictionary using hash codes as the object’s keys. Hash codes are integer values that identify objects. LinkedList: Removing or inserting elements in the middle of an array can be done using LinkedList. A LinkedList stores each object in a separate link whereas an array stores object references in consecutive locations. Enumeration: An object that implements the Enumeration interface generates a series of elements, one at a time. It has two methods, namely hasMoreElements() and nextElement(). HasMoreElemnts() tests if this enumeration has more elements and nextElement method returns successive elements of the series.

24)What is Singleton Class ?

Singleton ensure that the user can create only one instance of this class and provides access to this object from all areas of the application

public class ClassSingleton
private static ClassSingleton instance = null;
protected ClassSingleton()
// Exists only to defeat instantiation.
public static ClassSingleton getInstance()
if(instance == null)
instance = new ClassSingleton();
return instance;

25)What is the diffrence between Double vs BigDecimal ?

Double ::The Double class wraps a value of the primitive type double in an object. An object of type Double contains a single field whose type is double.

In addition, this class provides several methods for converting a double to a String and a String to a double, as well as other constants and methods useful when dealing with a double.

BigDecimal :: BigDecimal extends Number implements Comparable

It allows greater preceision than Double.

The BigDecimal class gives its user complete control over rounding behavior.

The BigDecimal class provides operations for arithmetic, scale manipulation, rounding, comparison, hashing, and format conversion. The toString() method provides a canonical representation of a BigDecimal.

Immutable, arbitrary-precision signed decimal numbers. A BigDecimal consists of an arbitrary precision integer unscaled value and a 32-bit integer scale.

26)What is the diffrence between Vector and ArrayList?

Vector and ArrayList both of these classes are implemented using dynamically resizable
The Vector is synchronized and the ArrayList is not synchronized
The Vector class is less efficient than the ArrayList class mainly because the Vector class is synchronized

27) Is it possible to override the main method?
NO, because main is a static method. A static method can't be overridden in Java.

28)What are the diffrence between JDK, JRE and JVM ?
JDK: Java Developer Kit contains tools needed to develop the Java programs, and JRE to run the programs. The tools include compiler (javac.exe), Java application launcher (java.exe), Appletviewer, etc…

Compiler converts java code into byte code. Java application launcher opens a JRE, loads the class, and invokes its main method.

You need JDK, if at all you want to write your own programs, and to compile them. For running java programs, JRE is sufficient.

JRE: Java Runtime Environment contains JVM, class libraries, and other supporting files. Actually JVM runs the program, and it uses the class libraries, and other supporting files provided in JRE. If you want to run any java program, you need to have JRE installed in the system.

JVM: Java Virtual Machine interprets the bytecode into the machine code depending upon the underlying operating system and hardware combination. It is responsible for all the things like garbage collection, array bounds checking, etc… JVM is platform dependent.

The JVM is called "virtual" because it provides a machine interface that does not depend on the underlying operating system and machine hardware architecture. This independence from hardware and operating system is a cornerstone of the write-once run-anywhere value of Java programs.

No comments:

Post a Comment

Your Ad Here ]]>