COMP102: Test 1 Model Solutions

Similar documents
COMP102: Test. 26 April, 2006

COMP102: Test 2 Model Solutions

COMP102: Test July, 2006

EXAMINATIONS 2008 END-OF-YEAR COMP 102 INTRODUCTION TO COMPUTER PROGRAM DESIGN

COMP102: Test 2: Part B Model Solutions

COMP 102: Test , September 25 ** WITH SOLUTIONS **

COMP 102: Test. 2017, May 15 ** WITH SOLUTIONS **

Model Solutions. COMP 102: Test 2. 5 May, 2011

Model Solutions. COMP 102: Test May, 2016

Questions COMP 102 INTRODUCTION TO COMPUTER PROGRAM DESIGN. Attempt ALL Questions.

Model Solutions. COMP 102: Test March, 2015

COMP 102: Test August, 2017

Model Solutions. COMP 102: Test May, 2015

COMP 102: Test August, 2017

COMP 102: Test , December?? ** WITH SOLUTIONS **

Model Solutions. COMP 102: Test May, 2014

COMP102: 181. Menu. More while loops. Admin: Test. Peter Andreae

COMP 102 : Test. 2017, Apr 3

Model Solutions. COMP 102: Test. 14 August, 2014

Model Solutions. COMP 102: Test May, 2012

A token is a sequence of characters not including any whitespace.

COMP 102 : Test. 2017, Apr 3 ** WITH SOLUTIONS **

Model Solutions. COMP 102: Test 1. 6 April, 2016

Model Solutions. COMP 102: Test March, 2014

Introduction to Programming: Test 2

COMP 102/112 : Test. 2019, Apr 1 ** WITH SOLUTIONS **

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

EXAMINATIONS 2011 Trimester 2, MID-TERM TEST. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

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

Model Solutions. COMP 102: Test. 18 August, 2016

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

Faculty of Science COMP-202A - Foundations of Computing (Fall 2012) - All Sections Midterm Examination

COMP 102: Test 2 Model Solutions

H212 Introduction to Software Systems Honors

Object-Oriented Programming in Java

Using APIs. Chapter 3. Outline Fields Overall Layout. Java By Abstraction Chapter 3. Field Summary static double PI

Solution Notes. COMP 151: Terms Test

Faculty of Science COMP-202A - Foundations of Computing (Fall 2013) - All Sections Midterm Examination

AP Computer Science Unit 1. Writing Programs Using BlueJ

CSCI 1103: File I/O, Scanner, PrintWriter

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

CSCI 1103: File I/O, Scanner, PrintWriter

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

EXAMINATIONS 2012 MID YEAR. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

COMP 202 Java in one week

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 7, Name:

COMP102: Test 1. Model Solutions

Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

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

Lecture 8 " INPUT " Instructor: Craig Duckett

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. File Input and Output

Java Coding 3. Over & over again!

EXAMINATIONS 2010 END-OF-YEAR COMP 102 INTRODUCTION TO COMPUTER PROGRAM DESIGN

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

COMP102: Test. 31 August, 2005

1. Boolean algebra. [6] 2. Constructing a circuit. [4] 3. Number representation [4] 4. Adders [4] 5. ALU [2] 6. Software [4]

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string

Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

Text User Interfaces. Keyboard IO plus

CMPT 125: Lecture 4 Conditionals and Loops

COMP 202. Java in one week

Fundamentals of Programming Data Types & Methods

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 26, Name: Key

Family Name:... Other Names:... ID Number:... Signature... Model Solutions. COMP 103: Test 1. 9th August, 2013

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

Example Program. public class ComputeArea {

Midterm Examination (MTA)

COMP102: Test. 18 April, 2005

BASIC INPUT/OUTPUT. Fundamentals of Computer Science

ing execution. That way, new results can be computed each time the Class The Scanner

First Java Program - Output to the Screen

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2

Java Programming: Test 1

Faculty of Science COMP-202A - Foundations of Computing (Fall 2013) - All Sections Midterm Examination

Adam Blank Lecture 2 Winter 2019 CS 2. Introduction to Programming Methods

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

4. Java Project Design, Input Methods

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 19: NOV. 15TH INSTRUCTOR: JIAYIN WANG

Repetition. Chapter 6

Repetition. Chapter 6

Activity 4: Methods. Content Learning Objectives. Process Skill Goals

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Formatted Output / User IO

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

CS111: PROGRAMMING LANGUAGE II

Using Classes and Objects Chapters 3 Creating Objects Section 3.1 The String Class Section 3.2 The Scanner Class Section 2.6

Algorithms in everyday life. Algorithms. Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

Robots. Byron Weber Becker. chapter 6

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

Console Input and Output

CS 302: INTRODUCTION TO PROGRAMMING. Lectures 7&8

Java I/O and Control Structures

Classes and Objects Part 1

2. [20] Suppose we start declaring a Rectangle class as follows:

Computational Expression

Starting Out with Java: From Control Structures Through Objects Sixth Edition

A+ Computer Science -

Java I/O and Control Structures Algorithms in everyday life

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

Transcription:

Family Name:.......................... Other Names:.......................... ID Number:............................ COMP102: Test 1 Model Solutions 27 July, 2007 Instructions Time allowed: 45 minutes. Answer all the questions. There are 45 marks in total. Write your answers in the boxes in this test paper and hand in all sheets. If you think some question is unclear, ask for clarification. There is some Java documentation at the end of the test paper. Model solutions for part of Assignment 2 are also included at the end of the test paper. This test will contribute 5% of your final grade, but only if it helps your grade. Non-electronic translation dictionaries and calculators without a full set of alphabet keys are permitted. Questions Marks 1. Basic Java [8] 2. Understanding variables [5] 3. Defining a Method [10] 4. Using a Scanner and println [15] 5. Loops and Conditionals [7] TOTAL:

SPARE PAGE FOR EXTRA ANSWERS Cross out rough working that you do not want marked. Specify the question number for work that you do want marked. COMP 102 (Terms Test 1) 2 continued...

ANSWERS Student ID:....................... Question 1. Basic Java [8 marks] For each of the following ten terms, find a corresponding element of the program below, and draw a labelled circle around the element. The first one is done as an example. 1. Class name 2. A string 3. An expression 4. A comment 5. A method header 6. A name of a type 7. A variable declaration 8. An assignment statement 9. A method call 1 public class Test { / Computes the volume of a box of variable width / public void computesize(){ System.out.print("width: "); Scanner scan = new Scanner(System.in); int width = scan.nextint (); int size = width 20 + 40; System.out.println("Size is " + size); public void doubleup(string name){ String item = "cheese"; System.out.printf("A %s %s %s please", name, name, item); COMP 102 (Terms Test 1) 3 continued...

ANSWERS Question 2. Understanding variables [5 marks] Suppose the following jumble method is called with an argument of 12, (eg, you call the method using BlueJ and enter 12 in the dialog box asking for the value of n). What will it print out? public void jumble(int n){ System.out.println("jumble(" + n + "):"); int a = n 2; System.out.println("a is: "+ a); int b = n 2; System.out.println("a is now: "+ b); b = a 5; a = b 2; a = a 1; System.out.println("a is b: " + a); System.out.println("b is a: " + b); System.out.printf("a %d b %d is ", n, a+b); jumble(12): a is: 24 a is now: 10 a is b: 37 b is a: 19 a 12 b 56 is COMP 102 (Terms Test 1) 4 continued...

ANSWERS Student ID:....................... Question 3. Defining a Method [10 marks] (a) [7 marks] Complete the following computelanes method so that it prints out the number of bowling lanes that will fit in a building of a specified width. The method should have one parameter an integer specifying the width of the building in meters. Each lane is 3 meters wide. There has to be an exit corridor of at least 4 meters wide against each wall of the building: The method should print out the result in a form such as: "A building 33 meters wide holds 8 lanes". (where the numbers depend on the argument value passed to the method.) public void computelanes(int width){ int lanes = (width 8)/3; if (lanes > 10) // part b lanes = (width 8 2)/3; // part b System.out.printf("A building %d meters wide holds %d lanes", width, lanes); (b) [3 marks] Modify your program above so that it also handles the additional requirement that whenever the building has more than 10 lanes, there has to be an additional exit corridor 2 meters wide in the middle of the building. (Put your answer to (b) in the box above) COMP 102 (Terms Test 1) 5 continued...

ANSWERS Question 4. Using a Scanner and println [15 marks] (a) [6 marks] Consider the following method which will prompt the user for some values and print something out. public void bankpayment(){ Scanner scan = new Scanner(System.in); System.out.print("Enter bank, payee name, code, and amounts: "); String b = scan.next(); String p = scan.nextline(); System.out.println("bank = " + b); System.out.println("payee = " + p); String message = p + b; System.out.println("line1 = " + message); int c = scan.nextint (); int a1 = scan.nextint (); int a2 = scan.nextint (); String rest = scan.nextline(); System.out.println("line2 = " + c + a1); System.out.println("line3 = " + (c + a2)); System.out.println("line4 = " + c + rest); What will the method print out if the user typed the following lines in response to the prompt: National Bank Jane Smith 4519 21 35 14 18 bank = National payee = Bank Jane Smith line1 = Bank Jane SmithNational line2 = 451921 line3 = 4554 line4 = 4519 14 18 (Question 4 continued on next page) COMP 102 (Terms Test 1) 6 continued...

Student ID:....................... (Question 4 continued) ANSWERS (b) [9 marks] Complete the following doorplate method so that it first asks the user to enter a room number, then asks for their name, and then prints out a sign for their office door like one shown below. It should use a Scanner to read the number and name from the user. If the user typed the number 423 and the name Chris Hughes, the output should look something like: Dr Chris Hughes, Computer Science Cotton 423 School of Engineering public void doorplate(){ Scanner scan = new Scanner(System.in); System.out.print("Enter office number: "); int office = scan.nextint (); System.out.print("Enter name: "); String name = scan.nextline(); System.out.println("Dr " + name + ", Computer Science"); System.out.println(" Cotton " + office); System.out.println(" School of Engineering"); COMP 102 (Terms Test 1) 7 continued...

ANSWERS Question 5. Loops and Conditionals (harder) [7 marks] (a) [5 marks] What will the following method print out if it is called with the argument 24? public void compute(int n){ System.out.printf("compute(%d)\n", n); int x = 0; int y = n; while (y > x){ System.out.printf("%d : %d\n", x, y); if (y % 2 == 0) { x = x 2; y = y 1; else { x = x + 2; y = y 3; System.out.printf("ans is %d\n", x); compute(24) 0 : 24 0 : 23 2 : 20 4 : 19 6 : 16 12 : 15 ans is 14 (b) [2 marks] Explain why the compute method is always guaranteed to stop, whatever its input. Every time round the loop, y gets smaller (by 1 or by 3). Every time round the loop x either stays the same (when it is 0 and y is even) or it gets larger (by 2, or doubled). Therefore, after some time y must become smaller than x, and then the loop will stop COMP 102 (Terms Test 1) 8 continued...

SPARE PAGE FOR EXTRA ANSWERS Student ID:....................... Cross out rough working that you do not want marked. Specify the question number for work that you do want marked. ******************************** COMP 102 (Terms Test 1) 9

You may tear off this sheet Brief and partial documentation of some classes and methods PrintStream class: // Methods you can call on System.out public void print (String s); / Prints s with no newline / public void print ( int i ); // Prints i with no newline public void print (double d); // Prints d with no newline public void println (); // Prints a newline public void println (String s); // Prints s followed by newline public void println ( int i ); // Prints i followed by newline public void println (double d); // Prints d followed by newline public void printf (String format,...); // Prints the format string, inserting the remaining // arguments at the % s in the format string : // %s for Strings // %10s for Strings, using at least 10 characters // %d for ints // %3d for ints, using at least 3 characters // %.2f for doubles, with 2dp // %6.2f for doubles, with 2dp and at least 6 characters ), // \n for newline Scanner class: // Methods you can call on a Scanner object public Scanner(InputStream i); // Constructor. eg new Scanner(System.in) public boolean hasnext(); // Returns true if there is more to read public boolean hasnextint(); // Returns true if the next token is an integer public boolean hasnextdouble(); // Returns true if the next token is a number public String next (); // Returns the next token ( characters up to a space / line ) public String nextline (); // Returns the rest of the current line public int nextint (); // Returns the integer value of the next token // ( error if next token is not an integer ) public double nextdouble(); // Returns the double value of the next token // ( error if next token is not a number) public void close (); // Closes the file ( if it is wrapping a File object ) DrawingCanvas class: // Methods you can call on a DrawingCanvas object public void clear (); // Clears the drawing canvas public void setcolor(color c); // Change the colour for later commands public void drawline(int x, int y, int u, int v); // Draws line from cd{(x, y) to cd{(u, v) public void drawrect(int x, int y, int wd, int ht ); // Draws outline of rectangle public void fillrect ( int x, int y, int wd, int ht ); // Draws solid rectangle public void clearrect(int x, int y, int wd, int ht ); // Draws clear rectangle public void drawoval(int x, int y, int wd, int ht ); // Draws outline of oval public void filloval ( int x, int y, int wd, int ht ); // Draws solid oval