Sessional Exam-2 Key

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Sessional Exam 2 ODD Sem 2023-2024 Java programming

Answers:
1. Distinguish between the sleep() and wait() in Multi-Threading.
Criteria Wait() in Java Sleep() in Java

Class It belongs to the object class. It belongs to the thread class.

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

2. Define threads in Java.


A thread is a lightweight sub process, a smallest unit of processing. It is a separate path of
execution. A thread, in the context of Java, is a single sequential flow of control within a
program / the path followed when executing a program. Threads are independent, if there
occurs exception in one thread, it doesn't affect other threads.

3. State the use of “this” keyword.


this can be used to refer current class instance variable.
this can be used to invoke current class method (implicitly)
this() can be used to invoke current class constructor.
this can be passed as an argument in the method call.
this can be passed as argument in the constructor call.
this can be used to return the current class instance from the method.

4. Explain Applet. Why we use applets in Java?


An Applet is the small special type of Java program that is run on web browser to create
dynamic web page. Java applets are used to provide interactive features to web
applications and can be executed by browsers for many platforms. They are small, portable
Java programs embedded in HTML pages and can run automatically when the pages are
viewed.

5. Show the line of code for creating an object of any one component in Java Swing.

JButton jb = new JButton(“Submit”);

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");
}
}

class Thread2 extends Thread{


public void run(){
System.out.println("Task 2");
}
}

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);

l1=new Label("Name : ");


l1.setBounds(10,50,150,30);
l2=new Label("Address : ");
l2.setBounds(10,100,150,30);
l3=new Label("Job : ");
l3.setBounds(10,150,80,30);

CheckboxGroup grp = new CheckboxGroup();

rb1 = new Checkbox("Student", grp, false);


rb1.setBounds(120,150,100,30);
rb2 = new Checkbox("Teacher", grp, false);
rb2.setBounds(220,150,100,30);

l4=new Label();
l4.setBounds(120,250,150,30);
b1.addActionListener(this);

add(tf1);add(tf2);add(b1);add(b2); add(l1); add(l2);


add(l3); add(l4); add(rb1); add(rb2);

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();
}
}

8. Compare Classes and Interfaces.


S.No Class Interface

1 The keyword used to create a class is The keyword used to create an interface is
“class” “interface”

2 A class can be instantiated i.e., objects of An Interface cannot be instantiated i.e.,


a class can be created. objects cannot be created.

3 Classes do not support multiple The interface supports


inheritance. multiple inheritance.

4 It can be inherited from another class. It cannot inherit a class.

5 It can be inherited by a class by using the


It can be inherited by another class using keyword ‘implements’ and it can be
the keyword ‘extends’. inherited by an interface using the
keyword ‘extends’.

6 It can contain constructors. It cannot contain constructors.

7 It cannot contain abstract methods. It contains abstract methods only.

8 Variables and methods in a class can be


All variables and methods in an interface
declared using any access specifier
are declared as public.
(public, private, default, protected).

9 Variables in a class can be static, final, or


All variables are static and final only.
neither.

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; }
}
}

You might also like