Solution of TU Exam Question 2071
Solution of TU Exam Question 2071
Solution of TU Exam Question 2071
The life cycle of a thread in java is controlled by JVM. The life cycle of a java thread involves the
following states in which the thread goes through.
1. New
2. Runnable
3. Running
4. Non-Runnable (Blocked)
5. Terminated
1) New: The thread is in new state if you create an instance of Thread class but before the
invocation of start() method.
2) Runnable: The thread is in runnable state after invocation of start() method, but the thread
scheduler has not selected it to be the running thread.
3) Running: The thread is in running state if the thread scheduler has selected it.
4) Non-Runnable (Blocked): This is the state when the thread is still alive, but is currently not
eligible to run.
5) Terminated: A thread is in terminated or dead state when its run() method exits.
2.) Write a program using swing components to find simple interest. Use text fields for inputs and
output. Your program should display the result when the user presses a button. (10)
Answer: //SimpleInterest.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SimpleInterest extends JFrame implements ActionListener {
JLabel l1 = new JLabel("Principal:");
JTextField t1 = new JTextField(10);
JLabel l2 = new JLabel("Time:");
JTextField t2 = new JTextField(10);
JLabel l3 = new JLabel("Rate:");
JTextField t3 = new JTextField(10);
public SimpleInterest() {
setLayout(new FlowLayout());
setSize(200, 200);
@Override
public void actionPerformed(ActionEvent e) {
double pr = Double.parseDouble(t1.getText());
double ti = Double.parseDouble(t2.getText());
double ra = Double.parseDouble(t3.getText());
double in = (pr*ti*ra)/100;
t4.setText("" + in);
}
3.) What is servlet? Differentiate it with JSP. Discuss life cycle of servlet in detail. (2+3+5)
Answer: Refer to TU Exam Solution 2069 and 2070.
Section B (8*5=40)
Attempt any eight questions.
4.) How do you achieve multiple inheritance in java? Discuss. (5)
Answer: Refer to TU Exam Solution 2070.
A JDBC driver is a set of Java classes that implement the JDBC interfaces, targeting a specific
database. The JDBC interface comes with standard Java, but the implementation of these interfaces is
specific to the database you need to connect to. Such an implementation is called a JDBC driver. There
are 4 different types of JDBC drivers:
The following program throws an exception whenever the user of the program gives an invalid age.
The invalid age condition is age being negative or age greater than 150. If exception has not been
handled here, then the user may possibly have provided the wrong input.
class Person {
public GroupLayoutTest() {
layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(one)
.addComponent(two)
.addComponent(three)) //first group
.addGroup(layout.createSequentialGroup()
.addComponent(four)
.addComponent(five)) //second group
.addComponent(six));
layout.setVerticalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(one).addComponent(two).addComponent(three))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(four).addComponent(five))
.addComponent(six));
panel.setLayout(layout);
add(panel);
setSize(400, 400);
setVisible(true);
8.) Write a simple java program to read from and write to files. (5)
Answer: Refer to TU Exam Solution 2070.
(3) MouseEvent: It is generated in case of mouse clicks and mouse motion when the cursor is over Swing
components such as JFrame. To have any component listen for a MouseEvent, we must register the component
with either MouseListener or MouseMotionListener as:
component.addMouseListener(new MyMouseListener);
OR
component.addMouseMotionListener(new MyMouseMotionListener);
and then write the
public void mouseDragged(MouseEvent e);
public void mouseMoved(MouseEvent e);
OR
public void mouseClicked(MouseEvent e);
public void mousePressed(MouseEvent e);
public void mouseReleased(MouseEvent e);
public void mouseEntered(MouseEvent e);
public void mouseExited(MouseEvent e);
methods for the component to react to the event.
(4) KeyEvent: It is generated in case of key presses and key depresses on text fields such as JTextField and
JTextArea. One example of KeyEvent is user typing in a textfield. To have any component listen for a
KeyEvent, we must register the component with KeyListener as:
component.addKeyListener(new MyKeyListener);
and then write the
public void keyPressed(KeyEvent e);
public void keyTyped(KeyEvent e);
public void keyReleased(KeyEvent e);
methods for the component to react to the event.
However, we also do have the option to use corresponding Adapter classes instead of these Listener
interfaces by which we can implement only the methods that are needed for the component and not
define all the methods that are not necessary.
13.) Write a simple JSP program to display “Tribhuvan University” 10 times. (5)
Answer: Refer to TU Exam Solution of 2070.