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

Similar documents
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:

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

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

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

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

CS 101 Spring 2007 Midterm 2 Name: ID:

Object Oriented Programming. Java-Lecture 6 - Arrays

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

Final Exam. COMP Summer I June 26, points

1 Short Answer (10 Points Each)

Ryerson University Vers HAL6891A-05 School of Computer Science CPS109 Midterm Test Fall 05 page 1 of 6

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

1. [3 pts] What is your section number, the period your discussion meets, and the name of your discussion leader?

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

CS 110 Practice Final Exam originally from Winter, Instructions: closed books, closed notes, open minds, 3 hour time limit.

Midterm Examination (MTA)

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

3/18/2015. Chapter 19 Sorting and Searching SELECTION SORT SORTING AN ARRAY OF INTEGERS SORTING AN ARRAY OF INTEGERS FILE SELECTIONSORTER.

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

YOU ARE ALLOWED TO HAVE ONLY THE FOLLOWING ON YOUR DESK OR WORKTABLE:

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

Question: Total Points: Score:

1 Short Answer (5 Points Each)

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

CS 2334: Programming Structures and Abstractions: Exam 1 October 3, 2016

APCS Semester #1 Final Exam Practice Problems

Warm up Exercise. What are the types and values of the following expressions: * (3 + 1) 3 / / 2.0 (int)1.0 / 2

CSEN202: Introduction to Computer Science Spring Semester 2017 Midterm Exam

Handout 5 cs180 - Programming Fundamentals Spring 15 Page 1 of 8. Handout 5. Loops.

ECE 242 Fall 13 Exam I Profs. Wolf and Tessier

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

2.2 - Making Decisions

CSE 1223: Exam II Autumn 2016

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

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

Question 1 [20 points]

Practice Midterm 1. Problem Points Score TOTAL 50

CSE 20. SAMPLE FINAL Version A Time: 180 minutes. The following precedence table is provided for your use:

Arrays and Lists CSC 121 Fall 2014 Howard Rosenthal

To keep track of this new wrinkle we need some new variables at the Class level:

Final Exam Practice. Partial credit will be awarded.

Chapter 3. Selections

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

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

CSE 131 Introduction to Computer Science Fall Exam II

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

CMPS 12A - Winter 2002 Midterm 2 March 5, Name: ID:

Controls Structure for Repetition

SFWR ENG/COMP SCI 2S03 Principles of Programming SOLUTIONS

CSC 1051 Villanova University. CSC 1051 Data Structures and Algorithms I. Course website:

CMPS 11 Introduction to Programming Midterm 1 Review Problems

Repetition, Looping. While Loop

Practice Midterm 1 Answer Key

CIS 110 Introduction to Computer Programming Spring 2016 Midterm

Loops. CSE 114, Computer Science 1 Stony Brook University

Some Sample AP Computer Science A Questions - Solutions

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

St. Edmund Preparatory High School Brooklyn, NY

CS115. Chapter 17 Exception Handling. Prof. Joe X. Zhou Department of Computer Science. To know what is exception and what is exception handling

CS 113 PRACTICE FINAL

Programming with Java

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

Java Coding 3. Over & over again!

University of Massachusetts Amherst, Electrical and Computer Engineering

CS 12 Fall 2003 Solutions for mid-term exam #2

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

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

University of Palestine. Mid Exam Total Grade: 100

COMP 110 Programming Exercise: Simulation of the Game of Craps

York University AS/AK/ITEC INTRODUCTION TO DATA STRUCTURES. Midterm Sample I. Examiner: S. Chen Duration: One Hour and 30 Minutes

Last Class. While loops Infinite loops Loop counters Iterations

COMP 202 Java in one week

CSE 114 Computer Science I

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

Lab Exercise 1. Objectives: Part 1. Introduction

Choose 3 of the 1 st 4 questions (#'s 1 through 4) to complete. Each question is worth 12 points.

Introduction to Computer Science Unit 2. Notes

Arrays. Eng. Mohammed Abdualal

COMP-202 Unit 4: Programming with Iterations

CS 113 MIDTERM EXAM 2 SPRING 2013

Big Java, 5e, Cay Horstmann, Copyright 2014 John Wiley and Sons, Inc. All rights reserved. A Sample Debugging Session

Sorting. Task Description. Selection Sort. Should we worry about speed?

Opening Problem EXAMPLE. 1. Read one hundred numbers, 2. compute their average, and 3. find out how many numbers are above the average.

Name Section Number. CS210 Exam #3 *** PLEASE TURN OFF ALL CELL PHONES*** Practice

Arrays and Lists CSC 121 Fall 2015 Howard Rosenthal

Control Statements: Part 1

Sorting Algorithms part 1

Exam Percentage: / 55 = %

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

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

CIS 110 Introduction to Computer Programming Summer 2014 Midterm. Name:

CSE 142, Autumn 2007 Midterm Exam, Friday, November 2, 2007

Assignment Checklist. Prelab Activities. Lab Exercises. Labs Provided by Instructor. Postlab Activities. Section:

ITERATION WEEK 4: EXMAPLES IN CLASS

CPS109 Lab 7. Source: Big Java, Chapter 7 Preparation: read Chapter 7 and the lecture notes for this week.

Exam 2, Version 2. For the following code, mark True or False for statements 1.8 to 1.10.

CSCI 135 Exam #0 Fundamentals of Computer Science I Fall 2012

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

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

Transcription:

Name: New York University Introduction to Computer Science Midterm2B Sample Problems Solution Key Instructions: KEEP TEST BOOKLET CLOSED UNTIL YOU ARE INSTRUCTED TO BEGIN. This exam is double sided (front and back)! No calculators, notes, textbooks, or any other aids are allowed except writing utensils (pens, pencils, crayons, erasers, etc.) or other aids provided to you by the instructor. If you need extra scratch paper, please pick it up from the front of the class. You should also be provided with an appendix that provides helpful documentation. All answers must be submitted on (or attached to) this exam sheet. All answers must be clearly legible. Commenting code: Comments aren't required, but if you provide good comments, they can earn you partial credit if your code isn't correct. 1

Example True or False (10 points): Instructions: Circle either True or False based on the validity of the statement. 1. In object oriented programming, the superclass inherits properties from the subclass. true false [There are usually 5-10 questions of this type] 2

Example Multiple Choice (10 points): Instructions: Circle the letter of the best answer. 1. The data for which of these data types is stored in the Heap memory (select all that apply): A. boolean B. int C. double D. String E. int[] [There are usually 5-10 questions of this type] 3

Example Short Answer (10 points): Instructions: In your own words, answer the questions as best as possible in one or two sentences. 1. What is the difference between a Class and an Object. A class provides the blueprint for creating new object instances and what data and methods each object will have. Classes can contain static fields and static methods that are shared between all objects. Objects store data associated with that particular object and each objects member fields are unique to that object. [There are usually 5-10 questions of this type] 4

Example Entomology - Study of Bugs (10 points): Instructions: Find 3 bugs in the following program (there are more than three): 1) Specify the line number of the bug (Note: there can be more than one bug per line) 2) Check the box next to the type of error; either Logic for logic errors or Other for (syntax, type, etc.). 3) Write a short explanation or fix for the error. 0 import java.util.scanner; // Other (unresolved symbols) 1 2 * Exercise06_03: Count occurrences of numbers entered (values between 3 * <code>start_num_range</code> and <code>end_num_range</code>) 4 5 public class Exercise06_03 { 6 public static final int START_NUM_RANGE = 1; 7 public static final int END_NUM_RANGE = 100; 8 9 10 * Main program driver 11 * @param args is ignored 12 13 public static void main(string[] args) { 14 // Initialize counter array (using one extra index to simply logic) 15 int[] numbercounter = new int[end_num_range + 1]; // syntax error 16 17 // Get user inputed values 18 Scanner scanner = new Scanner(System.in); // syntax error 19 System.out.print("Enter integers between " 20 + START_NUM_RANGE + " and " + END_NUM_RANGE + ": "); 21 int input = scanner.nextint(); // logic or type mismatch 22 while (input!= 0) { // logic error 23 numbercounter[input]++; // update counter // logic error 24 input = scanner.nextint(); // get next input 25 26 27 // Print out count for values that were encountered 28 for (int i = 0; i < numbercounter.length; i++) { // logic, syntax error 29 if (numbercounter[i] == 1) { 30 System.out.println(i + " occurs " + numbercounter[i] + " time"); 31 else if ((numbercounter[i] > 1) (numcounter[i] == 0) { 32 System.out.println(i + " occurs " + numbercounter[i] + " times"); // sytnax 33 34 35 36 Bug 1 2 3 Line # Type of Error Explanation or Fix 5

Example What is the Output (10 points): Instructions: What is the output if the following code is run? public class InsertionSort { public static void main(string[] args) { int[] ints = { 9, 5, 7, 1, 3 ; int indexunsorted; int itemtocompare; int indexsortedsorted; // move unsorted items their proper spot in the sorted part of the array for (indexunsorted = 1; indexunsorted < ints.length; indexunsorted++) { // skip first item itemtocompare = ints[indexunsorted]; // move value to proper position in sorted list for (indexsortedsorted = indexunsorted - 1; (indexsortedsorted >= 0) && (itemtocompare < ints[indexsortedsorted]); indexsortedsorted--) { ints[indexsortedsorted + 1] = ints[indexsortedsorted]; // move smaller values down ints[indexsortedsorted + 1] = itemtocompare; // Put the value in its proper location // After every item is moved, print out the new array for (int i : ints) System.out.print(i + " "); System.out.println(); * Takes and array and swaps values from <code>index1</code> and <code>index2</code> * * @param ints - array of values * @param index1 - one of the indexes to swap the contents of * @param index2 - the second index to swap the contents of public static void swap(int[] ints, int index1, int index2) { int temp = ints[index1]; ints[index1] = ints[index2]; ints[index2] = temp; Answer: 5 9 7 1 3 5 7 9 1 3 1 5 7 9 3 1 3 5 7 9 6

Example Comment the Code (10 points): Instructions: The following code was written, but the documentation was left out. Analyze the code to determine what each function does and then what the program as a whole does. Fill in the missing comments. Be as specific as possible. Remember that Javadocs should explain WHAT the code does, and inline comments are generally best if they explain why the code is doing what it does. * Generates an array of random numbers between <code>min</code> and * <code>max</code> inclusively. * @param size of the array to create * @param min minimum value in the array * @param max maximum value in the array * @return a list of random numbers static int[] dostuff(int size, int min, int max) { Random rand = new Random(); // Create an array of the size passed in int[] ints = new int[size]; // For each value in the array assign it a random value for (int i = 0; i < ints.length; i++) { int nextrandom = rand.nextint(max-min + 1); // put it back in the range from min-max nextrandom += min; // place that random number in the array ints[i] = nextrandom; return ints; * Get the sum of all values in an array <code>ints</code>. * * @param ints array of integers to sum * @return the sum of all values in the array * static int dosomething(int[] ints) { int sum = 0; // Add all values in the array for (int i = 0; i < ints.length; i++) { sum += ints[i]; // return the sum of all values return sum; 7

Example Fill in the Code (10 points): Instructions: Fill in the missing code. public class Player { String name; int wins, losses; int health = 100; // Number of wins and loses this player has had // Percentage from 0-100, if 0, player is dead Player() { name = "Player"; * Copy constructor * * @param player to make a copy of public Player(Player player) { this.name = player.name; this.wins = player.wins; this.losses = player.losses; this.health = player.health; * @return a String representation of this player public String tostring() { return "Player '" + name + "' wins=" + wins + " losses=" + losses + " health=" + health; * Determines if this player is equivalent to another player * * @param player to compare this player to * @return true if this player and <code>player</code> have all equivalent data, otherwise false public boolean equals(player player) { if (!this.name.equals(player.name)) return false; if (this.wins!= player.wins) return false; if (this.losses!= player.losses) return false; if (this.health!= player.health) return false; return true; // Rest of code cut for brevity 8

Example Write the Code: In java code, write a class that has the following data and methods associated with it: Ball: ---data----------------- - xposition: int - yposition: int - xspeed: int - yspeed: int - size: int ---methods------------ + Ball() + Ball(xPosition: int, yposition: int, xspeed: int, yspeed: int, size: int) + setposition() + reversespeedy() + reversespeedx() public class Ball() { protected int xpos; protected int ypos; protected int xspeed; protected int yspeed; protected int size; Ball() { Ball(int xposition, int yposition, int xspeed, int yspeed, int size) { this.xposition = xposition; this.yposition = yposition; this.xspeed = xspeed; this.yspeed = yspeed; this.size = size; public void setposition(int x, int y) { xpos = x; ypos = y; public void reversey() { yspeed = -yspeed; public void reversex() { xspeed = -xspeed; 9

Example Write the Code: In Java code, write the following method that implements Selection Sort: public static void selectionsort(int[] array) { int currentmin; // Current minimum found in unosrted portion of the list // advance the position through the entire array // (can do j < length-1 because single element is also min element) for (int j = 0; j < ints.length - 1; j++) { // find the min element in the unsorted ints[j.. n-1] // assume the min is the first element in the unsorted values currentmin = j; // test against elements after j to find the minimum for (int i = j + 1; i < ints.length; i++) { // if this element is smaller, then it is the new minimum if (ints[i] < ints[currentmin]) { // found new minimum; remember its index currentmin = i; // currentmin is the index of the minimum element. // Swap it with the current position to update the sorted list if (currentmin!= j) { swap(ints, j, currentmin); // Print out the results from this iteration of selection sort for (int i: ints) System.out.print(i + " "); System.out.println(); * Takes and array and swaps values from <code>index1</code> and <code>index2</code> * @param ints - array of values * @param index1 - one of the indexes to swap the contents of * @param index2 - the second index to swap the contents of public static void swap(int[] ints, int index1, int index2) { int temp = ints[index1]; ints[index1] = ints[index2]; ints[index2] = temp; 10