Set - 1

Question 1 :

What is Collection API ?

Answer :

The Collection API is a set of classes and interfaces that support operation on collections of objects. These classes and interfaces are more flexible, more powerful, and more regular than the vectors, arrays, and hashtables if effectively replaces.
Example of classes: HashSet, HashMap, ArrayList, LinkedList, TreeSet and TreeMap.
Example of interfaces: Collection, Set, List and Map.

Question 2 :

Is Iterator a Class or Interface? What is its use?

Answer :

Answer: Iterator is an interface which is used to step through the elements of a Collection.

Question 3 :

What is similarities/difference between an Abstract class and Interface?

Answer :

Differences are as follows:
Interfaces provide a form of multiple inheritance. 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 to find corresponding method in in the actual class. Abstract classes are fast.

Neither Abstract classes or Interface can be instantiated.

Question 4 :

Java Interview Questions - How to define an Abstract class? 

Answer :

A class containing abstract method is called Abstract class. An Abstract class can't be instantiated.
Example of Abstract class:

abstract class testAbstractClass {
	protected String myString; 
	public String getMyString() { 
		return myString; 
	public abstract string anyAbstractFunction();


Question 5 :

How to define an Interface in Java ?

Answer :

In Java Interface defines the methods but does not implement them. Interface can include constants. A class that implements the interfaces is bound to implement all the methods defined in Interface.
Emaple of Interface:

public interface sampleInterface {
	public void functionOne();
	public long CONSTANT_ONE = 1000; 


Question 6 :

If a class is located in a package, what do you need to change in the OS environment to be able to use it?

Answer :

You need to add a directory or a jar file that contains the package directories to the CLASSPATH environment variable. Let's say a class Employee belongs to a package; and is located in the file c:\dev\com\xyz\hr\ In this case, you'd need to add c:\dev to the variable CLASSPATH. If this class contains the method main(), you could test it from a command prompt window as follows:



Question 7 :

How many methods in the Serializable interface?

Answer :

There is no method in the Serializable interface. The Serializable interface acts as a marker, telling the object serialization tools that your class is serializable.

Question 8 :

How many methods in the Externalizable interface?

Answer :

There are two methods in the Externalizable interface. You have to implement these two methods in order to make your class externalizable. These two methods are readExternal() and writeExternal().

Question 9 :

What is the difference between Serializalble and Externalizable interface?

Answer :

When you use Serializable interface, your class is serialized automatically by default. But you can override writeObject() and readObject() two methods to control more complex object serailization process. When you use Externalizable interface, you have a complete control over your class's serialization process.

Question 10 :

What is a transient variable in Java?

Answer :

A transient variable is a variable that may not be serialized. If you don't want some field to be serialized, you can mark that field transient or static.

Question 11 :

Which containers use a border layout as their default layout?

Answer :

The Window, Frame and Dialog classes use a border layout as their default layout.

Question 12 :

How are Observer and Observable used?

Answer :

Objects that subclass the Observable class maintain a list of observers. When an Observable object is updated, it invokes the update() method of each of its observers to notify the observers that it has changed state. The Observer interface is implemented by objects that observe Observable objects.

Question 13 :

What is Java?

Answer :

Java is an object-oriented programming language developed initially by James Gosling and colleagues at Sun Microsystems. The language, initially called Oak (named after the oak trees outside Gosling's office), was intended to replace C++, although the feature set better resembles that of Objective C. Java should not be confused with JavaScript, which shares only the name and a similar C-like syntax. Sun Microsystems currently maintains and updates Java regularly.

Question 14 :

What does a well-written OO program look like?

Answer :

A well-written OO program exhibits recurring structures that promote abstraction, flexibility, modularity and elegance.

Question 15 :

Can you have virtual functions in Java?

Answer :

Yes, all functions in Java are virtual by default. This is actually a pseudo trick question because the word "virtual" is not part of the naming convention in Java (as it is in C++, C-sharp and VB.NET), so this would be a foreign concept for someone who has only coded in Java. Virtual functions or virtual methods are functions or methods that will be redefined in derived classes.

Question 16 :

Jack developed a program by using a Map container to hold key/value pairs. He wanted to make a change to the map. He decided to make a clone of the map in order to save the original data on side. What do you think of it?

Answer :

If Jack made a clone of the map, any changes to the clone or the original map would be seen on both maps, because the clone of Map is a shallow copy. So Jack made a wrong decision.

Question 17 :

What is more advisable to create a thread, by implementing a Runnable interface or by extending Thread class?

Answer :

Strategically speaking, threads created by implementing Runnable interface are more advisable. If you create a thread by extending a thread class, you cannot extend any other class. If you create a thread by implementing Runnable interface, you save a space for your class to extend another class now or in future.

Question 18 :

What is NullPointerException and how to handle it?

Answer :

When an object is not initialized, the default value is null. When the following things happen, the NullPointerException is thrown:
Calling the instance method of a null object.
Accessing or modifying the field of a null object.
Taking the length of a null as if it were an array.
Accessing or modifying the slots of null as if it were an array.
Throwing null as if it were a Throwable value.
The NullPointerException is a runtime exception. The best practice is to catch such exception even if it is not required by language design.

Question 19 :

An application needs to load a library before it starts to run, how to code?

Answer :

One option is to use a static block to load a library before anything is called. For example,

class Test { 
	static {

When you call new Test(), the static block will be called first before any initialization happens. Note that the static block position may matter.

Question 20 :

How could Java classes direct program messages to the system console, but error messages, say to a file?

Answer :

The class System has a variable out that represents the standard output, and the variable err that represents the standard error device. By default, they both point at the system console. This how the standard output could be re-directed:
Stream st = new Stream(new FileOutputStream("output.txt")); System.setErr(st); System.setOut(st);

Question 21 :

What's the difference between an interface and an abstract class?

Answer :

An abstract class may contain code in method bodies, which is not allowed in an interface. With abstract classes, you have to inherit your class from it and Java does not allow multiple inheritance. On the other hand, you can implement multiple interfaces in your class.

Question 22 :

Name the containers which uses Border Layout as their default layout?

Answer :

Containers which uses Border Layout as their default are: window, Frame and Dialog classes.

Question 23 :

What do you understand by Synchronization?

Answer :

Synchronization is a process of controlling the access of shared resources by the multiple threads in such a manner that only one thread can access one resource at a time. In non synchronized multithreaded application, it is possible for one thread to modify a shared object while another thread is in the process of using or updating the object's value.
Synchronization prevents such type of data corruption.
E.g. Synchronizing a function:

public synchronized void Method1 () {
	// Appropriate method-related code. 

E.g. Synchronizing a block of code inside a function:

public myFunction (){
	synchronized (this) { 
		// Synchronized code here.


Question 24 :

What is synchronization and why is it important?

Answer :

With respect to multithreading, synchronization is the capability to control the access of multiple threads to shared resources. Without synchronization, it is possible for one thread to modify a shared object while another thread is in the process of using or updating that object's value. This often causes dirty data and leads to significant errors.

Question 25 :

What are synchronized methods and synchronized statements?

Answer :

Synchronized methods are methods that are used to control access to a method or an object. A thread only executes a synchronized method after it has acquired the lock for the method's object or class. Synchronized statements are similar to synchronized methods. A synchronized statement can only be executed after a thread has acquired the lock for the object or class referenced in the synchronized statement.

Question 26 :

What are three ways in which a thread can enter the waiting state?

Answer :

A thread can enter the waiting state by invoking its sleep() method, by blocking on IO, by unsuccessfully attempting to acquire an object's lock, or by invoking an object's wait() method. It can also enter the waiting state by invoking its (deprecated) suspend() method.

Question 27 :

Can a lock be acquired on a class?

Answer :

Yes, a lock can be acquired on a class. This lock is acquired on the class's Class object.

Question 28 :

What's new with the stop(), suspend() and resume() methods in JDK 1.2?

Answer :

The stop(), suspend() and resume() methods have been deprecated in JDK 1.2.

Question 29 :

What is the preferred size of a component?

Answer :

The preferred size of a component is the minimum component size that will allow the component to display normally.

Question 30 :

What's the difference between J2SDK 1.5 and J2SDK 5.0?

Answer :

There's no difference, Sun Microsystems just re-branded this version.

Question 31 :

What would you use to compare two String variables - the operator == or the method equals()?

Answer :

I'd use the method equals() to compare the values of the Strings and the == to check if two variables point at the same instance of a String object.

Question 32 :

What is thread?

Answer :

A thread is an independent path of execution in a system.

Question 33 :

What is multi-threading?

Answer :

Multi-threading means various threads that run in a system.

Question 34 :

How does multi-threading take place on a computer with a single CPU?

Answer :

The operating system's task scheduler allocates execution time to multiple tasks. By quickly switching between executing tasks, it creates the impression that tasks execute sequentially.

Question 35 :

How to create a thread in a program?

Answer :

You have two ways to do so. First, making your class "extends" Thread class. Second, making your class "implements" Runnable interface. Put jobs in a run() method and call start() method to start the thread.

Question 36 :

Can Java object be locked down for exclusive use by a given thread?

Answer :

Yes. You can lock an object by putting it in a "synchronized" block. The locked object is inaccessible to any thread other than the one that explicitly claimed it.

Question 37 :

Can each Java object keep track of all the threads that want to exclusively access to it?

Answer :

Yes. Use Thread.currentThread() method to track the accessing thread.

Question 38 :

Does it matter in what order catch statements for FileNotFoundException and IOExceptipon are written?

Answer :

Yes, it does. The FileNoFoundException is inherited from the IOException. Exception's subclasses have to be caught first.

Question 39 :

What invokes a thread's run() method?

Answer :

After a thread is started, via its start() method of the Thread class, the JVM invokes the thread's run() method when the thread is initially executed.

Question 40 :

What is the purpose of the wait(), notify(), and notifyAll() methods?

Answer :

The wait(),notify(), and notifyAll() methods are used to provide an efficient way for threads to communicate each other.

Question 41 :

What are the high-level thread states?

Answer :

The high-level thread states are ready, running, waiting, and dead.

Question 42 :

What is the difference between yielding and sleeping?

Answer :

When a task invokes its yield() method, it returns to the ready state. When a task invokes its sleep() method, it returns to the waiting state.

Question 43 :

What happens when a thread cannot acquire a lock on an object?

Answer :

If a thread attempts to execute a synchronized method or synchronized statement and is unable to acquire an object's lock, it enters the waiting state until the lock becomes available.

Question 44 :

What is the difference between Process and Thread?

Answer :

A process can contain multiple threads. In most multithreading operating systems, a process gets its own memory address space; a thread doesn't. Threads typically share the heap belonging to their parent process. For instance, a JVM runs in a single process in the host O/S. Threads in the JVM share the heap belonging to that process; that's why several threads may access the same object. Typically, even though they share a common heap, threads have their own stack space. This is how one thread's invocation of a method is kept separate from another's. This is all a gross oversimplification, but it's accurate enough at a high level. Lots of details differ between operating systems. Process vs. Thread A program vs. similar to a sequential program an run on its own vs. Cannot run on its own Unit of allocation vs. Unit of execution Have its own memory space vs. Share with others Each process has one or more threads vs. Each thread belongs to one process Expensive, need to context switch vs. Cheap, can use process memory and may not need to context switch More secure. One process cannot corrupt another process vs. Less secure. A thread can write the memory used by another thread

Question 45 :

Can an inner class declared inside of a method access local variables of this method?

Answer :

It's possible if these variables are final.

Question 46 :

What can go wrong if you replace &emp;&emp; with &emp; in the following code: 

String a=null; 
if (a!=null && a.length()>10) 


Answer :

A single ampersand here would lead to a NullPointerException.

Question 47 :

What is the Vector class?

Answer :

The Vector class provides the capability to implement a growable array of objects

Question 48 :

What modifiers may be used with an inner class that is a member of an outer class?

Answer :

A (non-local) inner class may be declared as public, protected, private, static, final, or abstract.

Question 49 :

If a method is declared as protected, where may the method be accessed?

Answer :

A protected method may only be accessed by classes or interfaces of the same package or by subclasses of the class in which it is declared.

Question 50 :

What is an Iterator interface?

Answer :

The Iterator interface is used to step through the elements of a Collection.