• +91 9723535972
  • info@interviewmaterial.com

Java Interview Questions and Answers

Java Interview Questions and Answers

Question - 1 : -  What is ClassDefNotFoundException and NoClassDefFoundError and explain differences between these two?

Answer - 1 : - Given a class A.. A ClassNotFoundException means that the classloader cannot load class A. A ClassDefNotFoundError means that the classloader can load class A, but cannot instantiate it because it cannot load the other classes that class A depend on.

Question - 2 : - Is Iterator a Class or Interface? What is its use?

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

Question - 3 : - What is Collection API ?

Answer - 3 : - 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 - 4 : - What is similarities/difference between an Abstract class and Interface?

Answer - 4 : - 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. Similarities: Neither Abstract classes or Interface can be instantiated.

Question - 5 : - How to define an Abstract class?

Answer - 5 : - 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 - 6 : - How to define an Interface in Java ?

Answer - 6 : - 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; } If a class is located in a package, what do you need to change in the OS environment to be able to use it? 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 com.xyz.hr; and is located in the file c:\dev\com\xyz\hr\Employee.java. 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: c:\>java com.xyz.hr.Employee

Question - 7 : - How many methods in the Serializable interface?

Answer - 7 : - 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 - 8 : - 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 - 9 : - 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 - 10 : - 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.


NCERT Solutions

 

Share your email for latest updates

Name:
Email:

Our partners