University of Cape Town Department of Computer Science. Computer Science CSC115F. June 2003/2004 Supplementary Exam

Similar documents
University of Cape Town Department of Computer Science. Computer Science CSC115F

University of Cape Town Department of Computer Science. Computer Science CSC115F. June Exam 2002

University of Cape Town ~ Department of Computer Science Computer Science 1015F ~ June Exam

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

Algorithms and Problem Solving

University of Cape Town Department of Computer Science. Computer Science CSC115F

COMPUTER APPLICATIONS

Object Oriented Programming 2015/16. Final Exam June 28, 2016

SECONDARY SCHOOL, L-IMRIEĦEL HALF YEARLY EXAMINATIONS 2016/2017

F I N A L E X A M I N A T I O N

Lesson 3: Accepting User Input and Using Different Methods for Output

CSC 1214: Object-Oriented Programming

Ryerson University Vers HAL6891A-05 School of Computer Science CPS109 Midterm Test Fall 05 page 1 of 6

Tools : The Java Compiler. The Java Interpreter. The Java Debugger

University of Cape Town Department of Computer Science Computer Science CSC1017F

Birkbeck (University of London) Software and Programming 1 In-class Test Mar 2018

Selected Questions from by Nageshwara Rao

Inheritance and Polymorphism

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

Objects and Classes Lecture 2

BSc. (Hons.) Software Engineering. Examinations for / Semester 2

Arrays. Eng. Mohammed Abdualal

HALF-YEARLY EXAMINATIONS FEBRUARY Subject: Computing Form: 4 Time: 1 ½ hours MARKING SCHEME

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class.

CS 110 Practice Final Exam originally from Winter, Instructions: closed books, closed notes, open minds, 3 hour time limit.

We will work with Turtles in a World in Java. Seymour Papert at MIT in the 60s. We have to define what we mean by a Turtle to the computer

Topic 7: Algebraic Data Types

CSC-140 Assignment 6

Classes Basic Overview

A sample print out is: is is -11 key entered was: w

1.00/ Introduction to Computers and Engineering Problem Solving. Final / December 13, 2004

The University of Western Ontario Department of Computer Science Computer Science 1026a Midterm Exam 2 hours

Summer Session 2004 Prelim I July 12, CUID: NetID:

Exam 1 - (20 points)

BSc (Hons) Computer Science with Network Security/ BSc (Hons) Software Engineering/ BSc (Hons) Web Technologies. Examinations for 2016 Semester 1

Computer Science 217

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

SAMPLE QUESTIONS FOR DIPLOMA IN INFORMATION TECHNOLOGY; YEAR 1

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2013

HALF-YEARLY EXAMINATIONS FEBRUARY

Inheritance and Interfaces

COP 3330 Final Exam Review

Computer Science 1 Ah

CS165 Practice Final Exam

Outline. Turtles. Creating objects. Turtles. Turtles in Java 1/27/2011 TOPIC 3 INTRODUCTION TO PROGRAMMING. Making things to program with.

Exam Duration: 2hrs and 30min Software Design

Birkbeck (University of London) Software and Programming 1 In-class Test Mar Answer ALL Questions

CIS 110 Introduction To Computer Programming. February 29, 2012 Midterm

CMSC 331 Second Midterm Exam

CS 231 Data Structures and Algorithms, Fall 2016

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

University of Cape Town ~ Department of Computer Science Computer Science 1015F ~ Test 2. Question Max Mark Internal External

About this exam review

Instance Method Development Demo

APCS Semester #1 Final Exam Practice Problems

University of Cape Town Department of Computer Science. Computer Science CSC117F Solutions

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

Expressions & Flow Control

CSC System Development with Java. Exception Handling. Department of Statistics and Computer Science. Budditha Hettige

CISC 3115 Modern Programming Techniques Spring 2018 Section TY3 Exam 2 Solutions

Oracle 1Z Java SE 8 Programmer I. Download Full Version :

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 25, Name: KEY A

Computer Science II (20082) Week 1: Review and Inheritance

10/30/2010. Introduction to Control Statements. The if and if-else Statements (cont.) Principal forms: JAVA CONTROL STATEMENTS SELECTION STATEMENTS

Building Java Programs

Lab5. Wooseok Kim

Creating objects TOPIC 3 INTRODUCTION TO PROGRAMMING. Making things to program with.

INTRODUCTION TO SOFTWARE SYSTEMS (COMP1110/COMP1140/COMP1510/COMP6710)

CSC Algorithms and Data Structures I. Midterm Examination February 25, Name:

Introduction to Computer Science II (ITI 1121) Midterm Examination

Simple Java Reference

CS 139 Practice Midterm Questions #2

Computer Science II (20073) Week 1: Review and Inheritance

1.00/ Introduction to Computers and Engineering Problem Solving. Final / December 13, 2004

CSE wi Midterm Exam 2/8/18. Name UW ID #

Name CIS 201 Midterm II: Chapters 1-8

Full download all chapters instantly please go to Solutions Manual, Test Bank site: testbanklive.com

AP Programming - Chapter 6 Lecture

Software Practice 1 - Basic Grammar Basic Syntax Data Type Loop Control Making Decision

Topic 4 Expressions and variables

Object Oriented Programming with Java

Object- Oriented Analysis, Design and Programming

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

Framework Fundamentals

Introduction to Programming Using Java (98-388)

COSC 1010 Introduction to Computer Programming

FAQ: Classes & Objects

CS 1331 Exam 1. Fall Failure to properly fill in the information on this page will result in a deduction of up to 5 points from your exam score.

CIS 110 Introduction to Computer Programming. 17 December 2012 Final Exam

ESC101 : Fundamental of Computing

Topics. Java arrays. Definition. Data Structures and Information Systems Part 1: Data Structures. Lecture 3: Arrays (1)

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

Chapter 12: Inheritance

Computer Programming, I. Laboratory Manual. Experiment #3. Selections

Introduction to Computing II (ITI 1121) Final Examination

Object Oriented Programming 2013/14. Final Exam June 20, 2014

CSC 1051 Algorithms and Data Structures I. Final Examination May 12, Name

Problem Grade Total

CN208 Introduction to Computer Programming

Transcription:

University of Cape Town Department of Computer Science Computer Science CSC115F June 2003/2004 Supplementary Exam Answer all questions. All questions that refer to elements of programming make reference to the Java programming language as studied in class. Marks: 100 Time: 3 hours Surname NAME: STUDENT NO: Approximate marks per question are shown in brackets The use of calculators is permitted Initials COURSE CODE: CSC This paper consists of 14 questions and 20 pages (including this cover page). Mark Allocation Quest Marks Internal External Quest Marks Internal External 1 [5] 8 [8] 2 [5] 9 [7] 3 [5] 10 [10] 4 [5] 11 [11] 5 [5] 12 [5] 6 [15] 13 [5] 7 [10] 14 [5] Total Total Grand Total Final Mark Internal Examiner: External Examiner: 1

Question 1. Section 1. Introduction and Turtle Graphics [5 marks] Explain the following computing concepts in your own words, give examples where appropriate. a) Secondary memory b) Parameter [1] c) Java Virtual Machine [1] d) A high level language [1] e) one of the following is an illegal java name. Which one and why? myshape; 24age; Utility; work4s [1] [1] 2

Question 2. [5 marks] Using the Turtle class write a class called DrawFshape that draws the following shape. The final position of the turtle is not important. The Turtle instruction set: Move( ) // Moves the Turtle, if the pen is down leave a line. TurnRight( ) // Turns the turtle to the right. Max turn is 180 degrees. PenUp( ) // Picks up the Turtle pen. PenDown( ) // Puts down the Turtle pen. IsPenDown( ) // Returns true or false depending on the state of the Turtle. [5] 3

Question 3. [5 marks] Using the Turtle class write a method for a SmartTurtle class called drawrectangle(length, breadth) that accepts 2 parameters, length and breadth. The method should draw a rectangle with the given length and breadth. Your answer should include the definition of the SmartTurtle class. for example, drawrectangle(450, 200) produces: Use the Turtle instruction set from the previous question. [5] 4

Question 4. [5 marks] Write a java class called Shape to draw the following shape. The side of each equilateral triangle is 200 turtle steps. Use the information about the turtle given in the previous question. You should make your program as efficient as possible. [5] 5

Question 5. [5 marks] A programmer is writing a class using the Turtle class to draw a square (Note: the pen starts down). This is the first attempt and the compiler detects 5 errors. Underline the errors, and show the correction in the space provided. get turtlegraphics.*; public DrawSquare public static void main( String[] ) throws myturtleexception Turtle myturtle = new Turtle(); myturtle.move(500); myturtle.turnright(90); myturtle.move(500); myturtle.turnright 90 ; myturtle.move(500); myturtle.turnright(90); myturtle.move(500); myturtle.turnright(90); 1. 2. 3. 4. 5. [5] 6

Question 6. [15 marks] Section 2. Java Basics a) List the 3 syntax errors in the following code fragment (line numbers are added so you can refer to specific lines): line1: public double func ( int a, b ) line2: line3: float 3blindmice = 2a + 1; line4: return 12.0; line5: b) List one difference between local (method) variables and instance variables. [3] c) Prefix the expression 1/3 with an appropriate typecast to force the calculation to occur in a floating-point context. [2] [1] 7

d) Write the method processpoly to calculate the value of the expression for a given value of. Your method must assume that,, and are double instance variables. processpoly must take the double as its single parameter and return false if the value of the expression is negative and true otherwise. The value of the expression must be stored in the instance variable. Remember that the Math.pow (x, y) method calculates. [4] 8

e) Write a method isvalid to determine if a date is valid or not. Your method must take 2 integer parameters for the month and day and return a boolean result. Month values range from 1 to 12. Assume that the year is not a leap year. For example, 24/05 is valid but 29/02 is not. Remember: 30 days hath September, April, June and November, all the rest have 31 except February... [5] 9

Question 7. Section 3. Number Systems [10 marks] a) In the context of boolean algebra, state precisely how the XOR operator functions and draw a truth table for the operator. b) Convert into its octal representation. Show full calculations and clearly indicate your final answer. [3] c) Write an algorithm to convert a positive whole binary number into decimal. [3] [4] 10

Question 8. Section 4. Error Checking and Loops [8 marks] a) What is a sentinel loop? Give an example of a sentinel loop in your explanation (you can use the Keyboard.readInt() method). b) The following method prints out the first 12 multiples of 3: public static void main(string[] args) int i=1; while (i<=12) System.out.println(i + " times 3 = " + i*3); i++; Convert this method to use a for loop instead of a while loop. In addition, add lines of code to your new program so that the multiples of 3 that are also divisible by 2 (i.e. etc) are identified with a *. [3] [2] 11

c) Explain the black box testing stategy, including the use of equivalence testing and boundary analysis. [3] 12

Question 9. Section 5. Using Java System Classes [7 marks] Examine the program below: import java.io.*; import java.util.stringtokenizer; import java.text.numberformat; public class filetest2 public static void main(string[] args) throws java.io.ioexception BufferedReader instream = new BufferedReader(new InputStreamReader(System.in)); NumberFormat anumberformatter = NumberFormat.getInstance(); File infile = new File("myFile.dat"); if(infile.exists() && infile.canread()) BufferedReader fileinstream = new BufferedReader(new FileReader(inFile)); String line = fileinstream.readline(); //start of code to replace int sum=0; while (line!= null) StringTokenizer st = new StringTokenizer(line); st.nexttoken(); st.nexttoken(); sum += anumberformatter.parse(st.nexttoken()).doublevalue(); line = fileinstream.readline(); // end of code to replace System.out.println("Sum is: " + sum); fileinstream.close(); else System.out.println("could not open input file"); a) What is the purpose of the StringTokenizer class? [1] 13

b) If the input file myfile.dat contains the text: 35 40 30 60 95 hello 42 40 mouse 12 0 4 5 9 q describe what the program does and write down the exact output. c) The counttokens() method of the StringTokenizer class returns the number of tokens in an StringTokenizer object. Using the counttokens() method, write a replacement for the while loop in the program above. Your new code should output the number of words in each line of the myfile.dat file and finally output the total number of words in the whole file. e.g. For the myfile.dat example above, the output would be: No. words in line 1: 5 No. words in line 2: 6 Total: 11 Just write the replacement code. [2] [4] 14

Question 10. Section 6. Writing Your Own Classes [10 marks] A car hire company, Best Cars, wants a program to keep track of its vehicles. Examine the following skeleton definition for a class Car. public class Car public Car() this("",0,true); public Car(String make, int odometer, boolean ingarage) setdata(make, mileage, ingarage); NoCars++; public void setdata(string make, int odometer, boolean ingarage) this.make = make; this.odometer = odometer; this.ingarage = ingarage; private String make; private int odometer; private boolean ingarage; private static int NoCars=0; a) What is garbage collection? Using Car objects, give an example of when garbage collection would occur. [2] 15

b) Examine the setdata method. Explain the purpose of using the this keyword in the method. c) What is the signature of a method? Give an example from class Car. [2] d) Why would you want to write a tostring() method for a class? [2] e) Write a suitable tostring() method for the Car class. [1] f) Look carefully at the following method definition for the class Car and explain what is wrong with it. public static void CheckAllIn() ingarage = true; [2] [1] 16

Question 11. Section 7. Arrays and Software Engineering [11 marks] You are given the fragment of Java code below: a) Define 1-dimensional arrays to hold the sum of each column (called colsum) the number of positive values in each column (called pos) b) Write Java Code to compute the sum of each column and put this value into the appropriate element of colsum. compute the number of positive values in each column and put this into pos appropriately. find the column with the smallest sum print this column and the sum. public class TestArray public static void main (String[] args) int[] val = -1, 5, -2, 4, 6, 7, 6, -3, 8, 5, -4, -6, -2, 5, 3, 1, 3, -1, 2, -2; [11] 17

Question 12. [5 marks] Below you are given a class called Plane. Use the concepts of inheritance and polymorphism to: a) define a new class called Fighter that has an extra variable called bomb load of type float; b) write a constructor for this class; and c) write the tostring method for this class. public class Plane private String name; private int no_engines; public Plane (String n, int eng) name = n; no_engines = eng; public String tostring() return "name: " + name + " Engines: " + no_engines; [5] 18

Question 13. [5 marks] Consider a computer system for a new car dealer. The dealer keeps an inventory of cars and motorbikes. The dealer buys the cars and motorbikes from several different manufacturers and adds them to his inventory. He sells from his inventory. One manufacturer makes several different types of motorbike. Draw a UML class diagram giving: a) the classes involved; b) relationships between the classes; and c) methods on the classes. [5] 19

Question 14. [5 marks] Given the code below write the function enlarge that enlargens the array given to the new specified size. In this example, the array small is enlarged to 20 elements (You may NOT use the System.arraycopy() method to do this). public class exam public static void main (String[] args) int [] tiny = 4,2,5,3,1; enlarge(tiny,20); [5] 20