Java 英文面试题


Q: What is HashMap and Map?

A: Map is Interface and Hashmap is class that implements that.


Q: Difference between HashMap and HashTable?

A:HashMap allows null values as key and value whereas Hashtable doesnt allow

   HashMap is unsynchronized and Hashtable is synchronized. 

(All the elements  in the HashMap  and HashTable are not ordered).


.Q: Difference between Vector and ArrayList?

A: Vector is synchronized whereas arraylist is not.

(All the elements  in the Vector and ArrayList are ordered).


Q: What if I write static public void instead of public static void?

A: Program compiles and runs properly. 


Q: What is the difference between an Interface and an Abstract class?

A:  The interface is mainly used for framework design.

 An Interface can only declare constants and instance methods, but cannot implement default behavior and all methods are implicitly public and abstract .

The abstract class  is mainly used for code reuse and interface implementation.

An abstract class can have instance methods that implement a default behavior.

The methods  can be declared  as public ,private, protected; An abstract class can  also declare some variables and construction methods and staic methods, but cannot have abstract construction methods and abstract  staic  methods.







接口可以说是抽象类的特例,接口中所有的方法必须为抽象的即 public abstract 通常省略,所有的成员变量必须为静态的 public static final .





4. 抽象类中的抽象方法的访问类型可以是public,protected和(默认类型,虽然

eclipse下不报错,但应该也不行),但接口中的抽象方法只能是public类型的,并且默认即为public abstract类型。

5. 抽象类中可以包含静态方法,接口中不能包含静态方法

6. 抽象类和接口中都可以包含静态成员变量,抽象类中的静态成员变量的访问类型可以任意,但接口中定义的变量只能是public static final类型,并且默认即为public static final类型。

7. 一个类可以实现多个接口,但只能继承一个抽象类。)

6.Q: What is an abstract class?

A: Abstract class must be extended. It serves as a template. A class that is abstract may not be instantiated (ie, you may not call its constructor), abstract class may contain static data. Any class with an abstract method  must be declared as abstract class .

A class may be declared abstract even if it has no abstract methods. This prevents it from being instantiated.·

7.Q: What is the difference between a constructor and a method?

A: A constructor is  used to create objects of that class. It has the same name as the class itself, has no return type.

A method is an ordinary member function of a class. It has its own name, a return type (which may be void).

 8.Q: What is final?

A: A final class can’t be extended ie.,A final class may not be subclassed. A final method can’t be overridden when its class is inherited. You can’t change value of a final variable (is a constant).

9Q: Describe synchronization in respect to multithreading.

A:synchronization is the capability to control the access of multiple threads to shared resources. Without synchonization, it is possible for one thread to modify a shared variable while another thread is in the process of using or updating the same shared variable. This usually leads to serious errors.

10.Q: Explain different way of using thread?

A: The thread could be implemented by using runnable interface or by inheriting from the Thread class. The former is more better,cause when you are going for multiple inheritance..the only interface can help.

11.Q: What is an Iterator?

A: Some of the collection classes provide traversal of their contents via a java.util.Iterator interface. This interface allows you to walk through a collection of objects, operating on each object in turn.

( Remember when using Iterators that they contain a snapshot of the collection at the time the Iterator was obtained; generally it is not advisable to modify the collection itself while traversing an Iterator.)

12.Q: State the significance of public, private, protected, default modifiers both singly and in combination and state the effect of package relationships on declared items qualified by these modifiers.

A: public : Public class is visible in other packages, field is visible everywhere (class must be public too)

private : Private variables or methods may be used only by an instance of the same class that declares the variable or method, A private feature may only be accessed by the class that owns the feature.

protected : Is available to all classes in the same package and also available to all subclasses of the class that owns the protected feature.This access is provided even to subclasses that reside in a different package from the class that owns the protected feature.

default :What you get by default ie, without any access modifier (ie, public private or protected).It means that it is visible to all within a particular package.

13.Q: What is static in java?

 Static means  the variables or mehods belong to the calss instead of  each object no matter how many instance of a class might exist.

 This means that you can use them without creating an instance of a class.Static methods are implicitly final.

14.Q: What if the main method is declared as private?

A: The program compiles properly but at runtime it will give “Main method not public.” message.

15.Q: What is the first argument of the String array in main method?

A: The String array is empty. It does not have any element. This is unlike C/C++ where the first element by default is the program name.

16.Q: If I do not provide any arguments on the command line, then the String array of Main method will be empty or null?

A: It is empty. But not null. 

17.Q: How can one prove that the array is not null but empty using one line of code?

A: Print args.length. It will print 0. That means it is empty. But if it would have been null then it would have thrown a NullPointerException on attempting to print args.length.

18.Q: What environment variables do I need to set on my machine in order to be able to run Java programs?

A: CLASSPATH and PATH are the two variables. 

19.Q: Can an application have multiple classes having main method?

A: Yes it is possible. While starting the application we mention the class name to be run. The JVM will look for the Main method only in the class whose name you have mentioned. Hence there is not conflict amongst the multiple classes having main method.

20.Q: Can I have multiple main methods in the same class?

A: No the program fails to compile. The compiler says that the main method is already defined in the class. 

21.Q: Do I need to import java.lang package any time? Why ?

A: No. It is by default loaded internally by the JVM. 

22.Q: What are checked exceptions?

A: Checked exception are those which the Java compiler forces you to catch. e.g. IOException are checked Exceptions. 

23Q: Does importing a package imports the subpackages as well? e.g. Does importing com.MyTest.* also import com.MyTest.UnitTests.*?

A: No you will have to import the subpackages explicitly. Importing com.MyTest.* will import classes in the package MyTest only. It will not import any class in any of it’s subpackage. 

24.Q: What is the difference between declaring a variable and defining a variable?

A: In declaration we just mention the type of the variable and it’s name. We do not initialize it. But defining means declaration + initialization.

e.g String s; is just a declaration while String s = new String (“abcd”); Or String s = “abcd”; are both definitions. 

25.Q: What is the default value of an object reference declared as an instance variable?

A: null unless we define it explicitly.

26.Q: What type of parameter passing does Java support?

A: In Java the arguments are always passed by value .

27.Q: Primitive data types are passed by reference or pass by value?

A: Primitive data types are passed by value.

28.Q: Objects are passed by value or by reference?

A: Java only supports pass by value. With objects, the object reference itself is passed by value and so both the original reference and parameter copy both refer to the same object . 

29.Q: How do I serialize an object to a file?

A: The class whose instances are to be serialized should implement an interface Serializable. Then you pass the instance to the ObjectOutputStream which is connected to a fileoutputstream. This will save the object to a file. 

30.Q: What one should take care of while serializing the object?

A: One should make sure that all the included objects are also serializable. If any of the objects is not serializable then it throws a NotSerializableException.

31.Does Java provide any construct to find out the size of an object?

A: No there is not sizeof operator in Java. So there is not direct way to determine the size of an object directly in Java.

32.Q: What are wrapper classes?

A: Java provides specialized classes corresponding to each of the primitive data types. These are called wrapper classes. They are e.g. Integer, Character, Double etc.

33.Q: Why do we need wrapper classes?

A: It is sometimes easier to deal with primitives as objects. Moreover most of the collection classes store objects and not primitive data types. And also the wrapper classes provide many methods also. Because of these resons we need wrapper classes. And since we create instances of these classes we can store them in any of the collection classes and pass them around as a collection. Also we can pass them around as method parameters where a method expects an object.

34.Q: What are runtime exceptions?

A: Runtime exceptions are those exceptions that are thrown at runtime because of either wrong input data or because of wrong business logic etc. These are not checked by the compiler at compile time.

35.Q: What is the difference between error and an exception?

A: An error is an irrecoverable condition occurring at runtime. Such as OutOfMemory error. These JVM errors and you can not repair them at runtime. While exceptions are conditions that occur because of bad input etc. e.g. FileNotFoundException will be thrown if the specified file does not exist. Or a NullPointerException will take place if you try using a null reference. In most of the cases it is possible to recover from an exception (probably by giving user a feedback for entering proper values etc.).

36.Q: How to create custom exceptions?

A: Your class should extend class Exception, or some more specific type thereof.

37.Q: What are the different ways to handle exceptions?

A: There are two ways to handle exceptions,

1. By wrapping the desired code in a try block followed by a catch block to catch the exceptions. and

2. List the desired exceptions in the throws clause of the method and let the caller of the method hadle those exceptions.

38.Q: What are the steps in the JDBC connection?

A: While making a JDBC connection we go through the following steps :

Step 1 : Register the database driver by using :

Class.forName(\” driver classs for that specific database\” );

Step 2 : Now create a database connection using :

Connection con = DriverManager.getConnection(url,username,password);

Step 3: Now Create a query using :

Statement stmt = Connection.Statement(\”select * from TABLE NAME\”);

Step 4 : Exceute the query :


39.Q: What method must be implemented by all threads?

A: All tasks must implement the run() method, whether they are a subclass of Thread or implement the Runnable interface.

40.Q: What modifiers are allowed for methods in an Interface?

A: Only public and abstract modifiers are allowed for methods in interfaces.

41.Q: Is Empty .java file a valid source file?

A: Yes, an empty .java file is a perfectly valid source file.

42.Q: Can a .java file contain more than one java classes?

A: Yes, a .java file contain more than one java classes, provided at the most one of them is a public class.

43Q: Is String a primitive data type in Java?

A: No String is not a primitive data type in Java, even though it is one of the most extensively used object. Strings in Java are instances of String class defined in java.lang package.

 44.Q: What are Checked and UnChecked Exception?

A: A checked exception is some subclass of Exception (or Exception itself), excluding class RuntimeException and its subclasses.

Making an exception checked forces client programmers to deal with the possibility that the exception will be thrown. eg, IOException thrown by’s read() method·

Unchecked exceptions are RuntimeException and any of its subclasses. Class Error and its subclasses also are unchecked. With an unchecked exception, however, the compiler doesn’t force client programmers either to catch the exception or declare it in a throws clause. In fact, client programmers may not even know that the exception could be thrown. eg, StringIndexOutOfBoundsException thrown by String’s charAt() method· Checked exceptions must be caught at compile time. Runtime exceptions do not need to be. Errors often cannot be.

45.Q: What is transient variable? 

A: Transient variable can't be serialized. For example if a variable is declared as transient in a Serializable class and the class is written to an ObjectStream, the value of the variable can't be written to the stream instead when the class is retrieved from the ObjectStream the value of the variable becomes null.


Question: What is OOPS? 

Answer: OOP is the common abbreviation for Object-Oriented Programming. 


Question: Describe the principles of OOPS. 

Answer: There are three main features of oops which are called Polymorphism, Inheritance and Encapsulation.


uestion: Explain the different forms of Polymorphism.

Answer:The reference of the interface points to its implementation class,

              The reference of the parent class points to the object of its subclass

              Polymorphism exists in three distinct forms in Java:  

    Method overloading

    Method overriding through inheritance 

    Method overriding through the Java interface 


Question: What are Access Specifiers available in Java? 

Answer: Access specifiers are keywords that determines the type of access to the 

member of a class. These are: