Skip to document

Exam FINAL November 2017, questions and answers

Course

Java programming (CSC1016S)

174 Documents
Students shared 174 documents in this course
Academic year: 2017/2018
Uploaded by:
Anonymous Student
This document has been uploaded by a student, just like you, who decided to remain anonymous.
University of Cape Town

Comments

Please sign in or register to post comments.

Preview 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 ~ 2017 November Examination ** SOLUTIONS ** Question Max 1 12 2 7 3 6 4 21 5 10 6 12 7 10 8 24 TOTAL 100 Internal Marks : 100 Time : 10 minutes reading + 120 minutes exam External Instructions: a) Answer all questions. b) Write your answers in PEN in the spaces provided. c) You may use a calculator, BUT show all calculations where required. 1 Question 1 [12] For each question below, write down ONE letter corresponding to the correct answer. (a) What is invoked to create an object? a. a copy of the object a. The main method b. the object is copied, then the reference of the copied object b. A method with a return type c. the reference of the object c. A method with the void return type d. the contents of the object d. A constructor e. A location in memory e. the string containing the values of the instance variables ______________________ ______________________ D C (b) Given the declaration Circle x = new Circle() which of the following statements is most accurate ? (e) Analyze the following code. public class Test { int x; a. x contains an object of type Circle. public Test(String t) { System.out("Test"); } b. You can assign an int value to x. public static void main(String[] args) { Test test = null; System.out(test); } } c. x contains a reference to a Circle object. d. x contains an int value. e. x can't be a Circle. a. The program has a compile error because x has not been initialized. ______________________ C b. The program has a compile error because test is not initialized. (c) Which of the following is the most accurate? a. Java is an interpreted language c. T h e p r o g r a m h a s a r u n t i m e NullPointerException because test is null while executing test. b. Java is a compiled language c. Java is machine code d. The program has a compile error because you cannot create an object from the class that defines the object. d. a and b e. a and c ______________________ e. The program has a compile error because Test does not have a default constructor. D (d) When invoking a method with an object argument, ___________ is passed. ______________________ 2 B b. paint() c. setBackground() d. paintComponent() (l) What is the correct function name to override the paint function for a JPanel? ___________________________________ a. draw() D 4 Question 2 [7] (b) Every class has a toString() method and an equals() method. Where do they come from? How are they used? [3] _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ They come from the object class[1], since all class are subclasses of the object class, and these methods are defined in the Object class. If a class does not override these methods, the default implementation is used [1], otherwise the overridden methods will be used. [1] (c) What is the output of running class Test below? [2] public class Test { public static void main(String[] args) { new Circle9(); } } public abstract class GeometricObject { protected GeometricObject() { System.out("A"); } protected GeometricObject(String color, boolean filled) { System.out("B"); } } public class Circle9 extends GeometricObject { /** Default constructor */ public Circle9() { this(1); System.out("C"); } /** Construct circle with a specified radius */ public Circle9(double radius) { this(radius, "white", false); System.out("D"); } /** Construct a circle with specified radius, filled, and color */ public Circle9(double radius, String color, boolean filled) { super(color, filled); 5 Question 3 [6] Suppose that you are required to implement a system for a technical college to keep track of student information. The information stored for each student includes the student’s number, name, address, phone number and fees due. The college has two types of students, namely, oncampus students and distance-learning students. The matric points obtained by on-campus students must also be stored. Additional information that must be stored for distance-learning students includes the city of the study centre they will report to and the duration over which they are completing the course. The fee for on-campus students is the sum of the course fee and a lab charge. In the case of distance-learning students the fee is a sum of the course fee plus a postage cost for notes. Each student enrols for a number of courses. The details that are stored for each course is the name of the course, the course code, the year and semester that the course was take in, the number of credits for the course and the final mark achieved for the course. Draw a UML class diagram showing the classes and attributes you would use and the associations between them. Do not show any methods. [6] [1] for each class [1] for association [1] for inheritance 7 Question 4 [21] (a) Explain, using examples, the differences between variables of primitive types and reference types. [4] ________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ Every variable represents a memory location that holds a value. When you declare a variable, you are telling the compiler what type of value the variable can hold. For a variable of a primitive type, the value is of the primitive type.[1] For a variable of a reference type, the value is a reference to where an object is located. [1]For example, as shown in Figure, the value of int variable i is int value 1 [1], and the value of Circle object c holds a reference to where the contents of the Circle object are stored in the memory.[11. (b) Define the following terms using 2-3 sentences: i. Inheritance. [2] _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ ii. Polymorphism. [2] _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ Inheritance: inheritance (in Java) is when a class extends another class, and consequently includes all the visible methods and variables from the other class as part of its own interface. 8 return name; } public String getAddress(){ return address; } public abstract int getFee(); } i. Write a Java version of the UML class StandardMember assuming it has this constructor: public StandardMember(String name, String address) and the standard membership fee is fixed at R50. _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ public class StandardMember extends Member{ public StandardMember(String name, String address){ super(name, address); } public int getFee() { return 50; } } use of extends [1] Constructor [1 mark] correct get method [1 mark] ii. Write a Java version of class Society assuming it has this constructor: 10 [3] public Society(String societyName) The class should store all members (both standard and senior) in an ArrayList that will have an initial size of 100. [8] _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ import java.util; public class Society { private String name; private ArrayList<Member> members= new ArrayList<Member>(100); private ManagementCttee MGNTCttee = new ManagementCttee(); private int numMembers = 0; 11 Question 5 [10] (a) State two differences between an interface and an abstract class. [2] _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ An interface contains only method headings and not complete method implementations, and does not contain constructors or instance variables. while an abstract class can contain some methods that have complete implementations, constructors and instance variables(1) A class that implements an interface uses the keyword 'implements' while a class that inherits an abstract class uses the keyword 'extends'(1) An interface and all its methods should be declared public, while an abstract class can have some protected members(1 mark). (b) Can an class implement more than one interface? Explain why or why not and give a supporting example. [2] _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ Yes (0 marks), –If more than one interface is implemented, each is listed, separated by commas (0 marks) Example: public interfaceDemo implements Example, Example2, Example3 (1 mark) (c) List two advantages of inner classes. [2] _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ –They can make the outer class more self-contained since they are defined inside a class 13 –Both of their methods have access to each other's private methods and instance variables. (2 marks). (question continues on next page) (d) Consider the following class: public class Parcel1 { class Contents { private int i = 11; public int value() { return i; } } class Destination { private String label; Destination(String whereTo) { label = whereTo; } String readLabel() { return label; } } public void ship(String dest) { Contents c = new Contents(); Destination d = new Destination(dest); System.out(d()); } public static void main(String[] args) { Parcel1 p = new Parcel1(); p("Tanzania"); } } i. What type of class are Contents and Destination? _________________________________________________________________________ _________________________________________________________________________ 14 [2] Question 6 – Linear Data Structures [12] Consider the code listed in Appendix A and answer the questions that follow. (a) For each of the variables in lines labelled //1, //2, and //3, explain what their purpose is and the data that is stored in them. [3] _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ //1 – head stores a reference to the first node in the linked list (or null if list is empty) and without it no other node in the list can be accessed [1]. //2 The item variable stores the data for the node and will store integer values [1]. //3 The link variable stores a reference to the next node in the list (or null if there is none) [1]. (b) The method hasNext() is part of the iterator inner class and returns a Boolean indicating if there is another node in the list. Write code that will complete this method. [1] _________________________________________________________________________ return (positon != null); (c) T he delete() method in the iterator class is supposed to delete the current node at the iterators position, however the method is incomplete. Write the code that would correctly satisfy this functionality. [4] _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ 16 _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ public void delete() { //To be completed if (position == null) { throw new IllegalStateException(); } else if (previous == null) { head = head; [1] position = head; [1] } else { //Previous and position are consecutive nodes previous = position; [1] position = position; [1] } } (d) Explain what the main difference is between a stack and a queue. _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ Stack: Last in frst out (Items are removed in reverse order of which they were added) [1] Queue: First in frst out (Items added at the end and removed from the front) [1] 17 [2] Question 7 – Graphical User Interfaces [8] Consider the code in Appendix B and answer the questions that follow (a) Draw the GUI that is displayed when this program runs. [3] Title and Frame [0] Label [0] TextArea [0] Buttons [0] Correct Layout [1] (b) For the line labelled //1 i. Explain what the DISPOSE_ON_CLOSE option does. _________________________________________________________________________ 19 [1]

Was this document helpful?

Exam FINAL November 2017, questions and answers

Course: Java programming (CSC1016S)

174 Documents
Students shared 174 documents in this course
Was this document helpful?
University of Cape Town ~ Department of Computer Science
Computer Science 1016S ~ 2017
November Examination
** SOLUTIONS **
Marks : 100
Time : 10 minutes reading + 120 minutes exam
Instructions:
a) Answer all questions.
b) Write your answers in PEN in the spaces provided.
c) You may use a calculator, BUT show all calculations where required.
1
Please fill in your Student Number and Name.
Student Number : __________________________________
Name:
______________________
______________________
Student Number:
______________________
Question Max Internal External
1 12
2 7
3 6
4 21
5 10
6 12
7 10
8 24
TOTAL 100