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

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

Supplementary Test 1

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

Week 5-1: ADT Design

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

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

CS 113 PRACTICE FINAL

Introduction to Programming Using Java (98-388)

2012 Academic Challenge

CS141 Programming Assignment #5

Final Exam Practice. Partial credit will be awarded.

Chapter 4 Defining Classes I

It is a constructor and is called using the new statement, for example, MyStuff m = new MyStuff();

Inheritance and Polymorphism

COE 212 Engineering Programming. Welcome to the Final Exam Tuesday December 15, 2015

Discover how to get up and running with the Java Development Environment and with the Eclipse IDE to create Java programs.

University of Palestine. Mid Exam Total Grade: 100

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

Distributed Systems Recitation 1. Tamim Jabban

COMP-202B - Introduction to Computing I (Winter 2011) - All Sections Example Questions for In-Class Quiz

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Final exam. Final exam will be 12 problems, drop any 2. Cumulative up to and including week 14 (emphasis on weeks 9-14: classes & pointers)

TeenCoder : Java Programming (ISBN )

COE 212 Engineering Programming. Welcome to the Final Exam Thursday December 15, 2016

CISC-124. Dog.java looks like this. I have added some explanatory comments in the code, and more explanation after the code listing.

CSCI 136 Data Structures & Advanced Programming. Lecture 3 Fall 2018 Instructors: Bill & Bill

Sri Vidya College of Engineering & Technology

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

CSCI 136 Data Structures & Advanced Programming. Lecture 3 Fall 2017 Instructors: Bill & Bill

Two hours QUESTION PAPER MUST NOT BE REMOVED FROM THE EXAM ROOM UNIVERSITY OF MANCHESTER SCHOOL OF COMPUTER SCIENCE

Programming Exercise 14: Inheritance and Polymorphism

CS 112 Programming 2. Lecture 10. Abstract Classes & Interfaces (1) Chapter 13 Abstract Classes and Interfaces

EECS168 Exam 3 Review

Java for Non Majors Spring 2018

Programming Language Concepts: Lecture 2

HALF-YEARLY EXAMINATIONS FEBRUARY

Chapter 6 Lab Classes and Objects

1st Semester Examinations CITS1001 3

CS61B Lecture #3. Homework: Please see Homework #1 on the lab page. Last modified: Wed Sep 10 20:34: CS61B: Lecture #3 1

Chapter 6 Lab Classes and Objects

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

Midterm Examination (MTA)

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

Programming overview

Enumerated Types. CSE 114, Computer Science 1 Stony Brook University

Language Features. 1. The primitive types int, double, and boolean are part of the AP

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

Arrays. Chapter 6. Array Basics Arrays in Classes and Methods Programming with Arrays and Classes Sorting Arrays Multidimensional Arrays

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

CompuScholar, Inc. 9th - 12th grades

CO Java SE 8: Fundamentals

Chapter 6. Arrays. Array Basics Arrays in Classes and Methods Programming with Arrays and Classes Sorting Arrays Multidimensional Arrays

ENCAPSULATION AND POLYMORPHISM

APCS Semester #1 Final Exam Practice Problems

CSC 1214: Object-Oriented Programming

CS 170 Section 3, Spring 2015 Programming in Java Midterm Exam 1. Name (print):

CS100J Prelim I, 29 Sept. 2003

1 Short Answer (10 Points Each)

ITERATION WEEK 4: EXMAPLES IN CLASS

COMPUTER APPLICATIONS

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

Object Oriented Programming. Java-Lecture 6 - Arrays

This exam is open book. Each question is worth 3 points.

CSCI 355 Lab #2 Spring 2007

Software Practice 1 Basic Grammar

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

Agenda: Discussion Week 7. May 11, 2009

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: if Single-Selection Statement CSC 209 JAVA I. week 3- Control Statements: Part I

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

View a Students Schedule Through Student Services Trigger:

CS1083 Week 2: Arrays, ArrayList

Bjarne Stroustrup. creator of C++

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

Review Problems for Final Exam. 1. What is the output of the following program? #include <iostream> #include <string> using namespace std;

COE 211/COE 212 Computer Programming/Engineering Programming. Welcome to The Comprehensive Final Exam Monday June

Distributed Systems Recitation 1. Tamim Jabban

Cloning Enums. Cloning and Enums BIU OOP

COS 126 General Computer Science Spring Written Exam 1

Introduction to Computer Science Unit 2. Notes

Sample Examination. Family Name:... Other Names:... Signature:... Student Number:...

The Java language has a wide variety of modifiers, including the following:

Final Examination Semester 3 / Year 2010

CMPT 117: Tutorial 1. Craig Thompson. 12 January 2009

CS 170 Exam 2. Version: A Fall Name (as in OPUS) (print): Instructions:

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

H212 Introduction to Software Systems Honors

CSCE 145 Midterm 2 Review Answers. This exam totals to 100 points. Follow the instructions. Good luck!

Inheritance (Part 5) Odds and ends

CS171:Introduction to Computer Science II


Arrays. Eng. Mohammed Abdualal

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

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

Object Oriented Programming with Java

9/10/2018 Programming Data Structures Inheritance

Assignment 1 due Monday at 11:59pm

CS 1316 Exam 1 Summer 2009

CLASSES AND OBJECTS IN JAVA

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 ~ 2008 January Exam Question Max Internal External Question Max Internal External 1 9 7 3 2 8 8 8 3 8 9 15 4 15 10 10 5 10 6 14 TOTAL 100 Marks : 100 Time : 180 minutes Instructions: a) Answer all questions. b) Write your answers in the space provided. c) Show all calculations where applicable.

Question 1 [9] a) What is the purpose of an operating system? [1] manage the resources of a computer b) Give 2 examples of operating systems (from different companies or communities). [2] Windows XP [1], Linux [1], MacOS [1],... c) What is the purpose of random access memory? [1] store data temporarily while the computer is powered on d) What is an algorithm? [1] set of instructions to perform a task e) List 2 reasons why algorithms must be precise. [2] repeatable [1] can be executed by different people [1] predictable [1] f) Briefly describe 1 advantage and 1 disadvantage of using Java bytecode, as opposed to machine code. [2] ad: portability [1] disad: slower[1] more work [1] 2

Question 2 [8] Consider the following program and answer the questions that follow. import java.util.scanner; class Test public static void main ( String [] args ) Scanner input = new Scanner (System.in); int a = input.nextint(); int b = input.nextint(); int c = input.nextint(); int x = Math.min (Math.min (a, b), c); int y = Math.max (Math.max (a, b), c); System.out.println ((a+b)/6.0f+(b+c)/6.0f+(a+c)/6.0f); a) What does this program do? [2] calculates the average of 3 integers b) What is the output if the input is the numbers 5, 3 and 7? [1] 5 c) List all methods (other than main) from the program. [1] println,min,max,nextint d) Why must the main method be static? [1] it is invoked by the JVM without an instance being created e) Rewrite the first statement of the main method in such a way that there is a syntax error. [1] Anything syntactically invalid: e.g., Scanner input new Scanner (System.in); f) In the context of a debugger, what is a breakpoint? [2] 3

a specified line of code at which the debugger will stop when executing the program in debugging mode 4

Question 3 [8] a) Briefly describe an algorithm to take a taxi or bus to town. Assume you are at the bus/taxi stop. There should be at most 6 steps. [3] wait for correct bus, get onto bus, pay driver, sit down, wait for stop, get off bus b) Write the Java statement to input the number of taxis that pass you into the variable N. You may assume N is already declared as an int and there is already a Scanner object named input. [2] N = input.nextint(); c) Write the Java statement to calculate the variable totaltime (in seconds) as the time you wait for N taxis to pass if one taxi passes every timepertaxi seconds. You may assume totaltime and timepertaxi are already declared as float variables and N is declared as an int. [1] totaltime = N * timepertaxi; d) Write the Java statement to output Time wasted at bus stop:, immediately followed by the value of the variable totaltime. [2] System.out.println ( Time wasted at bus stop: + totaltime); 5

Question 4 [15] Examine the following code and answer the questions that follow. public class Driver public static void main (String[] args) for ( int date=0; date<10; date++ ) if (date%2 == 0) System.out.println(convertDate(date)); // convert a number to a day of the week public static String convertdate(int number) String date = ""; if (number == 2) date = "Monday"; if (number == 3) date = "Tuesday"; if (number == 4) date = "Wednesday"; if (number == 5) date = "Thursday"; if (number == 6) date = "Friday"; if (number == 7) date = "Saturday"; if (number == 8) date = "Sunday"; return date; a) What is the output of this program? [4] Monday Wednesday Friday Sunday 6

b) Rewrite the convertdate method, converting the if else statement to an equivalent switch statement. [4] public static String convertdate(int number) String date = ""; switch (number) case 2: date = "Monday"; break; case 3: date = "Tuesday"; break; case 4: date = "Wednesday"; break; case 5: date = "Thursday"; break; case 6: date = "Friday"; break; case 7: date = "Saturday"; break; case 8: date = "Sunday"; break; return date; c) What is the result of each of the following expressions? The result will be either true or false. Assume variable week = 7. [2] i. week > 6 week == 5 True [1/2] ii. week < 3 week > 4 True [1/2] iii. week > 3 && week < 0 False [1/2] 7

iv. week >= 5 week < 0 True [1/2] d) Write a Java method to print out a blank grid for a working week calendar, such as: + + + + + + + + + + + + + + + Your method should have the header: void printgrid ( int numberofweeks, int numberofworkingdays ) The above pattern is produced when invoking printgrid(2,4). [5] public void printgrid ( int numberofweeks, int numberofworkingdays) String out = "+"; for (int j=0; j<numberofworkingdays; j++) out += " +"; String out_mid = " "; for (int j=0; j<numberofworkingdays; j++) out_mid += " "; for (int i=0; i<numberofweeks; i++) System.out.println(out); System.out.println(out_mid); System.out.println(out); 8

Question 5 [10] a) If a class called Student is created and one of the data fields (or instance variables) for the Student class is a reference to a Date object, what is the relationship between the Student object and the Date object? [1] i. The Date object is a Student object. ii. The Date object has a Student object. iii. The Student object is a Date object. iv. The Student object has a Date object. The Student object has a Date object. b) Write skeleton Java code to illustrate the above description (i.e., create the Student class and a public Date object such as enrolmentdate). [2] public class Student public Date enrollmentdate = new Date(); /* Other code here*/ c) Write the body of the main method of a driver program to create an object of the Student class, called astudent. [1] Student astudent = new Student(); d) Can enrolmentdate be accessed by calling astudent.enrolmentdate from the driver program? Why or why not? [1] No. Because enrolmentdate was declared private e) What do you need to change in the Student class so that the value of enrolmentdate can be accessed from the driver class? List two ways and write code to illustrate your methods. Explain which one is the better way. [5] #1: enrolmentdate can be declared public in the Student class public Date enrolmentdate = new Date(); 9

#2: in the Student class, we can declare mutator and accessor methods to set and access value of enrolmentdate private Date enrolmentdate = new Date(); public Date getenrolmentdate() return enrolmentdate; #2 is a better way in terms of information hiding and data encapsulation 10

Question 6 [14] a) Consider the Person class below, and add constructors, accessor and mutator methods as described by the comments below: public class Person private String name; private int age; private double average_weight_gain; // i) Constructor with three arguments that are used to initialize the instance variables [2] public Person(String n, int a, double g) name = n; age = i; average_weight_gain = g; // ii) Accessor methods for all the instance variables [3] public String getname() return name; public int getage() return age; public double getgain() return average_weight_gain; // iii) Two mutator methods with one argument each to set the name and age instance variables [2] 11

public void setname(string n) name = n; public void setage(int a) age = a; // iv) One mutator method with an array argument that is used to calculate the // average_weight_gain instance variable, returning no value [3] public void calculateaveragegain (double [] g) double sum = 0.0; int i; for (i=0; i < g.length; g++) sum = sum + g[i]; average_weight_gain = sum / g.length; a) Consider the driver class below, and add a statements to create an object called aperson of type Person for Peter, age 21, with an average monthly weight_gain of 0.0kg, using your constructor above. Use your mutator method above to calculate the average weight gain of the person over the festive period from November to February his weight gains were 0.1 kg, 1.2 kg, 0.5 kg and 1.2 kg. Display the name, age and average weight gain for the person using your accessor methods above. [4] public class ConstructorsDemo public static void main (String args[]) Person aperson = new Person( Peter,21,0.0); double [] gain =0.1, 1.2, 0.5, 1.2; aperson.calculateaveragegain(gain); 12

System.out.println ( Name = + name + aperson.getname()+ Age = + aperson.getage() + Average Weight Gain = + aperson.getgain(); 13

Question 7 [3] a) How can arrays be tested to check if their content is identical? [3] The best way to test two arrays to see if the contents are the same is to write a method that accomplishes the task. This method would iterate through each array comparing indexed variables. If arrays are tested for equality using the == operator this only checks if it is the same array. 14

Question 8 [8] The following set of methods sorts an array of numbers in descending order. Complete the missing methods as indicated by the comments. /** Precondition: The array has values. Action:Sorts a so that a[0] >= a[1] >=... >= a[a.length 1] [4] */ public void selectionsort ( int[] a ) int indexoflargest; for ( int i=0; i<a.length; ++i ) indexoflargest = LargestIndex (i, a); swap (i, indexoflargest, a); /** Returns the index of the largest value among a[start], a[start + 1],... a[a.length 1] [4] */ private int LargestIndex ( int start, int[] a ) int max = a[start]; int indexofmax = start; for(int i=start + 1; i < a.length; i++) if(a[i] > max) max = a[i]; indexofmax = i; return indexofmax; 15

/** Precondition: i and j are legal indices for the array a. Postcondition: Values of a[i] and a[j] have been interchanged */ private void swap ( int i, int j, int[] a ) int temp; temp = a[i]; a[i] = a[j]; a[j] = temp; 16

Question 9 [15] a) Match each of the following terms to the concepts below: [2] overriding, overloading, inheritance, composition inheritance = variables and methods automatically exist in the subclasses of a base class overriding = methods in base class and subclass with same signature but different code overloading = methods in base class and subclass with same name but different signature composition = a class has an instance variable which is itself an Object [½ x 4] b) Suppose that a 2 dimensional array sales has been declared as below. int[ ][ ] sales ; Give Java code to fill sales with 120 zeroes: there must be 100 rows each with 12 elements, and every element must be initialised to 0. [4] sales = new int[100][12]; for (int j=0; j<sales.length; j++) for (int k=0; k<sales[j].length; k++) sales[j][k] = 0; c) Suppose your program has changed the values in sales to contain monthly sales data from 100 shops. Give Java code to print monthly totals i.e., print the sum of the values in each column of sales. The 12 values should appear one after the other on the same single line of output, separated by spaces. [4] int sum; for (int j=0; j<12; j++) sum = 0; for (int k=0; k<sales.length; k++) if (j < sales[k].length) //not expected to check this tho sum = sum + sales[k][j]; System.out.print(sum + " "); System.out.println(); d) Consider the classes below, then list the output of the main program. [5] 17

public class Person protected String name; protected String address; public Person( ) name = unknown ; address = N/A ; System.out.println( Created unknown person ); public Person( String pname, String adr ) name = pname; address = adr; System.out.println( Created person ); public class Child extends Person int age; public Child (String cname, String adr, int years ) super ( cname, adr ); age = years; System.out.println( Created child ); public Child ( ) age = 0; System.out.println( Created unknown child ); public String tostring( ) return (name + ( + address + ) aged + age)); public class Test public static void main ( String args[ ] ) Child ann = new Child( ann, UCT, 6); Child kiddie = new Child( ); System.out.println( kiddie ); Output from running Test is: Created person Created child 18

Created unknown person Created unknown child unknown ( N/A) aged 0 19

Question 10 [10] a) Convert the binary number 101010 2 to hexadecimal. [1] 2A b) Convert the hexadecimal number CAB 16 to binary. [1] 110010101011 c) Convert 43 10 to binary (i.e., convert the decimal number 43 to base 2). Show your working. [2] 101011 d) Convert 3.375 10 to binary (i.e., convert the decimal number 3.375 to base 2). Show your working. [3] 11.011 e) What is the 8 bit two s complement binary representation of the decimal number 13 10? Show your working. [3] 11110011 20