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

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

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)

Introduction to Programming Using Java (98-388)

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

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

CSCI 131, Midterm Exam 1 Review Questions This sheet is intended to help you prepare for the first exam in this course. The following topics have

Introduction to Computer Science I Spring 2010 Sample mid-term exam Answer key

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

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

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

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

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

SAMPLE QUESTIONS FOR DIPLOMA IN INFORMATION TECHNOLOGY; YEAR 1

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

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

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

Selected Questions from by Nageshwara Rao

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

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

Programming with Java

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

CS 231 Data Structures and Algorithms, Fall 2016

Loops. CSE 114, Computer Science 1 Stony Brook University

Course Outline. Introduction to java

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

Pace University. Fundamental Concepts of CS121 1

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

Zheng-Liang Lu Java Programming 45 / 79

Object-Oriented Programming

Midterm Examination - Answers October 25, 2012 In Class Instructor: Joseph Vybihal

CS 102 / CS Introduction to Programming Midterm Exam #1 - Prof. Reed Fall 2010

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

Full file at

Computer Science II Data Structures

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

Chapter 1. Introduction to Computers and Programming. M hiwa ahmad aziz

CMPT 125: Lecture 3 Data and Expressions

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

Practice Midterm 1 Answer Key

CSCI 1226 Sample Midterm Exam

Chapter 4: Control structures. Repetition

Following is the general form of a typical decision making structure found in most of the programming languages:

Getting started with Java

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

Practice Midterm 1. Problem Points Score TOTAL 50

CS111: PROGRAMMING LANGUAGE II

Midterm Examination (MTA)

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

Chapter 4: Control structures

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

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

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

Final Exam. COMP Summer I June 26, points

Control Structures in Java if-else and switch

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

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

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

Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4

CS2141 Software Development using C/C++ C++ Basics

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

Expressions and Casting

Java Coding 3. Over & over again!

EECS168 Exam 3 Review

COMP 202 Java in one week

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

Conditional Execution

CS313D: ADVANCED PROGRAMMING LANGUAGE

Chapter 3. Selections

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

A Foundation for Programming

COMP Primitive and Class Types. Yi Hong May 14, 2015

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

JAVA OPERATORS GENERAL

SECONDARY SCHOOL, L-IMRIEĦEL HALF YEARLY EXAMINATIONS 2016/2017

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CS 139 Practice Midterm Questions #2

DATA TYPES AND EXPRESSIONS

Program Fundamentals

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

Building Java Programs Chapter 2

1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

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

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

ENGR 2710U Midterm Exam UOIT SOLUTION SHEET

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

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

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

PROGRAMMING FUNDAMENTALS

CSC 1214: Object-Oriented Programming

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

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

All classes in a package can be imported by using only one import statement. If the postcondition of a method is not met, blame its implementer

Lecture Set 4: More About Methods and More About Operators

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

CS110D: PROGRAMMING LANGUAGE I

COMP-202: Foundations of Programming. Lecture 8: for Loops, Nested Loops and Arrays Jackie Cheung, Winter 2016

BASIC ELEMENTS OF A COMPUTER PROGRAM

STUDENT LESSON A12 Iterations

Transcription:

McGill University School of Computer Science COMP-202A Introduction to Computing 1 Midterm Exam Thursday, October 26, 2006, 18:00-20:00 (6:00 8:00 PM) Instructors: Mathieu Petitpas, Shah Asaduzzaman, Sherif Shaker Student Name: Student ID: Your Section: Mathieu Petitpas (Sect 1) Shah Asaduzzaman (Sect 2) Sherif Shaker (Sect 3) Instructions This is a 2-hour midterm No notebooks, calculators or textbooks permitted in this exam. Language translation dictionaries are permitted. You are permitted to write your answers in either English or French. Attempt all questions. Parts marks are given for all questions. Show your work and do not leave a question empty. Write all answers in the exam booklet provided. Grading Section Grade Your Mark Section 1: Multiple Choice 20 Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q9 Q10 Section 2: Problems 45 Section 3: Programming 35 -------- ------------- Total 100 % Instructor: Petitpas, Shah, Shaker Page 1 of 7

Section 1: Multiple Choice Questions (2 points each; 20 points total) Please circle your answer on this page AND write it in the box provided on the cover page of this questionnaire. 1. Which of the following is TRUE about fetch and execute cycle? In a fetch and execute cycle a) One byte of data is read from the main memory and stored into the hard-disk b) One byte of program code is loaded in the CPU's arithmetic logic unit, and the code is executed c) One machine instruction is read from main memory into the CPU and the instruction is executed d) One arithmetic operation is executed 2. Computers can only understand numbers. How is an English word represented in computer memory? a) Each word is represented by a number equal to the number of letters in it b) Each letter in the alphabet is given a standard numeric code. Codes for each letter in the word are stored one after the other c) Each word in the dictionary is given a serial number based on its position in the dictionary d) Each letter has position number in the alphabet (A=1, C=3, Z=26). The sum of position numbers for all the letters represent the word in memory 3. Which of the following is FALSE about Java syntax? a) A Java program is a collection of classes b) For an integer variable a, 3 == a is a valid Java expression c) A method can have more than one parameter but only one return value d) A variable can be declared outside a method in a Java program 4. What is the value of x after executing the following code fragment? int a = 5, b = 6, c = 1; int x = (a > b? a : (a < b? b : c)); a) 0 b) 5 c) 6 d) 1 5. Which of the following assignment statements will cause a compilation error? a) int i = 4 / 5; b) double d = 4 / 5; c) int i = 4 / 5.0; d) double d = 4 / 5.0; e) char c = (char)('a' + 3); Instructor: Petitpas, Shah, Shaker Page 2 of 7

6. Consider the following expression: a = b && c + d * e <= f g; Which of the following expressions is equivalent to the one above: a) a = ((b && (((c + d) * e) <= f)) g); b) a = ((b && ((c + (d * e)) <= f)) g); c) a = (b && ((((c + d) * e) <= f) g)); d) a = (b && (((c + (d * e)) <= f) g)); e) a = ((b && (c + d)) * (e <= (f g))); 7. Which of the following statements about control flow are FALSE: 1) A while loop always executes its body at least once. 2) If one of the cases inside a switch statement does not end with a break statement, control does not transfer at the end of the switch statement but instead transfers in the next case. 3) The initialization, condition, and increment part of a for loop are optional. 4) To put more than one statement inside the else clause of an if-else statement, one must use a block statement. 5) Control flow constructs (if, if-else, while,...) cannot be nested. a) 1, 3 b) 1, 5 c) 2, 3 d) 2, 4 e) 3, 4 8. Which of the following statements about classes and methods are FALSE? 1) One of the purposes of a Java class is to serve as a model or blueprint for creating objects. 2) Methods can be defined outside of classes in Java. 3) Methods must declare a return type (either void or some other type). 4) Methods can optionally take parameters. 5) Constructors must declare a return type (either void or some other type). a) 1, 3 b) 1, 5 c) 2, 4 d) 2, 5 e) 3, 4 Instructor: Petitpas, Shah, Shaker Page 3 of 7

9. What is the minimum number of bits required to represent 70 unique items? a) 5 bits b) 6 bits c) 7 bits d) 8 bits 10. The following expression involves what kind of data conversion? double d = (int) 3.0/4 + 6; a) Arithmetic promotion and casting b) Casting and assignment conversion c) Assignment conversion and arithmetic promotion d) Casting, arithmetic promotion ad assignment conversion Instructor: Petitpas, Shah, Shaker Page 4 of 7

Section 2: Problems (total 45 points) 1. (15 points) Consider the following program: public class Confuse { public static void main(string[] args) { int n; double d; 1 n = 3; 2 while (n < 50) { 3 n = n * n; 4 d = n / 2; 5 System.out.println("Result: " + n + d); 6 n = 24; 7 d = 0; 8 while (n > 0) { 9 if (n % 4 == 0) { 10 d += 6; 11 n--; else { 12 d += d - 5; 13 n = n - 5; 14 System.out.println(d + n + " == result"); Trace this program by writing down the list of the statements that will execute, in the order that they will be executed. Each statement must be included on your list exactly as many times as it is executed. Use the statement numbers to the left of each statement to indicate which statement is executed. If a statement changes the value of a variable, you must indicate which variable it changes as well as the new value of the variable. If a statement is a control flow construct (if or while), indicate the boolean value (true or false) to which the boolean expression evaluates. Finally, if the statement is a call to the println() method, indicate what will be displayed on the screen. To do this correctly, write your answer using a 2-column format. For each row, the first column will contain the line number of the statement being executed, and the second column will contain 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: the value to which the boolean expression controlling it Instructor: Petitpas, Shah, Shaker Page 5 of 7

evaluates. If the statement is a call to the println() method: what is displayed on the screen. 2. (15 points) 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 absolute value of variable a (of type int) is greater than 100. b) The values of variables a, b, and c are all different. c) The character stored in either variable c1, or variable c2 (both of type char), or both, is a digit. d) The value stored in exactly one of the two variables a and b (both of type int) is equal to 0. e) The character stored in both variable c1 and variable c2 (both of type char) is a letter. 3. (15 points) Rewrite the program below and fix all the errors that are in it (compilation, logic and scope). If something compiles and is not a logical error, then do not fix it. Also indicate what the program outputs at the end once it has been fixed. Keep in mind that the output result depends on scoping rules. public class TaxCalculator { double tax; public double calculatetax (float salary) { if (salary <= 40000) double tax = salary * 0.3F; else if ((salary > 40000) && (salary <= 60000)) tax = salary * 0.45; else { double tax = salary * 0.5; return tax; public static main(string args[ ]) { System.out.print ("Enter a number: "); double salary = Keyboard.readDouble(); float tax = calcuatetax(salary); System.out.println ("Calculated tax : " + tax); Instructor: Petitpas, Shah, Shaker Page 6 of 7

Section 3: Programming (35 points) A program needs to manipulate some cardboard boxes. Each box has 3 dimensions height (h), width (w) and length (l), as depicted on the diagram below: The program checks if one box can be contained in another box. Note that the tops of the boxes are open, so we cannot turn a box upside down, tilt it, or put it on its side. However, we can rotate a box, clockwise or counter-clockwise, on its bottom. Thus, a box with (w=10, h=4, l=6) fits in a box with (w=6, h=5, l=12). But a box with (w=5, h=10, l=6) does not fit inside a box with (w=10, h=5, l=6). Note that we ignore the thickness of the cardboard, so one box can be fitted inside another box of the same dimension. (A) (20 points) Write a class Box to represent box objects. This class should allow the initialization of each box using its three dimensions by a constructor. It should also allow construction of boxes without any parameters, in which case each of the dimensions will be set to 1. The class should implement two other methods: boolean cancontain(box otherbox) This should check whether the otherbox can be fit into this box, according to the rules mentioned above and return true or false based on the answer. int comparevolume(box otherbox) This method should compare the volume of this box with that of the otherbox. It should return a positive integer, 0 or negative integer, if the otherbox is smaller, equal or bigger in volume, respectively. (B) (15 points) Write another class called BoxTester that contains a main() method that will ask the user to create two boxes, A and B. The program will then test if A can be put into B and if B can be put into A. The program will display a message indicating which could and could not fit into each other. The program will then ask the user if they want to do this again. If the user answers Y, y, YES, yes, Yes (or any other case combination of these 1 or 3 letters) the program will repeat the test, otherwise the program will terminate. Instructor: Petitpas, Shah, Shaker Page 7 of 7