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

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

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

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

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

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

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

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

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

Midterm Examination (MTA)

Assignment 4. Aggregate Objects, Command-Line Arguments, ArrayLists. COMP-202B, Winter 2011, All Sections. Due: Tuesday, March 22, 2011 (13:00)

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

1 Short Answer (10 Points Each)

CSEN202: Introduction to Computer Science Spring Semester 2017 Midterm Exam

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

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

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

ASSIGNMENT 4 Classes and Objects

Oct Decision Structures cont d

Question: Total Points: Score:

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

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

COE 211 Computer Programming. Welcome to Exam I Tuesday March 13, 2018

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

Final Exam Practice. Partial credit will be awarded.

CEN 414 Java Programming

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

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

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

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

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

Chapter 2. Elementary Programming

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

Lecture 8 " INPUT " Instructor: Craig Duckett

Example Program. public class ComputeArea {

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

University of Massachusetts Amherst, Electrical and Computer Engineering

CS 101 Exam 2 Spring Id Name

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

Problem Points Score Grader Total 75

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

Chapter 9 Lab Text Processing and Wrapper Classes

Computer Science II Fall 2009

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

Programming with Java

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

Fall CS 101: Test 2 Name UVA ID. Grading. Page 1 / 4. Page3 / 20. Page 4 / 13. Page 5 / 10. Page 6 / 26. Page 7 / 17.

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

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

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

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

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

ASSIGNMENT 1 Expressions, Data Types, and Simple Calculations

CS 101 Spring 2007 Midterm 2 Name: ID:

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

CSE 8B Intro to CS: Java

Lecture 14 CSE11 Fall 2013 For loops, Do While, Break, Continue

Final Exam. COMP Summer I June 26, points

Computer Science is...

Program Fundamentals

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

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

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

New York University Intro to Computer Science (CSCI-UA.101) Fall 2014 Midterm #1 Test G. Instructions:

CSE 1223: Exam II Autumn 2016

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

COMP 110 Project 1 Programming Project Warm-Up Exercise

SAMPLE QUESTIONS FOR DIPLOMA IN INFORMATION TECHNOLOGY; YEAR 1

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

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

SFWR ENG/COMP SCI 2S03 Principles of Programming SOLUTIONS

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

Building Java Programs

Full file at

Chapter 5 Lab Methods

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

ENGR 2710U Midterm Exam UOIT SOLUTION SHEET

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

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

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

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

Question: Total Points: Score:

New York University Introduction to Computer Science Midterm2 Sample Problems 2013 Andrew I. Case. Instructions:

New York University Introduction to Computer Science Exam Sample Problems 2013 Andrew I. Case. Instructions:

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

Part (II): In the body method main : Write the call to method formatname and have the return value stored in the variable formatted.

Building Java Programs

1. Data types ( =13 points)

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

Software Practice 1 - Basic Grammar Basic Syntax Data Type Loop Control Making Decision

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

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

Place your name tag here

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2013

Loops. CSE 114, Computer Science 1 Stony Brook University

COE 212 Engineering Programming. Welcome to Exam II Thursday April 21, Instructors: Dr. Salim Haddad Dr. Joe Tekli Dr. Wissam F.

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

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

Intro to Programming in Java Practice Midterm

COE 212 Engineering Programming. Welcome to Exam I Tuesday November 11, 2014

Last Class. While loops Infinite loops Loop counters Iterations

Transcription:

First Name: Last Name: McGill ID: Section: Faculty of Science COMP-202B - Introduction to Computing I (Winter 2009) Midterm Examination Monday, March 9, 2009 Examiners: Mathieu Petitpas [Section 1] 18:30 20:30 Prof. Xue Liu [Section 2] Ekwa Duala-Ekoko [Section 3] Instructions: DO NOT TURN THIS PAGE UNTIL INSTRUCTED This is a closed book midterm examination; notes, slides, textbooks, and other forms of documentation are not allowed. Non-programmable calculators are allowed (though you should not need one). Computers, PDAs, cell phones, and other electronic devices are not allowed. Answer all questions on the examination paper; if you need additional space, use pages 15 or 16, 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 Subtotal /10 /6 /4 /20 4 5 Subtotal /15 /15 /30 6 7 Subtotal /30 /20 /50 Total /100 1

COMP-202B - Winter 2009 - Midterm Examination Page 2 Section 1 - Short Questions [10] 1. In one or two sentences, explain the differences between the concepts in each of the following pairs. BE BRIEF; overly long answers will be grounds for mark deductions. (a) Widening conversions and narrowing conversions (b) An int and a double (in terms of the kind of values that variables of each type can store) (c) A char and a String (d) The = operator and the == operator (e) Logical AND and logical OR

COMP-202B - Winter 2009 - Midterm Examination Page 3 [6] 2. What will be displayed to the screen after each of the following code fragments is executed? For full marks, describe BRIEFLY with a few words and a value what happens at intermediate steps. (a) int a = 4; int b = a % 3; int c = (int)1.5 * a + b; System.out.println("The answer is: " + (b + c)); (b) int a = 7; int b = 5; int c = 2; int d = 50; int e = 25; System.out.println(a / 2 == 3.5 b + c / 4.0 == 5.5 && d > e);

COMP-202B - Winter 2009 - Midterm Examination Page 4 [4] 3. Consider the following code fragment: public class Deceive { public static void main(string[] args) { int n, result, i; n = 5; i = 0; result = 1; while (i < n) i = i + 1; result = result * i; } } System.out.println(result); What will be displayed on the screen when the above program is executed? Briefly explain your answer. Total marks for Section 1: 20

COMP-202B - Winter 2009 - Midterm Examination Page 5 Section 2 - Long Questions [15] 4. Consider the class Tuple with the following members: Each Tuple object has two private attributes of type int public Tuple(): A constructor which initializes both attributes of the new Tuple to 0. public Tuple(int first, int second): A constructor which initializes the first attribute of the newly-created Tuple to first, and the second attribute of the newly-created Tuple to second. public void setfirst(int newvalue): Changes the value of a Tuple s first attribute to newvalue. public void setsecond(int newvalue): Changes the value of a Tuple s second attribute to newvalue. public String tostring(): Returns a String representation of a Tuple in the following format: {first, second} where first is the value of the Tuple s first attribute, and second is the value of the Tuple s second attribute. What is displayed when the main() method of class TupleTrouble is executed? Carefully track the state of memory in the space provided, then clearly indicate the program output below. public class TupleTrouble { public static void main(string[] args) { int x = 2, y = 4; Tuple t1 = new Tuple(); Tuple t2 = new Tuple(x, y); } } System.out.println("1: y == " + y); System.out.println("2: t1 == " + t1); System.out.println("3: t2 == " + t2); x = y; t1 = t2; t2.setfirst(x); y = 6; System.out.println("4: t1 == " + t1); System.out.println("5: x == " + x); t1.setsecond(8); System.out.println("6: t1 == " + t1); System.out.println("7: t2 == " + t2); t2 = null; System.out.println("8: t1 == " + t1); System.out.println("9: t2 == " + t2);

COMP-202B - Winter 2009 - Midterm Examination Page 6 USE THE SPACE BELOW AS A SCRATCH PAD TO TRACK THE STATE OF THE VARIABLES IN MEMORY (THE CONTENTS OF THIS SPACE WILL NOT BE GRADED): CLEARLY INDICATE THE PROGRAM S OUTPUT IN THE SPACE BELOW (THE CONTENTS OF THIS SPACE WILL BE GRADED):

COMP-202B - Winter 2009 - Midterm Examination Page 7 [15] 5. Consider the following program, saved in a file called ContainsAtIndex.java: 1 public class ContainsAtIndex { 2 public static void main(string[] args) { 3 Scanner keyboard = new Scanner(System.in); 4 String s; 5 String singlechar; 6 char c; 7 int i; 8 9 System.out.print("Enter a string: "); 10 s = keyboard.nextline(); 11 12 System.out.print("Enter a character: "); 13 singlechar = keyboard.nextline(); 14 // Assumes that the user will not enter an empty line 15 c = singlechar.charat(0); 16 17 found = false; 18 while (i < s.length() && found) { 19 if (s.charat(i) == c) { 20 found = true; 21 } 22 i = i + 1; 23 } 24 25 if (found) { 26 System.out.println("The first occurrence of " + c + 27 " in \"" + s + "\" is a position " + i); 28 } else { 29 System.out.println("There are no occurrences of " + c + 30 " in \"" + s + "\""); 31 } 32 } 33 } The above program is designed to check whether a String entered by the user contains a character entered by the user, and if it does, to display the position of the first occurrence of the character in the String. However, there are 5 errors in the above program. Find all the errors and list them. For each error you list, you MUST 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-202B - Winter 2009 - Midterm Examination Page 8 LIST THE ERRORS YOU FIND IN THE PROGRAM HERE: Total marks for Section 2: 30

COMP-202B - Winter 2009 - Midterm Examination Page 9 Section 3 - Programming Questions 6. This question has two parts. In the first part, you will write a class; an object which belongs to this class represents a point in two-dimensional space. 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 Point; each object which belongs to this class represents a point in twodimensional space. Each point can therefore be specified uniquely by two integers: its x-coordinate and its y-coordinate. Your Point class should provide the following public methods: A constructor, which takes as parameters two ints; these ints represent the values of the x and y coordinates of the Point to be created, in this order. An accessor method called getx(), which takes no parameters, and returns a value of type int that represents the x-coordinate of the Point object. An accessor method called gety(), which takes no parameters, and returns a value of type int that represents the y-coordinate of the Point object. A method called computedistance(), which takes as parameter a Point object, and returns a value of type double. This method should compute the distance between the Point object the computedistance() method is called on and the Point object it takes as parameter, and return this distance. The distance d between two points p 1 and p 2 in two-dimensional space is given by the following equation: d = (x 2 x 1 ) 2 + (y 2 y 1 ) 2 In the above equation, x 1 and y 1 respectively represent the x and y coordinates of point p 1, while x 2 and y 2 respectively represent the x and y coordinates of point p 2. You MAY assume that the Point object passed as parameter is not null. A method called equals(), which takes as parameter a Point object, and returns a value of type boolean. This method returns true if the Point passed as parameter represents the same point in two-dimensional space as the Point the equals() method is called on, false otherwise. Two points are the same if and only if their x-coordinates are equal and their y-coordinates are equal. If the Point object passed as parameter is null, then this method MUST return false. A method called tostring(), which takes no parameters and returns a String object. This String object is a textual representation of the Point object. This text representation should have the following format: (x, y) where x and y are replaced by the actual x and y coordinates of the Point object. Note that the state of a Point object MUST NOT change once it is created; in other words, other than the constructor, the methods of the Point MUST NOT change the state of the Point objects they are called on.

COMP-202B - Winter 2009 - Midterm Examination Page 10 WRITE YOUR Point CLASS IN THE SPACE BELOW:

COMP-202B - Winter 2009 - Midterm Examination Page 11 [10] Part 2: Write a class called ComputeDistance, which has only a main() method that does the following: Ask the user to enter the x and y coordinates of two points p 1 and p 2, and read these values from the keyboard. Create Point objects from the values entered by the user. Display both points in the following format: (x, y) Compute the distance between the two points, and display it. Sample session: Enter the coordinates of point p1: - x-coordinate: 0 - y-coordinate: 0 Enter the coordinates of point p2: - x-coordinate: 3 - y-coordinate: 4 p1 == (0, 0) p2 == (3, 4) The distance between p1 and p2 is: 5.0 You should use the Point 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-202B - Winter 2009 - Midterm Examination Page 12 YOUR ComputeDistance CLASS CONTINUED:

COMP-202B - Winter 2009 - Midterm Examination Page 13 [20] 7. In a computer, all information (including program instructions) is represented as numbers in the binary number system. In this system, there are only two digits, 0 and 1, which are often referred to as bits. A binary editor allows one to view information stored in a computer as series of bits. However, the binary number system is often impractical. Therefore, many binary editors convert binary numbers to the hexadecimal number system before displaying them. The hexadecimal number system has 16 digits: the well-known decimal digits from 0 to 9, and the letters from A to F. Converting a binary number to the hexadecimal number system is fairly straightforward: Break down the binary number into groups of 4 bits Convert each group of 4 bits to one hexadecimal digit according to the following conversion table: Binary Hexadecimal Binary Hexadecimal 0000 0 1000 8 0001 1 1001 9 0010 2 1010 A 0011 3 1011 B 0100 4 1100 C 0101 5 1101 D 0110 6 1110 E 0111 7 1111 F Write a class called ConvertToHexadecimal, which contains only a main() method that does the following: Ask the user to enter a String; this String will represent a binary number. Convert the binary number entered by the user to the hexadecimal number system, and display the resulting hexadecimal number. Ask whether the user wants to convert another binary number to the hexadecimal number system. If the user answers "y", "Y", "yes", "YES", or any other meaningful combination of these letters such as "YeS", then the program will repeat the steps listed above, otherwise it will terminate. Sample session: Enter a binary number: 11011110101011011011111011101111 The corresponding value in hexadecimal: DEADBEEF Would you like to convert more? Enter "y" or "yes" to continue: yes Enter a binary number: 11001010111111101011101010111110 The corresponding value in hexadecimal: CAFEBABE Would you like to convert more? Enter "y" or "yes" to continue: no You MAY assume that the String entered by the user contains no characters other than 0 and 1. You MAY also assume that the length of the String entered by the user is a multiple of 4.

COMP-202B - Winter 2009 - Midterm Examination Page 14 WRITE YOUR ConvertToHexadecimal CLASS IN THE SPACE BELOW: Total marks for Section 3: 50 Total marks: 100

COMP-202B - Winter 2009 - Midterm Examination Page 15 USE THIS PAGE IF YOU NEED ADDITIONAL SPACE. CLEARLY INDICATE WHICH QUESTION(S) YOU ARE ANSWERING HERE.

COMP-202B - Winter 2009 - Midterm Examination Page 16 USE THIS PAGE IF YOU NEED EVEN MORE ADDITIONAL SPACE. CLEARLY INDICATE WHICH QUESTION(S) YOU ARE ANSWERING HERE.