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

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

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

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

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

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

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

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

Midterm Examination (MTA)

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

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

Oct Decision Structures cont d

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

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

Java Classes: Random, Character A C S L E C T U R E 6

Programming with Java

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

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

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

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

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

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

Midterms Save the Dates!

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

Full file at

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 11, Name: KEY

COMP 110 Project 1 Programming Project Warm-Up Exercise

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

Chapter 5 Lab Methods

Introduction to Computer Programming

CS111: PROGRAMMING LANGUAGE II

Arrays. Eng. Mohammed Abdualal

1 Short Answer (10 Points Each)

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string

CSE 1223: Exam II Autumn 2016

Java Coding 3. Over & over again!

CMSC 150 INTRODUCTION TO COMPUTING LAB WEEK 3 STANDARD IO FORMATTING OUTPUT SCANNER REDIRECTING

Lab Exercise 1. Objectives: Part 1. Introduction

Conditional Programming

H212 Introduction to Software Systems Honors

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

Strings in Python: Cipher Applications CS 8: Introduction to Computer Science, Winter 2018 Lecture #9

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

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

STUDENT LESSON A12 Iterations

SAMPLE QUESTIONS FOR DIPLOMA IN INFORMATION TECHNOLOGY; YEAR 1

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

Question: Total Points: Score:

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

Darrell Bethea May 25, 2011

AP Computer Science Java Mr. Clausen Program 6A, 6B

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to write programs for executing statements repeatedly using a while, do while and for loop

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

Please answer the following questions. Do not re-code the enclosed codes if you have already completed them.

Chapter 9 Lab Text Processing and Wrapper Classes

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 6, Name:

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

CMPS 12A Winter 2006 Prof. Scott A. Brandt Final Exam, March 21, Name:

Section 003 Fall CS 170 Exam 1. Name (print): Instructions:

Building Java Programs

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

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4

What methods does the String class provide for ignoring case sensitive situations?

CSC 1051 Algorithms and Data Structures I. Midterm Examination March 1, Name: KEY A

Chapter 2. Elementary Programming

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

Lecture 9. Monday, January 31 CS 205 Programming for the Sciences - Lecture 9 1

Chapter 2 Part 2 Edited by JJ Shepherd, James O Reilly

Decisions in Java IF Statements

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 24, Name: KEY 1

Building Java Programs

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

Section 2.2 Your First Program in Java: Printing a Line of Text

ASSIGNMENT 1 Expressions, Data Types, and Simple Calculations

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

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

Loops. GEEN163 Introduction to Computer Programming

Intro to Programming in Java Practice Midterm

Introduction to Java Applications; Input/Output and Operators

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

Place your name tag here

CS 101 Fall 2006 Midterm 1 Name: ID:

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

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

Last Class. While loops Infinite loops Loop counters Iterations

CSE 1223: Introduction to Computer Programming in Java Chapter 3 Branching

Chapter 5 Lab Methods

COMP-202: Foundations of Programming. Lecture 7: Strings and Methods Jackie Cheung, Winter 2015

CSEN202: Introduction to Computer Science Spring Semester 2017 Midterm Exam

Lesson 7 Part 2 Flags

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

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

CSC 1051 Algorithms and Data Structures I. Midterm Examination March 2, Name:

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

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

CMSC131. Introduction to your Introduction to Java. Why Java?

Task #1 The if Statement, Comparing Strings, and Flags

Input. Scanner keyboard = new Scanner(System.in); String name;

I. True/False: (2 points each)

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

Chapter 4: Control Structures I

Transcription:

Student Name: Student Number: Section: Faculty of Science Midterm COMP-202B - Introduction to Computing I (Winter 2008) Friday, March 7, 2008 Examiners: Prof. Jörg 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 /20 /20 /40 Total /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: 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 [4] 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 Page 7 8. A parity bit is a binary digit that is added at the end of a binary number and is used for quality assurance. [20] More specifically, a parity bit is set to 1 if the number of ones in a given number is odd; it is set to 0 otherwise. For example, the parity bit of the 7-bit number 1111000 is 0 because there are 4 (an even number) of 1s in the 7-bit number. When information is transferred over the network or stored on devices such as hard disks, it sometimes happens that bits are corrupted (meaning that they are unrecognizable as 0 or 1), for instance because of electromagnetic interference. If only one bit of the original number is lost, the parity bit can help to rectify the error. For instance, given the number 11*01000 (where * stands for a corrupt bit), one can deduce that the missing bit should be a 1 as there are three 1s present and the parity is 0 indicating that there should be an even number of 1s present. (a) What is the missing bit in the number 1001*100? (b) What is the missing bit in the number 10*0011100110101? (c) What is the missing bit in the number 1100011*? (d) Complete the following program, which is supposed to correct any given binary number with an appended parity bit, provided that there is at most one erroneous bit. In your code, you may use any method of the String class, but you will probably only have to use the following: char charat(int index), which returns the character at position index in the String it is called on String replace(char oldchar, char newchar), which creates a new String in which all occurrences of the character oldchar in the original String are replaced by the character newchar. As a reminder, the method int indexof(char ch) returns the index position of the first occurrence of the character ch within the String it is called on. If the character does not occur in the String, the method returns -1.

COMP-202B Winter 2008 Midterm Page 8 public class Parity { public static void main(string[] args) { Scanner keyboard; String number, correct; boolean odd = false; keyboard = new Scanner(System.in); System.out.print("Enter a binary number: "); number = keyboard.nextline(); if (number.indexof( * )!= -1) { // calculate the value of odd for (int i = 0; i < number.length(); i++) { // correct the error by replacing the * with the correct digit if (odd) { else { else { // make sure the program does not crash in this case System.out.println("The corrected binary number is: " + correct); Total Marks for Section 2: 40

COMP-202B Winter 2008 Midterm Page 9 Section 3 - Programming Questions 9. This programming question has two parts. Part 1: [10] Write a class called QuadraticEquation that represents a quadratic equation of the form ax 2 + bx + c. The constructor of the class should receive the coefficients a, b and c. The class should provide three methods: (a) public boolean hassolutions(), that returns whether or not the quadratic equation has solutions in the real domain. (b) public double getfirstsolution(), that returns one of the solutions (if there are any). You may assume that this method will only be called if the quadratic equation has solutions. (c) public double getsecondsolution(), that returns the other solution (if there are any). You may assume that this method will only be called if the quadratic equation has solutions. The solutions can be calculated using the following formulae: x 1 = b+ b 2 4ac 2a, x 2 = b b 2 4ac 2a.

COMP-202B Winter 2008 Midterm Page 10 Part 2: [10] Write a class Solver that contains only a main() method that asks the user to input the coefficients a, b and c of a quadratic equation, and then outputs: The solutions are u and v. (where u and v represent the values of the two solutions). However, if there are no solutions in the real domain, your program should output: There are no solutions.

COMP-202B Winter 2008 Midterm Page 11 10. A monoalphabetic substitution cipher is a simple but insecure scheme used to encrypt messages. In a monoal- [20] phabetic subsitution cipher, for each possible symbol in the original message (called the plaintext), each occurrence of this symbol is replaced by another symbol in the encrypted message (called the ciphertext). A special value called the key determines which ciphertext symbol replaces each possible plaintext symbol. Write a program a class called Cipher which contains only a main() method that: Ask the user to enter the plaintext; you may assume that the plaintext entered by the user contains no lower-case letters Ask the user to enter the key; you may assume that the key entered by the user is 26 characters long, and contains each upper-case letter exactly once Encrypt the plaintext using the key and display the resulting ciphertext. The program should then ask whether to encrypt another plaintext using another key. 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. The encryption scheme your program implements will work as follows. As noted above, the key entered by the user is 26 characters long, and contains each upper-case letter only once. To determine which ciphertext letter corresponds to a plaintext letter, determine the position of the plaintext letter in the alphabet, subtract one, and retrieve the letter at the resulting position in the key. For example, each occurrence of the character A in the plaintext will be replaced by the character at position 0 in the key, each occurrence of the character B in the plaintext will be replaced by the character at position 1 in the key, and so on. Characters which occur in the plaintext and which are not upper-case letters will not be encrypted, and will appear as-is in the ciphertext. For example, the plaintext "HELLO WORLD!", when encrypted with the key "BCDEFGHIJKLMNOPQRSTUVWXYZA" will produce the ciphertext "IFMMP XPSME!"

COMP-202B Winter 2008 Midterm Page 12 YOUR PROGRAM CONTINUED: Total Marks for Section 3: 40 Total Marks: 100

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

COMP-202B Winter 2008 Midterm Page 14 USE THIS PAGE IF YOU NEED EVEN MORE ADDITIONAL SPACE. CLEARLY INDICATE WHICH QUES- TION(S) YOU ARE ANSWERING HERE.