Java 2marks Questions With Answer
Java 2marks Questions With Answer
Java 2marks Questions With Answer
IT 2301-JAVA PROGRAMMING
UNIT I
object or primitive type the method returns, a list of parameters and the body of the
method. A method’s signature is a combination of the first three parts mentioned above.
14) What gives java it’s “write once and run anywhere” nature?
All Java programs are compiled into class files that contain bytecodes. These
byte codes can be run in any platform and hence java is said to be platform independent.
15) What is a constructor? What is a destructor?
Constructor is an operation that creates an object and/or initialises its state. Destructor
is an operation that frees the state of an object and/or destroys the object itself. In Java,
there is no concept of destructors. Its taken care by the JVM.
UNIT II
3) What is a package?
A package is a collection of classes and interfaces that provides a high-level layer
of access protection and name space management.
Dynamic binding: Dynamic binding is a binding in which the class association is not
made until the object is created at execution time. It is also called as Late binding.
UNIT III
with built-in classes like Class, Method, Fields, Constructors etc. Example: Using Java
Reflection API we can get the class name, by using the getName method.
c) Abstract class must have subclasses whereas interface can’t have subclasses.
5) Can you have an inner class inside a method and what variables can you access Yes,
we can have an inner class inside a method and final variables can be accessed.
runtime when you create the proxy. To create a proxy, use the static method
java.lang.reflect.Proxy::newProxyInstance(). This method takes three arguments:
13) What is the relationship between the Canvas class and the Graphics class?
A Canvas object provides access to a Graphics object via its paint() method.
15) What is the difference between the ‘Font’ and ‘FontMetrics’ class?
Font Class is used to render ‘glyphs’ - the characters you see on the screen. FontMetrics
encapsulates information about a specific font on a specific Graphics object. (width of
the characters, ascent, descent)
16) What is the difference between the paint() and repaint() methods?
The paint() method supports painting via a Graphics object. The repaint() method is
used to cause paint() to be invoked by the AWT painting thread.
UNIT IV
4) What is a layout manager and what are different types of layout managers available in
java AWT?
A layout manager is an object that is used to organize components in a container. The
different layouts are available are FlowLayout, BorderLayout, CardLayout, GridLayout
and GridBagLayout.
5) How are the elements of different layouts organized?
FlowLayout: The elements of a FlowLayout are organized in a top to bottom, left to
right fashion.
BorderLayout: The elements of a BorderLayout are organized at the borders (North,
South, East and West) and the center of a container.
CardLayout: The elements of a CardLayout are stacked, on top of the other, like a deck
of cards.
GridLayout: The elements of a GridLayout are of equal size and are laid out using the
square of a grid.
GridBagLayout: The elements of a GridBagLayout are organized according to a grid.
However, the elements are of different size and may occupy more than one row or
column of the grid. In addition, the rows and columns may have different sizes.
The default Layout
Manager of Panel and Panel sub classes is FlowLayout.
6) Why would you use SwingUtilities.invokeAndWait or SwingUtilities.invokeLater?
I want to update a Swing component but I’m not in a callback. If I want the
update to happen immediately (perhaps for a progress bar component) then I’d use
invokeAndWait. If I don’t care when the update occurs, I’d use invokeLater.
7) What is an event and what are the models available for event handling?
An event is an event object that describes a state of change in a source. In other words,
event occurs when an action is generated, like pressing button, clicking mouse, selecting
a list, etc. There are two types of models for handling events and they are: a) event-
inheritance model and b) event-delegation model
9) Why won’t the JVM terminate when I close all the application windows?
The AWT event dispatcher thread is not a daemon thread. You must explicitly call
System.exit to terminate the JVM.
10) What is meant by controls and what are different types of controls in AWT?
Controls are components that allow a user to interact with your application and the
AWT supports the following types of controls: Labels, Push Buttons, Check Boxes,
Choice Lists, Lists, Scrollbars, and Text Components. These controls are subclasses of
Component.
11) What is the difference between a Choice and a List?
A Choice is displayed in a compact form that requires you to pull it down to see
the list of available choices. Only one item may be selected from a Choice. A List may be
displayed in such a way that several List items are visible. A List supports the selection of
one or more List items.
13) What is the difference between the File and RandomAccessFile classes?
The File class encapsulates the files and directories of the local file system. The
RandomAccessFile class provides the methods needed to directly access data contained
in any part of a file.
10
There are two types of exceptions in Java, unchecked exceptions and checked
exceptions.
11
Unchecked exceptions: All Exceptions that extend the RuntimeException class are
unchecked exceptions. Class Error and its subclasses also are unchecked.
28) How does a try statement determine which catch clause should be used to handle an
exception?
When an exception is thrown within the body of a try statement, the catch clauses
of the try statement are examined in the order in which they appear. The first catch clause
that is capable of handling the exception is executed. The remaining catch clauses are
ignored.
30) What is the difference between checked and Unchecked Exceptions in Java?
All predefined exceptions in Java are either a checked exception or an unchecked
exception. Checked exceptions must be caught using try.. catch () block or we should
throw the exception using throws clause. If you dont, compilation of program will fail.
12
If there is a return statement in the try block, the finally block executes right after the
return statement encountered, and before the return executes.
13
Exceptions thrown by Java relate to fundamental errors that violate the rules of
the Java language or the constraints of the Java execution environment.
2. Exceptions can be manually generated by your code.
Manually generated exceptions are typically used to report some error condition
to the caller of a method.
43) Where does Exception stand in the Java tree hierarchy?
java.lang.Object
java.lang.Throwable
java.lang.Exception
java.lang.Error
swap();
46) How do you get the descriptive information about the Exception occurred during the
program execution?
All the exceptions inherit a method printStackTrace() from the Throwable class. This
method prints the stack trace from where the exception occurred. It prints the most
recently entered method first and continues down, printing the name of each method as it
works its way down the call stack from the top.
14
UNIT V
15
4) What is multithreading and what are the methods for inter-thread communication and
what is the class in which these methods are defined?
Multithreading is the mechanism in which more than one thread run independent of each
other within the process. wait (), notify () and notifyAll() methods can be used for inter-
thread communication and these methods are in Object class. wait() : When a thread
executes a call to wait() method, it surrenders the object lock and enters into a waiting
state. notify() or notifyAll() : To remove a thread from the waiting state, some other
thread must make a call to notify() or notifyAll() method on the same object.
When a task's interrupt() method is executed, the task enters the ready state. The next
time the task enters the running state, an InterruptedException is thrown.
9) How can we create a thread?
A thread can be created by extending Thread class or by implementing
Runnable interface. Then we need to override the method public void run().
10) What are three ways in which a thread can enter the waiting state?
A thread can enter the waiting state by invoking its sleep() method, by blocking
on I/O, by unsuccessfully attempting to acquire an object's lock, or by invoking an
object's wait() method. It can also enter the waiting state by invoking its (deprecated)
suspend() method.
11) How can i tell what state a thread is in ?
to Java 5, isAlive() was commonly used to test a threads state. If isAlive() returned false
16
the thread was either new or terminated but there was simply no way to differentiate
between the two.
12) What is synchronized keyword? In what situations you will Use it?
Synchronization is the act of serializing access to critical sections of code. We
will use this keyword when we expect multiple threads to access/modify the same data.
To understand synchronization we need to look into thread execution manner.
13) What is serialization?
Serialization is the process of writing complete state of java object into output
stream, that stream can be file or byte array or stream associated with TCP/IP socket.
14) What does the Serializable interface do?
Serializable is a tagging interface; it prescribes no methods. It serves to assign the
Serializable data type to the tagged class and to identify the class as one which the
developer has designed for persistence. ObjectOutputStream serializes only those
objects which implement this interface.
15) When you will synchronize a piece of your code?
When you expect your code will be accessed by different threads and these
threads may change a particular data causing data corruption.
16) 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.
17
This method contains the code that creates a new executable thread and is very
specialised. Your threaded application should either pass a Runnable type to a new
Thread, or extend Thread and override the run() method.
18