Current Midterm Solved Papers: Muhammad Faisal Dar
Current Midterm Solved Papers: Muhammad Faisal Dar
By
Muhammad Faisal Dar
[email protected]
1 program tha jis may hum nay event handler or registration ka code likhna tha jo k
open karnay say" welceom" or close karnay say "good by" appear karay.5marks
Solution:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class EventsEx implements WindowListener
{
JFrame frame;
public EventsEx ( )
{
initGUI();
}
public void initGUI ( )
{
frame = new JFrame();
frame.setLayout(new BorderLayout( ) );
frame.setSize(350, 350);
frame.setVisible(true);
//Registering a window Event Handler
frame.addWindowListener(this);
}
public void windowOpened (WindowEvent we)
{
JOptionPane.showMessageDialog(null,"Wel Come");
}
public void windowClosing (WindowEvent we)
{
JOptionPane.showMessageDialog(null,"Good
Bye"); System.exit(1);
}
Public void windowActivated (WindowEvent we) { }
public void windowClosed (WindowEvent we) { }
Public void windowDeactivated (WindowEvent we) { }
public void windowDeiconified (WindowEvent we) { }
public void windowIconified (WindowEvent we) { }
Solution: import
java.net.*;
import java.io.*;
import javax.swing.*;
OutputStream os = s.getOutputStream();
PrintWriter pw = new PrintWriter(os,true);
// step 5: Send / Receive message
7. transient keyword is prevent serialization (Options: allow, prevent, avoid, none of above)
Subjective Questions
1. Why Applet is a panel? (2 number)
Solution:
An applet is a Panel that allows interaction with a Java program. Because it is
inherited from the container class, so the applet are also the panel.
OutputStream os = s.getOutputStream();
PrintWriter pw = new PrintWriter(os,true);
// step 5: Send / Receive message
Write SQL Query in Java to create a Result Set n columns using columns “employee
Name” and “employee id” from table “Employee”.
Solution:
String varsql=”SELECT [employee Name], [employee id] FROM
Employee”; PreparedStatement pStmt = con.prepateStatement(sql);
ResultSet rs= pStmt.executeQuery(varsql);
Paintcomponent() method
— it is a main method for painting
— By default, it first paints the background
— After that, it performs custom painting (drawing circle, rectangles etc.)
Write the names of any five event listener interfaces which are sub
interfaces of Java util.EventListner interface?
Solution:
1. ActionListener
2. AdjustmentListener
3. ComponentListener
4. ContainerListener
5. FocusListener
6. ItemListener
7. KeyListener
8. MouseListener
9. MouseMotionListener
10. TextListener
11. WindowListener
What are methods of Mouse Events ? describe few ?(Mouse Listener Interface
methods to handel mouse events)
Solution:
To handle Mouse events, two types of listener interfaces are available.
– MouseMotionListener
– MouseListener
The class that wants to handle mouse event needs to implement the corresponding
interface and needs to provide the definition of all the methods in that interface.
MouseMotionListener interface
– Used for processing mouse motion events
– Mouse motion event is generated when mouse is moved or dragged
MouseListener interface
– Used for processing “interesting” mouse events like when m
ƒ Pressed
ƒ Released
ƒ Clicked (pressed & released without moving the cursor)
ƒ Enter (mouse cursor enters the bounds of component)
ƒ Exit (mouse cursor leaves the bounds of component)
Question No 3 MARKS 3
Write java code that will update name Ali to Ahmed on 5th row. Consider the column
name is sname note write only necessary code
Solution:
Rs.absolute(5)
Question No 4 MARKS 3
Methods (paintBorder, paintChildren, paintComponent) write correct order of methods
call in if these methods are called
Solution:
1. paintComponent
2. paintBorder
3. paintChildren
First of all JComponent paint itself, painting the background and performing custom
painting is performed by the paintComponent method. Then paintBorder is get called
And finally Childern is called. All these methods are also called in the same order.
Which of the following methods are invoked by the AWT to support paint and
repaint operations?
Paint ()
Repaint()
Draw ()
Redraw()
Java gives us a solution in the from of repaint( ) method. Whenever we need to repaint,
we call this method that in fact makes a call to paint( ) method at appropriate time.
Adapter classes have been defined for listener interfaces except ________
interface 1. MouseListener
2. KeyListener
3. WindowListener
4. ActionListener
> Diff b/w Input/output streams class hierarchy and Read/Write class hierarchy?
> Can applets communicate with eachother? (5 marks)
Solution:
> As a developer of GUI, what should u know about top level containers and general
purpose containers?
Solution:
As a developer of GUI, a developer should know about top level containers and general
purpose containers that a top general purpose container cannot be visible at runtime
without top level containers. Only top levels container have a capability of displaying alone.
What is the difference between a static and a non-static inner class?
Solution:
A non-static inner class may have object instances that are associated with instances
of the class’s outer class. A static inner class does not have any object instances
What is the difference between Anonymous object and Named object? Give example with code.
Solution:
Has no name, Same as inner class in capabilities, much shorter and
difficult to understand.
Nave VS Anonymous Objects
Named
String s=”hello”;
System.out.println(s);
“Hello” had a named reference s.
Anonymous
System.out.println(“hello”);
We generally use anonymous object when there is just a one time use of a particular
object but in case of a repeated use we generally used named objects and use that
named reference to use that objects again and again.