0% found this document useful (0 votes)
76 views

Java Unit 3

This document provides sample questions and answers related to programming in Java (Unit-III). It includes 2-mark and 5-mark questions covering topics like packages, interfaces, threads and synchronization. For packages, it discusses what packages are and how to create packages and import them. For interfaces, it explains what interfaces are and how classes can implement interfaces. It also provides details about the life cycle of threads and describes some important methods in the Thread class.

Uploaded by

jagan nathan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views

Java Unit 3

This document provides sample questions and answers related to programming in Java (Unit-III). It includes 2-mark and 5-mark questions covering topics like packages, interfaces, threads and synchronization. For packages, it discusses what packages are and how to create packages and import them. For interfaces, it explains what interfaces are and how classes can implement interfaces. It also provides details about the life cycle of threads and describes some important methods in the Thread class.

Uploaded by

jagan nathan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

A.

M JAIN COLLEGE (SHIFT-II)


DEPARTMENT OF SOFTWARE APPLICATIONS
PREVIOUS YEAR QUESTION AND ANSWER
PROGRAMMING IN JAVA (UNIT-III)

Two Marks

1)What is a package?(apr-13,nov-14,nov-15,apr-16)
Packages contain a set of classes in order to ensure that class names are unique. Packages
are containers for classes that are used to compartmentalize the class name space. Packages are
stored in a hierarchical manner and are explicitly imported into new class definition. synatx:
package packagename;

2)What is an interface?(apr-12,nov-13,apr-14,apr-15,apr-16)
An interface is a collection of abstract behavior that individual classes can implement. An
interface is created in the same way as a class. Java does not support multiple inheritance to
overcome it interface has been used. All the methods of an interface are automatically public.
Syntax : Interface name
{
variable declarations;
return type method name1 (parameter list)
}

3)Name any two methods in the Thread class?(apr-13)


The Thread class has methods that can be used to control the behaviour of threads. The
two methods are,
1. start() method: This method is used to start a new thread. When this method is called the
thread enters the ready to run mode and this automatically invokes the run( ) method .Syntax:
void start( )
2. run() method: This method is the important method in the thread and it contains the
statements that are to be executed in our program. It should be overridden in our class, which is
derived from Thread class.
Syntax: void run( )
{
//Statements implementing thread
}

4)What is synchronization?(apr-12,nov-13,nov-14)
Two or more threads accessing the same data simultaneously may lead to loss of data
integrity. java enables to overcome this problem using technique known as synchronization.The
key word synchronized is used,so that the method that will read information from a file and the
method that will update the same file may be declared as synchronized.
syntax: synchronized void update()
{
statements;
}
5)What is a Thread?(apr-14,apr-15,nov15)
A thread is a line of execution. It is the smallest unit of code that is dispatched by the
scheduler. Thus, a process can contain multiple threads to execute its different sections. This is
called multithread.

6)Write syntax of synchronized class?(nov-13)


Refer question no: 4 in two marks
5 marks

1)How packages are created in java? Explain?(apr-14,apr-16)


Packages contain a set of classes in order to ensure that class names are unique. Packages
are containers for classes that are used to compartmentalize the class name space.
Creating a package :
To create an user defined package it includes a package command as the first statement in java
source file. We can assign the classes and interfaces in a source file to a particular package by
using a package. Syntax : package packagename ;
Importing a package :
We can import a specific class or the whole package. We can place import statements at the
beginning of your java source file.
Syntax : import packagename . * ;
Creating our own package :
1)Declare the package at the top of the java source file.
Package packagename ;
2)Declare the class as public
Package class classname ;
3)Create as directory and name the same name as the package .
4)The file should be named with the same name as their public class followed by the extension
as .Java
5)Compile the source file which creates the .class file in the directory .
6)Source file and class file should be in a directory as specified in the class path environment
variable.
Example :
Package x;
public class hello
{
public void display ( )
{
System.out.println(“welcome”) ;
}
}
import x.*;
class pack
{
public static void main(string args[])
{
hello h1=new hello();
h1.display();
}
}
output: welcome
2)Give a brief account on interfaces in java?(apr-12)
An interface is a group of constants and methods declarations that define the form of a
class. Java does not support multiple inheritance to overcome it interface has been used. All the
methods of an interface are automatically public.
Syntax : Interface name
{
data type varname 1 = value 1;
...................................................;
...................................................;
data type varname n = value n;
return type method name 1 (parameter list);
.......................................................................;
.......................................................................;
return type method name n (parameter list);
}
Example :
Interface x
{
int a=7 , b=8 , n=9 ;
void a (int , int);
void a (int);
}

Extending interface :An interface can be interfaced from other interface using the keyword
called extends. Syntax:

interface sub class extends base class


{
Variable declarations ;
Method declaration ;
}

Implementing an interface : A class can extend only a single class, it can implement a number
of interface. The keyword implements is used to implement an interface. Syntax:
class class name extends super class implements interface
{
Body of the statement;
}

Example:

interface area
{
final static float pi = 3.14f ;
float compute ( float x , float y ) ;
}
class Rectangle implements Area
{
public float compute ( float x , float y ) ;
{
return ( x * y) ;
}
}
class Circle implements Area
{
public float compute ( float x , float y)
{
return ( pi*x*y) ;
}
}
class InterfaceTest
{
public static void main(String args [ ]) throws IOException
{
Rectangle rect = new Rectangle ( ) ;
Circle cir = new Circle ( ) ;
Area a ;
a = rect ;
System.out.println ( “ Area of Rectangle”+a.compute(10,30)) ;
a = cir ;
System.out.println( “ Area of Circle”+a.compute(20,0));
}
}

Output :

Area of Rectangle 300.0


Area of Circle 1256.0

3)Explain life cycle of a thread?(apr-13,nov-14)


A thread moves through several states from its creation to its termination. The life cycle
of a thread consists of five shows. The following figure shorts the five states of a thread life
cycle.
1)New born state
2)Runnable state
3)Running state
4)Blocked state
5)Dead state
New thread New born Stop()

Start()

Active thread Running Runnable


Dead
Yield () stop()

Suspend() Resume()

Sleep() Notify()

Wait () Stop()

Idle thread (not runnable) Blocked

1)New born thread :


The thread is in new state if you create an instance of Thread class but before the
invocation of start() method. It can perform only one state.
(i) Scheduled it for running (i.e.) using start ( ) method
(ii) Kill it, by using stop ( ) method.

2)Runnable state :
The thread is in runnable state after invocation of start() method, but the thread
scheduler has not selected it to be the running thread. The process of assigning time to threads is
known as time-slicing.

3)Running state :
The thread comes to this state when it start its execution. A thread enters this stage
whenever its run ( ) method is called. It has been suspended using suspend ( ) method. A suspend
thread can be reviewed by using the resume ( ) method.sleep() method time will be in
millisecond.

4)Blocked state :
A thread comes to this state when it is made to stop its execution. The thread comes to
this state when its stop ( ) or wait ( ) method is called.
5)Dead state :
A thread comes to this state when it completes its execution.

4)Explain any five methods in the Thread class?(apr-15)


A thread is a line of execution. It is the smallest unit of code that is dispatched by the
scheduler. Thus, a process can contain multiple threads to execute its different sections. This is
called multithread. some of the methods are;

1. start() method
2. run() method
3. sleep() method
4. stop() method
5. wait() method
6. yield() method

1.start() method: This method is used to start a new thread. When this method is called the
thread enters the ready to run mode and this automatically invokes the run( ) method.
Syntax:void start( ).

2.run() method: This method is the important method in the thread and it contains the
statements that are to be executed in our program. It should be overridden in our class, which is
derived from Thread class.Syntax;
void run( )
{
//Statements implementing thread
}
3.sleep() method: This method is used to block the currently executing thread for the specific
time. Syntax: void sleep(time in milliseconds )
4.stop() method: It is used to stop the running thread. Syntax: void stop().
5. wait() method: This method is used to stop the currently executing thread until some event
occurs. Syntax: void wait()
6. yield() method: This method is used to bring the blocked thread to ready to run mode.
Syntax: void yield()

5)Explain about the concept of Thread Priority?(nov-13)


Each thread have a priority. Priorities are represented by a number between 1 and 10. In
most cases, thread schedular schedules the threads according to their priority (known as
preemptive scheduling).java permits setpriority() and getpriority().Default priority of a thread is
5(NORM_PRIORITY),3 types of priorities are,
1)public static int MIN_PRIORITY
2)public static int NORM_PRIORITY
3)public static int MAX_PRIORITY
2 types of methods are, 1.setpriority()
2.get priority()
1.setpriority()-Thread priority can be changed by this method.
syntax: void setPriority(int p)
2. get priority()-This method is used to get the priority of the thread.
syntax: int getPriority()

Whenever multiple threads are ready for execution,java system chooses the highest
priority thread and executes. If lower priority to gain control one of the things should happen,
1.It stops running at the end of run().
2.It is made to sleep using sleep()
3.It is told to wait using wait().
Example:

class TestMultiPriority1 extends Thread


{
public void run(){
System.out.println("running thread name is:"+Thread.currentThread().getName());
System.out.println("running thread priority is:"+Thread.currentThread().getPriority());

}
public static void main(String args[]){
TestMultiPriority1 m1=new TestMultiPriority1();
TestMultiPriority1 m2=new TestMultiPriority1();
m1.setPriority(Thread.MIN_PRIORITY);
m2.setPriority(Thread.MAX_PRIORITY);
m1.start();
m2.start();

}
}
Output: running thread name is:Thread-0
running thread priority is:10
running thread name is:Thread-1
running thread priority is:1

6) Describe the various forms of implementing interfaces?(nov-15)


Refer question no:2 in 5 marks
10 Marks

1)Explain life cycle of a thread?(apr-12,nov-15,apr-15,apr-16)


A thread moves through several states from its creation to its termination. The life cycle
of a thread consists of five shows. The following figure shorts the five states of a thread life
cycle.
1)New born state
2)Runnable state
3)Running state
4)Blocked state
5)Dead state

New thread New born Stop()

Start()

Active thread Running Runnable


Dead
Yield () stop()

Suspend() Resume()

Sleep() Notify()

Wait () Stop()

Idle thread (not runnable) Blocked

1)New born thread :


We create a thread object a thread is born and said to be new born state. At this stage a
thread is not scheduled for running state. It can perform only one state.
(i) Scheduled it for running (i.e.) using start ( ) method
(ii) Kill it, by using stop ( ) method.
New born state when we create an object of the class thread.

New born

Start Stop
Runnable Dead state
method
2)Runnable state :
The thread is ready for execution and waits for the processor to be available. All
the threads in a execution queue have equal priority. The threads comes to this state when the
start ( ) method is called. This process of assigning time to threads is known as time-slicing.If we
want a thread to relinguish control to another thread equal priority before its turn comes we can
do so by using the yield ( ) method.

…… …….
.. …
Running thread … Runnable thread
.

h
3)Running state :
. comes
The thread to this state when it start its execution. A thread enters this stage
whenever its run ( ) method is called. It has been suspended using suspend ( ) method. A suspend
thread can be reviewed by using the resume ( ) method.

Suspend

Resume

Running Runnable Suspended

We can put a thread in a specified time period using the method sleep ( ) method where time is in
millisecond.

Sleep (t)

After (t)

Running Runnable Suspended

It has been told to wait until some event occurs. This is done using the wait ( ) method. The
thread can be scheduled to run again using the notify ( ) method.
Wait

Notify

Running Runnable Suspended

4)Blocked state :
A thread comes to this state when it is made to stop its execution. The thread comes to
this state when its stop ( ) or wait ( ) method is called.

5)Dead state :
A thread comes to this state when it completes its execution.

2)How packages are created in java? Explain?(nov-13)


Packages contain a set of classes in order to ensure that class names are unique. Packages
are containers for classes that are used to compartmentalize the class name space.
Creating a package :
To create an user defined package it includes a package command as the first statement in java
source file. We can assign the classes and interfaces in a source file to a particular package by
using a package. Syntax : package packagename ;
Importing a package :
We can import a specific class or the whole package. We can place import statements at the
beginning of your java source file.
Syntax : import packagename . * ;
some of the java packages are;
 java.lang  — basic language functionality and fundamental types
 java.util  — collection data structure classes
 java.io  — file operations
 java.math  — multiprecision arithmetics
Creating our own package :
1)Declare the package at the top of the java source file.
Package packagename ;
2)Declare the class as public
Package class classname ;
3)Create as directory and name the same name as the package .
4)The file should be named with the same name as their public class followed by the extension
as .Java
5)Compile the source file which creates the .class file in the directory .
6)Source file and class file should be in a directory as specified in the class path environment
variable.
Example :
Package x;
public class hello
{
public void display ( )
{
System.out.println(“welcome”) ;
}
}
import x.*;
class pack
{
public static void main(string args[])
{
hello h1=new hello();
h1.display();
}
}
output: welcome

3) What is an interface? Discuss the various forms of implementing interface in java?(apr-


13)
An interface is a group of constants and methods declarations that define the form of a
class. Java does not support multiple inheritance to overcome it interface has been used. All the
methods of an interface are automatically public.

Syntax :
Interface name
{
data type varname 1 = value 1;
...................................................;
...................................................;
data type varname n = value n;
return type method name 1 (parameter list);
.......................................................................;
.......................................................................;
return type method name n (parameter list);
}

Example :
Interface x
{
int a=7 , b=8 , n=9 ;
void a (int , int);
void a (int);

Extending interface :An interface can be interfaced from other interface using the keyword
called extends.
Syntax: interface sub class extends base class
{
Variable declarations ;
Method declaration ;
}
Implementing an interface :A class can extend only a single class, it can implement a number
of interface. The keyword implements is used to implement an interface.
Syntax: class class name extends super class implements interface
{
Body of the statement;
}

Combining Multiple interfaces:


interface InterfaceName
{
// "constant" declarations
// method declarations
}
// inheritance between interfaces
interface InterfaceName extends InterfaceName
{
...
}
interface InterfaceName extends ClassName
{ ... }
// extends multiple interfaces (multiple inheritance like)
interface InterfaceName extends InterfaceName1, InterfaceName2
{ ...
}
Interfaces and Classes
// implements instead of extends
class ClassName implements InterfaceName
{
...
}
// combine inheritance and interface implementation
class ClassName extends SuperClass implements InterfaceName
{
...
}// multiple inheritance like again
class ClassName extends SuperClass
implements InterfaceName1, InterfaceName2
{
...
}
// multiple inheritance like
class ClassName implements InterfaceName1, InterfaceName2
{
...
}
Example:
interface area
{
final static float pi = 3.14f ;
float compute ( float x , float y ) ;
}
class Rectangle implements Area
{
public float compute ( float x , float y ) ;
{
return ( x * y) ;
}
}
class Circle implements Area
{
public float compute ( float x , float y)
{
return ( pi*x*y) ;
}
}
class InterfaceTest
{
public static void main(String args [ ]) throws IOException
{
Rectangle rect = new Rectangle ( ) ;
Circle cir = new Circle ( ) ;
Area a ;
a = rect ;
System.out.println ( “ Area of Rectangle”+a.compute(10,30)) ;
a = cir ;
System.out.println( “ Area of Circle”+a.compute(20,0));
}
}

Output :
Area of Rectangle 300.0
Area of Circle 1256.0

4)Explain the two ways of creating threads in java?(apr-14)


Java defines two ways by which a thread can be created.
1. By implementing the Runnable interface.
2. By extending the Thread class.
1. By implementing the Runnable interface
To create a thread is to create a class that implements the runnable interface. After
implementing runnable interface, the class needs to implement the run() method, syntax: public
void run()
1)Declare the class as implementing the Runnable interface.
2)Implement run() method.
3)Create a thread by defining an object that is instantiated from this runnable class as the traget
of the thread.
4)Call the thread start() method to run the thread.

Example:
class MyThread implements Runnable
{
public void run()
{
System.out.println("concurrent thread started running..");
}
}

class MyThreadDemo
{
public static void main( String args[] )
{
MyThread mt = new MyThread();
Thread t = new Thread(mt);
t.start();
}
}

Output :
concurrent thread started running..

Extending Thread class:


The another way to create a thread by a new class that extends Thread class and create an
instance of that class.
Declare the class as extending the Thread class
Implement the run() method that is responsible forr executing tha sequence of code that the
thread will execute.
Create a thread object and call start() method to initiate the thread execution.

Declaring of class: class MyThread extends Thread


{
.......................................
........................................
}
Example:

class MyThread extends Thread


{
public void run()
{
System.out.println("Concurrent thread started running..");
}
}

classMyThreadDemo
{
public static void main( String args[] )
{
MyThread mt = new MyThread();
mt.start();
}
}

Output :
concurrent thread started running..

5) How multiple inheritance is implemented in java? Explain (nov-14)


Refer question no:3 in 10 marks

You might also like