COMP 401 Spring 2014 Midterm 1

Similar documents
COMP 401 Spring 2013 Midterm 1

Final Exam COMP 401 Fall 2014

Final Exam COMP 401 Fall 2014

Introduction to Programming Using Java (98-388)

COMP 401 Spring 2013 Midterm 2

COMP 401 Spring 2013 Midterm 2

COMP 401 Fall 2014 Midterm 2

COMP 401 Fall 2014 Midterm 2

Midterm Exam CS 251, Intermediate Programming March 12, 2014

CSCI 136 Written Exam #0 Fundamentals of Computer Science II Spring 2015

Decision Making in C

CSCI 102L - Data Structures Midterm Exam #1 Fall 2011

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Index COPYRIGHTED MATERIAL

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

FORM 2 (Please put your name and form # on the scantron!!!!)

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Java Object Oriented Design. CSC207 Fall 2014

Object Oriented Programming

Logistics. Final Exam on Friday at 3pm in CHEM 102

Simple Java Reference

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Ticket Machine Project(s)

Chapter 4 Defining Classes I

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Midterm I - CSE11 Fall 2013 CLOSED BOOK, CLOSED NOTES 50 minutes, 100 points Total.

Object Oriented Programming

Recitation 3 Class and Objects

Comp Assignment 2: Object-Oriented Scanning for Numbers, Words, and Quoted Strings

CMSC132 Summer 2018 Midterm 1. Solution

Type Hierarchy. Comp-303 : Programming Techniques Lecture 9. Alexandre Denault Computer Science McGill University Winter 2004

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

Notes on Chapter Three

APCS Semester #1 Final Exam Practice Problems

ENCAPSULATION AND POLYMORPHISM

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

Sample Examination Paper Programming and Software Development

Compiler Errors. Flash CS4 Professional ActionScript 3.0 Language Reference. 1 of 18 9/6/2010 9:40 PM

QUESTIONS FOR AVERAGE BLOOMERS

1 Lexical Considerations

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

EECS168 Exam 3 Review

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

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CMSC 331 Second Midterm Exam

CSC Java Programming, Fall Java Data Types and Control Constructs

The Warhol Language Reference Manual

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

CS260 Intro to Java & Android 03.Java Language Basics

Computer Programming, I. Laboratory Manual. Final Exam Solution

JAVA MOCK TEST JAVA MOCK TEST II

Exam 1 - (20 points)

Generating/Updating code from whole project

SCHOOL OF COMPUTING, ENGINEERING AND MATHEMATICS SEMESTER 1 EXAMINATIONS 2015/2016 CI101 / CI177. Programming

Lesson 06 Arrays. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

CSE 131 Introduction to Computer Science Fall Exam II

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Lecture 2: Java & Javadoc

CMSC 132: Object-Oriented Programming II

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

UMBC CMSC 331 Final Exam

Java Review. Fundamentals of Computer Science

University of Massachusetts Amherst, Electrical and Computer Engineering

1Z Java SE 5 and 6, Certified Associate Exam Summary Syllabus Questions

Absolute C++ Walter Savitch

Everything You Ever Wanted To Know About Java O-O. 7-Dec-15

Spring 2013 COMP Midterm Exam Solutions March 07, 2013

CS 113 MIDTERM EXAM 2 SPRING 2013

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

class objects instances Fields Constructors Methods static

Final exam. Final exam will be 12 problems, drop any 2. Cumulative up to and including week 14 (emphasis on weeks 9-14: classes & pointers)

Week 6: Review. Java is Case Sensitive

CS 139 Practice Midterm Questions #2

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

Lab5. Wooseok Kim

Java for Non Majors. Final Study Guide. April 26, You will have an opportunity to earn 20 extra credit points.

BASIC ELEMENTS OF A COMPUTER PROGRAM

MELODY. Language Reference Manual. Music Programming Language

SELECTION. (Chapter 2)

There are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals:

CMSC132 Summer 2018 Midterm 1

Introduction to Java

Programming II (CS300)

Chapter 15: Object Oriented Programming

SOFT 161. Class Meeting 1.6

Problem Solving with C++

CS 1331 Exam 1. Fall Failure to properly fill in the information on this page will result in a deduction of up to 5 points from your exam score.

Discover how to get up and running with the Java Development Environment and with the Eclipse IDE to create Java programs.

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++

Object Class. EX: LightSwitch Class. Basic Class Concepts: Parts. CS257 Computer Science II Kevin Sahr, PhD. Lecture 5: Writing Object Classes

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

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

The Java language has a wide variety of modifiers, including the following:

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

CSCE 206: Structured Programming in C++

11. Arrays. For example, an array containing 5 integer values of type int called foo could be represented as:

Transcription:

COMP 401 Spring 2014 Midterm 1 I have not received nor given any unauthorized assistance in completing this exam. Signature: Name: PID: Please be sure to put your PID at the top of each page.

This page left intentionally blank. Do not write on it. Page 2 of 11

Part I: Multiple choice (45 points total) Directions: For each multiple choice question below, indicate all correct answers among the choices provided. More than one answer may be correct and at least one answer will be correct. Each question is worth 5 points. Selecting all of the choices or selecting none of the choices will result in no points awarded. 1) Which of these are NOT a value type? a) int b) null c) boolean d) String e) double[] 2) Which of these statements are true of an expression? a) A string is not an expression. b) An expression always results in a value. c) A literal interger is a valid expression. d) Expressions can not be combined using mathematical operators. e) A void method call can be part of an expression. 3) Which of these statements are true of loops? a) A for loop can always be rewritten as a while loop. b) The keyword end can be used to immediately exit the loop body. c) A loop cannot contain another loop. d) The body of a loop will execute at least once. e) A switch statement is a type of loop. 4) Which of the following are true of methods? a) The method signature indicates the return type of the method. b) Every statement in the method body will be executed. c) A return statement in a method ends execution of the method. d) A method can not call itself or cause itself to be called. e) A program is always executing within the context of some method. 5) Which of the following are true of arrays? a) The length of an array is available as a publicly accessible instance field. b) The index of the first item in an array is 1. c) An array can grow or shrink dynamically. d) An array is a reference type. e) Every element of an uninitialized array of a reference type is null. Page 3 of 11

6) Which of these statements about strings are true? a) Once created, a string can not change. b) The length of a string is available as a publicly accessible instance field. c) The == operator should be used to test the character- by- character equality of two strings. d) String is a built- in value type. e) A string literal is enclosed in double quotes. 7) Which of these statements are correctly related to the principle of encapsulation? a) Object fields should be marked as public. b) Encapsulation hides the details of how an abstraction is implemented. c) Interfaces should be used to define the publicly exposed behavior of an abstraction. d) Within a class, access to an object's fields must occur through getters and setters. e) Getters should validate their values if possible. 8) Which of these statements about Java interfaces are true? a) Interfaces can only specify public methods. b) An overloaded method can not be specified as part of an interface. c) An interface can by implemented by more than one class. d) A class can implement more than one interface. e) An interface can define an enumeration. 9) Which of these statements about method overloading is true? a) The return type of an overloaded method must remain the same. b) The access modifier of an overloaded method must remain the same. c) The number of parameters in an overloaded method must remain the same. d) The type of parameters in an overloaded method must remain the same. e) If one version of an overloaded method calls another version of the method, it must do so in the first statement of the method body. 10) Which of these statements about the iterator design pattern is true? a) An iterator will typically use a for or while loop internally. b) A collection is iterable if it provides a method that produces an iterator over its elements. c) The state of an iteration is encapsulated within the iterator. d) Multiple iterators can operate on a single iterable collection independently. e) Two iterators for the same iterable collection must produce the elements of the collection in the same order. Page 4 of 11

11) Which of these statements about interface extension is true? a) A class that implements an extended interface must also implement all of the methods declared in the parent interface. b) An extended interface may not overload methods already defined in its parent interface. c) An extended interface can only have a single parent interface. d) Extending an interface is an example of inheritance. e) Interface extension is most appropriate when the methods of the extended interface are independently meaningful with respect to the methods of the parent interface. 12) Which of these are valid variable names in Java? a) 1direction b) a_var c) break d) AVar_ e) more$moreproblems 13) Which of these statements about Java syntax is true? a) An expression can span multiple lines. b) A statement must end in a semicolon. c) A block of statements is enclosed by square braces. d) A.java file can be part of multiple packages. e) A variable first declared within a statement block is only accessible within that statement block. 14) Which of the following statements is true of classes? a) A class must include at least one constructor that accepts parameters. b) The this keyword is not meaningful within the body of a class method. c) A public class field can be accessed using the class name. d) The class name acts as a type name for objects of that class. e) Two classes in the same package may be given the same name as long as they define different instance fields. 15) Which of the following are legal method signatures? a) public void foo(int a) b) private static foo() c) double foo(int a, String b) d) int foo(a, b) e) public String[] foo(string foo) Page 5 of 11

Part II: Short answer / calculation (25 points total) Directions: Calculate the value of the variable or expression indicated. Each question is worth 5 points. 16) What is the value of the variable result after the code below executes? Answer: int[] a = new int[] {10, 20, 30; int[] b = new int[] {2, 1, 0; int[] c = a; int[] d = b; c[0] = d[2]; a[1] = c[d[b[2]]]; int result = a[0] + a[1] + a[2]; 17) What is the value of x after the code below executes? Answer: int x = 3; int y = 1; switch(x-y) { case 0: x += y; break; case 2: y += 3; case 4: x += y; break; case 6: x += 3; default: x = 0; y = 0; Page 6 of 11

Questions 18 and 19 use the definition of the function foo given below: String foo(string[] sa) { String s = ""; for (int i=0; i<sa.length; i++) { s += sa[i]; sa[i] = s; return s; Given the above definition for foo, assume the following two lines of code are executed: String[] my_strings = new String[] {"A", "B", "C"; String foo_result = foo(my_strings); 18) What is the value of foo_result? Answer: 19) What is the value of my_strings[1]? Answer: 20) What is the value of sum after the following code executes? Answer: int sum = 0; for (int i=0; i<=4; i++) { for (int j=i; j>0; j--) { if (i%2 == 0) { sum += j; else { sum -= j; Page 7 of 11

Part III: Reading and understanding code (15 points total) Directions: All of the questions in this section concern the code for the Invoice class given below. This class is intended to be an abstraction for keeping track of an invoice (i.e., a bill) of items. Each item is associated with a name and a price. The invoice as a whole associated with a tax rate, provided to the constructor when an Invoice object is created. Each question is worth 3 points. import java.util.list; import java.util.arraylist; public class Invoice { private double tax_rate; private List<String> item_names; private List<Double> item_prices; public Invoice(double tax_rate) { this.tax_rate = tax_rate; item_names = new ArrayList<String>(); item_prices = new ArrayList<Double>(); public void additem(string name, double price) { item_names.add(name); item_prices.add(price); public String finditemname(int index) { return item_names.get(index); public double finditemprice(int index) { return item_prices.get(index); public double gettotal() { double sum = 0.0; for (int i=0; i<item_names.size(); i++) { sum += item_prices.get(i); return sum; public double gettax() { return gettotal() * tax_rate; Page 8 of 11

21) How many instance fields does Invoice declare? 22) Which method(s), if any, act as getters following the JavaBeans convention? 23) Which method(s) act as a getter for a derived property? 24) Which method(s), if any, act as setters following the JavaBean convention? 25) Is the following statement true or false: The instance field tax_rate is immutable. Page 9 of 11

Part IV: Writing Code (20 points total) Directions: In this section, you will create and use an abstraction for the item information (i.e., name and price) used by the Invoice class as a new class called Item. Question 26 is worth 5 points, and Questions 27 is worth 15 points. 26) Write a definition for the class Item that includes an appropriate constructor and getter methods. Item objects should be immutable. Page 10 of 11

27) Rewrite the Invoice class so that it uses your new Item class and implements the following InvoiceInterface interface. In this new version, in addition to the existing constructor, add a second constructor that takes no parameters and creates a new Invoice object with a tax rate of 0.15. Extra credit for changing gettotal() to use a for- each loop to iterate over the items. public interface InvoiceInterface { void additem(item item); void additem(string name, double price); Item finditem(int index); double gettotal(); double gettax(); Page 11 of 11