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

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

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

ECE 122. Engineering Problem Solving with Java

Supplementary Test 1

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

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

CS111: PROGRAMMING LANGUAGE II

Q1 Q2 Q3 Q4 Q5 Total 1 * 7 1 * 5 20 * * Final marks Marks First Question

University of Cape Town Department of Computer Science Computer Science CSC1017F

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

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

AP Computer Science Homework Set 1 Fundamentals

Name :. Roll No. :... Invigilator s Signature : INTRODUCTION TO PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

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

Exam 1. CSC 121 Spring Lecturer: Howard Rosenthal. March 1, 2017

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

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

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

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

Repetition Structures

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Midterm Examination (MTA)

Loops. CSE 114, Computer Science 1 Stony Brook University

AP CS Unit 3: Control Structures Notes

( &% class MyClass { }

1. What does the following code fragment write to the monitor?

More Programming Constructs -- Introduction

CSEN 202 Introduction to Computer Programming

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

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

PROGRAMMING FUNDAMENTALS

Programming Projects: 2.1, 2.3, 2.4, 2.7, 2.8, 2.13

IST 297D Introduction to Application Programming Chapter 4 Problem Set. Name:

Java. Programming: Chapter Objectives. Why Is Repetition Needed? Chapter 5: Control Structures II. Program Design Including Data Structures

Assignment 2.4: Loops

Problem Solving With Loops

Lecture 14. 'for' loops and Arrays

Computer Programming, I. Laboratory Manual. Experiment #6. Loops

Chapter 4 Loops. int x = 0; while ( x <= 3 ) { x++; } System.out.println( x );

Logical Operators and switch

CS111: PROGRAMMING LANGUAGE II

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

Object-Oriented Programming. Topic 2: Fundamental Programming Structures in Java

Topic 5 for loops and nested loops

CS141 Programming Assignment #6

Java Coding 3. Over & over again!

ITERATION WEEK 4: EXMAPLES IN CLASS

Selection Statements and operators

1. [3 pts] What is your section number, the period your discussion meets, and the name of your discussion leader?

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

Fall 2002 Page 1 of 9 READ THIS NOW!

array Indexed same type

Repetition, Looping. While Loop

Simple Java Programming Constructs 4

Introduction. C provides two styles of flow control:

Activity 6: Loops. Content Learning Objectives. Process Skill Goals

Programming with Java

Accelerating Information Technology Innovation

CSc 2010 Principles of Computer Science, Fall 2013 Practice Problems for Midterm 3* * 3 17 % 9-20 % (26 / 7) "2"

Chapter 2: Basic Elements of Java

Question 1a) Trace of program

Chapter 20: Binary Trees

Selection and Repetition Revisited

King Saud University College of Computer and Information Sciences Computer Science Department

SCHOOL OF COMPUTING, ENGINEERING AND MATHEMATICS SEMESTER 1 EXAMINATIONS 2015/2016 CI101 / CI177. Programming

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Control Structures in Java if-else and switch

Do not start the test until instructed to do so!

Homework Set 1- Fundamentals

1. An operation in which an overall value is computed incrementally, often using a loop.

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

(c) ((!(a && b)) == (!a!b)) TRUE / FALSE. (f) ((!(a b)) == (!a &&!b)) TRUE / FALSE. (g) (!(!a) && (c-d > 0) && (b!b))

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

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

CS171:Introduction to Computer Science II

Lecture 2. CS118 Term planner. Refinement. Recall our first Java program. Program skeleton GCD. For your first seminar. For your second seminar

Programming: Java. Chapter Objectives. Chapter Objectives (continued) Program Design Including Data Structures. Chapter 7: User-Defined Methods

Introduction to Java Applications

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

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

1007 Imperative Programming Part II

Iteration: Intro. Two types of loops: 1. Pretest Condition precedes body Iterates 0+ times. 2. Posttest Condition follows body Iterates 1+ times

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading

Selection and Repetition

To become familiar with array manipulation, searching, and sorting.

The action of the program depends on the input We can create this program using an if statement

Exam 2. CSC 121 MW Class. Lecturer: Howard Rosenthal. April 26, 2017

Lab 9: Creating a Reusable Class

Chapter 2: Programming Concepts

SAMPLE QUESTIONS FOR DIPLOMA IN INFORMATION TECHNOLOGY; YEAR 1

COMP 202 Java in one week

University of the Western Cape Department of Computer Science

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

LAB 4.1 Relational Operators and the if Statement

AP Computer Science Unit 1. Programs

CompSci 125 Lecture 11

Combo Lecture Lecture 14/15. Instructor: Craig Duckett

Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, Global Edition. Pearson Education Limited

CS141 Programming Assignment #4

Transcription:

University of Cape Town Department of Computer Science Computer Science CSC115F Class Test 2 Solutions Wednesday 6 April 2005 Marks: 40 Approximate marks per question are shown in brackets Time: 40 minutes The use of calculators is permitted NAME: Surname Initials STUDENT NO: COURSE CODE: CSC This paper consists of 6 questions and 9 pages (including this cover page). Mark Allocation Question Marks Internal External Quest Marks Internal External 1 [5] 4 [5] 2 [8] 5 [11] 3 [5] 6 [6] Total Total Grand Total Final Mark Internal Examiner: External Examiner: 1

Question 1. [5 marks] a) What is iteration? Executing the same task or set of statements multiple times b) What is a counter-controlled loop? Name such a statement in Java. Loops that usually execute for a fixed number of times OR A special counter variable is used to control the loop and may be referred to within the loop Java provides the for statement [1] [2] c) What is a condition-controlled loop? Name such a statement in Java. If we do not know the number of iterations a priori (in advance), we can use a condition-controlled (or event-controlled) loop - where the loop executes while a condition is true Two statements: while OR do while [2] Question 2. [ 8 marks] Write a simple menu selection program that asks the user for a menu choice (integer value). If the user enters 1, the string Do this is displayed. If the user enters 2 or 3, the string Do that is displayed. If any other integer is entered, Do the other is displayed. You must use a switch statement in your answer. Use the structure below as a template for your answer: public class Menu int choice = Keyboard.readInt("Enter your choice (any integer)"); 2

switch (choice) case 1: System.out.println("Do this"); break; case 2: case 3: System.out.println("Do that"); break; default: System.out.println("Do the other"); [8] Question 3. [5 marks] Write a Java class that uses for loops to produce the following pattern 3456 345 34 3 Use the structure below as a template for your answer: public class Pattern1 for (int i = 3; i >= 0; i--) for (int j = 3; j <= 3 + i; j++) System.out.print(j); 1/2 System.out.println(); 1/2 3

[5] Question 4. [5 marks] Write a Java class that uses for loops to produce the following pattern ++++@ +++@@ ++@@@ +@@@@ @@@@@ Use the structure below as a template for your answer: public class Pattern2 for (int i = 0; i < 5; i++) for (int j = 5-i; j > 1; j--) System.out.print('+'); for (int j = 0; j <= i; j++) System.out.print('@'); [5] 4

Question 5. [11 marks] The code below is intended to find the floating point average of a list of integer numbers terminated by the user entering a "0" value, but it contains some errors: public class TestErrors int avg, num, count; // statement to change in (b) while(num!= 0) num = Keyboard.readInt("Enter next number: "); count++; avg = avg + num; avg = avg/count; System.out.println("The average is: " + avg); (a) Will this program compile? Give reasons for your answer. NO - avg, num, count not initialized will not compile (b) if you change the following statement in the program above [1] int avg, num, count; // statement to change in (b) to int avg = 0, num = 0, count = 0; // changed statement What kinds of error(s) occur during execution? Explain your answer. Logic errors e.g. num tested before it is read in average is an integer not declare as float Run-time error Divide by 0 [5] 5

(c) Rewrite the code so that it will execute correctly. public class TestErrors float avg=0f; int count=0; int num = Keyboard.readInt("Enter next number: "); while(num!= 0) count++; avg = avg + num; num = Keyboard.readInt("Enter next number: "); if (count > 0) avg = avg/count; System.out.println("The average is: " + avg); else System.out.println("Could not calculate average - no numbers entered"); [5] 6

Question 6. [ 6 marks] You are provided with the following program which is intended to help children learn mental addition public class MentalAdd int response, guessnum = 1, op1 = 8, op2 = 7; System.out.println("What is " + op1 + " plus " + op2 + "?"); response = Keyboard.readInt("Enter guess number " + guessnum + ": "); while(response!= op1 + op2) guessnum++; response = Keyboard.readInt("Enter guess number " + guessnum + ": "); System.out.println("Correct!"); Rewrite this code to: (a) use a do-while rather than a while loop and (b) stop on the first correct guess or after five incorrect guesses (in which case the program should display "Incorrect" and also provide the correct answer (op1 + op2)). public class MentalAdd int response, guessnum = 1, op1 = 8, op2 = 7; System.out.println("What is " + op1 + " plus " + op2 + "?"); do response = Keyboard.readInt("Enter guess number " + guessnum + ": "); guessnum++; while((response!= op1 + op2) && (guessnum <= 5)) ; 7

if (response == op1 + op2) System.out.println("Correct!"); else System.out.println("Incorrect!"); System.out.println("Correct Answer is : " + (op1 + op2)); 8