2marks and Questions CS8392
2marks and Questions CS8392
different behavior instances. Object oriented programs use polymorphism to carry out the same
operation in a manner customized to the object. It allows a single name/operator to be
associated with different operation depending on the type of data passed to it.
14 Define Objects and Classes in Java
Class is a collection of data and the function that manipulate the data.The data components
of the class are called data fields and the function components of the class are called
member functions or methods. The class that contains main function is called main class.
Object is an instance of a class. The objects represent real world entity. The objects are used
to provide a practical basis for the real world. Objects are used to understand the real
world. The object can be declared by specifying the name of the class.
15 Write the syntax for declaration of class and creation of objects?
A class is declared using class keyword. A class contains both data and method that operate on
that data. Thus, the instance variables and methods are known as class members. When creating
an object from a class
Declaration − A variable declaration with a variable name with an object type.
Instantiation − The 'new' keyword is used to create the object.
Initialization − The 'new' keyword is followed by a call to a constructor. This call initializes
the new object.
class Student
{
String name;
int rollno;
int age;
}
Student std=new Student();
std is instance/object of Student class.
new keyword creates an actual physical copy of the object and assign it to the std variable.
The new operator dynamically allocates memory for an object.
16 Define Encapsulation (Apr/May 2012) (Apr 2017)
The wrapping up of data and functions into a single unit is known as data encapsulation. Here
the data is not accessible to the outside the class. The data inside that class is accessible by the
function in the same class. It is normally not accessible from the outside of the component.
12 Why do we need run() and start() method both? Can we achieve it with only run method?
The separate start() and run() methods in the Thread class provide two ways to create
threaded programs. The start() method starts the execution of the new thread and calls the
run() method. The start() method returns immediately and the new thread normally
continues until the run() method returns.
The Thread class' run() method does nothing, so sub-classes should override the method
with code to execute in the second thread. If a Thread is instantiated with a Runnable
argument, the thread's run() method executes the run() method of the Runnable object in the
new thread instead.
Depending on the nature of your threaded program, calling the Thread run() method
directly can give the same output as calling via the start() method, but in the latter case the
code is actually executed in a new thread.
13 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.
14 Write short note on isAlive() and join()?
isAlive() and join() methods are used to determine whether a thread has finished or not.
First, you can call isAlive() on the thread. This method is defined by Thread, and its general
form is:
final Boolean isAlive()
The isAlive() method returns true if the thread upon which it is called is still running. It returns
false otherwise.
While isAlive() is occasionally useful, the method that you will more commonly use to
wait for a thread to finish is called join(). The general form is:
final void join() throws InterruptedException
This method waits until the thread on which it is called terminates
15 What do you mean by generic programming?
Generic programming is a style of computer programming in which algorithms are
written in terms of to-be-specified-later types that are then instantiated when needed for specific
types provided as parameters
16 Define Deadlock and When it will occur?
Deadlock describes a situation where two or more threads are blocked forever, waiting for each