II-YEAR-III-SEM-CS8391-OBJECT-ORIENTED-PROGRAMMING Print
II-YEAR-III-SEM-CS8391-OBJECT-ORIENTED-PROGRAMMING Print
Questions
Q.N
o
1 Define Objects and classes in java?
instance of a class depicting the state and behavior at that particular time in
import java.util.Vector;
class Test {
Vector vector;
Test() {
vector = new Vector();
}
}
3 What is the default access to a member in a class?
Default is no access specifier. For classes, and interface declarations, the
default is package private. This falls between protected and private, allowing
only classes in the same package access. (protected is like this, but also allowing
access to subclasses outside of the package.
For interface members (fields and methods), the default access is public. But
note that the interface declaration itself defaults to package private.
4 What is a package?
A package is a namespace that organizes a set of related classes and
interfaces. Conceptually can think of packages as being similar to different
folders on our computer.
5 Define class.
Class is a template for a set of objects that share a common structure and a
common behaviour. A class is a blueprint, or prototype, that defines the variables
and the methods common to all objects of a certain kind.
{ /**
Javadoc utility enables you to keep the code and the documentation in
sync easily. The javadoc utility lets you put your comments right next to
your code, inside your ".java" source files.
All you need to do after completing your code is to run the Javadoc utility
to create your HTML documentation automatically.
● public
● protected
● default (no specifier)
● private
Private
Private methods and fields can only be accessed within the same class to
which the methods and fields belong. private methods and fields are not visible
within subclasses and are not inherited by subclasses. So, theprivate access specifier
is opposite to the public access specifier. It is mostly used for encapsulation: data are
hidden within the class and accessor methods are provided. An example, in which the
position of the upper-left corner of a square can be set or obtained by accessor
methods, but individual coordinates are not accessible to the user.
● The Object’s behaviour – What can you do with this object , or what
methods can you apply to it?
● The Object’s state – How does the object react when you apply those
methods?
● The Object’s identity – How is the object distinguished from others that
may have the same behaviour and state
finalize( ) method is used just before an object is destroyed and can be called
just prior to garbage collection.
An object has state (it has various properties, which might change).
An object has behavior (it can do things and can have things done to it).
written with the expectation that its concrete subclasses will add to its
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 /**. It ends with*/.
In Java have javadoc tags
Tag Meaning
@author Identifies the author of a class
@deprecated Specifies that a class or member is deprecated
@param Documents a method’s parameter
@return Documents a method’s return value
helps resolve naming conflicts when different packages have classes with the
same names.
Packages access level also allows protecting data from being used by the
non-authorized
classes.
27 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.
28 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.
PART-B
Q.No Questions
(1) Concatenation
(2) Substrings.
Refer page no:53
(iii) Explain any four methods available in string handling.
(4)
Refer page no: 53
3 i) Explain what is OOPS and explain the features of OOPS. (8) BTL5
Refer page no: 106
(ii) Discuss about the usage of constructor with an example. Using
Java. (8)
Refer page no: 144
4 (ii) Define package. Explain the types of package with its BTL5
importance. (8)
5 Explain briefly about object oriented programming concepts differ from BTL5
structured programming concepts. (16)
6 Write a java program for push and pop operations in stack using
arrays in classes and object. (16)
Refer page no:90
Refer notes
8 i) Describe the concept of OOP. (5)
Refer page no: 106
Refer notes
9
What is meant by package? How it is created and implemented in
JAVA. (8)
(ii) Write a JAVA program to find the smallest number in the given
list. (8)
Refer notes
11 i) Define class?
ii)Write short notes on Access specifiers (3)
iv) Explain the term static fields and methods and explain
its types with examples (8)
Questions
Q.N
o
1 What is meant by parameter passing constructors? Give example.
written with the expectation that its concrete subclasses will add to its
The idea of inheritance is simple but powerful: When you want to create a
new class and there is already a class that includes some of the code that you want,
you can derive your new class from the existing class. In doing this, you can reuse
the fields and methods of the existing class without having to write (and debug!)
them.
5 In java what is the use of Interfaces?
In the Java programming language, an interface is a reference type, similar to a
class, that can contain only constants, method signatures, and nested types. There
are no method bodies. Interfaces cannot be instantiated—they can only
be implemented by classes or extended by other interfaces.
To use an interface, we have to write a class that implements the interface. When
an instantiable class implements an interface, it provides a method body for each
of the methods declared in the interface.
This means that the Account class cannot be a superclass and the OverdraftAccount
class can no longer be its subclass.
8 What is Interface?
An interface is basically a kind of class. Like classes, interfaces contain
methods and variables but but with a major difference.The difference is that
interfaces define only abstract methods and final fields. This means that interfaces
do not specify any code to implement these methods and data fields contain only
constants
9 What is object cloning? How to object clone? What is meant by object
cloning?
It is the process of duplicating an object so that two identical objects will exist in
the memory at the same time.
Cloning means creating a copy of the object. The precise meaning of "copy"
may depend on the class of the object. The general intent is that, for any
object x, the expression:
10 Differentiate copying and cloning.
● clone - create something new based on something that exists.
● copying - copy from something that exists to something else (that
also already exists).
12
those defined in methods are called inner classes. An inner class can
An inner class is a nested class whose instance exists within an instance of its
enclosing class and has direct access to the instance members of its enclosing
instance
class <EnclosingClass>
{
class <InnerClass>
{
}
}
15 What is the use of ‘Super’ Keyword? Give an example.
● Single Inheritance.
● Multiple Inheritance (Through Interface)
● Multilevel Inheritance.
● Hierarchical Inheritance.
● Hybrid Inheritance (Through Interface)
30 List out the types of Constructor?
There are two type of constructor in Java: No-argument constructor: A constructor
that has no parameter is known as default constructor. If we don't define a
constructor in a class, then compiler creates default constructor(with no arguments)
for the class
PART-B
Q.No Questions
2 How Strings are handled in java? Explain with code, the creation
of Substring, Concatenation and testing for equality.
UNIT III
Questions CO Bloom’
Q.N s Level
o
1 What is the use of final keyword?
The final keyword is used in several different contexts to define an entity which cannot later be changed.
the keyword final is a simple but powerful tool that allows us to write code that is more readable,
enables the compiler to catch some
logic errors, and prevents accidental misuse of classes and member functions.
BTL1
2 How will create throw exception in exception handling? BTL1
Throwing Exceptions
No. We shouldn’t write any other statements in between try, catch and finally blocks.
They form a one unit.
Checked exceptions are the exceptions which are known to compiler. These
exceptions are checked at compile time only. Hence the name checked
exceptions. These exceptions are also called compile time exceptions. Because, these exceptions will be known
during compile time.
Unchecked exceptions are those exceptions which are not at all known to compiler. These exceptions occur only at
run time. These exceptions are also called as run time exceptions. All sub classes of
java.lang.RunTimeException and java.lang.Error are unchecked exceptions.
The exceptions which occur at run time are called as run time exceptions. These exceptions are unknown to
compiler. All sub classes of java.lang.RunTimeException and java.lang.Error are run time exceptions. These
exceptions are unchecked type of exceptions. For example, NumberFormatException, NullPointerException,
ClassCastException, ArrayIndexOutOfBoundException, StackOverflowError etc.
9 There are three statements in a try block – statement1, statement2 and statement3. After that there is a
catch block to catch the exceptions occurred in the try block. Assume that exception has occurred in
statement2. Does statement3 get executed or not?
No. Once a try block throws an exception, remaining statements will not be executed. control comes directly to
catch block.
10 Can we write only try block without catch and finally blocks?
No, It shows compilation error. The try block must be followed by either catch or finally block. You
can remove either catch block or finally block but not both
object;
3.close the file when we are finished reading from it.
19 What is stream?
A stream is a sequential and contiguous one-way flow of data.Java does not differentiate between the
various types of data sources or sinks (e.g., file or network) in stream I/O.
BTL1
20 Mention the different ways to generate an Exception?
There are two different ways to generate an Exception.
1. Exceptions can be generated by the Java run-time system.
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 the code.
Manually generated exceptions are typically used to report some error condition to
the caller of a method.
BTL1
OutOfMemoryError is the sub class of java.lang.Error which occurs when JVM runs out of
memory.
BTL1
22 Give some examples to checked exceptions?
NullPointerException, ArrayIndexOutOfBoundsException,
NumberFormatException
BTL1
Errors are mainly caused by the environment in which an application is running. For example,
OutOfMemoryError happens when JVM runs out of memory. Where as exceptions are mainly caused by the
application itself. For example, NullPointerException occurs when an application tries to access null object.
BTL1
29 Give example for built in exception ?
Q.No
3 Explain briefly about user defined exceptions and stack trace elements in exception handling
mechanisms.
5 What is meant by exceptions? Why it is needed?Describe the exception hierarchy. Write note on
Stack Trace Elements. Give example.
Questions CO Bloom’
Q.N s Level
o
1 What are the different states in thread?
● New: A new thread begins its life cycle in the new state. It remains in this state until the
program starts the thread. It is also referred to as a born thread.
● Runnable: After a newly born thread is started, the thread becomes runnable. A
thread in this state is considered to be executing its task.
thread waits for another thread to perform a task.A thread transitions back to the runnable state
only when another thread signals the waiting thread to continue executing.
● Timed waiting: A runnable thread can enter the timed waiting state for a specified interval of
time. A thread in this state transitions back to the runnable state when that time interval expires or
when the event it is waiting for occurs.
Terminated: A runnable thread enters the terminated state when it completes its task or otherwise terminates.
2 What do you mean Synchronization?
When two or more threads need access to a shared resource, they need some way to ensure that the resource
will be used by only one thread at a time.
The process by which this synchronization is achieved is called thread synchronization. The synchronized
keyword in Java creates a block of code referred to as a critical section.
Every Java object with a critical section of code gets a lock associated with the object. To
enter a critical section, a thread needs to obtain the corresponding object's lock. This is the
general form of the synchronized statement:
synchronized(object) {
// statements to be synchronized
3 Mention the two mechanisms for protecting a code block from concurrent access.
Java 5 introduces general purpose synchronizer classes, including semaphores, mutexes, barriers,
latches, and exchangers, which facilitate coordination between threads. These classes are a apart of the
java.util.concurrent package
BTL1
A thread is a program's path of execution. Most programs written today run as a single thread, causing
problems when multiple events or actions need to occur at the
same time. Let's say, for example, a program is not capable of drawing pictures while reading keystrokes. The
program must give its full attention to the keyboard input lacking the ability to handle more than one event at
a time. The ideal solution to this problem is the seamless execution of two or more sections of a program at
the same time.
5 What is multithreading.
Multithreading enables us to write very efficient programs that make maximum use of the CPU,
because idle time can be kept to a minimum.
The Java Virtual Machine allows an application to have multiple threads of execution running concurrently
Multithreaded applications deliver their potent power by running many threads concurrently within a single
program. From a logical point of view, multithreading means multiple lines of a single program can be
executed at the same time,
To avoid polling, Java includes an elegant interprocess communication mechanism via the following
methods:
wait( ): This method tells the calling thread to give up the monitor and go to sleep until some other
thread enters the same monitor and calls notify( ).
notify( ): This method wakes up the first thread that called wait( ) on the same object.
notifyAll( ): This method wakes up all the threads that called wait( ) on the same object.c The highest
priority thread will run first.
1) public Thread()
2) public Thread(String name)
3) Thread t1=new Thread();
4) Thread t2=newThread(“MYTHREAD”);
A thread sends an interrupt by invoking interrupt on the Thread object for the thread to be interrupted. For
the interrupt mechanism to work correctly, the interrupted thread must support its own interruption.
name: the name of the thread (used mainly for logging or other diagnostics)
id: the thread's id (a unique, positive long generated by the system when the thread was created)
daemon: the thread's daemon status. A daemon thread is one that performs services for other threads, or
periodically runs some task, and is not expected to run to completion.
This is one of better interview question in Generics. Generics is implemented using Type erasure,
compiler erases all type related information during compile time and no type related information is
available during runtime. for example List<String> is represented by only List at runtime. This was
done to ensure binary compatibility with the libraries which were developed prior to Java 5. you
don't have access to Type argument at runtime and Generic type is translated to Raw type by
compiler during runtime.
17 How to write a generic method which accepts generic argument and return Generic Type?
writing generic method is not difficult, instead of using raw type you need to use Generic Type
like T, E or K,V which are well known placeholders for
BTL1
Type, Element and Key, Value. Look on Java Collection framework for examples of generics
methods. In simplest form a generic method would look like:
Let's see the important differences between wait and sleep methods.
wait() sleep()
wait() method releases the lock sleep() method doesn't release the
29 List out the different Methods for Java Daemon thread by Thread class
The java.lang.Thread class provides two methods for java daemon thread.
No. Method Description
PART-B
Q.No Questions
1 (i) What is a thread? Explain its states and methods.
Refer page no:716
Explain thread properties. (8)
2 (i) Explain the methods of interrupting threads in java.
3 Explain the procedure for running a task in a separate thread and running multiple threads.
Refer page no:731
7 Explain the following (i) States of a thread with a neat diagram. [10]
Questions
Q.No
name is followed by a type parameter section. As with generic methods, the type parameter section
of a generic class can have one or more type parameters separated by commas. These classes
are known as parameterized classes or parameterized types because they accept one or more
parameters.
second )
{ this.first = first; this.second = second; }
Java Generic methods and generic classes enable programmers to specify, with a single method
declaration, a set of related methods or, with a single class declaration, a set of related types,
respectively.
Generics also provide compile-time type safety that allows programmers to catch invalid types at compile
time.
Using Java Generic concept we might write a generic method for sorting an array of objects, then invoke
the generic method with Integer arrays, Double arrays, String arrays and so on, to sort the array
elements.
BTL1
Swing is the next-generation GUI toolkit that Sun Microsystems has developed for the Java
language. It is essentially a vast component framework built over parts of the older AWT component
libraries used in Java 1.0 and 1.1.
10 What are the steps needed to show a Frame?
First Step: JFrame
Second Step: WindowListener Third Step: Adding a
Panel Fourth Step: Fonts in Panels Fifth Step: Basic
Graphics
6th Step: Basic Event Handling 7th Step: Window
Events
8th Step: Event Classes and Listener Interfaces 9th Step: Focus Event
10th Step: KeyBoard Events and Sketch Demo 11th Step: Mouse
Events and Mouse Demo
12th Step: Action Interface
An adapter class provides the default implementation of all methods in an event listener interface. Adapter
classes are very useful to process only few of the events that are handled by a particular event listener
interface. Define a new class by extending one of the adapter classes and implement only those events
relevant.
12 Define frame.
In graphics and desktop publishing applications, a rectangular area in which text or graphics can appear.
In HTML, refers to dividing the browser display area into separate sections, each of which is really a
different Web page.
getUIClassID()
Returns a string that specifies the name of the L&F class that renders this component.
isDefaultButton()
Gets the value of the defaultButton property, which if true means that
this button is the current default button for its JRootPane.
isDefaultCapable()
paramString()
extends Object
implements WindowListener
The GridBagLayout class is a flexible layout manager that aligns components vertically and horizontally,
without requiring that the components be of the same size. Each GridBagLayout object maintains a
dynamic, rectangular grid of cells, with each component occupying one or more cells, called its display
area.
A Frame is a top-level window with a title and a border. The size of the frame includes any area
designated for the border. The dimensions of the border area may be obtained using the getInsets
method. Since the border area is included in the overall size of the frame, the border effectively
obscures a portion of the frame, constraining the area available for rendering and/or displaying
subcomponents to the rectangle which has an upper-left corner location
of (insets.left, insets.top), and has a size of width - (insets.left + insets.right) by height
- (insets.top + insets.bottom).
A frame, implemented as an instance of the JFrame class, is a window that has decorations such as a border, a
title, and supports button components that close or iconify the window. Applications with a GUI usually
include at least one frame
20 What method is used to distinguish between single, double and triple mouse clicks?
JPanel is a generic lightweight container. For examples and task-oriented documentation for
JPanel,
All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, Accessible
Direct Known Subclasses:
AbstractColorChooserPanel, JSpinner.DefaultEditor
BTL1
22 What is the function of
a. Set Layout - The SetLayout function changes the layout of a device context (DC).
b. Flow Layout
BTL1
23 Write a note on push Button Control?
A push button is a component that contains a label and that generates as event when it is pressed.
Push buttons are objects of type Button. Button defines these two constructors.
Button( )
Button( String str)
The first version creates an empty button. The second creates a button that contains str as a label.
BTL1
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)
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.
28 Difference between Swing and Awt?
AWT are heavy-weight componenets. Swings are light-weight components. Hence swing works
faster than AWT.
BTL2
29 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
BTL1
A Scrollbar is a Component, but not a Container whereas Scrollpane is a Conatiner and handles its
own events and perform its own scrolling.
BTL1
PART-B
Questions
8 Give the methods available in graphics for COLOR and FONTS Color and fonts Using Color
BTL5