Sessional Exam-2 Key
Sessional Exam-2 Key
Sessional Exam-2 Key
Answers:
1. Distinguish between the sleep() and wait() in Multi-Threading.
Criteria Wait() in Java Sleep() in Java
Object Lock The lock on an object is released The lock on an object isn’t released
during during synchronisation via the when synchronising using the
Synchronisation Wait() method. Sleep() method.
Synchronised We can only call Wait() from the We need not call Sleep() from the
Context synchronised context. synchronised context.
Static Method The Wait() method is not static. The Sleep() method is static.
Overloaded The Wait() method consists of three The Sleep() method consists of two
Methods overloaded methods, namely: overloaded methods, namely:
• wait (long timeout, int nanos). • sleep (long millis, int nanos).
Here, nanos: Nanoseconds Here, nanos: Nanoseconds
• wait (long timeout) • sleep (long millis). Here, millis:
• wait() Milliseconds
5. Show the line of code for creating an object of any one component in Java Swing.
6. Implement a Java program to create multiple threads in Java. Atleast 2 threads. Each thread
will print a message.
class MultiThreads{
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(){
System.out.println("Task 1");
}
}
7. Write a Java AWT Program to attain the following output and when Register Button is
pressed, it should display “Successfully Registered” below the Button.
import java.awt.*;
import java.awt.event.*;
public class Form extends Frame implements ActionListener
{
TextField tf1, tf2;
Button b1, b2;
Checkbox rb1, rb2;
Label l1, l2, l3, l4;
Form(){
tf1=new TextField();
tf1.setBounds(120,50,150,20);
tf2=new TextField();
tf2.setBounds(120,100,150,20);
b1=new Button("Register");
b1.setBounds(10,200,70,30);
b2=new Button("Exit");
b2.setBounds(90,200,35,30);
l4=new Label();
l4.setBounds(120,250,150,30);
b1.addActionListener(this);
setSize(350,350);
setLayout(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == b1)
l4.setText("Successfully Registered");
}
public static void main(String[] args) {
new Form();
}
}
1 The keyword used to create a class is The keyword used to create an interface is
“class” “interface”
10 It can contain final and static methods It cannot have final and static methods
S.No Class Interface
11 Example:
public class Point {
int x; Example:
int y;
Point(int x, int y){ public interface Drawable {
this.x = x; void draw();
this.y = y; }
}
}