Study Guides (248,357)
Canada (121,502)
COMP 202 (17)
Midterm

Midterm 2008 - COMP 202.pdf

14 Pages
150 Views
Unlock Document

Department
Computer Science (Sci)
Course
COMP 202
Professor
Mathieu Petitpas
Semester
Winter

Description
Student Name: Student Number: Section: Faculty of Science Midterm COMP-202B - Introduction to Computing I (Winter 2008) Friday, March 7, 2008 Examiners: Prof. Jorg Kienzle 18:15–20:15 Mathieu Petitpas Ladan Mahabadi 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 14 pages including this cover page. 1 2 3 4 5 SubTotal /4 /2 /4 /4 /6 /20 6 7 8 SubTotal /10 /10 /20 /40 9 10 SubTotal Total /20 /20 /40 /100 1 COMP-202B Winter 2008 Midterm Page 2 Section 1 - Short Questions (estimate 5 minutes for each question) 1. Read the following problem statement (the sentences are numbered on purpose): [4] (I) Write a program that calculates all prime number factors of a given positive integer. (II) Your program should first prompt the user to enter a positive integer from the keyboard. (III) The program should then display all prime factors of the integer on the terminal window. (IV) The factors should be displayed one per line, from the highest to the lowest. Please now answer the following questions: a) Which sentence describes what the program takes as input? b) Which sentence describes what the program should produce? c) Which sentence describes what the program should output? d) Which sentence prescribes the format of the program’s output? 2. Name at least 4 Java primitive types. [2] 3. The following code prints a shape to the screen using the character ’*’. What shape is it?[4] class Secret { public static void main(String[] args) { final int WIDTH = 12; for (int i = 0; i < WIDTH - 2; i++) { System.out.print(" ");* for (int j = 0; j < i; j++) { System.out.print(" "); } System.out.print(" "); * for (int j = 0; j < WIDTH - 3 - i; j++) { System.out.print(" "); } System.out.println(" "); * } } } COMP-202B Winter 2008 Midterm Page 3 4. Complete the following truth table: [4] p q r (p && q) || !r false false false false false true false true false false true true true false false true false true true true false true true true 5. For each of the following conditions, write a boolean expression that evaluates to true if and only if the [6] 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) Each of the values stored in the two char variables c1 and c2 is either an apostrophe (’) or a quotation mark ("). b) The value stored in exactly one of the three int variables a, b, and c is positive. c) At least one of the two values stored in int variables a and b is a multiple of the other. Total Marks for Section 1: 20 COMP-202B Winter 2008 Midterm Page 4 Section 2 - Long Questions 6. Consider the following program: [10] public class Confuse { public static void main(String[] args) { int n1, n2, stop; double d1, d2; 1 stop = 50; 2 n1 = 7; 3 n2 = 4; 4 d1 = 3.5; 5 while (stop > 25) { 6 if (stop % 4 == 0) { 7 d1 *= d1 + 3.5; } 8 if (n1 >= 10) { 9 n2 = (int)d1 * n2; 10 d1 = 1.5; } else { 11 n1 = n1 + 3; } 12 stop = stop - n1; } 13 d2 = n1 / 20; 14 System.out.println("Result == " + n2 + d2); } } Trace this program by writing down the list of the statements which will be executed, in the order that they will be executed in. 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. 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 stop ← 50 2 n1 ← 7 3 n2 ← 4 4 d1 ← 3.5 5 true COMP-202B Winter 2008 Midterm Page 5 Continue the trace until the program terminates: COMP-202B Winter 2008 Midterm Page 6 7. Consider the following program, saved in a file called Reverse.java: [10] 1 import java.util.Scanner; 2 3 public class ReverseString { 4 public static void main(String[] args) { 5 scanner keyboard; 6 String line; 7 String reverseLine; 8 9 keyboard = new Scanner(System.in); 10 System.out.print("Enter a character string: "); 11 line = keyboard.nextLine(); 12 13 reverseLine = ""; 14 for (int i = line.length(); i >= 0; i--) { 15 reverseLine = line.charAt(i) + reverseLine; 16 } 17 18 System.out.println("The reversed character 19 String is: " + reverseLine); 20 } 21 } The above program is designed to reverse a String entered by the user and display the result. 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 line number(s) where the error is located, 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 2008 Midterm
More Less

Related notes for COMP 202

Log In


OR

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


OR

By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.


Submit