M257 MTA Faculty of Computer Studies Information Technology and Computing Date: /1/2011 Duration: 60 minutes 1-Version 1 M 257: Putting Java to Work Midterm assessment - MAKEUP Fall 2010 Student Name: Student ID: Section No.: IMPORTANT INSTRUCTIONS 1. This exam has 3 parts and ALL questions must be answered. 2. This exam consists of 7 pages (including the space you should use for your answer). 3. Student handbooks are NOT permitted in this examination. 4. The use of electronic devices that could have a memory is NOT permitted. 5. At the end of the examination, check that you have written your student ID, name and your section number on the first page. P a g e 1 M257 MTA Fall 2010
Multiple Choice Questions: Identify the choice that best completes the statement or answers the question. (16 marks) 1.... translates and executes Java bytecode. a. Java interpreter c. Java debugger b. Java compiler d. Java disassembler 2.... means that subclasses modifies the methods they inherit from a superclass. a. Inheritance c. Extension b. Overriding d. Overloading 3. The Java Swing library includes... a. containers c. visual components b. layout managers d. all of the above 4. 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 5. is a reference to the class in which it appears. a. Constructor c. super b. this d. None of the above 6. The keyword that specifies that a particular class cannot be instantiated is: a. static c. abstract b. final d. protected 7. Which of these data type requires the most amount of memory? a. long c. short b. int d. byte 8. Strings that cannot be changed at all, once created are said to be: a. adjustable c. immutable b. replaceable d. mutable Essay: Answer the following questions. (28 marks) 1. What is the main difference between private, protected, and public class members in terms of how they can be accessed? (7 marks) Private: private members can be accessed only within its defining class Public: any class can access public members of a public class using a qualified name. Protected: classes in the same package AND subclasses can access protected members using a qualified name. P a g e 2 M257 MTA Fall 2010
2. In Java threads, what is the difference between the runnable state and the running state? (7 marks) Runnable State: The start method moves a thread into the runnable state. This does not mean that it will run immediately (there may be another thread that is being run by the CPU). Running state: When a thread is in the running state, it means the thread is actually executing. 3. What is meant by static variables and static methods? (7 marks) Static Variables: When you declare a static variable in a class, you are specifying that there is exactly one copy of the variable for all objects defined by that class. They are usually used to define constants, or one value for all objects Static Methods: Static methods carry out general functions not associated with objects. 4. In the context of Java programming, what is meant by composition? Give an example of a code with illustration of how it is implemented by the computer (7 marks) Composition occurs when objects of a class contain objects of another class; i.e. an object of the class is 'composed' of objects from one or more other classes. For Example: (any other example is acceptable) P a g e 3 M257 MTA Fall 2010
Problem Solving: Answer the following questions (56 marks) 1. Re-write the following code using for loops: (10 marks) int i, j; i = 1; while(i < 10){ System.out.print("O"); j = 10; while(j > 1){ System.out.print("X"); j = j - 2; System.out.println("O"); i = i + 2; for (int i = 1; i < 10; i = i + 2) { System.out.print("O"); //first loop //second loop //first loop for (int j = 10; j > 1; j = j - 2) { //second loop System.out.print("X"); System.out.println("O"); 2. Write Java code to read only the first line of text from the file c:\test.txt, and then display it on the screen. Your program should have the following specifications: (12 marks) Use the BufferedReader class to read the input. If an IOException occurred, an appropriate error message should be displayed. Use try catch statement to handle the IOException try{ BufferedReader in = new BufferedReader(new FileReader("c:/test1.txt")); System.out.println(in.readLine()); catch(ioexception exception){ System.out.println("Error reading the file"); P a g e 4 M257 MTA Fall 2010
3. Consider the following class, and then answer the questions about it. (20 marks) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 public class GUI extends JFrame { JLabel msg; JButton b1, b2; public GUI() { setsize(100, 150); setlayout(new FlowLayout()); msg = new JLabel("How old are you?"); b1 = new JButton("Over 30"); b2 = new JButton("Under 20"); add(msg); add(b1); add(b2); public static void main(string[] args) { GUI g = new GUI(); g.setvisible(true); a) Draw (or describe) the result obtained when running the main method in the above class. Your drawing doesn t have to of an accurate scale. (5 marks) b) What will happen if you clicked the close button (i.e. the X button on the top right corner) on the displayed output window? (3 marks) The window will not be closed (i.e. it will be hidden). But the program will still be running in the background. P a g e 5 M257 MTA Fall 2010
c) Modify the above code by adding an action listener so that: (11 marks) When the button b1 is clicked, the message You are old is displayed in the label msg. When the button b2 is clicked, the message You are young is displayed in the label msg. In your answer, you need to write a private class name myhandler that implements the ActionListener interface. The class myhandler should be used to handle the events of both buttons b1 and b2. Note: you don t need to rewrite the above code. You may just write the line numbers of the statements to be modified, or the line numbers at which additional code is inserted. After line 9: After line 17 b1.addactionlistener(new myhandler()); b2.addactionlistener(new myhandler()); private class myhandler implements ActionListener{ public void actionperformed(actionevent e) { JButton b = (JButton) e.getsource(); if(b==b1) msg.settext("you are old"); else if(b==b2) msg.settext("you are young"); 4. Consider the following Java code then answer the questions about it: (14 marks) 1 public class ThreadQ extends Thread{ 2 private String name; 3 public ThreadQ(String s) {name = s; 4 public void run() { 5 for (int i = 0; i < 3; i++) { 6 System.out.println(name + " is active"); 7 try { 8 Thread.sleep(100); 9 catch (InterruptedException ex) { 10 11 System.out.println(name + "is DONE!"); 12 13 public static void main(string[] args) { 14 ThreadQ t1q = new ThreadQ("Thread 1"); 15 ThreadQ t2q = new ThreadQ("Thread 2"); 16 ThreadQ t3q = new ThreadQ("Thread 3"); 17 t1q.start(); 18 t2q.start(); 19 t3q.start(); 20 P a g e 6 M257 MTA Fall 2010
(a) What is the output of the above code as displayed on the screen? Justify your answer. (6 marks) (a) The output would be something like the following:: Thread 2 is active Thread 1 is active Thread 3 is active Thread 2 is active Thread 1 is active Thread 3 is active Thread 2 is active Thread 1 is active Thread 3 is active Thread 2is DONE! Thread 3is DONE! Thread 1is DONE! (This output could be different (i.e. the order of threads is different; e.g. 123231132 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) Modify the above class so that it uses alternative technique for creating threads. (8 marks) Note: you don t need to rewrite the above code. You may just write the line numbers of the statements to be modified, or the line numbers at which additional code is inserted. public class ThreadQ implements Runnable{... //SAME CODE UP TO THE MAIN METHOD public static void main(string[] args) { ThreadQ t1q = new ThreadQ("Thread 1"); ThreadQ t2q = new ThreadQ("Thread 2"); ThreadQ t3q = new ThreadQ("Thread 3"); Thread t1 = new Thread(t1Q); Thread t2 = new Thread(t2Q); Thread t3 = new Thread(t3Q); t1.start(); t2.start(); t3.start(); P a g e 7 M257 MTA Fall 2010