M257 MTA Spring2010 Multiple Choice Questions: Identify the choice that best completes the statement or answers the question. (15 marks) 1. If we need various objects that are similar in structure, but not exactly the same, we use. a. composition c. inheritance b. overloading d. None of the above 2. A mutator method is also known as a method. a. main c. private b. setter d. getter 3. Default access level is sometimes known as ' access'. a. public c. package b. protected d. private 4. Which of the following exception types must be caught or declared by the programmer? a. Error c. FileNotFoundException b. RunTimeException d. All of the above 5. HashMap class belongs to package. a. java.lang c. java.io b. java.util d. java.swing 6. ArrayList<E> class is a(an) class. a. abstract c. generic b. object wrapper d. All of the above 7. In Java, allow the primitive data types to be treated as objects. a. abstract classes c. generic classes b. object wrappers d. None of the above 8. Which of the following Java GUI components is known as a container? a. JTextField c. JList b. JPanel d. JButton 9. Which of the following events can be handled by a MouseListener interface? a. mouse button is pressed c. mouse button is released b. mouse enters a component d. All of the above 10. The Java system has a component known as the that makes decisions about which thread to run next. a. schedular c. event handler b. runnable d. None of the above
Problem Solving: Answer the following questions 1. Write Java code to read a line of text from the user and then display it. Use the BufferedReader class to read the input. 2. Re-write the following code using if-else statement: char c;... switch(c){ case 'y': case 'Y': System.out.println("Yes"); break; case 'n': case 'N': System.out.println("No"); break; default: System.out.println("Wrong answer!"); 3. Write a temperature conversion program that converts from Celsius to Fahrenheit. The Celsius temperature should be entered from the keyboard (via a JTextField). A JLabel should be used to display the converted temperature. Your program should have an interface similar to the following one: Your interface should have the following characteristics/behaviors: - Title: Temp Conversion - Size: 250 x 100 - Layout manager: FlowLayout - The program is terminated when the x button is pressed.
4. (a) What is the output of the following code? Justify your answer. public class Counter extends Thread { private int num; public Counter(int n) { num = n; public void run() { for (int i = 0; i < 5; i++) { System.out.print(num); try { Thread.sleep(10); catch (InterruptedException ex) { public static void main(string[] args) { Counter c1 = new Counter(1); Counter c2 = new Counter(2); c1.start(); c2.start(); (b) Modify the above class so that it uses alternative technique for creating threads. In your answer, underline the changes you make in the above code. Essay: Answer the following questions. 1. What is the difference between the method sleep and the method wait? 2. (a) When an exception is declared in the method header, what happens if the code throws that exception? (b) Briefly explain the meaning of propagating an exception. (c) What happens if the exception mentioned in (a) is not handled in any try-catch statements in the program? 3. In Java event handling, briefly explain the advantage of spearating the source of the event (i.e. the visual components) and the code where the event is handled. 4. List one similarity and one difference between the class ArrayList and arrays.
M257 MTA Spring2010 Answer Section MULTIPLE CHOICE 1. ANS: C 2. ANS: B 3. ANS: C 4. ANS: C 5. ANS: B 6. ANS: C 7. ANS: B 8. ANS: B 9. ANS: D 10. ANS: A PROBLEM 1. ANS: PTS: 3 KEY: Unit 4 2. ANS: char c;... if(c=='y' c=='y') System.out.println("Yes"); else if(c=='n' c=='n') System.out.println("No"); else System.out.println("Wrong answer!"); PTS: 10 KEY: Unit 6 3. ANS: package MTA; import java.awt.*; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.*; public class TempConversion extends JFrame{ JButton tofeh; JTextField cel;
JLabel result; public TempConversion() { //frame properties setdefaultcloseoperation(jframe.exit_on_close); settitle("temp Conversion"); setsize(250, 100); setlayout(new FlowLayout()); //creating gui elements tofeh = new JButton("Covert to Fahrenheit"); cel = new JTextField(10); result = new JLabel(" "); //adding gui elements add(cel); add(result); add(tofeh); //add events public static void main(string[] args) { new TempConversion().setVisible(true); PTS: 10 KEY: Unit 6 4. ANS: (a) The ouput would be something like the following: 1122211122 This output could be different (e.g. 1111122222, 1112211222, etc). Justification: No one thread had exclusive access to the CPU for its entire execution. It is not the case that the threads take it in turns to run. One thread has access for a certain amount of time and then another thread has access, and so on. (b) The alternative code is as follows (changes underlined). public class Counter implements Runnable { private int num; public Counter(int n) { num = n; public void run() { for (int i = 0; i < 5; i++) { System.out.print(num); try { Thread.sleep(10); catch (InterruptedException ex) { public static void main(string[] args) { Counter c1 = new Counter(1); Counter c2 = new Counter(2); Thread t1 = new Thread(c1); Thread t2 = new Thread(c2) t1.start(); t2.start(); PTS: 10 KEY: Unit 8
ESSAY 1. ANS: When a thread invokes the sleep method it holds onto any lock that it has got through having entered a synchronized method. This prevents any other thread getting access to this method (or any other synchronized methods in the same object) while it sleeps. However, with wait, the thread releases the lock it has on the object and so allows other threads to have access while it is waiting. PTS: 4 KEY: Unit 8 2. ANS: (a) then the exception will be passed to the method that invoked this method. (b) Any exception not handled in a particular method will be passed up a chain of invoking methods to a higher level, until an appropriate exception handler is found. This is known as propagating the exception. (c) If no handler exists at any level for this exception, then the program terminates with an error message PTS: 3 KEY: Unit 4 3. ANS: If, at a later date, either the graphical user interface or the processing code has to change then these changes can be localized. PTS: 3 KEY: Unit 7 4. ANS: Similarities - Both can store a number of references - and the data can be accessed via an index. Differences - The ArrayList can automatically increase in capacity as necessary - The ArrayList object requests more space from the Java run-time system, if necessary. - The ArrayList is not ideal in its use of memory. As soon as an ArrayList requires more space than its current capacity, the system will allocate more memory space for it. The extension is more than required for the items about to be added. So there will often be empty locations within an ArrayList that is, the size will often be less than the capacity. PTS: 4 KEY: Unit 5