Solutions to the 2005 exam

Similar documents
CSI 1100 / 1500 Fall 2004 Introduction to Computer Science I Final Examination

Lecture 13: Two- Dimensional Arrays

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

APCS Semester #1 Final Exam Practice Problems

Computer Programming, I. Laboratory Manual. Final Exam Solution

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

CMPS 12A - Winter 2002 Final Exam A March 16, Name: ID:

CSE 143 SAMPLE MIDTERM

COS 126 General Computer Science Spring Written Exam 1

Midterm I - CSE11 Fall 2013 CLOSED BOOK, CLOSED NOTES 50 minutes, 100 points Total.

Final Exam Practice. Partial credit will be awarded.

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

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

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

Final Examination Semester 2 / Year 2011

Course Outline. Introduction to java

CONCORDIA UNIVERSITY Summer 2005 Comp 248 /1 Section AA Introduction to Programming Final Examination/A

CS212 Midterm. 1. Read the following code fragments and answer the questions.

CIS 265/506 Exam1 Spring 2012 Prof. V. Matos Exam Last Name First Name:

ITI 1120 Lab #9. Slides by: Diana Inkpen, Alan Williams, Daniel Amyot Some original material by Romelia Plesa

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

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

CS170 Introduction to Computer Science Midterm 2

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

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

Programming with Java

CONDITIONAL EXECUTION

EXAM Computer Science 1 Part 1

Recap: Assignment as an Operator CS 112 Introduction to Programming

SPRING 13 CS 0007 FINAL EXAM V2 (Roberts) Your Name: A pt each. B pt each. C pt each. D or 2 pts each

Lec 3. Compilers, Debugging, Hello World, and Variables

CSIS 10A PRACTICE FINAL EXAM Name Closed Book Closed Computer 3 Sheets of Notes Allowed

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

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

Chapter 4: Control structures. Repetition

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char

University of Palestine. Mid Exam Total Grade: 100

CS 112 Introduction to Programming

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

Problem Grade Total

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

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

COS 126 Midterm 1 Written Exam, Fall 2009

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

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

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

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Solutions to Sample JAC444 Midterm Test

PROGRAMMING FUNDAMENTALS

Building Java Programs Chapter 2

CS 101 Fall 2006 Midterm 3 Name: ID:

CS 113 MIDTERM EXAM 2 SPRING 2013

Chapter 4: Control structures

CS 113 PRACTICE FINAL

Getting started with Java

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

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

CSC 126 FINAL EXAMINATION FINAL Spring 2012 B. Name (last, First) Instructor. Total Possible. Received

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

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

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

Selected Questions from by Nageshwara Rao

Midterm Examination (MTA)

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

Comp 170 Test 1 SAMPLE June 8, 2000

Building Java Programs Chapter 2. bug. Primitive Data and Definite Loops. Copyright (c) Pearson All rights reserved. Software Flaw.

Building Java Programs Chapter 2

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

Thomas Jefferson Invitational Open in Informatics 2012

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

CS111: PROGRAMMING LANGUAGE II

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

Java Identifiers, Data Types & Variables

COS 126 General Computer Science Fall Exam 1

COMPUTER APPLICATIONS

CSE 143 SAMPLE MIDTERM SOLUTION

Zheng-Liang Lu Java Programming 45 / 79

Lec 7. for loops and methods

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

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

Chapter 6. Arrays. Java Actually: A Comprehensive Primer in Programming

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

CS 1063 Introduction to Computer Programming Midterm Exam 2 Section 1 Sample Exam

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

CSCI 135 Exam #0 Fundamentals of Computer Science I Fall 2012

Building Java Programs. Chapter 2: Primitive Data and Definite Loops

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

Chapter 8 Multidimensional Arrays

Introduction to Software Development (ISD) Week 3

1.00 Introduction to Computers and Engineering Problem Solving Quiz 1 March 4, 2005

SAMPLE QUESTIONS FOR DIPLOMA IN INFORMATION TECHNOLOGY; YEAR 1

( &% class MyClass { }

public static String[] manyme() {

1. Find the output of following java program. class MainClass { public static void main (String arg[])

CS 307 Midterm 1 Fall 2007

Section 002 Spring CS 170 Exam 1. Name (print): Instructions:

Exam Duration: 2hrs and 30min Software Design

Java Bytecode (binary file)

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

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Transcription:

Solutions to the 2005 exam Question 1A [4] In this question, use only the following (algorithm format) Boolean expressions: comparison operators: <, >, =,,, and Boolean logical operators: NOT, AND, OR arithmetic operators: +, -, *, /, and MOD (modulo) variable names and constants. Use parentheses where necessary. Do not use Java syntax! The Ontario Drive Clean program requires that a car must pass an emissions test for licence renewal if both of the following conditions are met: The car s model year MY is odd and the current year CY is even, or vice versa The difference between the current year and the model year is at least 3 but no more than 20. Write a Boolean expression that is true if a car needs a Drive Clean emissions test, and false

Question 1A (((MY MOD 2 = 1) AND (CY MOD 2 = 0)) OR ((MY MOD 2 = 0) AND (CY MOD 2 = 1))) AND ((CY MY 3) AND (CY MY 20)) Alternative ((MY MOD 2 + CY MOD 2) = 1) AND ((CY MY 3) AND (CY MY 20)) Question 1B [4] What will be printed by the following program? Write your answer below the program. class ABC public static void main(string[ ] args) char[][] x = 'i', 'j', '4', '5'; char[][] y = 'x', 'y', 'a', 'b'; int i; char t; for (i = 0; i < 2; i=i+1) t = y[(i+1)%2][1]; y[i][(i+1)%2] = x[i][0]; y[(i+1)%2][1] = t; System.out.print(y[0][1]); System.out.println(y[1][0]); t = y[1][1] = b y[0][1] = x[0][0]= i y[1][1] = b t=y[0][1] = i y[1][0] = x[1][0] = 4 y[0][1] = i

Question 1B Answer: i4 class C1 private int[] v1 = 1,4,9; public int v3; public static int m1 (C2 p)... private C1 m2 (int m)... Question 1C [4] class C2 public static char v3; public C2 (int n)... private void m3 (int m)... Suppose that the following instructions are used in the main() method in a class Test. Each choice should be considered independently as it if were in its own main() method. Circle the letter of the statement which does NOT cause a compilation error.

Question 1C (a) (b) (c) int v2 = C1.m1(this); char v3 = C2.v3; C1 x = new m2(4); Error: m1 expects object of type C2 as parameter No error Error: no constructor m2 (d) (e) C1 w = new C1(); w.v1[2] = 3; C2 y = new C2(); int z = C1.m1(y); Error: v1 is private in C1 Error: no constructor with zero arguments in C2 Question 2 [8] class AClass public static void main(string[ ] args) amethod(137210); System.out.println( ); public static void amethod(int i) // see next slide Here is a program that uses recursion. What is printed by this program?

public static void amethod(int i) if (i == 0) ; // do nothing if ( i%10 == 1 ) System.out.print( "one " ); if ( i%10 == 2 ) System.out.print( "two " ); if ( i%10 == 3 ) System.out.print( "three " ); ; // do nothing amethod(i/10); Question 2 Question 2 Answer: one two three one

Question 3 [15] Translate the following algorithm to a Java method: GIVENS: Phrase: Left: Right: RESULT: IsPal (an array of characters representing a phrase to be checked) (index of leftmost character to check in Phrase) (index of rightmost character to check in Phrase) (true if Phrase is a palindrome, and false otherwise) INTERMEDIATES: Length (length of the interval examined in Phrase) HEADER: IsPal PalindromeRec(Phrase, Left, Right) BODY: Length Right - Left false Length < 1? true Phrase[Left] Phrase[Right]? IsPal true false IsPal PalindromeRec(Phrase, Left+1, Right-1) true IsPal false

Question 3 public boolean palindromerec (char [] phrase, int left, int right) int length; boolean ispal; length = right - left; if (length < 1) ispal = true; if (phrase[left]!= phrase[right]) ispal = false; ispal = palindromerec(phrase, left + 1, right - 1); return ispal; Question 4 [15] In the upcoming election, the candidate who has the most votes in an electoral district wins the riding and becomes the Member of Parliament. However, if the difference in the number of votes between the top two candidates (that is. the candidates with the largest and second-largest numbers of votes) is very close, a recount of the votes will take place. Design an algorithm that will take an array Votes containing the number of votes for each of the N candidates in an electoral district, and a value Diff where if the difference in the number of votes for the top two candidates is less than or equal to this value, a recount will take place. The algorithm should return true if the votes should be recounted and false otherwise. The array Votes is not sorted.

Answer GIVENS: Votes N Diff (array of integers) (length of array) (minimum difference in number of votes) INTERMEDIATES: First (the highest number of votes in the array) Second (the second highest number of votes in the array) RESULTS: Recount (Boolean: true if recount is needed, false otherwise) HEADER: Recount VotesRecountNeeded(Votes, N, Diff) BODY: 1. First -1 2. Second -1 3. Index 0 3. Index < N? false true 4. Votes[Index] > First? 10. Recount (First - Second Di ff) false false 5. Votes[Index] > Second? true 6. Second Votes[Index] true 7. Second First 8. First Votes[Index] 9. Index Index + 1

Question 5 [15] A "magic square" is a square matrix of integers where every row and every column can be summed to the same single value. For example, in the matrix A below, the sum of every row and column is 15. 2 9 4 A = 7 5 3 6 1 8 Write a Java method that will take a square matrix of integers A and returns true if A represents a "magic square" and false otherwise. Your method should be efficient. public static boolean ismagic( int[][] a ) public static boolean ismagic( int[][] m ) int magicsum; int sum; int row; int col; boolean magic; Question 5 magic = true; magicsum = -1; row = 0; while ( row < m.length && magic ) sum = 0; col = 0; while ( col < m[row].length ) sum = sum + m[row][col]; col = col + 1;

if ( row == 0 ) magicsum = sum; magic = ( sum == magicsum ); row = row + 1; col = 0; while ( col < m[0].length && magic ) sum = 0; row = 0; while ( row < m.length ) sum = sum + m[row][col]; row = row + 1; magic = ( sum == magicsum ); col = col + 1; return magic; Question 5 Question 6 [25] In this question, you will create a class AddressBook that will be able to store a collection of Friend objects. The class Friend, which has already been implemented, provides storage for a person's name and phone number. The class contains a constructor, and two accessor methods. A UML class diagram for the class Friend is as follows: - name : String - phone: String Friend + Friend( aname : String, aphone : String ) + getname( ) : String + getphone( ) : String + setphone( newnumber : String ) : void

Question 6 Fill in the methods for AddressBook. Your AddressBook class should provide a constructor and three public methods that would permit the following class TestAddressBook to execute: class TestAddressBook public static void main (String[] args) AddressBook mybook = new AddressBook( 2 ); mybook.addfriend( new Friend( "Alice", "555-1212" ) ); mybook.addfriend( new Friend( "Tommy", "555-3434 " ) ); mybook.addfriend( new Friend( "Pizza", "737-1111" ) ); mybook.changephone( "Tommy", "867-5309" ); mybook.print(); mybook.changephone( "Pizza", "310-1010" ); Question 6 Executing main() would result in the following being printed on the screen : The address book is full. Name: Alice, Phone: 555-1212 Name: Tommy, Phone: 867-5309 Pizza is not a name in the address book. Complete the class AddressBook

Question 6 class AddressBook // ATTRIBUTES: (3 marks) Friend [] list; int numelements; // CONSTRUCTOR: (5 marks) // Takes one integer parameter representing the // maximum number of entries that can be put into // the address book. public AddressBook(int n) list = new Friend[n]; numelements = 0; Question 6 // METHOD addfriend: (6 marks) // Method parameters: a Friend object that should be added to // the AddressBook. // Results: will print a message if the address book is full // Modified: the AddressBook object public void addfriend( Friend newfriend) if (numelements < list.length) list[numelements] = newfriend; numelements ++; System.out.println("The address book is full.");

Question 6 // METHOD print: (5 marks) // Method parameters: (none) // Returns: (none) // This method prints a list of names and phone numbers in // the address book. public void print() int index; for (index=0; index < numelements; index++) System.out.print("Name: " + list[index].getname() + ", "); System.out.println("Phone: " + list[index].getphone()); Question 6 // METHOD changephone (6 marks) // Updates the phone number of the first Friend with the // specified name // Method parameters: a String which is the name of a Friend, // and a String which is a new phone number for that Friend. // Result: an error message if the friend is not in the book // NOTE: recall that two strings s1 and s2 can be checked // for equality with s1.equals(s2)

Question 6 public void changephone(string aname, String aphone) int index; boolean found = false; for (index=0; index < numelements &&!found; index++) if (aname.equals( list[index].getname())) list[index].setphone(aphone); found = true; if (!found) System.out.println(aName + " is not a valid name in the address book."); // End of class AddressBook Question 7 [10] Write a recursive method that takes as a parameter a non-negative integer and generates the following pattern of stars. You may use a loop to generate one line of stars, but not the entire pattern. If the non-negative integer is 4, then the pattern generated is: **** *** ** * * ** *** **** The header of the method should be the following: public static void stars(int n)

Question 7 public static void stars( int n ) int index; if ( n > 0 ) for (index = 0; index < n; index++ ) System.out.print("*"); System.out.println( ); stars( n - 1 ); for (index = 0; index < n; index++ ) System.out.print("*"); System.out.println( );