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

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

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

Supplementary Test 1

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

APCS Semester #1 Final Exam Practice Problems

Midterm Examination (MTA)

a) Answer all questions. b) Write your answers in the space provided. c) Show all calculations where applicable.

Practice Midterm 1. Problem Points Score TOTAL 50

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

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

COE 212 Engineering Programming. Welcome to Exam II Thursday April 21, Instructors: Dr. Salim Haddad Dr. Joe Tekli Dr. Wissam F.

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

Table of Contents Date(s) Title/Topic Page #s. Chapter 4: Writing Classes 4.1 Objects Revisited

Module Contact: Dr Gavin Cawley, CMP Copyright of the University of East Anglia Version 1

Practice Midterm 1 Answer Key

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014

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

Lesson 10B Class Design. By John B. Owen All rights reserved 2011, revised 2014

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

Introduction to Algorithms and Data Structures

3. Convert 2E from hexadecimal to decimal. 4. Convert from binary to hexadecimal

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

McGill University School of Computer Science COMP-202A Introduction to Computing 1

Introduction to Computer Science I Spring 2010 Sample mid-term exam Answer key

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

Chapter 4 Defining Classes I

if (x == 0); System.out.println( x=0 ); if (x = 0) System.out.println( x=0 );

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Midterm Examination

Exam 2. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 40 Obtained Marks:

CSC 1051 Algorithms and Data Structures I. Final Examination May 12, Name: KEY. Question Value Score

CS1083 Week 2: Arrays, ArrayList

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

NATIONAL UNIVERSITY OF SINGAPORE

Fall 2002 Page 1 of 9 READ THIS NOW!

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

CS 102/107 - Introduction to Programming Midterm Exam #2 - Prof. Reed Spring 2011

Happy Cinco de Mayo!!!!

CISC-124. Passing Parameters. A Java method cannot change the value of any of the arguments passed to its parameters.

Computing Science 114 Solutions to Midterm Examination Tuesday October 19, In Questions 1 20, Circle EXACTLY ONE choice as the best answer

Lecture 13: Two- Dimensional Arrays

CSIS-120 Final Exam Fall 2015 Name:

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

CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2014

Do not start the test until instructed to do so!

COE 212 Engineering Programming. Welcome to Exam II Tuesday November 28, 2018

AP Computer Science A Unit 7. Notes on Arrays

Final Exam. COMP Summer I June 26, points

Happy Cinco de Mayo!!!!

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++

SAMPLE QUESTIONS FOR DIPLOMA IN INFORMATION TECHNOLOGY; YEAR 1

Question: Total Points: Score:

CS 2334: Programming Structures and Abstractions: Exam 1 October 3, 2016

Instance Method Development Demo

Repetition, Looping. While Loop

CPSC 219 Extra review and solutions

Course Outline. Introduction to java

CSE115 Introduction to Computer Science I Coding Exercise #7 Retrospective Fall 2017

Objects as a programming concept

Last Class. While loops Infinite loops Loop counters Iterations

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

I. True/False: (2 points each)

CSCI 355 Lab #2 Spring 2007

Question 1 [20 points]

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

Solutions to the 2005 exam

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

More on Classes. 1 tostring

Check out how to use the random number generator (introduced in section 4.11 of the text) to get a number between 1 and 6 to create the simulation.

Prelim 1. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants

Use the scantron sheet to enter the answer to questions (pages 1-6)

Distinguish between a class and an object

Advanced Java Concept Unit 1. Mostly Review

Question 1a) Trace of program

CSIS-120 Final Exam Fall 2015 Name:

Top-Down Program Development

Give one example where you might wish to use a three dimensional array

CSCI 355 LAB #2 Spring 2004

Polymorphism. return a.doublevalue() + b.doublevalue();

COMP 202 Java in one week

Introduction to Computer Science Unit 2. Notes

CSCI 111 Midterm Spring 2014

1 Short Answer (10 Points Each)

Fall CS 101: Test 2 Name UVA ID. Grading. Page 1 / 4. Page3 / 20. Page 4 / 13. Page 5 / 10. Page 6 / 26. Page 7 / 17.

Faculty of Science COMP-202B - Introduction to Computing I (Winter 2009) Midterm Examination

1. What is the difference between a compiler and an interpreter? Also, discuss Java s method.

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

AP Computer Science Homework Set 5 2D Arrays

1 Short Answer (10 Points Each)

Program 12 - Spring 2018

CSCI 1226 Sample Midterm Exam

Chapter 4: Writing Classes

SOLUTIONS (INCOMPLETE, UNCHECKED)

CSC 1051 Algorithms and Data Structures I. Final Examination May 2, Name: Question Value Score

Coding Standards for Java

The Irving K. Barber School of Arts and Sciences COSC 111 Final Exam Winter Term II Instructor: Dr. Bowen Hui. Tuesday, April 19, 2016

COE 212 Engineering Programming. Welcome to Exam I Thursday June 21, Instructor: Dr. Wissam F. Fawaz

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

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

true false Imperative Programming III, sections , 3.0, 3.9 Introductory Programming Control flow of programs While loops: generally Loops

Transcription:

Name: Please fill in your Student Number and Name. Student Number : Student Number: University of Cape Town ~ Department of Computer Science Computer Science 1015F ~ 2007 Supplementary Examination Question Max Mark Interna External 1 15 2 15 3 15 4 15 5 4 6 12 7 12 8 12 TOTAL 100 Marks : 100 Time : 3 hours

Instructions: a) Answer all questions. b) Write your answers in the space provided. c) Show all calculations where applicable. 2

Question 1 [15] Read the program below carefully. import java.util.scanner; public class Question1supp public static void main ( String [] args ) for ( int i=0; i<3; i++ ) switch (i) case 3: System.out.println ( "Me" ); break; case 2: System.out.println ( "Fa" ); case 1: System.out.println ( "Do" ); break; default: System.out.println ( "Re" ); a) Give an example of a selection statement from the program above. [1] b) Give an example of a constant used in the program above. [1] c) Give an example of a variable used in the program above. [1] d) Write down the exact output of this program. [4] 3

e) Rewrite the switch statement using if else statements. Just write down the replacement code. The new code should give exactly the same output as the original code. [5] f) You now want to alter the program so that, instead of using the for statement, it will prompt the user to enter a number and will pass the number to the switch statement. Write down the code that you would add to do this. [3] 4

Question 2 [15] Read the following Java program carefully. public class Question2supp public static void main (String [] args ) double x = Math.random(); int i = 0; while (i<3) i++; if (x<0.5) System.out.println ("Heads you win!"); break; else System.out.println ("Tails"); x = Math.random(); if ((x<0.5)&&(i==3)) System.out.println("You won on the last throw!"); a) Describe what this program does and what its outputs are. [3] b) Give an example of an initialization statement in the program above. [1] c) What is the difference between a continue statement and a break statement in a loop? [2] 5

6

d) Explain what happens if the break statement in the program is replaced with a continue statement. [1] e) Rewrite the main method in this program so that it does not use the break statement and uses a do while loop instead of a while loop. The program s output should be exactly the same as before. [5] f) Explain how lazy evaluation of boolean expressions works, giving an example from the program listed above. [3] 7

8

Question 3 [15] Consider the following program fragment: public void doaction () for ( int i=1; i<=x; i++ ) for ( int j=1; j<=(x i); j++ ) System.out.print ( ); for ( int j=1; j<=(2*i) 1; j++ ) System.out.println ( * ); System.out.println; a) What does this code do? [2] b) doaction is a method. What is a method? [1] c) What does the word void signify in the method header? [1] d) The method uses a variable x that is neither defined locally nor passed in as a parameter. What type of variable is x? [1] 9

e) If we wish to test this method, we can use equivalence classes for input values. i. What are equivalence classes? [2] ii. Give an example of an equivalence class applicable to this program. [1] f) Discuss 2 different ways to find errors in a program. [2] g) This code is written in a high level programming language. Discuss one major difference between a high level and a low level language. [2] 10

h) A modern computer that can execute such a program contains a CPU, memory (primary storage) and secondary storage (e.g., hard drives). Explain what the purpose of each of these parts is. [3] 11

Question 4 [15] You need to calculate basic statistics for a class test, namely the average, highest and lowest marks. In the context of this problem, answer the following questions. a) Describe an algorithm to calculate the statistics. Do not store all marks in an array. [4] b) Describe how you would model your solution above using object oriented programming (mention the major classes and methods). [3] 12

13

c) When we write classes, we usually adopt the principles of information hiding. i. Why do we want information hiding? [1] ii. What is the difference between the private and public modifiers? [2] d) If some of your variables are declared as private, you need an accessor to access them from outside the class. Write an accessor method for one of the statistics. [2] e) Some classes include methods that are prefixed with the word static. i. What are static methods and variables? [1] ii. Give one reason why we might possibly use them, and one reason why they should really be avoided. [2] 14

15

Question 5 [4] You are given the following code fragment. public class Data private int pay; private double cost; public void what ( int val ) val = this.pay; System.out.println ( in what val = + val); public void query ( Data dd ) dd.cost = this.cost; public void set ( int i, double d ) pay = i; cost = d; public class Test public static void main ( String[] args ) Data place1 = new Data(), place2 = new Data(); place1.set ( 5, 22.0); place2.set (10, 66.6); int val = 70; place2.what(val); System.out.println ( in Main val = + val); place1.query(place2); System.out.println ( in Main place1.cost= +place1.cost); System.out.println ( in Main place2.cost= +place2.cost); a) What is the output of the code fragment? [4] 16

17

Question 6 [12] a) Write Java code to define a 1 dimensional array called calc that contains the following 7 values (2, 1, 3, 5, 8, 8, 0 ), and write a loop that processes these values as follows: All zeroes have 10 added to them, All negatives are doubled and All positives are changed to negatives E.g., (2, 1, 3, 5, 8, 8, 0) becomes ( 2, 2, 3, 10, 8, 16, 10). [5] Note: Do not put your code into a class or method just write the Java statements. b) Write Java code to print out each alternate value of the result from the previous question, for example ( 2, 3, 8, 10). [1] Note: Do not put your code into a class or method just write the Java statements. 18

19

c) Write Java code to set the elements of an array to the values indicated below. Two loops must be sensibly used and no value may be read in from the keyboard. Define all variables used. The code should work for any square array. [6] Note: Do not put your code into a class or method just write the Java statements. 0 1 2 3 4 10 2 2 3 4 20 20 4 3 4 30 30 30 6 4 40 40 40 40 8 20

Question 7 [12] Complete the two classes given below as instructed. A Run class with a main method that: o o Fills an array in the Data class Finds the index of the row in the array that has the largest sum. A Data class that has a predefined method called Fill, as shown below, that assigns values to each element of the array. a) Write a method called RowWithLargestSum that calculates the sum of the values in each row of the array and returns the index of the row with the largest sum. (A loop must be sensibly used). [10] public class Data private int[][] value; private int[] calsum; public Data ( int maxrow, int maxcol ) // Constructor already written // An array with 6 rows x 5 columns is produced in // this example public void Fill () // Already written method to fill entire array // Write the RowWithLargestSum method 21

b) Complete the main method of the Run class. [2] public class Run public static void main ( String [] args ) int maxrow = 6; int maxcol = 5; int largestrowindex; Data x = new Data (maxrow, maxcol); System.out.println ( Row with largest sum is row + largestrowindex); 22

23

Question 8 [12] Study the following incomplete class definitions. Use the concepts of inheritance and polymorphism in your answers to the questions that follow. class Woman private String name; private int age; //parameterized constructor public Woman ( String n, int yrs ) name = n; age = yrs; public String tostring () return ( Name + name + Age + age); class Wife extends Woman private String husbandname; public Wife ( ); //parameterized constructor public String tostring ( ); class Mother extends Wife private int numberkids; public Mother ( ); //parameterized constructor public String tostring ( ); //Assume this is given a) Write parameterized constructors for Wife and Mother (The constructor for Woman is included in the class definition of Woman). // Wife Constructor [3] 24

25

// Mother Constructor [2] b) Write the tostring method for Wife (The tostring method for the class Woman has been given in the definition of Woman). You may assume that the tostring method for Mother has been provided. [2] c) Complete the main method that creates at least one parameterized object of each class (Woman, Wife and Mother). Print out all the data of each of those objects from inside a loop (thus illustrating the general method of doing this). [5] public static void main ( String [] args ) 26

27