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

java QB final

The document covers various Java programming concepts including stream classes, thread creation methods, graphics class methods, file handling, applet life cycle, and exception handling. It provides code examples for creating threads, handling files, and designing applets. Additionally, it explains thread priorities and includes syntax for try-catch-finally blocks.

Uploaded by

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

java QB final

The document covers various Java programming concepts including stream classes, thread creation methods, graphics class methods, file handling, applet life cycle, and exception handling. It provides code examples for creating threads, handling files, and designing applets. Additionally, it explains thread priorities and includes syntax for try-catch-finally blocks.

Uploaded by

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

Q.

1 Define stream class and List its type

Java provides I/O Streams to read and write data where, a Stream represents
an input source or an output destination which could be a file, i/o device,
other program etc.

In general, a Stream will be an input stream or, an output stream. Based on


the data they handle there are two types of streams −

• Byte Streams
• Character Streams

Q.2 what are the two ways of creating thread in java

There are two ways of creating a thread

• Extending a thread class


• Implementing runnable

Q.3 list four methods of graphics class


Q.5 differentiate java application and applet(any two points)

Q.6 Write any four methods of file class

1. String getName()- returns the name of the file


2. String getParent()-returns te name of the parent directory
3. Long length()-returns the length of the file
4. Boolean exists()-returns true if the file exists
Q.7 Give syntax of drawPolygon();

drawPolygon(int x[] , int y[] , int number_of_points)

Q.8 Describe the use of drawOval(), getFont(), drawRect(),

drawRoundRect()
Ans:

drawOval()- draws a hollow oval.

getFont()-retrieves the currently used font.

drawRect()-draws a hollow rectangle. drawRoundRect()-draws a

hollow rectangle with rounded corners.

Q.9 wap to copy content of one file to another file import

java.io.*; public class FileCopy { public static void

main(String[] args) {

try (FileInputStream in = new

FileInputStream("input.txt"); FileOutputStream

out = new FileOutputStream("output.txt")) {

int c; while ((c =

in.read()) != -1) {

out.write(c);

} catch (IOException e) {

e.printStackTrace();

}
Q. 10 wap to create a two thread one thread will display the even numbers from 1 to 10 and
other thread will print odd numbers from 1 to 10

class DemoThread {
public static void main(String args[]) {
Even t1 = new Even();
Odd t2 = new Odd();
t1.start();
t2.start();
}
}
class Even extends Thread
{
public void run()
{
for (int i = 0; i <= 10; i += 2)
{
try {
System.out.println("Even number=" + i);
Thread.sleep(500);
}
catch (InterruptedException e)
{ }
}
}}
class Odd extends Thread {
public void run() {
for (int i = 1; i <= 10; i += 2) {
try {
System.out.println("Odd number=" + i);
Thread.sleep(500);
}
catch (InterruptedException e)
{}
}
}
}
Q.11 Wap to input name and salary of employee and throw user defined exception if

entered salary is negative import java.util.Scanner; class NegativeSalaryException extends

Exception { public NegativeSalaryException(String message) { super(message);

public class EmployeeInput { public

static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter employee name: ");

String name = scanner.nextLine();

System.out.print("Enter employee salary: ");

double salary = scanner.nextDouble();

try { if

(salary < 0) {

throw new NegativeSalaryException("Salary cannot be negative.");

System.out.println("Employee name: " + name);

System.out.println("Employee salary: " + salary);

} catch (NegativeSalaryException e) {

System.out.println(e.getMessage());

Q12. Describe the applet life cycle in detail


An applet is a Java program that can be embedded into a web page. It runs inside
the web browser and works on the client-side. An applet is embedded in an HTML
page using the APPLET or OBJECT tag and hosted on a web server.

In order to implement the Applet we need to import awt package :


java.awt.applet.*;
Life Cycle of Applet
Step 1: Initialization
public void init()

There is no main method unlike our normal java programs. Every Applet will start it’s
execution from init() method. It is executed only once
Step 2: Start
public void start()

After init() method start() method is invoked. Executed when the browser is
maximized
Step 3: Paint
public void paint (Graphics g)

Paint method is used to display the content on the applet. We can create the objects
or components to the applet or we can directly write a message on the applet. It will
take Graphics class as a parameter.
Step 4: Stop
public void stop()

stop() method is used to stop the applet. It is executed when the browser is
minimized.
Step 5: Destroy
public void destroy()

destroy() method is used to completely close the applet. It is executed when the
applet is closed.

Q. 13 describe life cycle in thread with diagram


Thread Life Cycle Thread has five different states throughout its life.

1. Newborn State

2. Runnable State

3. Running State

4. Blocked State

5. Dead State

Thread should be in any one state of above and it can be move from one state to another by
different methods and ways.

Newborn state: When a thread object is created it is said to be in a new born state. When the
thread is in a new born state it is not scheduled running from this state it can be scheduled for
running by start() or killed by stop(). If put in a queue it moves to runnable state.

Runnable State: It means that thread is ready for execution and is waiting for the availability of the
processor i.e. the thread has joined the queue and is waiting for execution. If all threads have equal
priority, then they are given time slots for execution in round robin fashion. The thread that
relinquishes control joins the queue at the end and again waits for its turn. A thread can relinquish
the control to another before its turn comes by yield().

Running State: It means that the processor has given its time to the thread for execution. The thread
runs until it relinquishes control on its own or it is pre-empted by a higher priority thread.

Blocked state: A thread can be temporarily suspended or blocked from entering into the runnable
and running state by using either of the following thread method.

1) suspend() : Thread can be suspended by this method. It can be rescheduled by resume().

2) wait(): If a thread requires to wait until some event occurs, it can be done using wait method and
can be scheduled to run again by notify().

3) sleep(): We can put a thread to sleep for a specified time period using sleep(time) where time is in
ms. It re-enters the runnable state as soon as period has elapsed /over

Dead State: Whenever we want to stop a thread form running further we can call its stop().The
statement causes the thread to move to a dead state. A thread will also move to dead state
automatically when it reaches to end of the method. The stop method may be used when the
premature death is required.
Q.14 wap to read a file(use character stream

class) import java.io.*; class Read_Bytes

public static void main (String args[])

FileInputStream infile = null;

int b;

try

infile = new FileInputStream(args [0]);

while ( (b = infile.read ()) != -1)

System.out.print ((char) b);

infile.close();

catch (IOException ioe)

System.out.print (ioe);

}
Q. 15 Give the usage of following methods

drawPolygon() , drawOval(), drawLine(), drawArc()

drawPolygon() -draws a hollow polygon

drawOval() -draws a hollow oval

drawLine() -draws a straight line

drawArc() -draws a hollow arc

Q 16 design an applet to convert centigrade to farenheit temperature using param tag


and display the result

import java.awt.*;

import javax.applet.*;

public class Temperature extends Applet

public void paint(Graphics g)

int c= Integer.parseInt(getParameter(p));

double f = 1.8 * c + 32

g.drawString (" " + f ,20,30)

/* <applet code="Temperature .java" width="300" height="100">

<param name = "P" value = "37">

</applet> */
Q. 17 WAP to create an applet to display

polygon

/* <applet code="polygon .java" width="200" height="100">

</applet> */

Q. 18 write a syntax and example of

drawRect() and drawOval()

Syntax:

drawRect(int x, int y, int width, int height)

drawOval(int x, int y, int width, int height)

Code:

import java.awt.*;

import javax.applet.*;

public class DrawRectOval extends Applet

public void paint(Graphics g)

g.drawRect(20, 20, 50, 50);


g.drawOval(80, 20, 50, 50);

*/

<applet code="DrawRectOval.java" width="200" height="100"></applet>

*/
Q.19 write an applet to accept username in the form of parameter and print “hello<username>”

import java.applet.*;

import java.awt.*;

public class Demo extends Applet

public void paint (Graphics g)

String s = getParameter ("Mohit");

String str = "hello" + s;

g.drawString (str , 10, 50)

/*<applet code= Demo.java width=300 height= 300>

<param name = "Mohit" value = “Mohit” >

</applet>*/

Q.20 wap to create a two thread one thread will display the numbers from 1 to 50 and other
thread will print numbers from 50to 1

class demoThread
{
public static void main (String args[])
{
Thread1 t1= new Thread1();
Thread2 t2= new Thread2 ();
t1.start();
t2.start();
}
}
class Thread1 extends Thread
{
public void run ()
{
for (int i=1;i<=50;i++)
{

try

{
System.out.println("Thread 1= "+ i);
Thread.sleep(500);
}
catch (InterruptedException e)
{}
}
}
}
class Thread2 extends Thread
{
public void run()
{
for (int i=50;i>=1;i--)
{
try {
System.out.println("Thread 2 = " +i);
Thread.sleep(500);
}
catch (InterruptedException e)
{}
}
}
}

Q.21 Define an exception called “no match exception” that is thrown when the password accepted
is not equal to “msbte” write the program

import java.util.*;
class NoMatchException extends Exception
{
public NoMatchException(String msg)
{
super (msg);
}
}
public class Demo
{
public static void main (String args[])
{
Scanner sc = new Scanner (System.in);
System.out.print("Enter password ");
String pass= sc.nextLine();
try
{
if (! (pass.equals("msbte")))
{
throw new NoMatchException("Password does not match!");
}
}
catch(NoMatchException e)
{
System.out.print(e);
}
}
}

Q. 22 What is thread priority? Write default priority values and methods to change them

Ans: In java, each thread is assigned a priority, which affects the order in which it is scheduled for
running. The thread of same priority are given equal treatment by the java scheduler and , therefore
, they share the microprocessor on a first-come, first-serve basis.

Java permits us to set the priority of a thread using the setPriority() method as follows:

Thread_name.setPriority ( int number);

The thread class defines several priority constants

MIN_PRIORITY = 1

DEFAULT_PRIORITY = 5

MAX_PRIORITY= 10

Q.23 Design an applet which accepts username as a parameter for html page and display number

of characters from it.

import java.applet.*;

import java.awt.*;

public class Demo extends Applet

{
String username ;

public void init()

username = getParameter("username");

public void paint(Graphics g)

g.drawString("Username: " + username, 20, 20);

g.drawString("Number of characters: " + username.length(), 20, 40);

/*<applet code="Demo.java" width="300" height="100">

<param name="username" value="Mohit">

</applet>

*/

Q. 24 Define applet. Write a program to create an applet to display message “Welcome to


java applet”.

import java.applet.*;

import java.awt.*;

public class Welcome extends Applet {

public void paint(Graphics g) {

g.drawString("Welcome to Java Applet", 20, 20);

/*<applet code="Welcome.java" width="300" height="100”>

</applet> */
Q. 25 List any two methods of FileInputStream class

1. int read()- Reads a byte of data from this input stream

2. void close()- Closes this file input stream and releases any system resources associated
with the stream.

Q. 26 Define thread mention 2 ways to create thread

A thread in Java is the direction or path that is taken while a program is being executed.
Generally, all the programs have at least one thread, known as the main thread, that is
provided by the JVM or Java Virtual Machine at the starting of the program's execution.

There are two ways of creating a thread

• Extending a thread class


• Implementing runnable

Q. 27 List any two methods of FileWriter class

1. void write(String text) - It is used to write the string into FileWriter.


2. void close()- It is used to close the FileWriter.

Q. 28 Write syntax of try-catch-finally block

try {
// Statements that may throw an exception
} catch (Exception e) {
// Statements that will execute if an exception is thrown
} finally {
// Statements that execute whether the exception occurs or not
}

You might also like