Professional Documents
Culture Documents
com 1
2. Pointers may crash a program easily, for example , when we add two pointers, the program crashers
immediately.
3. Pointers break security. Using pointers, harmful programs like Virus and other hacking programs can
be developed. Because of the above reasons, pointers have been eliminated from java.
3) Which part of JVM will allocate the memory for a java program.?
Ans). Class loader subsystem of JVM will allocate the necessary memory needed by the java program.
4). Which algorithm is used by garbage collector to remove the unused variables or
objects from memory.?
Ans). Garbage collector uses many algorithems but the most commonly used algorithm is mark and
sweep.
myclipta@gmail.com 10/sep/2010
Java FAQs http://myclipta.blogspot.com 2
Ans). #include directive makes the compiler go to the C/C++ standard library and copy the code from
the header files into the program. As a result, the program size increases, thus wasting memory and
procssor’s time.
import statement makes the JVM go to the Java standard library, execute the code there , and substitute
the result into the program. Here, no code is copied and hence no waste of memory or processor’s
time.so import is an efficient mechanism than #include.
The code will compile but JVM cannot run the code beause it cannot recognize the main( ) as the
method from were it should start execution of the Java program. Remember JVM always looks for main(
) method with string type array as parameter.
myclipta@gmail.com 10/sep/2010
Java FAQs http://myclipta.blogspot.com 3
Ans). Control statements are the statements which alter the flow of execution and provide better control
to the programmer on the flow of execution. They are useful to write better and complex programs.
23) Can you call the main( ) method of a class from another class ?
myclipta@gmail.com 10/sep/2010
Java FAQs http://myclipta.blogspot.com 4
Ans). Yes , we can call the main( ) method of a class from another class using Classname.main( ) . At
the time of calling the main( ) method, we should pass a string type array to it.
27) What is difference between == and equals() while comparing strings ? which one is
reliable ?
Ans). = = operator compares the references of the sting objects. It does not compare the contents of the
objects. equals( ) method compares the contents. While comparing the strings, equals( ) method should
be used as it yields the correct result.
1. String s=”Hello”
In the second statement, new operator is used to create the string object, in this case, JVM always creates
a new object without looking in the string constant pool.
myclipta@gmail.com 10/sep/2010
Java FAQs http://myclipta.blogspot.com 5
Ans). String class objects are immutable and hence their contents cannot be modified. StringBuffer class
objects are mutable, so they can be modified. Moreover the methods that directly manipulate data of the
object are not available in String class. Such methods are available in StringBuffer class.
31) Are there any other classes whose objects are immutalbe ?
Ans). Yes, classes like Character, Byte, Integer, Float, Double, Long..called ‘wrapper classes’ are
created as ‘immutable’.Classes like Class, BigInteger, Big Decimal are also immutable.
A good example for abstraction is a car. Any car will have some parts like engine, radiator, mechanical
and electrical equipment etc. The user of the ca r (driver) should know how to drive the car and does not
require any knowledge of these parts. For example driver is never bothered about how the engine is
designed and the internal parts of the engine. This is why, the car manufacturers hide these parts from
the driver in a separate panel, generally at the front.
Example in java:
Class Bank
{
Private int accno;
myclipta@gmail.com 10/sep/2010
Java FAQs http://myclipta.blogspot.com 6
System.out.println(“Accno= “+accno);
System.out.println(“Name=” +name);
System.out.println(“Balance=”+balance);
}
}
39) What is the difference between object oriented programming launguages and object
based programming languages ?
Ans). Object oriented programming languages follow all the features of Object Oriented Programming
System(OOPS). Smalltalk, Simula-67,C++, Java are examples for OOPS languages.
Object based programming languages follow all the features of OOPS except Inheritance. For example,
JavaScript and VBScript will come under object based programming languages.
myclipta@gmail.com 10/sep/2010
Java FAQs http://myclipta.blogspot.com 7
44) What is the difference between default constructor and parameterized constructor?
Default constructor does not have any Parameterized constructor will have 1 or
parameters. more parameteres
When data is not passed at the time of When data is passed at the time of
creating an object, default constructor is creating an object parameterized
called. constructor is called.
Constructors Methods
A constructor is used to initialize the A mehtod is used for any general purpose
instance variables of a class. processing or calcultaions.
A constructor’s name and class name should A mehtod’s name and class name can be
be same. same or different.
A constructor is called at the time of A method can be called after creating the
creating object. object.
A constructor is called only once per object. A method can be called several times on the
object.
myclipta@gmail.com 10/sep/2010
Java FAQs http://myclipta.blogspot.com 8
49) What is the difference between instance variables and class variables(static
variables) ?
Ans). 1. An Instance variable is a variable whose separate copy is availabe to each object. A class
variable is a variable whose single copy in memory is shared by all objects.
2. Instance variables are created in the objects on heap memory. Class variables are stored on method
area.
51) Is it possible to compile and run a Java program without writing main( ) method ?
Ans). Yes , it is possible by using a static block in the Java program.
myclipta@gmail.com 10/sep/2010
Java FAQs http://myclipta.blogspot.com 9
Here , we are creating Employee class object ‘obj’ using new operator.
Here, we are creating NumberFormat object using the factory method getNumberInstance( )
(a) First, store the class name ‘Employee’ as a string into an object. For this purpose, factory
metod forName( ) of the class ‘Class’ will be useful:
Class c = Class.forName(“Employee”);
We should note that there is a class with the name ‘Class’ in java.lang package.
(b) Next, create another object to the class whose name is in the object c. For this purpose , we
need newInstance( ) method of the class ‘Class’ as:
4. By cloning an already available object, we can create another object. Creating exact copy of an
existing object is called ‘cloning’.
Earlier, we created obj2 by cloning the Employee object obj1.clone( ) method of Object class is
used to clone object.We should note that there is a class by the name ‘Object’ in java.lang
package.
myclipta@gmail.com 10/sep/2010
Java FAQs http://myclipta.blogspot.com 10
Ans). Deriving new classes from existing classes such that the new classes acquire all the features of
existing classes is called inheritance.
3. The programmer can achieve multiple inheritance by repeatedly using single inheritance.
myclipta@gmail.com 10/sep/2010
Java FAQs http://myclipta.blogspot.com 11
67) What is the difference between method overloading and method overriding ?
Writing two or more methods with the same name Writing two or more methods with the same name
but with different signatures is called method and same signatures is called method overriding.
overloading.
Method overloading is done in the same class. Method overriding is done in super and sub classes.
In method overloading, method return type can be In method overriding method return type should
same or different. also be same.
JVM decides which method is called depending on JVM decides which method is called depending on
the difference in the method signatures. the data type (class) of the object used to call the
method.
Method overloading is done when the programmer Method overriding is done when the programmer
wants to extend the already available features. wants to provide a different implementation(body)
for the same feature.
Method overloading is code refinement. Same Method overriding is code replacement. The sub
method is refined to perform a different task. class method overrides(replaces) the super class
method.
myclipta@gmail.com 10/sep/2010
Java FAQs http://myclipta.blogspot.com 12
71) What is the difference between dynamic polymorphism and static polymorphism ?
Ans). Dynamic polymorphism is the polymorphism existed at runtime. Here, Java compiler does not
understand which method is called at compilation time. Only JVM decides which method is called at
runtime. Method overloading and method overriding using instance methods are the examples for
dynamic polymorphism.
Static polymorphism is the polymorphism exhibited at compile time. Here, Java compiler knows which
method is called. Method overloading and method overriding using static methods; method overriding
using private or final methods are examples for static polymorphism.
72) What is difference between primitive data types and advanced data types ?
Ans). Primitive data types represent single values. Advanced data types represent a group of values.
Also methods are not available to handle the primitive data types. In case of advanced data types,
methods are available to perform various operations.
myclipta@gmail.com 10/sep/2010
Java FAQs http://myclipta.blogspot.com 13
Ans). An interface without any members is called marking interface or tagging interface. It marks the
class objects for a special purpose. For example, Clonable(java.lang) and Serializable(java.io) are two
marking interfaces. Clonable interface indicates that a particular class objects are cloneable while
Serializable interface indicates that a particular class objects are serializable.
81) How can you force your programmers to implement only the features of your class ?
Ans). By writing an abstract class or an interface.
84) Why the methods of interface are public and abstract by default ?
Ans). Interface methods are public since they should be available to third party vendors to provide
implementation. They are abstract because their implementation is left for third party vendors.
myclipta@gmail.com 10/sep/2010
Java FAQs http://myclipta.blogspot.com 14
*An interface will have 0 or more abstract methods which are all public and abstract by default.
* An interface can have variables which are public static and final by default. This means all the
variables of the interface are constants.
An abstract class is written when there are An interface is written when all the features are
some common features shared by all the implemented differently in different objects.
objects.
When an abstract class is written, it is the duty An interface is written when the programmer
of the programmer to provide sub classes to it. wants to leave the implementation to the third
party vendors.
An abstract class contains some abstract An interface contains only abstract methods.
methods and also some concrete methods.
An abstract class contain instance variables An interface can not contain instance
also. variables. It contains only constants.
All the abstract methods of the abstract class All the (abstract) methods of the interface
should be implemented in its sub classes. should be implemented in its implementation
classes.
Abstract class is declared by using the Interface is declared using the keyword
keyword abstract. interface.
1. import java.awt.*;
2. import java.awt.event.*;
Should he write both the statements in his program or the first onw is enough ?
Ans). event is a sub package of java.awt package. But, when a package is imported, its sub packages are
not automatically imported into a program. So, for every package or sub package, a separate import
statement should be written. Hence if the programmer wants the classes and interfaces of both the
java.awt and java.awt.event packages, then he should both the preceding statements in his program.
myclipta@gmail.com 10/sep/2010
Java FAQs http://myclipta.blogspot.com 15
System.gc( );
Runtime.getRuntime( ).gc( );
1. import pack.Addition;
2. import pack.*;
Ans) . In statement 1, only the Addition class of the package pack is imported into the program and in
statement 2, all the classes and interfaces of the package pack are available to the program.
If a programmer wants to import only one class of a package say BufferedReader of java.io package,
we can write import java.io.BufferedReader;
92) What is the differentiate between .ear, .jar and .war files.?
Ans). These files are simply zipped file using java jar tool. These files are created for different purposes.
Here is the description of these files:
.jar files: These files are with the .jar extenstion. The .jar files contains the libraries, resources and
accessories files like property files.
.war files: These files are with the .war extension. The war file contains the web application that can be
deployed on the any servlet/jsp container. The .war file contains jsp, html, javascript and other files for
necessary for the development of web applications.
.ear files: The .ear file contains the EJB modules of the application.
Ans) . The CLASSPATH is an environment variable that tells the Java compiler where to look for class
files to import. CLASSPATH is generally set to a directory or a JAR(Java Archive)file.
Ans) A Java Archive file (JAR) is a file that contains compressed version of several .class files, audio
files, image files or directories. JAR file is useful to bundle up several files related to a project and use
them easily.
myclipta@gmail.com 10/sep/2010
Java FAQs http://myclipta.blogspot.com 16
Ans). Default members are available within the same package, but not outside of the package. So their
scope is package scope.
Ans). The code compiles but JVM cannot run it, as it cannot see the main( ) method with String args[ ].
Ans). The exceptions that are checked at compilation-time by the Java compiler are called ‘checked
exceptions’. The exceptions that are checked by the JVM are called ‘unchecked exceptions’.
Ans). Throwable is a class that represents all errors and exceptions which may occur in Java.
Ans). An exception is an error which can be handled. It means when an exception happens, the
programmer can do something to avoid any harm. But an error is an error which cannot be handled, it
happens and the programmer cannot do any thing.
Ans). throws clause is used when the programmer does not want to handle the exception and throw it out
of a method. throw clause is used when the programmer wants to throw an exception explicitly and wants
to handle it using catch block. Hence, throws and throw are contracictory.
Ans). Yes, we can re-throw an exception from catch block to another class where it can be handled.
1. They convert primitive data types into objects and this is needed on Internet to mommunicate between
two applications.
2. The classes in java.util package handle only objects and hence wrapper classes help in this case also.
104). Which of the wrapper classes contains only one constructor ? (or) Which of the wrapper classes
does not contain a constructor with String as parameter ?
Ans). Character.
myclipta@gmail.com 10/sep/2010
Java FAQs http://myclipta.blogspot.com 17
Ans). Converting an object into its corresponding primitive datatype is called unboxing.
Ans). Ideally a string with an integer value should be passed to parseInt ( ) method. So, on parsing
“Hello”, an exception called “NumberFormatException’ occurs since the parseInt( ) method cannot
convert the given string “Hello” into an integer value.
Ans). A collection framework is a class library to handle groups of objects. Collection framework is
implemented in java.util.package.
108). Does a collection object store copies of other objects or their references ?
Ans). Both are useful to retreive elements from a collection. Iterator can retrieve the elements only in
forward direction. But Listener can retrieve the elements in forward and backward direction also. So
ListIterator is preferred to Iterator.
Ans). Both are useful to retreive elements from a collection. Iterator has methods whose names are easy
to follow and Enumeration methods are difficult to remember. Also Iterator has an option to remove
elements from the collection which is not available in Enumeration. So, Iterator is preferred to
Enumeration.
Ans). 1. A Stack is generally used for the purpose of evaluation of expression. A LinkedList is used to
store and retrieve data.
2. Insertion and deletion of elements only from the top of the Stack is possible. Insertion and
deletion of elements from any where is possible in case of a LinkedList.
ArrayList Vector
myclipta@gmail.com 10/sep/2010
Java FAQs http://myclipta.blogspot.com 18
default
Incase of a single thread, using ArrayList is In case of multiple threads, using Vector is
faster than the Vector. advisable. With a single thread, Vector
becomes slow.
ArrayList increases its size every time by 50 Vector increases its size every time by
percent (half). doubling it.
Ans). Yes, we can use synchronizedList( ) method to synchronize the ArrayList, as:
Collections.synchronizedList(new ArrayList( ));
Ans). 0.75.
Ans).
HashMap Hashtable
In case of a single thread, using HashMap is faster In case of multiple threads, using Hashtable is
than the Hashtable. advisable, with a single thread, Hashtable becomes
slow.
HashMap allows null keys and null values to be Hashtable does not allow null keys or values.
stored.
Iterator in the HashMap is fail-fast. This means Enumeration for the Hashtable is not fail-fast. This
Iterator will produce exeception if concurrent means even if concurrent updations are done to
updates are made to the HashMap. Hashtable, there will not be any incorrect results
produced by the Enumeration.
Ans). Yes, we can make HashMap object synchronized using synchronizedMap( ) method as shown here:
Collections.synchronizedMap(new HashMap( ));
Ans).
myclipta@gmail.com 10/sep/2010
Java FAQs http://myclipta.blogspot.com 19
Set List
Set will not allow duplicate values to be stored. List will allow duplicate values.
Accessing elements by their index (position Accessing elements by index is possible in lists.
number) is not possible in case of sets.
Sets will not allow null elements. Lists allow null elements to be stored.
Ans). Both are used to display messages on the monitor. System.out is used to display normal messages
As:
System.out.println(“This is nayanimuralidhar”);
System.err.println(“This is an error”);
Ans). Streams are mainly useful to move data from one place to another place. This concept can be used
to receive data from an input device and send data to an output device.
121). What is the default buffer size used by any buffered class ?
Ans). Serialization is the process of storing object contents into a file. The class whose objects are stored
in the file should implement ‘serializable’ interface of java.io.package.
Once the objects are stored into a file, they can be later retrieved and used as and when needed.This is
called de-serialization.
myclipta@gmail.com 10/sep/2010
Java FAQs http://myclipta.blogspot.com 20
Ans). Domain Naming Service is a service on Internet that maps the IP address with corresponding
website names.
Ans). Port number ia a 2 byte number which is used to identify a socket uniquely.
Ans). main thread. A thread represents execution of statements. The way the statements are executed is of
two types: 1). Single tasking 2). Multi tasking.
Ans). Threads are light-weight because they utilize minimum resources of the system. This means they
take less memory and less processor time.
Ans). Executing only one job at a time is called single tasking. Executing several jobs at a time is called
multi tasking. In single tasking, the processor time is wasted, but in multi tasking, we can utilize the
processor time in an optimum way.
Ans). First of all , we should create a boolean type variable which stores ‘ false’ . When the user wants to
stop the thread. We should store ‘true’into the variable. The status of the variable is checked in the run ( )
method and if it is true, the thread executes ‘return’ statement and then stops.
132). What is the difference between ‘ extends Thread’ and ‘implements Runnable’ ? Which one
is advatageous ?
Ans). extends Thread and implements Runnable – both are functionally same. But when we write extends
Thread, there is no scope to extend another class, as multiple inheritance is not supported in Java.
If we write implements Runnable, then still there is scope to extend another class.
myclipta@gmail.com 10/sep/2010
Java FAQs http://myclipta.blogspot.com 21
This is definitely advantageous when the programmer wants to use threads and also wants to access the
features of another class.
Ans). When a thread is already acting on an object, preventing any other thread from acting on the same
object is called ‘Thread synchronization’ or ‘Thread safe’ The object on which the threads are
synchronized is called ‘synchronized object’. Thread synchronization is recommended when multiple
threads are used on the same object(in multithreading).
135). What is the difference between synchronized block and synchronized keyword ?
Ans). When a thread has locked an object and waiting for another object to be released by another
thread.and the other thread is also waiting for the first thread to release the first object, both the threads
will continue waiting forever. This is called ‘Thread deadlock’.
139). What is the difference between the sleep( ) and wait( ) methods ?
Ans). Both the sleep( ) and wait( ) methods are used to suspend a thread execution for a specified time.
When sleep( ) is executed inside a synchronized block, the object is still under lock. When wait( ) method
is executed, it breaks the synchronized block, so that the object lock is removed and it is available.
Generally, sleep( ) is used for making a thread to wait for some time. But wait( ) is used in connection
with notify ( ) or notifyAll( ) mehtods in therad communication.
Ans). A daemon thread is a thread is a thread that executes continuously. Daemon threads are service
providers for other threads or objects. It generally provides a background procssing.
myclipta@gmail.com 10/sep/2010
Java FAQs http://myclipta.blogspot.com 22
Ans). A thread is created using new Thread( ) statement and is executed by start( ) method. The thread
enters ‘runnable’ state and when sleep( ) or wait( ) methods are used or when the thread is blocked on I/O,
it then goes into ‘not runnable’ state. From ‘not runnable’ state, the thread comes back to the ‘runnable’
state and continues running the statements. The thread dies when it comes out of run( ) mehtod . These
state thransitions of a thread are called ‘life cycle of a thread’.
Ans). A window is a frame without any borders and title, whereas a frame contains borders and title.
Ans). Event delegation model represents that when an event is generated by the user on a component, it is
delegated to a listener interface and the listener calls a mehtod in response to the event. Finally , the event
is handled by the method.
Ans). An adapter class is an implementation class of a listener which contains all methods implemented
with empty body. For example, WindowAdapter is an adapter class of WindowListener interface.
Adapter classes reduce overhead on programming while working with listener interfaces.
Ans). Anonymous inner class is an inner class whose name is not mentioned, and for which only one
object is created.
Ans). BorderLayout.
Ans). FlowLayout.
Ans). Java Foundation classes (JFC) represented a class library developed in pure Java which is an
extension to AWT.
myclipta@gmail.com 10/sep/2010
Java FAQs http://myclipta.blogspot.com 23
Ans). Model- View – Controller is a model used in swing components. Model represents the data of the
component. View represents its appearance and controller is a mediater between the model and the
view.MVC represents the separation of model of an object from its view and how it is controlled.
Ans). There are 4 window panes: Glass pane, Root pane, Layered pane, and Content pane.
Ans). An applet is born with init( ) method and starts functioning with start( ) method. To stop the applet,
the stop( ) method is called and to terminate the applet completely from memory, the destroy( ) method is
called. Once the applet is terminated, we should reload the HTML page again to get the applet start once
again from init( ) method. This cyclic way of executing the methods is called applet life cycle.
Ans). Applets are executed by a program called applet engine which is similar to virtual machine that
exists inside the web browser at client side.
Ans).Hot Java is the first applet-enabled browser developed in Java to support running of applets.
Ans). A generic type represents a class or an interface that is type-safe. It can act on any data type.
Ans). Creating non-generic version of a generic type by the Java compiler is called erasure.
Ans). Auto boxing refers to creating objects and storing primitive data types automatically by the
compiler.
myclipta@gmail.com 10/sep/2010
Java FAQs http://myclipta.blogspot.com 24
Ans). JDBC (Java Database Connectivity) is an API that is useful to write Java programs to connect to
any database, retreive the data from the database and utilize the data in a Java program.
Ans). A database driver is a set of classes and interfaces, written according to JDBC API to communicate
with a database.
myclipta@gmail.com 10/sep/2010