Anda di halaman 1dari 13

Mittuniversitetet

Department of Information Technology and Media DT007G - Java II, 7.5 hp


October 29, 2010 Teacher - Jamie Walters Instructions
No aids are permitted in this exam, these include but are not limited to: source code listings, Java textbooks, lecture notes, slides, or other material related to Java. A standard dictionary or translation dictionary is permitted. This paper consists of TWO sections. Attempt ALL the questions. If you have not completed a question, you will still be given points based on the parts that have been completed. Before beginning each section, read the instructions for that section carefully. Unless otherwise stated, all questions refer only to the Java Programming Language.

DT007G - Java II, 7.5 hp

Section A
This section consists of TEN multiple choice questions. You should attempt ALL questions. Each correct answer is worth one (1) mark. Write each answer on a new line along with the question number. If there are more than one answers on a single line, the rst letter will be marked as the answer.

1. If class A contains a method which has no implementation within A, then the method must be declared with which keyword? A. static B. abstract C. nal D. Either A or B 2. Look at the code below:
import j a v a . i o . ; c l a s s Test { public s t a t i c void main ( S t r i n g a r g s [ ] ) { try { F i l e f i l e = new F i l e ( t e s t . t x t ) ; f i l e . createNewFile () ; } catch ( IOException ex ) { ex . p r i n t S t a c k T r a c e ( ) ; } } }

(1)

(1)

Assuming the le test.txt already exists within the programs target folder, what would be the result of executing the code above. A. An exception would be thrown B. The original le would be overwritten with the new le C. The new le would be appended with to the original le. D. Both A & B

Page 1 of 12

DT007G - Java II, 7.5 hp

Solution: There was an error with this question the correct answer: The le would not be created. 3. The following interfaces extend java.util.Collection. Which interface(s) allow for duplicate elements? A. java.util.Set B. java.util.List C. Both A & B D. Neither A nor B 4. In class inheritance, a programmer is allowed to dene a new implementation for a method inherited from the superclass. This concept is known as: A. Overriding B. Overloading C. Abstraction D. Reusability 5. Which of the following is used to arrange components within a Graphical User Interface container? A. JPanelManager B. LayoutManager C. InterfaceManager D. All of the above 6. Which of the following can be used to set the layout manager of a container? A. setManager B. setLayoutManager C. setLayout D. setGUILayoutManager 7. A programmer has created the class shown below, in an attempt to print all the numbers between 0 and innity. However the class cannot be compiled.
public c l a s s L i s t e n e r implements Runnable { public void p r i n t ( ) { int x =0;

(1)

(1)

(1)

(1)

(1)

Page 2 of 12

Question 7 continues on the next page. . .

Question 7 continues. . .

DT007G - Java II, 7.5 hp

while ( true ) { System . out . p r i n t l n ( x ) ; x++; } } }

Page 3 of 12

Question 7 continues on the next page. . .

Question 7 continues. . .

DT007G - Java II, 7.5 hp

Which of the following is the likely reason? A. The class is missing the method with the signature: public int run() B. The class is missing the method with the signature: public void run() C. The class is missing the method with the signature: public runThread() D. The class is missing the method with the signature: public static run() 8. Java programs embedded within webpages usually directly extend which class? A. Application B. Applet C. ChatRoom D. Internet 9. Which of the following is not a method of the java.awt.Graphics object? (parameters are ignored) A. g.drawString B. g.drawRect C. g.drawButton D. g.drawLine 10. From the choices given below, which can be used to implement multithreaded applications? A. Implementing the Thread interface B. Implementing the Runnable interface C. Extending the abstract class Runnable D. A, B & C (1) (1) (1)

End of Section A. Continue to Section B.

Page 4 of 12

DT007G - Java II, 7.5 hp

Section B
This section consists of TEN questions. Attempt ALL questions. The marks for each question is stated beside the question.

11. User Datagram Protocol (UDP) is a connection-oriented protocol. Explain why you may agree / disagree with this statement. Solution: UDP is NOT a connection-oriented protocol. Mention that it provides no delivery guarantee, no end point availability guarantee, no end point listening for messages guarantee. Messages maybe delivered out of order, late, or corrupted. 12. Explain what is meant by the term multithreading Solution: For this question just mention the ability to execute multiple threads simultaneously, allowing a program to execute multiple concurrent tasks, background tasks, etc No need to mention processes or even dene what a thread is. 13. Look carefully at the code below:
import j a v a . i o . ; public c l a s s Person { public s t a t i c void main ( S t r i n g [ ] a r g s ) { try { FileOutputStream f o u t = new FileOutputStream ( myobject . data ) ; ObjectOutputStream o b j o u t = new ObjectOutputStream ( f o u t ) ; o b j o u t . w r i t e O b j e c t (new Student ( Johan K a r l s s o n ) ) ; } catch ( E x c e p t i o n ex ) { ex . p r i n t S t a c k T r a c e ( ) ; } } } c l a s s Student { private S t r i n g name ;

(3)

(2)

(3)

Page 5 of 12

DT007G - Java II, 7.5 hp


public Student ( S t r i n g name ) { t h i s . name = name ; } }

If you were to try and compile and execute the code above, what results would you expect and why? Solution: Code compiles but throws a runtime exception. Program is trying to serialize an object of type Student, however Student is not serializable. Notice that for a class to be serialized successfully, two conditions must be met: 1. The class must implement the java.io.Serializable interface. 2. All of the elds in the class must be serializable. If a eld is not serializable, it must be marked transient. 14. (a) In Java, two approaches to inheritance is using abstract classes or interfaces. When is it appropriate to to use an abstract class and when is it appropriate to use an interface? Solution: Mention that Interfaces have no implementation, you might want the inheriting class to implement the functionality. Interfaces permit multiple inheritance Where you want inheritance but not in the super/subclass hierarchy Absract classes have some implementation You might want some base common behaviour that you can dene Keep inheritance in the super/subclass hierarchy (b) You are a programmer working with a bank and are required to maintain a Java program to handle bank accounts. There exists a class called BankAccount with the class implementation:
c l a s s BankAccount { private int accountnumber ; private S t r i n g customername ; private double a c c o u n t b a l a n c e ; public int getAccountnumber ( ) { return accountnumber ;

(4)

(3)

Page 6 of 12

DT007G - Java II, 7.5 hp


} public f i n a l void setAccountnumber ( int accountnumber ) { t h i s . accountnumber = accountnumber ; } public S t r i n g getCustomername ( ) { return customername ; } public void setCustomername ( S t r i n g customername ) { t h i s . customername = customername ; } public double g e t A c c o u n t b a l a n c e ( ) { return a c c o u n t b a l a n c e ; } public void s e t A c c o u n t b a l a n c e ( double a c c o u n t b a l a n c e ) { this . accountbalance = accountbalance ; } }

And a class PersonalAccount with the following implementation:


c l a s s P e r s o n a l A c c o u n t extends BankAccount { @Override public void setAccountnumber ( int accountnumber ) { } }

Suggest why the class PersonalAccount would have a compilation error, suggesting a way to resolve this without modifying the class PersonalAccount. Solution: public void setAccountnumber(int accountnumber) is set to nal in the BankAccount class, it cannot be overridden in PersonalAccount. Fix this by removing the nal keyword. 15. A thread (java.lang.Thread), in Java, can be in one of several states (java.lang.Thread.State) at a time. Identify any two states in which a thread can exist. (2)

Page 7 of 12

DT007G - Java II, 7.5 hp

Solution: Two of the following: NEW - A thread that has not yet started is in this state. RUNNABLE - A thread executing in the Java virtual machine is in this state. BLOCKED - A thread that is blocked waiting for a monitor lock is in this state. WAITING - A thread that is waiting indenitely for another thread to perform a particular action is in this state. TIMED WAITING -A thread that is waiting for another thread to perform an action for up to a specied waiting time is in this state. TERMINATED - A thread that has exited is in this state. Points were awarded even if the words were not exact. So running was acceptable instead of runnable. 16. (a) Explain what is meant by the terms: Exception and Exception Handling? Solution: Exceptions are any abnormal, unexpected events or extraordinary conditions that may occur at runtime that disrupts the normal ow of instructions. Exception Handling is the process of resolving exceptions that occur in a program at runtime by using code that provides for error resolutions or alternative instructions for recovering and continuing with normal processing, terminating application ow or aborting the program completely. (b) A programmer is attempting to throw a custom exception MyException in the class TestExceptions. However, the code shown below, as written by the programmer, has one compilation error, identify this error and suggest a way to improve the code to resolve this.
class TestExceptions { public s t a t i c void main ( S t r i n g [ ] a r g s ) { try { throw new MyException ( ) ; } catch ( MyException ex ) { System . out . p r i n t l n ( A new E x c e p t i o n has been thrown ) ; } catch ( E x c e p t i o n ex ) { System . out . p r i n t l n ( A new MyException has been thrown ) ;

(4)

(3)

Page 8 of 12

DT007G - Java II, 7.5 hp


} } }

And
c l a s s MyException { public MyException ( ) { // do n o t h i n g } }

Solution: MyException needs to extend some existing exceptions class. One possible solution is:
c l a s s MyException extends E x c e p t i o n { public MyException ( ) { // do n o t h i n g } }

17. (a) In Java, what is meant by a layout manager ? Solution: A layout manager is an object that implements the LayoutManager interface* and determines the size and position of the components within a container. (b) Describe the features of the following layout managers: i. GridBagLayout Solution: The GridBagLayout class is a exible layout manager that aligns components vertically, horizontally or along their baseline without requiring that

(2)

(2)

Page 9 of 12

Question 17 continues on the next page. . .

Question 17 continues. . .

DT007G - Java II, 7.5 hp

the components be of the same size. Each GridBagLayout object maintains a dynamic, rectangular grid of cells, with each component occupying one or more cells, called its display area. Each component managed by a GridBagLayout is associated with an instance of GridBagConstraints. The constraints object species where a components display area should be located on the grid and how the component should be positioned within its display area. In addition to its constraints object, the GridBagLayout also considers each components minimum and preferred sizes in order to determine a components size. ii. BorderLayoutManager Solution: BorderLayout lays out a container, arranging and resizing its components to t in ve regions: north, south, east, west, and center. Each region may contain no more than one component, and is identied by a corresponding constant: NORTH, SOUTH, EAST, WEST, and CENTER. When adding a component to a container with a border layout, use one of these ve constants. As a convenience, BorderLayout interprets the absence of a string specication the same as the constant CENTER. (c) Draw an example of a Graphical User Interface with ve buttons arranged using the BorderLayoutManager. Solution: See below (2) (2)

18. Look carefully at the two classes below: FriendsList & Friend:
import j a v a . u t i l . ; class FriendsList { public s t a t i c void main ( S t r i n g [ ] a r g s ) {

(3)

Page 10 of 12

DT007G - Java II, 7.5 hp


L i n k e d L i s t m y l i s t = new L i n k e d L i s t ( ) ; f or ( int i =0; i <10; i ++) { m y l i s t . add (new f r i e n d ( i ) ) ; } Collections . sort ( mylist ) ; } }

And
class Friend { private int f r i e n d i d ; public f r i e n d ( int i d ) { t h i s . f r i e n d i d=i d ; } }

The programmer wishes to sort the list of friends in order to print them to the screen. However, at runtime the program throws an exception. State why this happens and suggest one way of xing the problem. Solution: Friend is not sortable, it needs to implement Comprable or you need to create a Comparator. However there was an error in the code, which results in Friend having no constructor. If either was identied, the grades would have been awarded accordingly. However only one person saw this error. 19. What is the purpose of the javax.swing.SwingWorker class? Solution: SwingWorker is designed for situations where you need to have a long running task run in a background thread and provide updates to the UI either when done, or while processing. 20. Consider the following with relation to Java Networking: 1. Datagrams Page 11 of 12 Question 20 continues on the next page. . . (3) (2)

Question 20 continues. . . 2. Sockets

DT007G - Java II, 7.5 hp

Give a brief scenario, of where using Datagrams would be more suitable than Sockets and one example where Sockets would be more suitable than Datagrams. You are expected to write no more than 5 sentences for each scenario. Solution: For this, I do not expect any denition of sockets or datagrams, no explanation of their advantages or disadvantages. Simple state two scenarios where one is more appropriate than the other. Datagrams, broadcasting messages to computers on a network, audio or video streaming, etc. Socket secure communication, an online game, le transfers, etc.

End of the Examination

Page 12 of 12