Java Interview Questions
Java Interview Questions
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.
What is an Iterators?
Some of the collection classes provide traversal of their contents via a java.util.Iterator
interface. This interface allows you to walk 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.
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.
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.
What is an abstract class?
Abstract class must be extended/subclassed (to be useful). 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 is automatically abstract itself, and must
be declared as such.
A class may be declared abstract even if it has no abstract methods. This prevents it from being
instantiated.
What is final?
A final class can't be extended ie., 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).
Question: What if the static modifier is removed from the signature of the main method?
Question: What if I write static public void instead of public static void?
Q:Question: What if I do not provide the String array as the argument to the method?
Question: What is the first argument of the String array in main method?
Question: If I do not provide any arguments on the command line, then the String array of
Main method will be empty of null?
Question: How can one prove that the array is not null but empty?
Question: What environment variables do I need to set on my machine in order to be able
to run Java programs?
Question: Can I import same package/class twice? Will the JVM load the package twice at
runtime?
What if the static modifier is removed from the signature of the main method?
Program compiles. But at runtime throws an error "NoSuchMethodError".
http://www.allapplabs.com/interview_questions/ - [ Received from Sandesh Sadhale]
What if I do not provide the String array as the argument to the method?
Program compiles but throws a runtime error "NoSuchMethodError".
http://www.allapplabs.com/interview_questions/ - [ Received from Sandesh Sadhale]
If I do not provide any arguments on the command line, then the String array of Main method
will be empty of null?
It is empty. But not null.
http://www.allapplabs.com/interview_questions/ - [ Received from Sandesh Sadhale]
How can one prove that the array is not null but empty?
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.
http://www.allapplabs.com/interview_questions/ - [ Received from Sandesh Sadhale]
What environment variables do I need to set on my machine in order to be able to run Java
programs?
CLASSPATH and PATH are the two variables.
http://www.allapplabs.com/interview_questions/ - [ Received from Sandesh Sadhale]
Can I import same package/class twice? Will the JVM load the package twice at runtime?
One can import the same package or same class multiple times. Neither compiler nor JVM
complains abt it. And the JVM will internally load the class only once no matter how many times
you import the same class.
http://www.allapplabs.com/interview_questions/ - [ Received from Sandesh Sadhale]
What is Overriding?
When a class defines a method using the same name, return type, and arguments as a method
in its superclass, the method in the class overrides the method in the superclass.
When the method is invoked for an object of the class, it is the new definition of the method
that is called, and not the method definition from superclass. Methods may be overridden to be
more public, not more private.
Local classes - Local classes are like local variables, specific to a block of code. Their visibility is
only within the block of their declaration. In order for the class to be useful beyond the
declaration block, it would need to implement a
more publicly available interface.Because local classes are not members, the modifiers public,
protected, private, and static are not usable.
Anonymous classes - Anonymous inner classes extend local inner classes one level further. As
anonymous classes have no name, you cannot provide a constructor.
Question: Are the imports checked for validity at compile time? e.g. will the code
containing an import such as java.lang.ABCD compile?
Question: Does importing a package imports the subpackages as well? e.g. Does importing
com.MyTest.* also import com.MyTest.UnitTests.*?
Question: What is the difference between declaring a variable and defining a variable?
Question: How can I customize the seralization process? i.e. how can one have a control
over the serialization process?
Question: What is the common usage of serialization?
Question: What happens to the object references included in the object serialized?
Question: What one should take care of while serializing the object?
Are the imports checked for validity at compile time? e.g. will the code containing an import
such as java.lang.ABCD compile?
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;
http://www.allapplabs.com/interview_questions/ - [ Received from Sandesh Sadhale]
Does importing a package imports the subpackages as well? e.g. Does importing com.MyTest.*
also import com.MyTest.UnitTests.*?
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.
http://www.allapplabs.com/interview_questions/ - [ Received from Sandesh Sadhale]
What is serialization?
Serialization is a mechanism by which you can save the state of an object by converting it to a
byte stream.
http://www.allapplabs.com/interview_questions/ - [ Received from Sandesh Sadhale]
How can I customize the seralization process? i.e. how can one have a control over the
serialization process?
Yes it is possible to have control over serialization process. The class should implement
Externalizable interface. This interface contains two methods namely readExternal and
writeExternal. You should implement these methods and write the logic for customizing the
serialization process.
http://www.allapplabs.com/interview_questions/ - [ Received from Sandesh Sadhale]
Question: Does Java provide any construct to find out the size of an object?
Question: Give a simplest way to find out the time a method takes for execution without
using any profiling tool?
Question: If my class already extends from some other class what should I do if I want an
instance of my class to be thrown as an exception object?
Question: If I write System.exit (0); at the end of the try block, will the finally block still
execute?
Does Java provide any construct to find out the size of an object?
No there is not sizeof operator in Java. So there is not direct way to determine the size of an
object directly in Java.
http://www.allapplabs.com/interview_questions/ - [ Received from Sandesh Sadhale]
Does importing a package imports the subpackages as well? e.g. Does importing com.MyTest.*
also import com.MyTest.UnitTests.*?
Read the system time just before the method is invoked and immediately after method returns.
Take the time difference, which will give you the time taken by a method for execution.
To put it in code...
long start = System.currentTimeMillis ();
method ();
long end = System.currentTimeMillis ();
System.out.println ("Time taken for execution is " + (end - start));
Remember that if the time taken for execution is too small, it might show that it is taking zero
milliseconds for execution. Try it on a method which is big enough, in the sense the one which
is doing considerable amout of processing.
http://www.allapplabs.com/interview_questions/ - [ Received from Sandesh Sadhale]
If my class already extends from some other class what should I do if I want an instance of my
class to be thrown as an exception object?
One can not do anytihng in this scenarion. Because Java does not allow multiple inheritance
and does not provide any exception interface as well.
http://www.allapplabs.com/interview_questions/ - [ Received from Sandesh Sadhale]
If I write return at the end of the try block, will the finally block still execute?
Yes even if you write return as the last statement in the try block and no exception occurs, the
finally block will execute. The finally block will execute and then the control return.
http://www.allapplabs.com/interview_questions/ - [ Received from Sandesh Sadhale]
If I write System.exit (0); at the end of the try block, will the finally block still execute?
No in this case the finally block will not execute because when you say System.exit (0); the
control immediately goes out of the program, and thus finally never executes.
Question: Does garbage collection guarantee that a program will not run out of
memory?
Question: What is the difference between preemptive scheduling and time slicing?
Question: When a thread is created and started, what is its initial state?
Question: What is daemon thread and which method is used to create the daemon thread?
Question: How does a try statement determine which catch clause should be used to
handle an exception?
Does garbage collection guarantee that a program will not run out of memory?
Garbage collection does not guarantee that a program will not run out of memory. It is possible
for programs to use up memory resources faster than they are garbage collected. It is also
possible for programs to create objects that are not subject to garbage collection
.
http://www.allapplabs.com/interview_questions/ - [ Received from Venkateswara
Manam]
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.
http://www.allapplabs.com/interview_questions/ - [ Received from Shipra Kamra]
An applet can also get references to all other applets on the same page using the getApplets()
method of java.applet.AppletContext. Once you\'ve got a reference to an applet, you can
communicate with it by using its public members.
It is conceivable to have applets in different virtual machines that talk to a server somewhere
on the Internet and store any data that needs to be serialized there. Then, when another applet
needs this data, it could connect to this same server. Implementing this is non-trivial.
http://www.allapplabs.com/interview_questions/ - [ Received from Krishna Kumar ]
Question: What is the difference between preemptive scheduling and time slicing?
What is Externalizable?
Externalizable is an Interface that extends Serializable Interface. And sends data into Streams in
Compressed Format. It has two methods, writeExternal(ObjectOuput out) and
readExternal(ObjectInput in)
http://www.allapplabs.com/interview_questions/ - [ Received from Venkateswara
Manam]