java QB final
java QB final
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.
• Byte Streams
• Character Streams
drawRoundRect()
Ans:
main(String[] args) {
FileInputStream("input.txt"); FileOutputStream
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
try { if
(salary < 0) {
} catch (NegativeSalaryException e) {
System.out.println(e.getMessage());
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.
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.
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
int b;
try
infile.close();
System.out.print (ioe);
}
Q. 15 Give the usage of following methods
import java.awt.*;
import javax.applet.*;
int c= Integer.parseInt(getParameter(p));
double f = 1.8 * c + 32
</applet> */
Q. 17 WAP to create an applet to display
polygon
</applet> */
Syntax:
Code:
import java.awt.*;
import javax.applet.*;
*/
*/
Q.19 write an applet to accept username in the form of parameter and print “hello<username>”
import java.applet.*;
import java.awt.*;
</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:
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
import java.applet.*;
import java.awt.*;
{
String username ;
username = getParameter("username");
</applet>
*/
import java.applet.*;
import java.awt.*;
</applet> */
Q. 25 List any two methods of FileInputStream class
2. void close()- Closes this file input stream and releases any system resources associated
with the stream.
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.
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
}