- Information
- AI Chat
EXAM January 2012, questions
Java programming (CSC1016S)
University of Cape Town
Recommended for you
Related Studylists
JavaPreview text
Name: ______________________ Please fill in your Student Number and Name. ______________________ Student Number : __________________________________ Student Number: ______________________ University of Cape Town ~ Department of Computer Science Computer Science 1016S ~ 2012 January Examination ** SOLUTIONS ** Question Max 1 20 2 12 3 8 4 8 5 4 6 5 7 3 8 20 9 20 TOTAL 100 Marks : 100 Time : 180 minutes Internal External Instructions: a) Answer all questions. b) Write your answers in PEN in the spaces provided. c) Show all calculations where applicable. Question 1 [20] (a) Identify two instance variables and two methods from the case below. [2] An application that manipulates Points on a plane is to be designed using Objected Oriented Design. In this application, Points must have X and Y coordinates, move on the plane as well as display their current values of X and Y. Points can also determine if they are in the zero position (0,0) with a boolean answer. Instance variable ? X, Y (1 mark) Methods ? Move(), DisplayValues(), ZeroPosition() (1 mark) (b) Assume the existence of a Java file myClass. Explain how Java uses both a compiler and interpreter to achieve its architecture-neutral characteristic. [2] Using the compiler (javac), Java converts the source code (myClass ) into bytecodes (myClass). The bytecodes are intermediary between the high-level language (human-readable) and machine language (machine-readable) The bytecodes are then interpreted by the java interpreter (java); the latter is available on most computer platforms such as Mac-OS and Windows. Hence, it is possible to run same java code on diverse architectures. <1 mark> (c) Using an example, explain the implication of using the keyword void in a method declaration. [1] public void readInput() the method does not return any value (d) Given an Object Oriented System that manages student records (name, date of birth, gender etc), use examples to differentiate between Class and Object as used in OOP. [4] A class describes a collection of related objects; in this case, a class called Student is a collection of all students. An object is an instance of a class; in this case, a specific student, identified by actual values (say Tom, 23 March 1996, Male) is an object of type student. <2 marks each> (e) Study the code below and answer the questions that follow. import java.util; public class TestDoWhile { public static void main (String [] args) { int count, number; System.out ("Enter an integer number between 5 and 10"); Scanner keyboard = new Scanner (System); number = keyboard (); 2 Class methods are called within the class; they are not associated with an instance of the class. The syntax for invoking such methods is ClassName(...). For example, System.out(...), here, println is a class method. <2 marks> Instance methods are associated with an instance and their invocation syntax is: objectName(...). For example, test1(...) is a instance method, its invocation in this case is associated with the instance called test1 <2 marks> 4 Question 2 [12] (a) Explain each of the following Java programming terms: i. extended [1] ii. protected [1] iii. instanceOf [1] iv. abstract [1] Give 2 or 3 lines each, 1 mark for any reasonable explanation of each. (b) Use the program below to answer the questions that follow: abstract class Creature{ public abstract void makeNoise(); } class Beast extends Creature { public Beast( ) {System.out("beast"); } public void makeNoise() {System.out("argh!"); } } class Unicorn extends Beast { public Unicorn( ) {System.out("unicorn"); } public void makeNoise() { System.out("Neigh!"); } } class CreatureDemo { public static void main(String[] args) { Beast m = new Beast(); m(); Unicorn x = new Unicorn(); x(); } } i. Give the exact output this program will produce when it runs. beast argh! beast unicorn Neigh! 5 [3] Question 3 [8] Use the program below to answer the questions that follow: import javax.*; import java.*; import java.awt.*; public class Sup extends JFrame implements ActionListener { public static void main( String[ ] args ) { Sup gui = new Sup( ); gui(true); } public Sup( ) { setSize(600,600); setTitle("sup"); setDefaultCloseOperation(JFrame_ON_CLOSE); setLayout( new GridLayout(2,2) ); JButton b = new JButton( "BUTTON" ); add( b ); JLabel lab; lab = new JLabel( "LABEL" ); add( lab ); } public void actionPerformed( ActionEvent e ) { String what = e( ) ; } } (a) Draw the GUI that is displayed when this program runs. [3] 1 mark each for frame, button, label. (b) To which object does the method add belong that is called in the statement below: [1] add( lab ); this (c) Give all the Java statements that are needed to change the label from "LABEL" to "GREAT" when the button is pressed. As part of your answer, show the place(s) where these statements would go in the code above. [3] in Sup( ) add b(this); in actionPerformed add lab("GREAT"); and move the statement below from inside the constructor to become an instance variable (or similar) 7 JLabel lab; (d) Given that we have GridLayout(2,2), what would happen if we changed the code by adding 6 buttons and 6 labels (instead of just 1 button and 1 label)? [1] They would still be arranged in 2 rows (but now of 6 each). 8 ii. char iii. float iv. Double Answer: D (d) When using ArrayLists the method _____________ can be used to save memory. i. trimOff() ii. Downsize() iii. update() iv. trimToSize() Answer: D (e) A class that uses an interface must use the keyword: i. extends ii. implements iii. super iv. inherits Answer: B (f) In Java a derived class can have _____________ base classes. i. one ii. two iii. three iv. unlimited Answer: A 10 (g) Java has a way of officially hiding details of a class definition. To hide details, you mark them as _______________. i. public ii. protected iii. private iv. all of the above Answer: C (h) Accessor methods: i. return something equivalent to the value of an instance variable ii. promote abstraction iii. Either A or B iv. Both A and B Answer :D 11 Question 6 [5] : Generics The following generic class definition contains a method public T max() that returns the maximum of two elements in an ordered pair. public class Pair<T> { ... public T max() { if (first(second) <= 0) return first else return second } public static <T> T getMidpoint(T[] a) { return a[a/2]; } ... } When implementing generic classes or methods of this sort, in Java, it sometimes makes sense to restrict the possible types that can be plugged in for the type parameter T. For instance, in order to ensure that the types plugged in for T satisfy the Comparable interface, we would begin the class definition as follows: public class Pair<T extends Comparable> (a) Using the interfaces Comparable and Clonable, give a Java example of a class definition in which the bounds expression contains multiple interfaces. [2] public class Pair<T extends Comparable & Clonable> [2] (b) Define a generic method named getMidIndex, which is similar (like) getMidPoint (see code snippet above), but returns the index of the array midpoint as opposed to the element at the midpoint. [3] public static <T> int geMidindex(T[] a) – (1 mark) { return a/2; -- (2 marks) } 13 Question 7 [3] : Linked Data Structures What operations are difficult or expensive to perform in a doubly linked list? Why? Give one example to support your explanation. [3] Updating links is expensive [1] because for each operation we update two links as opposed to one in singly linked lists [1]. (For instance both the next and previous links are update instead of just the next)[1] 14 Question 9 [20] Information kiosks are desktop computers placed in secure locations for members of the public to access. You have been asked to develop software to allow people to keep photographs on existing kiosks, so that they can keep not only photos of friends and family there, but also photos of important documents (such as receipts or medical prescriptions) in case they are needed in future. The software will allow users to upload, search and download photographs. A directory keeps the following information for each photograph: user name, file name, date uploaded and description (short text given by user to remember what the photo is about). (a) Give a class declaration for the class DirectoryEntry. It must contain at least three of the main instance variables with their appropriate member-access modifiers. Do not include methods.[2] public class DirectoryEntry { private String userName, fileName, description; private Date fileDate; } <0 for each variable; it must have the keyword ‘private’ … give a mark if the student gives String type for the fileDate variable> (b) Provide method signatures for three possible methods of the DirectoryEntry class. Include a one-line comment to explain the functionality of each of your methods. DO NOT write the body of the methods. [3] public void download(String theFile){ //downloads the photo whose file name is 'theFile' } public boolean upload(String theFile) { //uploads the photo whose file name is 'theFile'; it returns True if successful and False otherwise } public String search(String theFile){ //searches for the photo whose file name is 'theFile'; it returns the file description if found and null otherwise } <1 mark each; must include the ‘public’ keyword.> (c) Draw a rough diagram to show the user interface you would design for this and explain briefly how users would interact with it (use it). [5] (d) Consider the following Java code snippet that inserts photographs into a doubly linked list. Each insertion contains entries of type String for the user name, file name and description of the photograph. The method addToStart(...) adds a node to the front of the linked list while outputList prints the contents of the nodes in the linked list. LinkedListDouble list = new LinkedListDouble(); 16 list(“Ratonga”,“MyPiks1”,”Just Cape Town”); list(“SheBaby”,“MyPiks2”,”Table Mountain”); list(“TwoTies1”,“MyPiks3”,”Ratanga”); list(“12345pik”,“MyPiks4”,”Hout Bay”); list(); i. Give the output, in the order printed to the screen, of the code snippet above. [2] 12345pik,MyPiks4,Hout Bay;TwoTies1,MyPiks3,Ratanga; SheBaby,MyPiks2,Table Mountain; Ratonga, MyPiks1, Just Cape Town ii. Write a boolean-valued method named checkEmpty that returns true if the list is empty and false if the list has at least one node in it. [3] public boolean checkEmpty () – (1 mark) { return (head == null); -- (2 marks) } (e) Discuss 2 major professional issues that arise in developing this application. 17 [5]
EXAM January 2012, questions
Course: Java programming (CSC1016S)
University: University of Cape Town
- Discover more from: