Skip to document

Exam November 2016, answers

Course

Java programming (CSC1016S)

174 Documents
Students shared 174 documents in this course
Academic year: 2016/2017
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.

Related Studylists

JavaOdetola Study List

Preview text

Name: ______________________ Please fill in your Student Number and Name. ______________________ Student Number : __________________________________ Stude nt Number: ______________________ University of Cape Town ~ Department of Computer Science Computer Science 1016S ~ 2016 November Examination ** SOLUTIONS ** Question Max 1 4 2 15 3 31 4 5 5 10 6 8 7 12 8 15 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. Note: This exam consists of 20 pages Question 1 [4] For each question below, write down ONE letter corresponding to the correct answer. (a) What type of code does the Java compiler (c) An immutable class is a class: [1] produce? [1] A. where all attribute values remain A. machine code fixed after instantiation of objects of B. byte code the class C. source code B. that may only have static attributes D. both A and B C. that contains no attributes E. both A and C D. that can have at most one object _____________________ instance at any given time b E. that can not have any object instances (b) What is the output of the following Java code snippet? [1] int a = 2; int b = 0; _____________________ a (d) A class that is defined as final is a class which: [1] if( (b > 0) && (a/b > 5) ) System.out("Success"); else System.out("Failure"); A. does not permit its methods to be overridden in any of its derived classes A. Divide by zero exception B. Logical error B. can not extend any other class C. Syntax error C. can not have any derived classes D. Success D. can only have objects with unchanging attribute values E. Failure E. may only have static attributes _____________________ ______________________ e E 2 original object then this change is reflected in the new object which results in the privacy leak. (d) Give the output of the following Java code snippet. [3] int j = 1; int sum =0; for(int i=2; i<4; i++){ sum+=(++j)+i; } System.out(j + " " + sum); _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ 3 10 (e) Consider the static method below: static boolean isEven(int i){ return <INSERT YOUR CODE HERE>; } Use the ternary operator to complete the return statement so that the method determines whether i is even or not. [3] _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ return (i%2==0) ? true : false; 4 Question 3 [31] Consider the Employee and Developer classes (found in Appendix A), which form part of a system to manage information about employees at a software development firm. Now consider a driver program that creates and displays two Developer objects: 1 public class TestDeveloper{ 2 public static void main(String[] args){ 3 4 Developer d1 = new Developer(1, "Ntando", "Xulu", 5 "Java", 1); 6 Developer d2 = new Developer(2, "Sameera", "Wahad", 7 "Java", 4); 8 System.out(d1); 9 System.out(d2); 10 } 11 } (a) Describe the consequences of making the attributes of the Employee class protected rather than private. [2] _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ All derived classes and any class in the same package will now have direct access to these variables, whereas if they were private this would not be allowed. (b) Describe the steps in sequence that occur for the statement at line 4 in the driver program. For each step you should state if any variables are created, if any memory is allocated and if any values are stored in memory. [5] _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ 1 reference variable d1 (1) 5 1, Ntando, Xulu, Java, 1 4, Ntando, Xulu, Java, 1 (f) Describe the purpose of the statement at line 8 in the Developer class. [2] _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ It calls the constructor of the superclass, in this case the constructor in the Employee class (g) Explain what would happen if line 8 in the Developer class was removed. [2] _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ A call to the default constructor super() will be made and this will result in a compile error since it is not provided in the Employee class (h) Write a copy constructor for the Developer class. For full marks, the constructor must make a call to the constructor at line 5 of the Developer class. [4] _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ public Developer(Developer d){ this(d, d, d, d, d); } (i) Suppose that you want to create a derived class of Developer, called LeadDeveloper. A lead developer leads a team of developers. Give the class definition for a LeadDeveloper class with the following members: • an attribute called team which uses an ArrayList to store the current team members • a constructor to create appropriate LeadDeveloper objects • A copy constructor Do not include any accessor or mutator methods. You may use any methods that you defined in the previous questions. Make sure that your class prevents any privacy leaks. [10] _________________________________________________________________________ _________________________________________________________________________ 7 _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ import java.*; public class LeadDeveloper extends Developer{ private ArrayList<Developer> team; public LeadDeveloper(int empID, String firstName, String lastName,String language, double yearsExperience, ArrayList<Developer> team){ super(empID, firstName, lastName, language, yearsExperience); this = new ArrayList<Developer>(); for(int i=0; i<team(); i++){ this.team(team(i)); } } public LeadDeveloper(LeadDeveloper ld){ this(ld, ld, ld, ld, ld, ld); } } [0] private String name = “Old man” [0] private int age = 100; Or write it out. 8 D. All of the above _____________________ 10 Question 5 [10] (a) Describe Unit Testing in one sentence. [1] _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ Smallest testable parts of an application are individually and independently checked for proper operation. (b) Briefly describe two advantages of automated Unit Testing. [2] _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ Any two of the following: 1 mark for each - Fast/Can be executed in a relatively shorter time frame - More reliable/Precise - Requires less human resource (c) In the context of unit testing, what is a Unit Test Case? [1] _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ A unit test case is part of code that ensures that another part of code (method) works as expected (d) What are the characteristics of a formal unit test case? _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ 11 [2] _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ A JUnit Assertion is a method that performs a comparison in a test case method – 1 mark assertEquals(...); - checks whether two values are equal – 1 mark 13 Question 6 [8] (a) What is the difference between an Array and ArrayList object? [1] _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ An ArrayList object can change size at runtime whilst an Array object has a fixed length (b) Describe what happens when the following statement is executed. [2] ArrayList<String> myNames = new ArrayList<String>(); _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ An empty ArrayList object of type String is created – 1 mark the new ArrayList object is created with an initial size of 10 – 1 mark (c) Suppose the statements below are executed after executing the statement provided in (b). List the order of elements in the myNames ArrayList object. [2] myNames(“Vusi”); myNames(“Iminathi”); myNames(“Peter”); myNames(2, “Jane”); myNames(1, “Retha”); _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ “Vusi”, “Retha”, “Jane”, “Peter” - 0 marks for each element in the correct position 14 Question 7 [12] Consider the code given in Appendix B. (a) The code in Appendix B implements a simple Stack data structure using Generics, Linked Lists and Inner Classes. Define the terms: Stack, Generics, Linked List and Inner Class in this context. [4] _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ Generics - are classes that have type parameters – 1 mark A Stack - is a last-in/first-out(LIFO) data structure – 1 mark A Linked List – is a linked data structure consisting of capsules known as nodes that are connected via links – 1 mark An Inner Class - is a class that is defined inside another class – 1 mark (b) Briefly describe the difference between a Queue and a Stack data structure. _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ 16 [2] _________________________________________________________________________ A Queue - is a first-in/first-out(FIFO) data structure such that the first item to be added to the data structure, is the first item to be accessed/removed from the data structure A Stack - is a last-in/first-out(LIFO) data structure such that the last item to be added to the data structure, is the first item to be accessed/removed from the data structure 1 mark for mentioning LIFO vs FIFO 0 mark for any reasonable description of FIFO 0 mark for any reasonable description of LIFO (c) Provide the code that implements the push method of the Stack. [2] _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ top = new Node<T>(itemData, top); 1 mark for correct type parameter and arguments in constructor: Node<T>(itemData, top) 1 mark for creating a new top node in the linked list: top = ... (d) Provide suitable code that implements a peek method for the Stack class in Appendix B. [4] _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ 17 Question 8 [15] Answer the questions that follow, given the code below. public abstract class Sport{ public abstract String scoreBoard(); } public class PhysicalSport extends Sport { String homeTeam; String awayTeam; public PhysicalSport(String hTeam, String aTeam){ this = hTeam; this = aTeam; } public String scoreBoard(){ return homeTeam + " vs " + awayTeam; } } public class FootballGame extends PhysicalSport { private String score; public FootballGame(String hTeam, String aTeam, String score){ super(hTeam, aTeam); this = score; } public String scoreBoard(){ return homeTeam + " vs " + awayTeam + " : " + score; } } (a) Show the exact output of the following code. [2] FootballGame a = new FootballGame("Spurs", "Toon", "3-0"); PhysicalSport b = new PhysicalSport("76ers", "Bulls"); if(b instanceof PhysicalSport){ System.out("Yes"); }else{ System.out("No"); } b = a; FootballGame c = (FootballGame) b; System.out(b()); _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ Yes Spurs vs Toon : 3-0 1 mark for each correct line (b) Which statement in the above code uses upcasting? 19 [1]

Was this document helpful?

Exam November 2016, 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 ~ 2016
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.
Note: This exam consists of 20 pages
Please fill in your Student Number and Name.
Student Number : __________________________________
Name:
______________________
______________________
Student Number:
______________________
Question Max Internal External
1 4
2 15
3 31
4 5
5 10
6 8
7 12
8 15
TOTAL 100