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

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

Faculty of Science Midterm. COMP-202B - Introduction to Computing I (Winter 2008)

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

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

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

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

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

Midterm Examination (MTA)

Oct Decision Structures cont d

1 Short Answer (10 Points Each)

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

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

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

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

Question: Total Points: Score:

CIS 1068 Program Design and Abstraction Spring2016 Midterm Exam 1. Name SOLUTION

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

Full file at

Place your name tag here

Final Exam Practice. Partial credit will be awarded.

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

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

Programming with Java

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

Arrays. Eng. Mohammed Abdualal

CSEN202: Introduction to Computer Science Spring Semester 2017 Midterm Exam

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

ASSIGNMENT 4 Classes and Objects

H212 Introduction to Software Systems Honors

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

Top-Down Program Development

CS141 Programming Assignment #6

CONTENTS: While loops Class (static) variables and constants Top Down Programming For loops Nested Loops

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

Section 2: Introduction to Java. Historical note

Practice Midterm 1. Problem Points Score TOTAL 50

SAMPLE QUESTIONS FOR DIPLOMA IN INFORMATION TECHNOLOGY; YEAR 1

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

COMP-202: Foundations of Programming. Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015

CSE 114 Computer Science I

Example: Computing prime numbers

Mid Term Exam 1. Programming I (CPCS 202) Instructor: M. G. Abbas Malik Date: Sunday November 3, 2013 Total Marks: 50 Obtained Marks:

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

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

St. Edmund Preparatory High School Brooklyn, NY

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

STUDENT LESSON A12 Iterations

Computer Programming, I. Laboratory Manual. Experiment #3. Selections

Lab Exercise 1. Objectives: Part 1. Introduction

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

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

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

COMP 202 Java in one week

Question: Total Points: Score:

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

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

Chapter 5 Lab Methods

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

Computer Science is...

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

CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays

First Exam Computer Programming 326 Dr. St. John Lehman College City University of New York Thursday, 7 October 2010

COE 212 Engineering Programming. Welcome to Exam II Monday May 13, 2013

Selected Questions from by Nageshwara Rao

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

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

COMP 202. Java in one week

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

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

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

Chapter 2. Elementary Programming

Getting started with Java

Practice Midterm 1 Answer Key

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

COMP 202 Java in one week

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

Computer Programming, I. Laboratory Manual. Final Exam Solution

Final Exam. COMP Summer I June 26, points

while (/* array size less than 1*/){ System.out.print("Number of students is invalid. Enter" + "number of students: "); /* read array size again */

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

Java Coding 3. Over & over again!

Over and Over Again GEEN163

Decisions in Java IF Statements

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Fall 2009

Loops. CSE 114, Computer Science 1 Stony Brook University

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

Controls Structure for Repetition

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

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

Midterm Practice Problems - answer key Answers appear in boldface.

b. Suppose you enter input from the console, when you run the program. What is the output?

Conditional Execution

Last Class. While loops Infinite loops Loop counters Iterations

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

SFWR ENG/COMP SCI 2S03 Principles of Programming SOLUTIONS

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

Solutions to the 2005 exam

COMP 401 Midterm. Tuesday, Oct 18, pm-3:15pm. Instructions

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

CSE 1223: Exam II Autumn 2016

Transcription:

First Name: Last Name: McGill ID: Section: Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Midterm Examination Tuesday, November 4, 2008 Examiners: Mathieu Petitpas [Section 1] 18:30 20:30 Prof. Paul Kry [Section 2] Gregory B. Prokopski [Section 3] Instructions: DO NOT TURN THIS PAGE UNTIL INSTRUCTED This is a closed book midterm; notes, slides, textbooks, and other forms of documentation are not allowed. Non-programmable calculators are allowed (though you should not need one). Computers, PDAs, cellphones, and other electronic devices are not allowed. Answer all questions on the midterm paper; if you need additional space, use the last page or the booklets supplied and clearly indicate where each question is continued. In order to receive full marks, you must show all work. This midterm has examination 16 pages including this cover page. 1 2 3 4 Subtotal /4 /6 /6 /4 /20 5 6 Subtotal /15 /15 /30 7 8 Subtotal /30 /20 /50 Total /100 1

COMP-202A - Fall 2008 - Midterm Examination Page 2 Section 1 - Short Questions [4] 1. List 4 Java reserved words.

COMP-202A - Fall 2008 - Midterm Examination Page 3 [6] 2. What will be the value stored in variable result after each of the following code fragments is executed? Describe with a few words and a value what happens at all intermediate steps. Here is an example: int a = 2, b = 1, c = 5; double result = a * (b + c); Answer: Addition produces produces 6 Multiplication produces 12 Conversion on assignment to result gives 12.0 (a) int a = 23; int b = 46; double c = 3.0; double d = 4.5; double result = (int)d * c + a / b; (b) int a = 4; int b = 1; double c = a * 2; double d = c / a; String result = a + c + " == " + b + d;

COMP-202A - Fall 2008 - Midterm Examination Page 4 [6] 3. For each of the following conditions, write a boolean expression that evaluates to true if and only if the condition is true. You are not allowed to use any methods from the Java Platform API (that is, the Java standard library) to answer this question. (a) The number stored in variable y (of type int) is the number of a leap year; a leap year is a year whose number is divisible by 4, but not by 100, unless it is divisible by 400. (b) Exactly two of the values stored in variables a, b, and c (all of type int) are equal. (c) The value stored in variable c (of type char) is an upper-case consonant. In the following set (which contains all the letters of the English alphabet), the letters in bold are consonants, while the letters in italics are not: {A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z.

COMP-202A - Fall 2008 - Midterm Examination Page 5 [4] 4. The following program prints a shape to the screen using the character *. What shape is it and what are its dimensions? public class Shape { public static void main(string[] args) { final int SIZE = 6; for (int i = 0; i <= SIZE; i++) { for (int j = 0; j <= SIZE; j++) { if ((j == i) (j == SIZE-i) (j == SIZE/2)) { System.out.print("*"); else { System.out.print(" "); System.out.println(); Total marks for Section 1: 20

COMP-202A - Fall 2008 - Midterm Examination Page 6 Section 2 - Long Questions [15] 5. Consider the following program: public class TraceMe { public static void main(string[] args) { String text, chunk; int i; char c; 1 text = "C u"; 2 chunk = ""; 3 i = 0; 4 while (i < text.length()) { 5 c = text.charat(i); 6 if ((c == ) (c == \n )) { 7 System.out.println("Chunk: " + chunk + " "); 8 chunk = ""; else { 9 chunk += c; 10 i++; 11 System.out.println("Last chunk: " + chunk + " "); Trace this program by writing down the list of the statements which will be executed, in the order in which they will be executed in. Each statement must be included on your list exactly as many times as it is executed. Use the numbers to the left of each statement to indicate which statement is executed. To do this correctly, write your answer using a 2-column format. For each row, the first column contains the statement number of the statement being executed, and the second column contains the following information, according to the type of the statement: If the statement is an assignment statement: the variable whose value is being changed, and its new value If the statement is a control flow construct (if or while): the value to which the boolean expression controlling it evaluates If the statement is a call to the println() method: what is displayed on the screen As an example, here are the first five lines of the answer: 1 text "C u" 2 chunk "" 3 i 0 4 true 5 c C

COMP-202A - Fall 2008 - Midterm Examination Page 7 Continue the trace until the program terminates:

COMP-202A - Fall 2008 - Midterm Examination Page 8 [15] 6. Consider the following program, saved in a file called EqualStringChecker.java: 1 import java.util.scanner; 2 3 public class EqualStringChecker { 4 Public static void main(string[] args) { 5 Scanner keyboard = null; 6 boolean equal; 7 String s1, s2; 8 int s1length; 9 10 System.out.print("Enter the first String: "); 11 s1 = keyboard.nextline(); 12 System.out.print("Enter the second String: "); 13 s2 = keyboard.nextline(); 14 15 s1length = s1.length(); 16 if (s1length!= s2.length()) { 17 equal = false; 18 else { 19 equal = true; 20 i = 0; 21 while (i < s1length equal) { 22 if (s1.charat(i)!= s2.charat(i)) { 23 equal = false; 24 else { 25 i = i + 1; 26 27 28 29 if (equal) { 30 System.out.println("The Strings are equal."); 31 else { 32 System.out.println("The Strings are not equal."); 33 34 35 The above program is designed to check whether two Strings entered by the user are equal (that is, whether they contain the same characters in the same order). However, there are 5 errors in the above program. Find all the errors and list them. For each error you list, you must include the number of the line where the error occurs, the type of error (syntactic or semantic) and a description of the error. Do not list more than 5 errors, as you will be penalized for every error in excess of 5 that you list. Note that the line numbers to the left of the above program are included solely to help you make it easier for you to list the line numbers where errors occur; they are not part of the actual program.

COMP-202A - Fall 2008 - Midterm Examination Page 9 List the errors you find in the program here: Total marks for Section 2: 30

COMP-202A - Fall 2008 - Midterm Examination Page 10 Section 3 - Programming Questions 7. This question has two parts. In the first part, you will write a class; an object which belongs to this class represents a 2 by 2 rectangular table of elements called a matrix. In the second part, you will write a short program which creates and uses objects which belong to the class you wrote in the first part. [20] Part 1: Write a class called Matrix; each object which belongs to this class represents a 2 2 matrix whose elements are real numbers. You should use the labels a, b, c, and d, to represent the 4 entries of the matrix, where a is the value in row 1, column 1 b is the value in row 1, column 2 c is the value in row 2, column 1 d is the value in row 2, column 2 Thus, a 2 2 matrix M with values a, b, c, and d looks like this: ( ) a b M = c d Your Matrix class should provide the following public methods: A constructor, which takes as parameters four doubles; these doubles represent the values for a, b, c, and d of the Matrix to be created, in this order. A method called hasinverse(), which takes no parameters and returns a value of type boolean. This value is true if the Matrix object has an inverse, false otherwise; the general 2 2 matrix M defined above has an inverse if and only if ad bc is not equal to 0. A method called computeinverse(), which takes no parameters and returns a Matrix. This method should compute the inverse of the Matrix object on which it is invoked, and return a new Matrix object representing this inverse matrix. If the Matrix object has no inverse, this method should return null. In all cases, the state of the Matrix object on which this method is invoked should not change as a result of calling this method. The inverse of the general 2 2 matrix M defined above is denoted M 1 and has the following form: ( ) M 1 d/(ad bc) b/(ad bc) = c/(ad bc) a/(ad bc) A method called tostring(), which takes no parameters and returns a String object. This String object is a textual representation of the Matrix object. This text representation should have the following format: [a, b \ c, d], where a, b, c, and d are replaced by the actual values of the Matrix object.

COMP-202A - Fall 2008 - Midterm Examination Page 11 WRITE YOUR Matrix CLASS IN THE SPACE BELOW:

COMP-202A - Fall 2008 - Midterm Examination Page 12 [10] Part 2: Write a class called ComputeInverse, which has only a main() method that does the following: Ask the user to enter values a, b, c, and d to be stored in a 2 2 matrix, and read these values from the keyboard. Display the matrix in the following format: [a, b \ c, d] Compute the inverse of the matrix entered by the user if it exists, and display this inverse matrix in the same format as the original matrix. If the matrix created using the values entered by the user has no inverse, your program should display: M has no inverse Sample session: Enter the values of the matrix M: Row 1, column 1 (a): -1 Row 1, column 2 (b): 3 Row 2, column 1 (c): 2 Row 2, column 2 (d): -4 M == [-1.0, 3.0 \ 2.0, -4.0] Mˆ-1 == [2.0, 1.5 \ 1.0, 0.5] You should use the Matrix class written in the first part of this question as much as possible to write your program for this question. You may write print() instead of System.out.print() and println() instead of System.out.println() in order to save time.

COMP-202A - Fall 2008 - Midterm Examination Page 13 YOUR ComputeInverse CLASS CONTINUED:

COMP-202A - Fall 2008 - Midterm Examination Page 14 [20] 8. Write a method called add() that takes two Strings as parameters, each representing a non negative integer. Your add() method should construct a new String representing the sum. You may only use the String methods length() and charat() in writing your method; you cannot call any other method defined in the String class or any other class. You may assume that neither of the String parameters represents the empty String, that is, "", and that all characters in the two parameters will only consist of digits. Note that your method should work for Strings representing numbers which consist of an arbitray number of digits. Here follows a program with some test cases you may want to consider when writing your method. public class AddTwoStrings { public static String add(string s1, String s2) { // Your code here public static void main( String args[] ) { System.out.println( "7 + 5 = " + add("7", "5") ); System.out.println( "18 + 6 = " + add("18", "6") ); System.out.println( "6 + 18 = " + add("6", "18") ); System.out.println( "999 + 1 = " + add("999", "1") ); // A silly example String coffee = "12648430"; // 0xC0FFEE String mersenneprime61 = "2305843009213693951"; // Pervushin 1883 String usnationaldebt = "10474000336886"; System.out.print( "coffee + mersenneprime61 + usnationaldebt = "); System.out.println( add( add( coffee, mersenneprime61 ), usnationaldebt) ); This program generates the following output: 7 + 5 = 12 18 + 6 = 24 6 + 18 = 24 999 + 1 = 1000 coffee + mersenneprime61 + usnationaldebt = 2305853483226679267 Write your answer in the space provided on the following page. Hint: Think of how you add two numbers by hand.

COMP-202A - Fall 2008 - Midterm Examination Page 15 WRITE YOUR add() METHOD IN THE SPACE BELOW: public static String add(string s1, String s2) { // your code here Total marks for Section 3: 50 Total marks: 100

COMP-202A - Fall 2008 - Midterm Examination Page 16 USE THIS PAGE IF YOU NEED ADDITIONAL SPACE. CLEARLY INDICATE WHICH QUESTION(S) YOU ARE ANSWERING HERE.