- Information
- AI Chat
Exam November 2015, answers
Java programming (CSC1016S)
University of Cape Town
Recommended for you
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 ~ 2015 November Examination ** SOLUTIONS ** Question Max 1 7 2 8 3 30 4 10 5 15 6 5 7 25 Internal External TOTAL Marks : 100 Time : 120 minutes 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. Question 1 [7] Examine the program below and answer the questions that follow. import java.util; public class Question1 { public static void main(String[] args) { Scanner s = new Scanner(System); System.out("Enter your age. "); int age = s(); if( age > 0 ) { if( age % 2 == 0 ) { System.out("Of course!"); } else { System.out("That's odd."); } } else { System.out("You are too young!"); } } } For each question below, write down ONE letter corresponding to the correct answer. (a) What is an example of a variable that is a primitive? B (c) Which of the following is a true statement? a. s a. Java is an interpreted language b. Question1 b. Java is a compiled language c. Scanner c. Java is machine code d. age d. a and b e. nextInt() ______________________ e. a and c ______________________ D D (b) What is an example of a constructor being invoked? (d) Which of the following is a true statement about Java? a. println("Enter your age. ") b. Scanner(System) a. All classes must have a main() method c. nextInt() b. All methods must be inside a class d. main(String[] args) c. Variable types are determined at runtime e. public class Question1 ______________________ d. Compiler will catch logic errors 2 Question 2 [8] Imagine you are given a String array containing names, for example: String[] names = ["Brian", "Chao", "Sonia", "Edwin"]; (a) Write a code that takes a String array and creates a Person array with one Person object per name. Use the number of characters in a persons name for his/her age. See below for the implementation of the Person class. Your code should use the names and people variables, but should not be hardcoded in anyway. (That is, we should be able to change the content of the names variable without breaking your code) [5] Person class for Question 2: public class Person { private String name; private int age; public Person(n,a) { this = n; this = a; } } public class Question2 { public static void main(String[] args) { String[] names = ["Brian", "Chao", "Sonia", "Edwin"]; Person[] people; // Your code will go here } } _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ 4 (b) Write an overloading constructor for the Person class that takes no parameters and sets the name to “Old man” and the age to 100. [2] _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ [1] correct method signature [1] correct (c) What is another way to set the default values for a new Person object? Without rewriting the whole Person class, write out any code that will change or be added. [1] _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ [0] private String name = “Old man” [0] private int age = 100; Or write it out. 5 (b) Consider the Java class Exam below and answer the questions that follow. Refer to the last page of this exam for the Swing 'handout'. import javax.*; import java.*; import java.awt.*; public class Exam extends JFrame implements ActionListener { private JTextField txt = new JTextField(); public Exam() { setSize(400,400); setDefaultCloseOperation( JFrame_ON_CLOSE); setLayout( new GridLayout(2,2) ); add(txt); JLabel lab = new JLabel(" "); add(lab); JButton pea = new JButton("PEAS"); add(pea); JButton nut = new JButton("NUTS"); add(nut); pea(this); nut(this); } public void actionPerformed( ActionEvent e) { String veg = e(); if (veg("PEAS")) txt("R4"); if (veg("NUTS")) txt("R9"); } public static void main(String[] args) { Exam w = new Exam(); w(true); } } i. Draw the window that would appear on the screen when this program runs. [4] _________________________________________________________________________ ___________ ______________________________________________________________ _________________________________________________________________________ _______________________________________________________________________ ____________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _____________________________________________________________________________ ___________________________________________________________________ 1 mark for frame; 1 mark for placing 2 buttons; 1 mark for text field; 1 mark for layout 7 ii. Variables lab, pea and nut are not instance variables of class Exam, so why is the variable txt an instance variable of Exam? [1] _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ So that it is accessible in both methods iii. The Java code below is trying to add a menu with items “PEAS” and “NUTS” to this GUI so that when one of these menu items is clicked/chosen by the user, the effect is exactly the same as if the button with that name was pressed. Give the missing statements so that this code does this correctly [4] JMenu m = new JMenu(“veg”); JMenuItem p = new JMenuItem( “PEAS”); JMenuItem n = new JMenuItem( “NUTS”); m(p); m(n); _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ JMenuBar bar = new JMenuBar( ); setJMenuBar(bar); bar(m); n(this); p(this); // 1 mark per line above 8 i. Why will this code compile and run, even though the area method of Shape has no body (no statements, just a heading) ? [1] _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ Because it is abstract ii. Show the exact output produced when the main method is run. [5] _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ 1 mark per println iii. Draw a UML diagram showing the relationship between the 3 classes Shape, Circle and Toy. Do NOT show their instance variables or methods. [4] _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ ______ ___________________________________________________________________ 10 ____________________________________________________________________ 1 mark for each relationship +1 mark for inheritance and 1 mark for aggregation iv. Briefly explain the problem in the Toy constructor. _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ 11 [1] System.out(s); return s; } public int ExamStuff() { int y; System.out("int interface with " + y); return y; } } _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ No the program will not compile (1 mark) because ExamStuff() is defined in both interface1 and interface2 without different number or types of parameters (2 marks). (d) Modify interface2 and also modify the integer method ExamStuff() in Question (c) so that the program compiles correctly. Write code below for only interface2 and the integer method ExamStuff()showing both modifications. [2] _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ public interface Interface2 { 13 public int ExamStuff(int y); (1 mark for declaration of a parameter) } public int ExamStuff(int y;) (1 mark for declaration of a parameter) { System.out("int interface with " + y); return y; } (e) T h e Comparable interface provides the method heading public int compareTo(Object other); If the parameter other is not of the same type as the class being defined, then an exception is thrown. Which exception is this? [1] _________________________________________________________________________ _________________________________________________________________________ ClassCastException (1 mark) 14 for (int i = 0; i < list(); i++) (1 mark for correct for loop and 1 mark for correct use of size()) { System.out(list(i)); (1 mark for correct use of get()) } (c) Consider the two linked lists created in the code on the next page. Write the segment of code that merges the items from the two lists into list a and prints the merged list. [5] LinkedList a = new LinkedList(); a("Chao"); a("Noxolo"); a("Krystal"); LinkedList b = new LinkedList(); b("Bob"); b("Dylan"); b("Frances"); b("Gloria"); _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ ListIterator aIter = a(); ListIterator bIter = b(); (1 mark for correctly declaring ListIterator objects 0 marks if partially correct) while (bIter()) (1 mark for use of hasNext()) { if (aIter()) aIter(); (1 mark) aIter(bIter()); (1 mark) } 16 (d) ArrayDeques can be implemented as both Stacks and Queues. The program below shows a program that creates both stack and queue objects and then pushes elements onto a stack. public class StackAndQueue { public static void main(String args[]) { Deque<String> stack = new ArrayDeque<String>(); Deque<String> queue = new ArrayDeque<String>(); stack("A"); stack("B"); stack("C"); stack("D"); } } (i) Write the code segment that puts A,B,C,D into a queue. [2] _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ queue("A"); queue("B"); queue("C"); queue("D"); (2 marks for complete implementation) (ii) Write the code segment that outputs the elements in the queue as long as the queue is not empty. [2] _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ while (!queue()) (1 mark for use of isEmpty()) System.out(queue() + " "); (1 mark for use of pop()). 17 _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ line 1 @RunWith(Parameterized) (1 mark) line 2 @Parameters (1 mark) line 3 @Test (1 mark) line 4 assertEquals(fb(number),"Fizz"); (1 mark for use of assertEquals and 1 mark for correct use of arguments). 19
Exam November 2015, answers
Course: Java programming (CSC1016S)
University: University of Cape Town
- Discover more from: