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

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

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

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

Faculty of Science COMP-202B - Foundations of Computing (Winter 2016) - All Sections Midterm Examination

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

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2011) - All Sections 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 2008) Final Examination

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

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

Faculty of Science COMP-202B - Foundations of Computing (Winter 2016) - All Sections Final Examination

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

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

Faculty of Science COMP-202A - Foundations of Computing (Fall 2014) - All Sections Final Examination

COMP102: Test. 26 April, 2006

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

Midterm Exam 2 Thursday, November 15th, points (15% of final grade) Instructors: Jim Williams and Marc Renault

H212 Introduction to Software Systems Honors

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

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

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

Using APIs. Chapter 3. Outline Fields Overall Layout. Java By Abstraction Chapter 3. Field Summary static double PI

Computational Expression

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

Using Java Classes Fall 2018 Margaret Reid-Miller

CS 211: Existing Classes in the Java Library

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

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

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

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

Computer Science 300 Sample Exam Today s Date 100 points (XX% of final grade) Instructor Name(s) (Family) Last Name: (Given) First Name:

We now start exploring some key elements of the Java programming language and ways of performing I/O

A variable is a name for a location in memory A variable must be declared

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

ing execution. That way, new results can be computed each time the Class The Scanner

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

Computer Science 145 Midterm 1 Fall 2016

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

Object Oriented Programming. Java-Lecture 1

SFWR ENG/COMP SCI 2S03 Principles of Programming SOLUTIONS

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

COMP200 INPUT/OUTPUT. OOP using Java, based on slides by Shayan Javed

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

Classes and Objects Part 1

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

Full file at

COMP102: Test 1 Model Solutions

Fundamentals of Programming Data Types & Methods

York University AK/ITEC OBJECT-BASED PROGRAMMING. Midterm Test Sample. Examiner: S.Y. Chen Duration: One Hour and Fifteen Minutes

Final Exam Practice. Partial credit will be awarded.

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

COMP102: Test 2 Model Solutions

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 26, Name: Key

Midterm Examination (MTA)

Text User Interfaces. Keyboard IO plus

Computer Science 145

Programming with Java

The Irving K. Barber School of Arts and Sciences COSC 111 Final Exam Winter Term II Instructor: Dr. Bowen Hui. Tuesday, April 19, 2016

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

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

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

COMP 202. Programming With Iterations. CONTENT: The WHILE, DO and FOR Statements. COMP Loops 1

New York University Introduction to Computer Science Midterm2B Sample Problems Solution Key. Instructions:

Appendix 3. Description: Syntax: Parameters: Return Value: Example: Java - String charat() Method

COMP-202 Unit 8: Basics of Using Objects

JAVA OPERATORS GENERAL

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

Formatting Output & Enumerated Types & Wrapper Classes

Example Program. public class ComputeArea {

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

boolean, char, class, const, double, else, final, float, for, if, import, int, long, new, public, return, static, throws, void, while

Practice Midterm 1. Problem Points Score TOTAL 50

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

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

Important Java terminology

COMP 202 Java in one week

A Quick and Dirty Overview of Java and. Java Programming

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

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

CSEN202: Introduction to Computer Science Spring Semester 2017 Midterm Exam

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

Some Sample AP Computer Science A Questions - Solutions

Introduction to Computer Science Unit 2. Notes

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

Repetition. Chapter 6

Repetition. Chapter 6

CS170 (005): Introduction to Computer Science Exam 2

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

Arrays. Eng. Mohammed Abdualal

Lecture 8 " INPUT " Instructor: Craig Duckett

CSCI 1103: File I/O, Scanner, PrintWriter

COMP-202 More Complex OOP

Midterm Exam CS 251, Intermediate Programming March 12, 2014

CSE 1223: Exam II Autumn 2016

CSE 114 Midterm 1. Please leave one seat between yourself and each of your neighbors.

AP COMPUTER SCIENCE A

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

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

CSC 1051 Data Structures and Algorithms I

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

Transcription:

First Name: Last Name: McGill ID: Section: Faculty of Science COMP-202A - Foundations of Computing (Fall 2012) - All Sections Midterm Examination November 7th, 2012 Examiners: Daniel Pomerantz [Sections 1 (9:30-10:30) and 3 (12:30-1:30)] 18:00-21:00 Ioannis Rekleitis [Section 2] Instructions: DO NOT TURN THIS PAGE UNTIL INSTRUCTED This is a closed book examination; only a letter-sized (8.5 by 11 ) crib sheet is permitted. This crib sheet can be single or double-sided; it can be handwritten or typed. Non-electronic translation dictionaries are permitted, but instructors and invigilators reserve the right to inspect them at any time during the examination. Besides the above, only writing implements (pens, pencils, erasers, pencil sharpeners, etc.) are allowed. The possession of any other tools or devices is prohibited. Answer all questions on this examination paper and return it. If you need additional space, use page 13-13, or the booklets supplied upon request, and clearly indicate where each question is continued. In order to receive full marks for a question, you must show all work unless otherwise stated. This examination has 14 pages including this cover page, and is printed on both sides of the paper. On page 14, you will find information about useful classes and methods. You may detach page 14 from the examination if you wish. 1 2 3 4 Subtotal /10 /10 /10 /20 /50 5 6 7 8 9 Subtotal Total /12 /6 /10 /10 /12 /50 /100

COMP-202A - Fall 2012 - Midterm Examination Page 2 Short Answer Questions 1. What are the types and values of the following Java expressions? (10 points) Expression Type Value 1 + 2 1 + 2.0 "" + 1 + 2 1/2 + 1/2 (double)(1/2)

COMP-202A - Fall 2012 - Midterm Examination Page 3 2. For each of the following, write a short piece of Java code: (10 points) Using a loop, write code to print all multiples of 3 from 0 to 102 (inclusive) in reverse. (e.g. Your loop should print 102 99 96 etc) Assume there is a method isprime which takes as input an int n and returns a boolean of true if the number is prime and false otherwise. Write a loop to print all prime numbers from 0 until 100,000. Use nested loops to print the following pattern: ***** **** *** ** * Hint: One of your loop counters should have a range dependent on the other counter s current value.

COMP-202A - Fall 2012 - Midterm Examination Page 4 3. Consider the following three methods. Assume that they are all part of the same class. (10 points) public static boolean foo(int x, boolean b) if (x < 0) return true; return!b; public static int bar(boolean b) if (!b) return 1; return -1; public static double chocolate(double a, int b) if (a >= b) return 1; else return -1; Assume also that you have the following main method inside of the same class: public static void main(string[] args) int a = 3; double b = 4; boolean c = false; System.out.println(???) ; You will now be shown several expressions that could be written before the final ; instead of the???. For each of the following, answer in the box whether the expression will compile. If you think it does not compile, write why not. If you think it does compile, then write the type and value of what will print.

COMP-202A - Fall 2012 - Midterm Examination Page 5 Code Does it compile? Why not or type/value a + 2 Yes int 4 true + 2 No You can t add a boolean to an int foo(b, false) chocolate(1/2, 1/2) chocolate( bar( foo ( -3, c!c)), a) bar ( chocolate ( foo ( 3, c!c ))) bar( foo(3, true) && foo(3,!false)) 4. In this question, you will be shown several pieces of code. After each one, you should answer in the box what prints to the screen when the main method of that class is executed. (20 points) Part 1 Part 2 Part 3 Part 4 public class Part1 public static void printvariables(int a, int b) System.out.println(a); System.out.println(b); public static void main(string[] args) int a = 4; int b = 5; printvariables(b,a);

COMP-202A - Fall 2012 - Midterm Examination Page 6 public class Part2 public static void swapvariables(int a, int b) int temp = a; a = b; b = temp; public static void main(string[] args) int a = 4; int b = 5; swapvariables(a,b); System.out.println(a); System.out.println(b); public class Part3 public static void main(string[] args) for (int i = 0; i < 5; i++) System.out.print(i); for (int i = 0; i < 5; i++) System.out.print(i); i++; public class Part4 public static void main(string[] args) int[] x = 100,200,300; int[] y = x; y[2] = 5000; y = new int[10]; y[0] = 6000; System.out.println(x[0] + " " + x[1] + " " + x[2]);

COMP-202A - Fall 2012 - Midterm Examination Page 7 Programming 5. Define a new type WebpageLog. A WebpageLog object is used to manage the counts of how many visitors have gone to a page. A WebpageLog object should have 2 private properties: (12 points) private String url; private int visitors; The url is what url the WebpageLog object is from. The property visitors represents the number of people who have visited the page. In addition you should define the following non static methods in the WebpageLog class: A public constructor that takes as input a String page and assigns the url property appropriately. It should also set the visitors count to be 0. public String geturl() : This method should return the url public int getvisitorcount() : This method should return the value of the visitor property. public void increasevisitorcount() : This method should increase the visitors count by one.

COMP-202A - Fall 2012 - Midterm Examination Page 8

COMP-202A - Fall 2012 - Midterm Examination Page 9 6. Write a static method comparecounts that takes as input two WebpageLog objects and returns an int. This int should be: (6 points) (a) -1 if the number of visitors to the first WebpageLog is less than the number of visitors to the second. (b) 0 if the number of visitors is equal. (c) 1 if the number of visitors to the first WebpageLog is greater than the number of visitors to the second.

COMP-202A - Fall 2012 - Midterm Examination Page 10 7. Write a static method maxvalue which takes as input an array of int and returns the largest value inside the array. (10 points)

COMP-202A - Fall 2012 - Midterm Examination Page 11 8. Write a static method countvalues which takes as input an array of int called values and an int maximum. The values in values will range from 0 up to but not including maximum, and returns an array of integers representing how many times each integer occurred in the incoming array. The element at index 0 of your returned array should correspond with how many times the number 0 appeared in the array values. The element at index 1 should correspond with how many times the number 1 appeared in the array values etc. For example, if values contains the contents 1,2,3,5 and maximum is 10 then your returned array would look like: 0, 1, 1, 1, 0, 1, 0, 0, 0, 0 (10 points)

COMP-202A - Fall 2012 - Midterm Examination Page 12 9. Using the methods defined above, write a program that does the following: (12 points) (a) Using a Scanner ask the user how many non-negative integer values he/she would like to enter. Store this result into a variable. (You may assume the user enters a positive integer) (b) Create an array large enough to store the number of values the user enters. (c) Using the same scanner, read values from the user and insert them into the array until they have entered as many valid values as they said they would. You may assume that the user enters an integer at each step but you MAY NOT assume that they enter a positive integer. If they enter a negative number, you should allow them to re-enter a value. (Hint: There are several ways to accomplish this, but one way is to only increment your loop counter i when a valid value is entered.) (d) Using your methods above, calculate and print which number they entered the most times. Hint: You can use the two methods countvalues() and maxvalue() to accomplish this. Even if you did not code them correctly you can still get full credit on this part. Note that you should assume that the two methods were defined in a class ArrayUtilities. The program is started for you below. (If you wish, you may ignore the suggestion and write your own version instead.) import java.util.scanner; public class ModeProgram public static void main(string[] args) Scanner reader = new Scanner(System.in); System.out.println("How many integer values would you like to enter?"); int size = reader.nextint(); //INSERT CODE HERE //CREATE AN INT ARRAY OF SIZE size //LOOP TO READ ALL VALUES INTO THE ARRAY //CALCULATE AND PRINT THE STATISTICS USING THE METHODS

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

COMP-202A - Fall 2012 - Midterm Examination - Appendix Page 14 SUMMARY OF JAVA STANDARD LIBRARY METHODS FOR SELECTED CLASSES String (package java.lang) Methods: public boolean equals(object anobject): Compares this String to anobject. public int length(): Calculates the length of this String. public boolean equalsignorecase(string anotherstring): Compares, ignoring case considerations, this String to anotherstring. public int compareto(string anotherstring): Compares this String to anotherstring lexicographically; returns a negative value if this String occurs before anotherstring, a positive value if this String occurs after anotherstring, and 0 if both Strings are equal. public int comparetoignorecase(string anotherstring): Compares, ignoring case considerations, this String to anotherstring lexicographically; returns a negative value if this String occurs before anotherstring, a positive value if this String occurs after anotherstring, and 0 if both Strings are equal. public String substring(int start, int finish): Returns a new String composed of the this String starting from index start and up to, but not including index of finish public String replace(char c, char d) : Returns a new String with all occurrences of the character c in the this String replaced by the character d. public char[] tochararray(): Converts this String to a new character array. File (package java.io) Methods: public FileSstring pathname): Creates a new File instance that corresponds to the given pathname. Scanner (package java.util) Methods: public Scanner(Inputstream source): Constructs a new Scanner that produces values scanned from the specified input stream. public Scanner(File f): Constructs a new Scanner that produces values scanned from the specified File public double nextdouble(): Scans the next token of the input as a double. public boolean nextboolean(): Scans the next token of the input as a boolean. public int nextint(): Scans the next token of the input as an int. public String nextline(): Advances this Scanner past the current line and returns the input read. public boolean hasnextline(): Checks whether there are further lines left to scan. PrintStream (package java.io) Methods: public void print(boolean b): Prints boolean value b. public void print(double d): Prints double value d. public void print(int i): Prints int value i. public void print(object o): Prints Object o. public void print(string s): Prints String s. public void println(): Terminates the current line by writing the line separator string. public void println(boolean b): Prints boolean value b and then terminates the line. public void println(double d): Prints double value d and then terminates the line. public void println(int i): Prints int value i and then terminates the line. public void println(object o): Prints Object o and then terminates the line. public void println(string s): Prints String s and then terminates the line. Math (package java.lang) Methods: public static double pow(double a, double b): Returns the value of a raised to the power of b. public static double sqrt(double a): Returns the correctly rounded positive square root of double value a. public static double random(): Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. public static double exp(double a): Returns Euler s number e raised to the power of double value a. (base e) of double value a. of double value a.