Define OOP.: Unit I - Introduction To Oop and Fundamentals of Java
Define OOP.: Unit I - Introduction To Oop and Fundamentals of Java
1. Define OOP.
Object-Oriented Programming (OOP) is a methodology or paradigm to design a program
using classes and objects. It simplifies the software development and maintenance by
providing some concepts:
○ Object
○ Class
○ Inheritance
○ Polymorphism
○ Abstraction
○ Encapsulation
2. Define object and class.
Any entity that has state and behavior is known as an object. For example: chair, pen, table,
keyboard, bike etc. It can be either physical or logical.
A class is the basic building block of an object-oriented language. It is a template that
describes the data and behavior associated with instances of that class. The data associ- ated
with a class or object is stored in variables and the behavior associated with a class or object
is implemented with methods.
3. How can we create an instance of a class in java?
To create an instance of a class:
Declare an instance identifier (instance name) of a particular class.
Construct the instance (i.e., allocate storage for the instance and initialize the instance)
using the “new” operator.
4. Define Inheritance.
When one object acquires all the properties and behaviours of parent object, it is known as
inheritance. It provides code reusability. It is used to achieve runtime polymorphism.
5. What are the types of inheritance in java?
1. Single Inheritance
2. Multilevel Inheritance
3. Hierarchical Inheritance
4. Hybrid Inheritance
6. Define Polymorphism.
Polymorphism means taking many forms, where „poly‟ means many and „morph‟ means
forms. It is the ability of a variable, function or object to take on multiple forms.
7. Define abstraction.
Hiding internal details and showing functionality is known as abstraction. For example:
phone call, we don‟t know the internal processing.
8. Define encapsulation.
Binding (or wrapping) code and data together into a single unit is known as
encapsulation. For example: capsule, it is wrapped with different medicines.
A java class is the example of encapsulation.
9. List the advantage of OOPs over Procedure-oriented programming language
OOPs makes development and maintenance easier. But in Procedure-oriented program- ming
language, it is not easy to manage if code grows as project size grows.
OOPs provides data hiding whereas in Procedure-oriented programming
language, global data can be accessed from anywhere.
OOPs provides ability to simulate real-world event much more effectively. We can
provide the solution of real word problem if we are using the Object- Oriented
Programming language.
10. What is difference between object-oriented programming language and object-based
programming language?
Object oriented language
Object-oriented language supports all the features of OOPs.
Object-oriented language doesn‟t has in-built object.
Object-oriented languages are C++, C#, Java etc.
type size
byte 8 bits
short 16 bits
int 32 bits
long 64 bits
type size
float 32 bits
double 64 bits
Example:
PART-B
1. Explain about packages. Give an example program which uses packages. (16) (CS1261
NOV /DEC 2016) (IT2301 NOV/DEC-2012)
2. Explain with the help of a program how object oriented programming overcomes the
shortcomings of procedure oriented programming.(8) (IT2301 APR/MAY-2015)
3. Given two one dimensional arrays A and B which are sorted in ascending order. Write a Java
program to merge them into a single sorted array, see that is contains every item from array
A and B, in ascending order. (8) (IT2301 APR/MAY-2015)
4. With an example, describe in detail about how polymorphism plays a useful role in Java. (8)
(IT2301 APR/MAY-2015)
5. Elaborate on the various object oriented concepts, with necessary illustrations. (16) (IT2301
MAY/JUNE-2014)
6. Write a program to perform the following functions using classes, objects, constructors and
destructors where essential. (IT2301 MAY/JUNE-2014)
a. Get as input the marks of 5 students in 5 subjects. (4)
b. Calculate the total and average. (8)
c. Print the formatted result on the screen. (4)
7. With suitable examples explain how packages can be created, imported and used. Also
elaborate on its scope. (16) (IT2301 MAY/JUNE-2014)
8. Write a program to perform following functions on a given matrix.
(IT2301 MAY/JUNE-2014)
a. Find the row and column sum. (8)
b. Interchange rows and columns. (8)
9. Describe the structure of Java program. (8) (IT2301 NOV/DEC-2012)
10. Explain the features of Java. (8) (IT2301 NOV/DEC-2012)
11. Write a simple Java program to implement basic Calculator operations.(16)
(CS2311 NOV/DEC-2014)
12. How packages are used to resolve naming conflicts in Java? With an example show to add
classes to packages and how to import packages in classes. (16)
(CS2311 NOV/DEC-2014)
13. Write a program in Java that interchanges the odd and even components of an array in
Java. (16) (CS2311 APR/MAY-2015)
14. Write a Java program to sort set of names stored in an array in alphabetical order. (16)
(CS2311 APR/MAY-2015)
15. Explain the arrays and its types in detail with example program.
(CS2311 MAY/JUNE-2014)
16. Briefly explain about the key elements of Object Oriented Programming.(16)
(CS1361 NOV/DEC-2014)
17. Explain about Class, Objects and Methods in Java with an example program. (16)
(CS1361 NOV/DEC-2014)
21. Explain the arrays and its types in detail with example program.
(CS2311 MAY/JUNE-2014)
22. Briefly explain about the key elements of Object Oriented Programming.(16)
(CS1361 NOV/DEC-2014)
23. Explain about Class, Objects and Methods in Java with an example program. (16)
(CS1361 NOV/DEC-2014)
24. Describe the following: (CS1361 NOV/DEC-2014)
a. Features of Java (8)
b. Data types in Java (8)
25. Explain about Package in Java. List built in Java API packages. (16)
(CS1361 NOV/DEC-2014)
26. Discuss the various parameter passing methods in Java. Illustrate with examples.
(CS1361 NOV/DEC-2014)
{
System.out.println(“running...”);
}
}
public class Honda2 extends Bike
{
public static void main(String args[]){
new Honda2().run();
}
}
16. What is blank or uninitialized final variable?
A final variable that is not initialized at the time of declaration is known as blank
final variable.
If we want to create a variable that is initialized at the time of creating object and
once initialized may not be changed, it is useful.
It can be initialized only in constructor.
17. Define static blank final variable.
A static final variable that is not initialized at the time of declaration is known as
static blank final variable. It can be initialized only in static block.
Example of static blank final variable
public class A{
static final int data;//static blank final variable
static{ data=50;}
public static void main(String args[]){
System.out.println(A.data);
}
}
18. What is meant by interface?
An interface is a reference type in Java. It is similar to class. It is a collection of abstract
methods. Along with abstract methods, an interface may also contain constants, default
methods, static methods, and nested types.
19. What’s the difference between an interface and an abstract class?
An abstract class may contain code in method bodies, which is not allowed in an interface.
With abstract classes, we have to inherit our class from it and Java does not allow multiple
inheritance. On the other hand, we can implement multiple interfaces in your class.
20. What are the uses of interface?
Interfaces are used to implement abstraction.
Since java does not support multiple inheritance in case of class, it can be achieved by
using interface.
It is also used to achieve loose coupling.
21. Define nested interface.
An interface which is declared inside another interface or class is called nested inter-
face. They are also known as inner interface. Since nested interface cannot be accessed
directly, the main purpose of using them is to resolve the namespace by grouping
related interfaces (or related interface and class) together.
22. How will you find out the length of a string in java? Give an example?
The length ( ) method is used to number of characters is string. For example,
String str=”Hello”;
24. How would you make a copy of an entire java object with its state?
Have this class implement Cloneable interface and call its method clone().
25. What is the basic difference between string and stringbuffer object? –
String is an immutable object. StringBuffer is a mutable object.
26. What is inner class?
If the methods of the inner class can only be accessed via the instance of the inner
class, then it is called inner class.
27. What’s the difference between an interface and an abstract class?
An abstract class may contain code in method bodies, which is not allowed in an inter-
face. With abstract classes, you have to inherit your class from it and Java does not allow
multiple inheritance. On the other hand, you can implement multiple interfaces in your
class.
28. What would you use to compare two string variables - the operator == or the method
equals()?
I’d use the method equals() to compare the values of the Strings and the = = to check
if two variables point at the same instance of a String object.
29. can an inner class declared inside of method access local variables of this
method?
It’s possible if these variables are final.
30. What’s the main difference between a vector and an arraylist ?
Java Vector class is internally synchronized and ArrayList is not.
PART -B
1. Explain overriding methods and final methods in Java.(8) (CS1261 MAY /JUNE 2016)
2. Create a Java class Shape with constructor to initialize the one parameter “dimension”.
Now create three sub classes of Shape with following methods: (16)
(IT2301 APR/MAY-2015)
a. “Circle” with methods to calculate the area and circumference of the circle with di-
mension as radius
b. “Square” with methods to calculate the area and length of diagonal of the square with
dimension as length of one side.
c. “Sphere” with methods to calculate the volume and surface area of the sphere with
dimension as radius of the sphere. Write appropriate main method to create object of each
class and test every method.
3. Explain with example how multiple inheritance is achieved in Java. (16)
(IT2301 APR/MAY-2015)
4. What are interfaces? Explain with an example how multiple inheritance is
implemented using interfaces. (16) (CS2311 NOV/DEC-2014)
5.Develop a Library interface which has drawbook(), returnbook() (with fine), checksta- tus()
and reservebook() methods. All the methods are tagged with public in the following ways:
(16) (IT2301 APR/MAY-2015)
a. Using draw book() - get the required book based on title
b. Using checkstatus – user book returned date details
c. Using with fine() – whether failed to return the book within a time period charge
–
Rs.5/day
d. Using reserve book() – block or reserve particular book for their account.
5. Explain about inheritance in Java. (16) (IT2301 NOV/DEC-2012) (CS1361
NOV/DEC- 2014)
6. Explain the interfaces in detail with suitable example. (CS2311 MAY/JUNE-2014)
Syntax:
class User_defined_name extends Exception{
………..
}
Java throw keyword is used to explicitly Java throws keyword is used to declare an
throw an exception. exception.
Throw is used within the method. Throws is used with the method signa-
ture.
You cannot throw multiple exceptions. You can declare multiple exceptions e.g.
public void method()throws IO Exception,
SQL Exception.
25. What is Byte stream in Java? list some of the Bytestream classes.
The byte stream classes provide a rich environment for handling byte-oriented I/O.
List of Byte Stream classes
Byte Array Input Stream
Byte Array Output Stream
Filtered Byte Streams
26. What is character stream in Java? list some of the characterstream classes.
The Character Stream classes provide a rich environment for handling character-oriented
I/O.
List of Character Stream classes
File Reader
File Writer
Char Array Reader
Char Array Writer
Objects can be created using the keyword new and there are several types of
constructors available.
ii) Following constructor takes a file object to create an output stream object to
write the file. First, we create a file object using File() method as follows:
File f = new File(“filename “);
OutputStream f = new FileOutputStream(f);
PART -B
1. Explain exception handling in Java with an example. (8)(CS1261 MAY /JUNE 2016)
2. Explain with an example the exception handling feature in Java.(8)
(CS1261 NOV /DEC 2016) (CS2311 APR/MAY-2015)
3. Explain I/O streams with suitable examples. (8) (IT2301 APR/MAY-2015)
4. Discuss about the Java error handling mechanism? What is the difference between “un-
checked exceptions” and “checked exceptions”? What is the implication of catching all the
exceptions with the type “Exception”? (8) (IT2301 APR/MAY-2015)
5. How are exceptions handled in Java? Elaborate with suitable examples. (8)
(IT2301 MAY/JUNE-
2014)
6. Describe about different input and output streams and their classes. (16)
(IT2301 NOV/DEC-
2012)
9. Explain the use of File stream classes and file modes. Write an example program for file
manipulation. (16) (CS1361 NOV/DEC-2014)
10. Describe different types of exceptions. Write a program for handling Array out of bounds
Exception. (16) (CS1361 NOV/DEC-2014)
11. Write a Java program that asks the user for a number and displays ten times the number. Also
the program must throw an exception when the user enters a value greater than 10. (16)
(CS1361 MAY/JUNE-2013)
UNIT –IV
1.What are the benefits of Multithreading?
Multithreading increases the responsiveness of system as, if one thread of the
application is not responding, the other would respond in that sense the user would not
have to sit idle.
Multithreading allows resource sharing as threads belonging to the same process can
share code and data of the process and it allows a process to have multiple threads at the
same time active in same address space.
Creating a different process is costlier as the system has to allocate different memory
and resources to each process, but creating threads is easy as it does not require
allocating separate memory and resources for threads of the same process.
2.What are the differences between Multithreading and Multitasking?
Parameter Multithreading Multitasking
Definition Multithreading is to execute Multitasking is to run mul-
multiple threads in a process tiple processes on a computer
concurrently. concurrently.
3. Define thread.
A thread is a basic execution unit which has its own program counter, set of the register and
stack. But it shares the code, data, and file of the process to which it belongs.
4. list the states of thread life cycle.
A thread at any point of time exists in any one of the following states.
New
Runnable
Blocked
Waiting
Timed Waiting
Terminated
5.list some methods supported by threads.
join(): It makes to wait for this thread to die. We can wait for a thread to finish by
calling its join() method.
sleep(): It makes current executing thread to sleep for a specified interval of time.
Time is in milli seconds.
yield(): It makes current executing thread object to pause temporarily and gives
control to other thread to execute.
notify(): This method is inherited from Object class. This method wakes up a single
thread that is waiting on this object’s monitor to acquire lock.
notifyAll(): This method is inherited from Object class. This method wakes up all
threads that are waiting on this object’s monitor to acquire lock.
wait(): This method is inherited from Object class. This method makes current thread to
wait until another thread invokes the notify() or the notifyAll() for this object.
6.Can we start a thread twice?
No. After starting a thread, it can never be started again. If we do so, an Illegal Thread-State
Exception is thrown. In such case, thread will run once but for second time, it will throw
exception.
Syntax:
<type-parameter> return_type method_name (parameters) {...}
Example:
public void display(T data) {
System.out.println(data);
}
29.What is the question mark in Java Generics used for?
The wildcard element is represented by ? and it specifies an unknown type i.e. it means
any type. For example,
<? extends Number>
specifies any child classes of class Number e.g. Integer, Float, double etc.
30.What do you mean by bounded type parameters?
In Generics, bounded type parameter set restriction on the type that will be allowed to
pass to a type-parameter.
Syntax:
31.What is Erasure?
The Java compiler applies type erasure to implement generic programming. It replaces all
type parameters in generic types with their bounds or Object if the type parameters are
unbounded.Example: Java class with Generics
Example: T replaced by java.lang.Object
class Gen<T>{
Part-B
2. Write a complex program to illustrate about thread priorities. Imagine that the first thread
has just begun to run, even before it has a chance to do anything. Now the higher priority
thread that wants to run as well. Now the higher priority thread has to do its work before
the first thread starts. (16). (IT2301 APR/MAY-2015)
3. Explain life cycle of a thread with help of a diagram. How do the wait and notify all/no-tify
methods enable cooperation between thread? (8) (IT2301 APR/MAY-2015)
4. Explain in detail about generic method with a suitable example. (8)
i. (IT2301 APR/MAY-2015)
5. With illustrations explain multithreading, interrupting threads, thread states and thread
priorities. (16) (IT2301 MAY/JUNE-2014)
6. What is a Thread? What are the different states of Thread? Explain the creation of Thread
with an example program. (16) (CS1361 NOV/DEC-2014)
7. Using generic classes, write a program to perform the following operations on an array.
(IT2301 MAY/JUNE-2014)
Add an element in the beginning/middle/end. (8)
Delete an element from a given position. (8)
iii) What are interrupting threads? Explain thread states and synchronization. (16)
(IT2301 NOV/DEC-2012)
8.What is multithreading? Explain the two methods of implementing threads with an ex-ample.
(16)(CS2311 NOV/DEC-2014)
9.Explain the lifecycle of a thread in detail with example program.
(CS2311 MAY/JUNE-2014)
UNIT-V
TWO MARKS QUESTION & ANSWER
1. What is the difference between the ‘Font’ and ‘FontMetrics’ class?
Font class is used to set or retrieve the screen fonts.The Font class maps the characters of the
language to their respective glyphs.
The FontMetrics class defines a font metrics object, which encapsulates information about
the rendering of a particular font on a particular screen.i.e they provide access to the
attributes of the font object.
2. What is the difference between the paint() and repaint() methods?
paint() repaint()
The paint() method is called when Whenever a repaint method is called, the up-
some action is performed on the win- date method is also called along with paint()
dow. method.
This method supports painting via This method is used to cause paint() to be in-
graphics object. voked by the AWT painting thread.
Java application on contains a main An applet does not contain a main method
method
Generally used for console programs Generally used for GUI interfaces
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. 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, select-ing a list,
etc. There are two types of models for handling events and they are: a) event-inheritance
model and b) event-delegation model
Look and Feel of swing can be changed. There is no such feature in AWT.
AWT doesn’t support pluggable look and Swing supports pluggable look and
feel. feel.
AWT provides less components than Swing provides more powerful compo-
Swing. nentssuch as tables, lists, scrollpanes,
colorchooser, tabbedpane etc.
The elements of a GridBagLayout are organized accord-ing 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 dif-ferent sizes. The default Layout Manager of Panel and Panel
sub classes is FlowLayout.
25 Why would you use Swing Utilities.invoke And Wait or Swing Utilities.
invoke Later?
To make update in a Swing component but not in a callback. And If the update to be hap-
pened immediately (perhaps for a progress bar component) then use invokeAndWait. When
the update response is not need immediately, then use invokeLater.
26. 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, select-ing a
list, etc. There are two types of models for handling events and they are: a) event-
inheritance model and b) event-delegation model.
27. 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.
28. 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 Sys-
tem.exit to terminate the JVM.
29. 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 Compo-nent.
30. 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
31. 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 en-
ableEvents() method is used by objects that handle events by overriding their eventdis-patch
methods.
32. 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 hierar-chy.
PART-B
1 Create a simple menu application that enables a user to select one of the following items:
(16) (IT2301 APR/MAY-2015)
a. Radio1 b. Radio2 c. Radio3 d. Radio4 e. Radio5
i. From the menu bar of the application
From a pop-up menu
From a toolbar
2. Add tooltips to each menu item that indicates some information about the Radio station
such as type of music and its broadcast frequency.
3. Write a program to create a frame with the following menus, such that the corresponding
geometric object is created when a menu is clicked. (IT2301 MAY/JUNE-2014)
Circle. (4)
Rectangle. (4)
Line. (4)
Diagonal for the rectangle. (4)
4. Write a program to stimulate the layout and working of a calculator. (16) (IT2301 MAY/
JUNE-2014)
5. Explain in detail about AWT event hierarchy. (16) (IT2301 MAY/JUNE-2014)