0% found this document useful (0 votes)
2K views4 pages

Mid Exam For SE 2nd With Answers

This document contains the mid-exam questions and results for a Java programming class at Bahir Dar University in Ethiopia dated April 11, 2009. The exam contains 11 multiple choice and coding questions related to Java concepts like threads, Swing components, database connectivity etc. The student scored 25/30 on the exam.

Uploaded by

abreham ashebir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views4 pages

Mid Exam For SE 2nd With Answers

This document contains the mid-exam questions and results for a Java programming class at Bahir Dar University in Ethiopia dated April 11, 2009. The exam contains 11 multiple choice and coding questions related to Java concepts like threads, Swing components, database connectivity etc. The student scored 25/30 on the exam.

Uploaded by

abreham ashebir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Bahir Dar University, Bahir Dar Institute of Technology, Faculty of Computing,

Advanced Programming- Java, SEng2072: Mid-Exam.


[Wednesday; April 11, 2009 E.C; 8:00-10:00 (local)]

Result
25

Name ____________________________________
ID No:______________________________________

General Directions:
 Write your Name and ID
 Put your answer on the space provided on the question sheet itself.
 Keep your eyes on your own paper or I will give you ZERO.
 Given Time- 2:00 hours
Answer the following questions shortly by writing ONLY the needed lines of codes
1. Write the lines of codes that create five radio buttons (named A and B) and adds them to a
panel named optionPanel. (2 marks)
JRadioButton radioButton1 = new JRadioButton("A");
JRadioButton radioButton2 = new JRadioButton("B");
ButtonGroup btnGroup = new ButtonGroup();
JPanel optionPanel = new JPanel();
btnGroup.add(radioButton1);
btnGroup.add(radioButton2);
optionPanel.add(btnGroup);
2. Assume you have a class with three synchronized methods namely m1() and m2(). You also
want to create two threads th1 and th2 corresponding to the two methods. These threads want
to execute the two methods without relying on the monitor of the instance which contains
these thread safe methods. Now, how must you define the methods? Show ONLY important
line of codes that are relevant to your justification. (3 marks)
All the two methods should be synchronized using 'synchronized block' approach
as show in the following example.
Object obj1 = new Object();
Object obj2 = new Object();
public void m1(){
synchronized(obj1){
//statements to be synchronized here
}
}
public void m2(){
synchronized(obj2){
//statements to be synchronized here
}
}

3. What happen to a thread when it calls directly a run() method of another thread? (1 mark)
A new thread of execution will not be created. The main thread will call the method run() as it does for
any ordinary method. That is, no new thread will be created.
4. Where do you call sleep()of class thread in your code. What is the name of the exception
which can possibly be thrown when you call this method from within your code? (1mark)
Method sleep() of class Thread is a static method which can be invoked anywhere in our code.
Java.lang.InterrruptedException can possibly be thrown from the call to this method.
5. Write the full qualified name of a class in java where wait(), notify() and
notifyAll() methods are defined in. (1 mark)
All these methods are found in the super class of all java classes – Object class. It full qualified name is
java.lang.Object

Given the following user interface, answer the questions starting from 6 to 9.

6. Write line of codes which create the label and the combo box pointed as „ሀ‟ and adds them to
panel named as countryPanel. Besides, your code should fill the combo box with the
elements depicted in the combobox. Use GridLayout to arrange the components as shown
on the picture. (2 marks)
JLabel label = new JLabel("ሃ ገ ር");
String[] listOfCountries = {"ኢትዮጵያ","ኬን ያ ","ሱዳን ","ጅቡቲ","ኤርትራ","ግብፅ "};
JComboBox combo = new JComboBox(listOfCountries);
JPanel countryPanel = new JPanel();
countryPanel.setLayout(new GridLayout(1,1));
countryPanel.add(label);
countryPanel.add(combo);
7. Write line of codes which create the panel and all the buttons indicated as „ለ‟. Use
FlowLayout to arrange them as shown on the picture. (2 marks)
JButton button1 = new JButton("ተን ብይ");
JButton button2 = new JButton("ሰርዝ");
JButton button3 = new JButton("አ ጥፋ");
JButton button4 = new JButton("የ ሃ ገ ራት ዯረጃ");
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
panel.add(button1);
panel.add(button2);
panel.add(button3);
panel.add(button4);
8. Write a listener class (use anonymous class) and register it to handle the event created when
button “ተንብይ” is clicked. The listener class should create three threads (use anonymous class
to create all the threads) on the body of its actionPerformed() method where:
a. the first thread displays the message “መተንበይ አይቻልም” five times. (2 marks)
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
Thread thread1 = new Thread(new Runnable(){
public void run(){
for(int i=0; i<5; ++i)
System.out.println("መተን በይ አ ይቻልም");
}
});
thread1.start();
}
});
b. the second thread displays the message “መተንበይ ይቻላል” ten times (2 marks)
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
Thread thread2 = new Thread(new Runnable(){
public void run(){
for(int i=0; i<10; ++i)
System.out.println("መተን በይ ይቻላ ል");
}
});
thread2.start();
}
});
c. the third thread changes the label of the button to “በድጋሜ ተንብይ”. (2 marks)
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
Thread thread3 = new Thread(new Runnable() {
public void run() {
button1.setText("በድጋሜ ተን ብይ");
}
});
thread3.start();
}
});
9. Write a listener class (use member inner class) and register it to handle the event created
when button “ሰርዝ” is clicked. This listener class should create a thread pool of 2 which
display the following tasks: (3 marks)
Task-1: “ሰርዝ ማለት አጥፋ ማለት አይደለም”
Task-2: “ሰርዝ አስወግድ ማለትም አይደለም”
Task-3: “ሰርዝ ማለት አቋርጥ ማለትም አይደለም”
Task-4: “ሰርዝ ማለት ደምስስ ማለት ነው”
//registering the listener to the button
MidExamListener mel = new MidExamListener();
button2.addActionListener(mel);

private class MidExamListener implements ActionListener{


public void actionPerformed(ActionEvent event) {
ExecutorService executor = Executors.newFixedThreadPool(2);
String tasks[] = {"ሰርዝ ማለት አ ጥፋ ማለት አይዯለም" ,"ሰርዝ አ ስወግድ ማለትም አይዯለም",
"ሰርዝ ማለት አቋርጥ ማለትም አይዯለም","ሰርዝ ማለት ዯምስስ ማለት ነ ው"};
for(final String message:tasks){
executor.execute(new Runnable(){
public void run(){
System.out.println("Task-"+Thread.currentThread().getId()+" "+message);
}
});
}
}
}
10. [Questions related the project which you are currently working on]
a. Write the full statements of your project work given in class. (1mark)

b. Pick one method from your project work. First, write clearly what task is intended
to be done by that method. And then write its full implementation. (2 marks)

11. Assume that you have a database named SuperMarkerDB and a database table Item. Now,
answer the following questions: (4 marks)
a. Write a single line of code which loads a JDBC driver for MS SQL server.
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
b. Write a single line of code which establishes a connection to the database.
con=DriverManager.getConnection(
"jdbc:sqlserver://localhost:1433;databaseName=SuperMarkerDB", "sa", "12345678");
c. Write a single line of code which creates a statement object to issue a query to the database.
PreparedStatement prepStmt=con.prepareStatement("insert into Item values(?,?,?,?)");
d. Construct a query which deletes a single row from the Item table.
delete from Item where itemId = '"+id+"'"

You might also like