Computer Science 145

Similar documents
Computer Science 145 Midterm 1 Fall 2016

Programming II (CS300)

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

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

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

Programming with Java

Using Java Classes Fall 2018 Margaret Reid-Miller

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

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

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

Programming II (CS300)

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

Computational Expression

חומר עזר לבחינה מבוא למדעי המחשב

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

CS 307 Final Spring 2008

Computer Science 252 Problem Solving with Java The College of Saint Rose Spring Topic Notes: Java and ObjectDraw Reference

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

CS111: PROGRAMMING LANGUAGE II

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

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

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

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

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

CS 101 Exam 1 Spring 200 Id Name

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

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

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

Java Classes: Math, Integer A C S L E C T U R E 8

Array Based Lists. Collections

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

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

Lists. Chapter 12. Copyright 2012 by Pearson Education, Inc. All rights reserved

Use of the ArrayList class

CS201 ArrayLists, Generics, and Dynamic Data Structures (Chapters 14, 15)

Lab & Assignment 1. Lecture 3: ArrayList & Standard Java Graphics. Random Number Generator. Read Lab & Assignment Before Lab Wednesday!

Classes and Objects Part 1

חומר עזר לבחינה במבוא למדעי המחשב // Indicates whether some other object is "equal to" // this one. boolean equals(object other)

Primitive Data Types: Intro

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

Introduction to Computer Science Unit 2. Exercises

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Chapter 3: Using Classes and Objects

+! Today. Lecture 3: ArrayList & Standard Java Graphics 1/26/14! n Reading. n Objectives. n Reminders. n Standard Java Graphics (on course webpage)

CSC 222: Object-Oriented Programming. Fall 2017

Full file at

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

APS101- Midterm Sample Questions

Introduction to Object-Oriented Programming

Department of Computer Science Purdue University, West Lafayette

CSE 1223: Introduction to Computer Programming in Java Chapter 6 ArrayLists

EXAMINATIONS 2011 Trimester 2, MID-TERM TEST. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

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

CS1083 Week 2: Arrays, ArrayList

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

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

Peer Instruction 1. Elementary Programming

Page 1 of 16. Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Unit 4: Classes and Objects Notes

Advanced if/else & Cumulative Sum

CS 455 Midterm Exam 2 Fall 2016 [Bono] November 8, 2016

ENGR 2710U Midterm Exam UOIT SOLUTION SHEET

Data abstractions: ADTs Invariants, Abstraction function. Lecture 4: OOP, autumn 2003

CS 312 Final Fall 2016

CIS 110 Introduction To Computer Programming. November 21st, 2011 Exam 2

Lecture 5: Methods CS2301

CS 211: Existing Classes in the Java Library

Building Java Programs

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

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

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

CMSC 433 Section 0101 Fall 2012 Midterm Exam #1

Sample Examination Paper Programming and Software Development

AP Computer Science A

EXAMINATIONS 2017 TRIMESTER 2

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

CIS 110 Introduction To Computer Programming. December 19th, 2011 Final. Answer key

MIDTERM REVIEW. midterminformation.htm

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

You must bring your ID to the exam.

EXAMINATIONS 2016 TRIMESTER 2

CS 307 Midterm 2 Fall 2009

Lecture Set 2: Starting Java

Building Java Programs Sample Final Exam #1. Name of Student. Section (e.g., AA)

TeenCoder : Java Programming (ISBN )

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

Lecture Set 2: Starting Java

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

Sample Midterm Exam #2

Arrays and ArrayLists. David Greenstein Monta Vista High School

Java Programming. MSc Induction Tutorials Stefan Stafrace PhD Student Department of Computing

WARNING for Autumn 2004:

CS106X Handout 17 Winter 2015 January 28 th, 2015 CS106X Practice Exam

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

Java Review. Fundamentals of Computer Science

Here is how we use an arraylist. To access the arraylist code we can import the class via:

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

Final Exam Practice. Partial credit will be awarded.

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

Computational Expression

Transcription:

Name: Computer Science 145 Final Exam Answer Sheet Fall 2016 1. a b c d 8. 2. a b c d 9. a b c d 3. a b c d e 10. a b c d 4. a b c d 11. 5. a b c d 12. 6. 13. 7. 14. a b c d 15. 16. 17. 1

18. 19. 2

20. 3

21. 4

Computer Science 145 Final Exam Fall 2016 Doodle here. This is a closed-book, no-calculator, no-electronic-devices, individual-effort exam. You may reference one page of handwritten notes. All answers should be clearly written. Questions that require code should be written using correct Java syntax. You may write SOP to represent System.out.println. Class Method/Constructor Description Scanner Scanner(System.in) create Scanner for parsing System.in Scanner(String text) create Scanner for parsing text String next() get next delimited word double nextdouble() get next delimited double boolean nextboolean() get next delimited boolean int nextint() get next delimited integer String int length() get number of characters char charat(int i) get the character at index i String touppercase() get a String like this one, but in all-caps int indexof(char c) find the index of the first c String substring(int a) get substring from index a to String s end String substring(int a, int b) get substring from index a to before index b Math int max(int a, int b) get the maximum of a and b. int min(int a, int b) get the minimum of a and b. double pow(double base, double exponent) raise base to the exponent power. Random Random() create a random number generator. nextint(int i) get random number in [0, i 1]. nextdouble() get random number in [0.0, 1.0). File File(String path) create a File object for the given path boolean exists() returns true if path refers to an existing file ArrayList<T> ArrayList<T>() create empty list for holding T references boolean add(t e) adds e to end of list; always returns true void set(int i, T e) replaces element i with e void clear() removes all elements T remove(int i) removes and returns element i int indexof(t e) finds index of first element matching e T get(int i) gets the item at index i int size() gets the number of items in the list 5

1. Which of the following statements are true of instance variables? Check zero or more. (a) They must be marked static. (b) They should be given their initial assignments by the class s constructor. (c) They should be marked private. (d) They can only be accessed by non-static methods. 2. Which of the following classes contain valid constructors? Check zero or more. (a) class Monster { private int hitpoints; public Monster(int hitpoints) { return new Monster(hitPoints); (b) class State { private String name; public void State(String name) { this.name = name; (c) class Polycephalus { private int nheads; public Polycephalus(int nheads) { this.nheads = nheads; (d) class Number { private double number; public Integer(int i) { number = i; 3. Which of the following methods of the class ArrayList are getters/accessors? Check zero or more. (a) size (b) contains (c) isempty (d) indexof (e) remove 4. Which of the following methods of the class ArrayList are setters/mutators? Check zero or more. (a) set (b) clear (c) get (d) add 6

5. Which of the following statements are true ArrayList? Check zero or more. (a) The length property of a list s backing array yields the same value as the list s size method. (b) A list may allocate many arrays over the list s lifetime. (c) ArrayList is an object. (d) The ArrayList class encapsulates algorithms for adding and removing elements, operations which are not directly supported by arrays. 6. Provide an example of a multi-word class name that follows Java case conventions. 7. Provide an example of a multi-word method name that follows Java case conventions. 8. Provide an example of a multi-word variable name that follows Java case conventions. 9. Which of the following assignments make the expression a!b evaluate to true? Check zero or more. (a) a = false, b = false (b) a = true, b = false (c) a = false, b = true (d) a = true, b = true 10. Which of the following assignments make the expression a && b &&!a evaluate to true? Check zero or more. (a) a = false, b = false (b) a = true, b = false (c) a = false, b = true (d) a = true, b = true 11. Write an expression that evaluates to (not prints, not returns) the number of characters of a String named text. 12. Write an expression that evaluates to (not prints, not returns) the number of elements in array nums. 13. Write an expression that evaluates to (not prints, not returns) the number of elements in an ArrayList named list. 14. Which of the following are benefits of object-oriented programming? Check zero or more. (a) Objects help organize our code, bundling code together with the data it acts upon. (b) Objects allow us to model the problem we are trying to solve at a high level. (c) Objects hide complexity. (d) Objects are a means of adding new, custom types to our programs. 15. What are the values of array r after the following code is executed? String[] r = {"o", "", "", ""; for (int i = 1; i < r.length; ++i) { r[i] = r[i - 1] + r[i - 1]; 7

16. What are the values of array r after the following code is executed? int[] r = {1, 5, 3, 6, 10, 2; for (int i = 0; i < r.length; ++i) { r[i] += i % 3; 17. What are the values of array dst after the following code is executed? boolean[] src = {true, false, true, true, false, false, false, true; boolean[] dst = new boolean[src.length]; for (int i = 1; i < dst.length - 1; ++i) { dst[i] = src[i - 1] src[i] src[i + 1]; 18. Write method add that accepts two arrays of ints as parameters. Assume the arrays have the same number of elements. It returns back a brand new int array. Each element in the returned array is the sum of the corresponding elements of the parameter arrays. For example, add(new int[] {1, 3, new int[] {10, 20) {11, 23. 19. Write static method hastwice that accepts two parameters: an ArrayList of Strings and a String to search for. It returns true if and only if the parameter String appears twice in the list. 20. Write a class Banner, which outlaws certain words, with the following: A constructor that sets up the Banner to report all Strings as okay not banned. Method ban that accepts a String parameter. It bans the String such that calling isokay on the parameter String will report false. Method isokay that accepts a String parameter and returns true if the parameter has not been banned and false otherwise. 21. Write class Rover, which roves across a 2D Cartesian grid, with the following: A constructor that accepts the Rovers initial x- and y-coordinates (as ints). Methods n, e, s, and w, which move the Rover one unit north, east, south, or west. For example, n moves the Rover one unit along the positive y-axis, while e moves the Rover one unit along the positive x-axis. Methods getx and gety that return the Rover s current coordinates. Method reset which returns the Rover to its starting location. 8