Interview Questions

 
 
 
 

 
Java Interview Questions


Links to important java interview questions
Q:

In How Many Ways can you create an object for a class in java?


Q:

What is the difference betweeen error and exception?


Q:

How to synchronize arraylist?


Q:

Can we create constructor for an abstract class?


Q:

Is it possible to override static method?


Q:

Is it possible to access non-static method from a static method?


Q:

Why is java not a fully object oriented language?


Some more java interview questions with answers
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.
 


Q:

Are the imports checked for validity at compile time? e.g. will the code containing an import such as java.lang.ABCD com compile sucessfully?

A: Yes the imports are checked for the semantic validity at compile time. The code containing above line of import will not compile. It will throw an error saying,can not resolve symbol

symbol : class ABCD

location: package io

import java.io.ABCD;

 

Q:

What are checked exceptions?

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

Q:

How can container knows that JSP has been changed ? ex: I created one.jsp and the result has been displayed. Next i modified one.jsp and got the new result. but how can container know that one.jsp has got changed?

A: By checking the "time stamp" of the jsp file
.
Q:

What is the difference between Exception & RuntimeException in Java?

A: RuntimeException is a child class of Exception class. You can see the details here. This is one of the many child classes of Exception class. RuntimeException is the superclass of those exceptions that can be thrown during the normal operation of the Java Virtual Machine. A method is not required to declare in its throws clause any subclasses of RuntimeException that might be thrown during the execution of the method but not caught.

The hierchy is

java.lang.Object

---java.lang.Throwable

-------java.lang.Exception

-------------java.lang.RuntimeException

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/RuntimeException.html
.

Q:

Is it possible to use try-catch in the finally block of java

A: Yes it is possible to use try catch inside the finally block of java. As a matter of fact it is a good practice to do so as the methods called in finally block may throw an exception. Importance: Highest
.
Q:

What is the difference between ApplicationServer and webserver?

A: Web Server is limited to Web Technology and more over it can't deploy the entriprise applications. So inorder to deploy entriprise applications(EAR Files), we need Application Server. And More Over Web server supports all kinds of protocols not only http.It can support FTP and any, provided the concern jar files must be placed in the lib folder of the Web Server.
Q:

Write a recursive programme to reverse a string i.e given an input "catch" the output should be "hctac"

A:
  • public String reverse(String str)
  • {
  • if ((null == str) || (str.length() <= 1))
  • {
  • return str; /*End */
  • }
  • return reverse(str.substring(1)) + str.charAt(0); /* Recursion */

  • }
    .
    Q:

    What's the difference between the methods sleep() and wait()

    A: The code sleep(1000); puts thread aside for exactly one second. The code wait(1000), causes a wait of up to one second. A thread could stop waiting earlier if it receives the notify() or notifyAll() call. The method wait() is defined in the class Object and the method sleep() is defined in the class Thread.
    .

    Q:

    Can you call one constructor from another if a class has multiple constructors

    A: Yes. Use this() syntax
    .

    Q:

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

    A: 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.
    .

    Q:

    How can a subclass call a method or a constructor defined in a superclass?

    A: To call a method use the following syntax: super.myMethod();
    To call a constructor of the superclass, just write super(); in the first line of the subclass's constructor
    .

    Q:

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

    A: An abstract class can have instance methods that implement a default behavior. An Interface can only declare constants and instance methods, but cannot implement default behavior and all methods are implicitly abstract. An interface has all public members and no implementation. An abstract class is a class which may have the usual flavors of class members (private, protected, etc.), but has some abstract methods.
    .



    Java Interview Questions

    Related Topics
    Exceptions Interview Questions

    Garbage Collection Interview Questions
    Object Serialization Interview Questions
    Interview Questions about Java Threads

    Fastrevise is a mumbai based website dealing with important java interview questions