CS170 Introduction to Computer Science Midterm 2

Similar documents
CS 113 MIDTERM EXAM 2 SPRING 2013

CIS 265/506 Exam1 Spring 2012 Prof. V. Matos Exam Last Name First Name:

CS171 Midterm Exam. October 29, Name:

Question: Total Points: Score:

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

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

Midterm Exam CS 251, Intermediate Programming March 12, 2014

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

CPSC-112 Introduction to Programming

Birkbeck (University of London) Software and Programming 1 In-class Test Mar Answer ALL Questions

COS 126 Midterm 1 Written Exam Fall 2011

CS 101 Spring 2007 Midterm 2 Name: ID:

CS 201, Fall 2016 Sep 28th Exam 1

CS170 (005): Introduction to Computer Science Exam 2

CS 201, Fall 2016 Oct 28th Exam 2. Question 1. [5 points] Consider the following static method:

CSE 1223: Exam II Autumn 2016

CMSC 433 Section 0101 Fall 2012 Midterm Exam #1

Lecture 12: Classes II

CIS 110 Introduction to Computer Programming. 12 February 2013 Midterm. Answer Key

I. True/False: (2 points each) On your bubble form fill out a for true and b for false.

CIS 110 Introduction To Computer Programming. February 29, 2012 Midterm

COS 126 General Computer Science Spring Written Exam 1

CS180. Exam 1 Review

Midterm 2 A. 10 Questions. While some questions may seem familiar to practice problems, there are likely to be subtle

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

CSEN 202: Introduction to Computer Programming

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

Name CIS 201 Midterm II: Chapters 1-8

University of Palestine. Mid Exam Total Grade: 100

Arrays. Eng. Mohammed Abdualal

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

Lecture 5: Methods CS2301

Esc101 Mid Semester Exam - II

CS 113 PRACTICE FINAL

CS 116 Week 8 Page 1

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

This exam is open book. Each question is worth 3 points.

Question: Total Points: Score:

SPRING 13 CS 0007 FINAL EXAM V2 (Roberts) Your Name: A pt each. B pt each. C pt each. D or 2 pts each

Final Exam. COMP Summer I June 26, points

I. True/False: (2 points each)

CIS 110 Introduction to Computer Programming Spring 2016 Midterm

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

Introduction to Programming (CS112): Sample

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

CS 455 Midterm Exam 1 Spring 2015 [Bono] Thursday, Feb. 19, 2015

CS 101 Fall 2006 Midterm 3 Name: ID:

Object- Oriented Analysis, Design and Programming

CIS 110 Fall 2016 Introduction to Computer Programming 13 Oct 2016 Midterm Exam Answer Key

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

I. True/False: (2 points each)

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

COMPUTER APPLICATIONS

CS171 Final Practice Exam

Final Exam CS 152, Computer Programming Fundamentals December 5, 2014

if (x == 0); System.out.println( x=0 ); if (x = 0) System.out.println( x=0 );

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

An introduction to Java II

CSE 373 Spring 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

Exam 2. CSI 201: Computer Science 1 Fall 2016 Professors: Shaun Ramsey and Kyle Wilson. Question Points Score Total: 80

CIS 110 Introduction to Computer Programming. 12 February 2013 Midterm

Practice Midterm 1. Problem Points Score TOTAL 50

CIS 110 Introduction to Computer Programming. 17 December 2012 Final Exam

Solutions to the 2005 exam

Final Exam COMP Fall 2004 Dec 16, 2004

More Java Basics. class Vector { Object[] myarray;... //insert x in the array void insert(object x) {...} Then we can use Vector to hold any objects.

COS 126 Midterm 1 Written Exam Fall 2012

Prelim One Solution. CS211 Fall Name. NetID

Objects and Classes (1)

CIS 110 Spring 2013 Make-Up Midterm, 13 February 2013, Answer Key. Miscellaneous

CS 101 Fall 2005 Midterm 2 Name: ID:

CS : Introduction to Computer Programming Final Exam December 9, Please wait to open this exam booklet until you are told to do so.

CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2014

COE 212 Engineering Programming. Welcome to the Final Exam Monday May 18, 2015

Use the scantron sheet to enter the answer to questions (pages 1-6)

CS 455 Midterm Exam 1 Fall 2016 [Bono] Thursday, Sept. 29, 2016

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

COE318 Lecture Notes Week 4 (Sept 26, 2011)

Prelim 1 Solutions. CS 2110, March 10, 2015, 5:30 PM Total Question True False. Loop Invariants Max Score Grader

CMPS 12A Fall 2017 Final (A)

CS 162 Intro to CS II. Structs vs. Classes

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

CS 101 Fall 2006 Midterm 1 Name: ID:

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

CS 170 Exam 2. Version: A Spring Name (as in OPUS) (print): Instructions:

Fall CS 101: Test 2 Name UVA ID. Grading. Page 1 / 4. Page3 / 20. Page 4 / 13. Page 5 / 10. Page 6 / 26. Page 7 / 17.

Final Exam CS 251, Intermediate Programming December 10, 2014

CS 101 Exam 1 Spring 200 Id Name

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

CS 101 Spring 2006 Final Exam Name: ID:

CS 170 Exam 2. Version: A Fall Name (as in OPUS) (print): Instructions:

CS101 Part 2: Practice Questions Algorithms on Arrays, Classes and Objects, String Class, Stack Class

Exam 1 - (20 points)

Problem Grade Total

CS 307 Midterm 2 Fall 2009

CIS 265 Summer 2015 Exam 1 First Name Last Name

CIS 110 Fall 2016 Introduction to Computer Programming 13 Oct 2016 Midterm Exam

Spring 2013 COMP Midterm Exam Solutions March 07, 2013

I. True/False: (2 points each)

CS 101 Exam 2 Spring Id Name

Transcription:

CS170 Introduction to Computer Science Midterm 2 03/25/2009 Name: Solution You are to honor the Emory Honor Code. This is a closed book and closednotes exam, and you are not to use any other resource than yourself in completing this exam. You have 50 minutes to complete this exam. The point values for each problem are indicated both below and beside each problem. Read each problem carefully, and review your answers! Problem Points Earned 1 20 2 30 3 15 4 15 5 20 6 30 7 30 Total points 160

(20 pts) 1. Each of the questions below is worth 2 points. Select the correct answer (each question has only one correct answer). 1.1 Analyze the following code: public class Test { public static void main(string[] args) { int[] x = {1, 2, 3, 4; x = new int[2]; for (int i = 0; i < x.length; i++) { System.out.print(x[i] + " "); A) The program displays 0 0 B) The program displays 0 0 0 0 C) The program displays 0 0 3 4 D) The program displays 1 2 3 4 1.2 Consider the following code fragment: int[] list = new int[10]; for (int i = 0; i <= list.length; i++) { list[i] = (int)(math.random() * 10); A) The code has a runtime error indicating that the array is out of bound B) The loop body will execute 10 times, filling up the array with zeros C) The loop body will execute 10 times, filling up the array with random numbers D) list[5] will always have value 5 1.3 Given the following statement: int[] list = new int[10]; list.length has the value: A) 9 B) 10 C) 11 D) list.length is illegal expression 1.4 is a construct that defines objects of the same type A) a method B) a class C) an object D) a data field 1.5 What is the output of the following code? public class Test { public static void main(string[] args) { String s1 = new String("Welcome to Java!"); String s2 = new String("Welcome to Java!"); if (s1.equals(s2)) { System.out.println("s1 and s2 have the same contents"); else { System.out.println("s1 and s2 have different contents"); A) s1 and s2 have the same contents B) s1 and s2 have different contents

1.6 Suppose s is a string with the value "fluffy". What will be assigned to x if you execute the following code? char x = s.charat(2); A) l B) u C) f D) This statement is illegal 1.7 What is the return value of "SELECT".substring(0, 5)? A) SELE B) ELE C) SELEC D) SELECT 1.8 When you pass an object to a method, the method receives A) the reference to the object B) the size of the object C) a copy of the object D) This cannot be done in java 1.9 The default constructor has no arguments. A) true B) false 1.10 The constructor can be executed only when new object is created. A) true B) false 2 (30 pts) Write a method called getlargest() that takes one argument, an array of integers and returns an int value. The method should return the largest item from the array passed as an argument. To get the full credit do not sort the array. public static int getlargest(int[] arr) { int max = arr[0]; for (int i= 1; i < arr.length; i++) { return max; if (max < arr[i]) max = arr[i]; The following class is used in questions 3 7

public class Point { private int x; private int y; public Point() { public Point(int x, int y) { this.x = x; this.y = y; public int getx() { return x; public int gety() { return y; public void mystery(point otherpoint) { int newx = (x + otherpoint.getx()) / 2; int newy = (y + otherpoint.gety()) / 2; x = newx; y = newy; 3 (15 pts) Identify all data fields, their types and visibility modifiers in the class Point x of type int and with modifier private y of type int and with modifier private 4 (15 pts) How many constructors does the Point class have? Identify the parameters of each of the constructors. Two: Point() no arguments and Point(x, y) two int arguments

5 (20 pts) Explain the behavior of mystery method The method updates coordinates of given point to the midpoint between this point and otherpoint. 6 (30 pts) Declare a new method in class Point called distance. The method takes one argument of type Point and returns a double. The result should be a distance between this point and point passed as an argument. Hint: use Math.sqrt(double val) to get square root of given number. See the mystery method for hint how to use Point as an argument of a method inside Point class. If you have 2 points with coordinates (x 1,y 1 ) and (x 2,y 2 ), the distance between those points is defined as follows: d (x 1 x 2 ) 2 (y 1 y 2 ) 2 public double distance(point p) { int difx = p.getx() x; int dify = p.gety() x; return Math.sqrt(difX*difX + dify*dify);

7 (30 pts) Write a class called Perimeter. The class will only have a main method (so that you can run it typing java Perimeter ). The main method will have the following behavior. It will create an array of 10 objects of type Point (defined above). The points in the array should have random x and y coordinates (in the range 0 10). Once you create the array, you will calculate and print sum of distances between points in the array as follows: distance between first and second point + distance between second and third point + distance between third and fourth point + + distance between last and first point You can assume that the class Point provides a method called distance that takes one argument, another instance of class Point and returns the distance between the points: public double distance(point p); Method returns distance between this point and Point p. In that case, to get distance between two points, p1 and p2 you can use: double dst = p1.distance(p2); Hint: Be careful when creating an array of objects. Remember that creating an array does not create actually objects inside that array it has to be done manually. public class Perimeter { public static void main(string[] args) { Point[] points = new Point[10]; for (int i = 0; i < point.length; i++) { points[i] = new Point((int)(Math.random() * 11), (int)(math.random() * 11)); double sum = 0; for (int i = 0; i < point.length 1; i++) { sum += points[i].distance(points[i+1]); sum += points[0].distance(points[points.length 1]); System.out.println(sum);