It2301 QB
It2301 QB
http://www.francisxavier.ac.in
Department of Information Technology Francis Xavier Engineering College
Object is an instance of a class and it is a software unit that combines a structured set of
data with a set of operations for inspecting and manipulating that data. When an object is
created using new operator, memory is allocated to it.
12) Explain the usage of Java packages.
This is a way to organize files when a project consists of multiple modules. It also
helps resolve naming conflicts when different packages have classes with the same names.
Packages access level also allows you to protect data from being used by the non-
authorized classes.
13) What is method overloading and method overriding?
Method overloading: When a method in a class having the same method name
with different arguments is said to be method overloading.
Method overriding: When a method in a class having the same method name with
same arguments is said to be method overriding.
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 initializes 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.
16) What is the difference between constructor and method?
Constructor will be automatically invoked when an object is created whereas
method has to be called explicitly
17) What are Static member classes?
A static member class is a static member of a class. Like any other static method,
a static member class has access to all static methods of the parent, or top-level, class.
18) What is Garbage Collection and how to call it explicitly?
When an object is no longer referred to by any variable, java automatically
reclaims memory used by that object. This is known as garbage collection.
System. gc() method may be used to call it explicitly
19) In Java, How to make an object completely encapsulated?
All the instance variables should be declared as private and public getter and
setter methods should be provided for accessing the instance variables.
20) What is static variable and static method?
Static variable is a class variable which value remains constant for the entire class. Static
method is the one which can be called with the class itself and can hold only the static
variables
21) What is finalize () method?
Finalize () method is used just before an object is destroyed and can be called just
prior to garbage collection.
22) What is the difference between String and String Buffer?
a) String objects are constants and immutable whereas StringBuffer objects are not.
b) String class supports constant strings whereas StringBuffer class supports growable
and modifiable strings.
http://www.francisxavier.ac.in
Department of Information Technology Francis Xavier Engineering College
http://www.francisxavier.ac.in
Department of Information Technology Francis Xavier Engineering College
http://www.francisxavier.ac.in
Department of Information Technology Francis Xavier Engineering College
b) In abstract class, key word abstract must be used for the methods whereas
interface we need not use that keyword for the methods.
c) Abstract class must have subclasses whereas interface can‟t have subclasses.
19) 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.
20) What is interface and its use?
Interface is similar to a class which may contain method‟s signature only but not
bodies and it is a formal set of method and constant declarations that must be defined by
the class that implements it. Interfaces are useful for:
a) Declaring methods that one or more classes are expected to implement
b) Capturing similarities between unrelated classes without forcing a class relationship.
c) Determining an object‟s programming interface without revealing the actual body of
the class.
21) How is polymorphism acheived in java?
Inheritance, Overloading and Overriding are used to acheive Polymorphism in java.
22) What modifiers may be used with top-level class?
public, abstract and final can be used for top-level class.
23) What is a cloneable interface and how many methods does it contain?
It is not having any method because it is a TAGGED or MARKER interface.
24) What are the methods provided by the object class?
The Object class provides five methods that are critical when writing multithreaded Java
programs:
notify
notifyAll
wait (three versions)
25) Define: Dynamic proxy.
A dynamic proxy is a class that implements a list of interfaces, which you specify at
runtime when you create the proxy. To create a proxy, use the static method
java.lang.reflect.Proxy::newProxyInstance(). This method takes three arguments:
The class loader to define the proxy class
An invocation handler to intercept and handle method calls
A list of interfaces that the proxy instance implements
26) What is object cloning?
It is the process of duplicating an object so that two identical objects will exist in
the memory at the same time.
UNIT III
1) 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.
2) How would you create a button with rounded edges?
There‟s 2 ways. The first thing is to know that a JButton‟s edges are drawn by a
Border. so you can override the Button‟s paintComponent(Graphics) method and draw a
circle or rounded rectangle (whatever), and turn off the border. Or you can create a
custom border that draws a circle or rounded rectangle around any component and set the
button‟s border to it.
3) What is the difference between the „Font‟ and „FontMetrics‟ class?
http://www.francisxavier.ac.in
Department of Information Technology Francis Xavier Engineering College
The 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)
4) 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.
5) Which containers use a border Layout as their default layout?
The window, Frame and Dialog classes use a border layout as their default layout.
6) What is the difference between applications and applets?
a) Application must be run on local machine whereas applet needs no explicit installation
on local machine.
b) Application must be run explicitly within a java-compatible virtual machine whereas
applet loads and runs itself automatically in a java-enabled browser.
c) Application starts execution with its main method whereas applet starts execution with
its init method.
d) Application can run with or without graphical user interface whereas applet must run
within a graphical user interface.
7) Difference between Swing and Awt?
AWT are heavy-weight components. Swings are light-weight components. Hence
swing works faster than AWT.
8) 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.
9) 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.
10) 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.
11) 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
http://www.francisxavier.ac.in
Department of Information Technology Francis Xavier Engineering College
a list, etc. There are two types of models for handling events and they are: a) event-
inheritance model and b) event-delegation model
12) What is the difference between scrollbar and scrollpane?
A Scrollbar is a Component, but not a Container whereas Scrollpane is a Conatiner and
handles its own events and perform its own scrolling.
13) 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.
14) 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.
15) 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.
16) What is the purpose of the enableEvents() method?
The enableEvents() method is used to enable an event for a particular object.
Normally,an event is enabled when a listener is added to an object for a particular event.
The enableEvents() method is used by objects that handle events by overriding their
eventdispatch methods.
17) 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.
18) What is the lifecycle of an applet?
init() method - Can be called when an applet is first loaded start() method - Can
be called each time an applet is started. paint() method - Can be called when the applet is
minimized or maximized. stop() method - Can be used when the browser moves off the
applet‟s page. destroy() method - Can be called when the browser is finished with the
applet.
19) What is the difference between a MenuItem and a CheckboxMenuItem?
The CheckboxMenuItem class extends the MenuItem class to support a menu item that
may be checked or unchecked.
20) What class is the top of the AWT event hierarchy?
The java.awt.AWTEvent class is the highest-level class in the AWT event-class hierarchy.
21) What is source and listener?
source : A source is an object that generates an event. This occurs when the internal state
of that object changes in some way.
listener : A listener is an object that is notified when an event occurs. It has two major
requirements. First, it must have been registered with one or more sources to receive
notifications about specific types of events. Second, it must implement methods to
receive and process these notifications.
http://www.francisxavier.ac.in
Department of Information Technology Francis Xavier Engineering College
http://www.francisxavier.ac.in
Department of Information Technology Francis Xavier Engineering College
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.
http://www.francisxavier.ac.in
Department of Information Technology Francis Xavier Engineering College
http://www.francisxavier.ac.in
Department of Information Technology Francis Xavier Engineering College
UNIT V
1) Explain different way of using thread?
The thread could be implemented by using runnable interface or by inheriting from the
Thread class. The former is more advantageous, 'cause when you are going for multiple
inheritance..the only interface can help.
http://www.francisxavier.ac.in
Department of Information Technology Francis Xavier Engineering College
Prior to Java 5, isAlive() was commonly used to test a threads state. If isAlive() returned
false 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) What is the difference between yielding and sleeping?
When a task invokes its yield() method, it returns to the ready state. When a task
invokes its sleep() method, it returns to the waiting state.
18) What is casting?
There are two types of casting, casting between primitive numeric types and
casting between object references. Casting between numeric types is used to convert
larger values, such as double values, to smaller values, such as byte values. Casting
between object references is used to refer to an object by a compatible class, interface,
or array type reference.
19) What classes of exceptions may be thrown by a throw statement?
A throw statement may throw any expression that may be assigned to the
Throwable type.
20) A Thread is runnable, how does that work?
The Thread class' run method normally invokes the run method of the Runnable
type it is passed in its constructor. However, it is possible to override the thread's run
method with your own.
21) Can I implement my own start() method?
The Thread start() method is not marked final, but should not be overridden. 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.
22) Do I need to use synchronized on setValue(int)?
It depends whether the method affects method local variables, class static or
http://www.francisxavier.ac.in
Department of Information Technology Francis Xavier Engineering College
instance variables. If only method local variables are changed, the value is said to be
confined by the method and is not prone to threading issues.
23) What is thread priority?
Thread Priority is an integer value that identifies the relative order in which it
should be executed with respect to others. The thread priority values ranging from 1- 10
and the default value is 5. But if a thread have higher priority doesn't means that it will
execute first. The thread scheduling depends on the OS.
24) What are the different ways in which a thread can enter into waiting state?
There are three ways for a thread to enter into 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.
25) How would you implement a thread pool?
The ThreadPool class is a generic implementation of a thread pool, which takes
the following input Size of the pool to be constructed and name of the class which
implements Runnable (which has a visible default constructor) and constructs a thread
pool with active threads that are waiting for activation. once the threads have finished
processing they come back and wait once again in the pool.
26) What is a thread group?
A thread group is a data structure that controls the state of collection of thread as a whole
managed by the particular runtime environment.
UNIT I
1. Explain briefly the following object oriented paradigms
i. abstraction and encapsulation (4)
An essential element of object-oriented programming is abstraction. Abstraction
refers to the act of representing essential features without including the background
details or explanations. Encapsulation is the mechanism that binds together code and
the data it manipulates, and keeps both safe from outside interference and misuse.
Types : function abstraction, data abstraction.
ii. Methods and messages (4)
Message: a request for execution of a procedure
Method: a receiving object that generates the desired result.
Benefits of messages.
iii. inheritance (4)
Inheritance is the process by which one object acquires the properties of another
object. This is important because it supports the concept of hierarchical classification.
iv. Polymorphism (4)
Polymorphism means the ability to take more than one form. Subclasses of a class
can define their own unique behaviors and yet share some of the same functionality of
the parent class.
http://www.francisxavier.ac.in
Department of Information Technology Francis Xavier Engineering College
4. Give the syntax for static method and its initialization? (4)
Static methods are methods that do not operate on objects.
Static
{
// code
}
Example program
5. What is a class? How do you define a class in java? (4)
A class is a user-defined data type with a template that serves to define its properties.
The basic form of a class definition is:
Syntax:
Class classname [ extends superclassname]
{
Field declaration;
http://www.francisxavier.ac.in
Department of Information Technology Francis Xavier Engineering College
Method definition;
}
Instantiating a class
Accessing the members of a class.
6. Create a Class called Math that supports the following constants and methods.
(8)
a. pi=3.14159
b. Area of rectangle that is passed a height and a width.
c. perimeter of rectangle that is passed a height and width.
d. area of circle that is passed a radius
e. perimeter of circle that is passed a radius
class math
{
Const long pi=3.14154;
Void peri(int height,int width);
Void area(int height, int width);
Void peri(int radius);
Void area(int radius);
}
Class Mathtest
{
Public static void main(String args[])
{
Math m= new math();
m.peri(10,20);
m.area(10.20);
m.peri(20);
m.area(20);
}
http://www.francisxavier.ac.in
Department of Information Technology Francis Xavier Engineering College
Types:
1. Default constructor
Default field initialization
Explicit field initialization
2. Parameterized constructor
8. Explain the basic concepts of object oriented programming with real time
example. (16)
1. object
2. class
3. data abstraction
4. encapsulation
5. inheritance
6. polymorphism
7. dynamic binding
8. message communication
UNIT II
1. What is a package? What are the benefits of using packages? Write down the
steps in creating packages and using it in a java program with an example.
(16)
Java allows you to group classes in a collection called a package. A class can use
all classes from its own package and all public classes from other packages. You
can import a specific class or the whole package. You place import statements at
the top of your source files (but below any package statements). For example, you
can import all classes in the java.util package with the statement: import
java.util.*;
Package scope
Public features can be used by any class.
Private features can only be used by the class that defines them.
If you don't specify either public or private, then the feature (that is, the class,
method, or variable) can be accessed by all methods in the same package.
Advantages of Packages:
Classes can be easily reused.
Two classes in different packages can have same name.
It provide a way to hide classes.
http://www.francisxavier.ac.in
Department of Information Technology Francis Xavier Engineering College
http://www.francisxavier.ac.in
Department of Information Technology Francis Xavier Engineering College
The substring() method creates a new string that is the substring of the string that
invokes the method. The method has two forms:
public String substring(int startindex)
public String substring(int startindex, int endindex)
where, startindex specifies the index at which the substring will begin and endindex
specifies the index at which the substring will end. In the first form where the
endindex is not present, the substring begins at startindex and runs till the end of the
invoking string.
http://www.francisxavier.ac.in
Department of Information Technology Francis Xavier Engineering College
Advantages
Types
a. Single inheritance
b. Multiple inheritance
c. Multilevel inheritance
d. Hierarchical inheritance
e. Hybrid inheritance
11. Explain polymorphism in detail? (16)
Polymorphism is the ability of an object to take on many forms. The most
common use of polymorphism in OOP occurs when a parent class reference is
used to refer to a child class object.
Method overloading
Method overriding
12. Discuss the various methods of java documentation. (8)
Java supports three types of comments. The first two are the // and the /* */. The
third type is called a documentation comment. It begins with the character
sequence /** and it ends with */.
Documentation comments allow you to embed information about your program
into the program itself. You can then use the javadoc utility program to extract the
information and put it into an HTML file.
Documentation comments make it convenient to document your programs.
Tag Description Example
@author Identifies the author of a class. @author description
Specifies that a class or member is
@deprecated @deprecated description
deprecated.
Specifies the path to the root
{@docRoot} directory of the current Directory Path
documentation
Identifies an exception thrown by a @exception exception-name
@exception
method. explanation
Inherits a comment from the Inherits a comment from the
{@inheritDoc}
immediate superclass. immediate surperclass.
Inserts an in-line link to another
{@link} {@link name text}
topic.
Inserts an in-line link to another
Inserts an in-line link to another
{@linkplain} topic, but the link is displayed in a
topic.
plain-text font.
@param parameter-name
@param Documents a method's parameter.
explanation
@return Documents a method's return @return explanation
http://www.francisxavier.ac.in
Department of Information Technology Francis Xavier Engineering College
value.
@see Specifies a link to another topic. @see anchor
Documents a default serializable
@serial @serial description
field.
Documents the data written by the
@serialData writeObject( ) or writeExternal( ) @serialData description
methods
Documents an ObjectStreamField @serialField name type
@serialField
component. description
States the release when a specific
@since @since release
change was introduced.
The @throws tag has the same
@throws Same as @exception.
meaning as the @exception tag.
Displays the value of a constant, Displays the value of a constant,
{@value}
which must be a static field. which must be a static field.
@version Specifies the version of a class. @version info
if ( n != p )
http://www.francisxavier.ac.in
Department of Information Technology Francis Xavier Engineering College
UNIT III
1. What is object cloning? Why it is needed? Explain how objects are cloned?
(16)
A clone of an object is a new object that has the same state as the original. In
particular, you can modify the clone without affecting the original.
http://www.francisxavier.ac.in
Department of Information Technology Francis Xavier Engineering College
http://www.francisxavier.ac.in
Department of Information Technology Francis Xavier Engineering College
9. Explain briefly the various stream classes with suitable examples. (6)
Byte stream
o Input stream
o Output stream
Character stream
o Reader class
o Writer class
http://www.francisxavier.ac.in
Department of Information Technology Francis Xavier Engineering College
Since a proxy class implements all of the interfaces specified at its creation,
invoking getInterfaces on its Class object will return an array containing the same
list of interfaces (in the order specified at its creation), invoking getMethods on its
Class object will return an array of Method objects that include all of the methods
in those interfaces, and invoking getMethod will find methods in the proxy
interfaces as would be expected.
The Proxy.isProxyClass method will return true if it is passed a proxy class-- a
class returned by Proxy.getProxyClass or the class of an object returned by
Proxy.newProxyInstance-- and false otherwise.
The java.security.ProtectionDomain of a proxy class is the same as that of system
classes loaded by the bootstrap class loader, such as java.lang.Object, because the
code for a proxy class is generated by trusted system code. This protection
domain will typically be granted java.security.AllPermission.
UNIT IV
1. Explain generic classes and methods.
They are static methods that can be invoked for all methods.
Generic classes are classes that can be used for all types of values
2. Explain exception hierarchy.
Try
Catch
Throw
Throws
finally
3. What are the advantages of Generic Programming?
To be specified later types
That are instantiated when needed for specific types
4. Explain the different ways to handle exceptions.
Checked exceptions
Unchecked exceptions
5. Explain the model view controller design pattern and list the advantages and
disadvantages.
Model
View
http://www.francisxavier.ac.in
Department of Information Technology Francis Xavier Engineering College
Controller
UNIT V
1. Explain the different states of a thread.
Newborn
Runnable
Running
Waiting
Timed waiting
Death state
http://www.francisxavier.ac.in