COMP 401 Spring 2013 Midterm 1

Similar documents
COMP 401 Spring 2014 Midterm 1

Final Exam COMP 401 Fall 2014

Final Exam COMP 401 Fall 2014

COMP 401 Spring 2013 Midterm 2

COMP 401 Spring 2013 Midterm 2

COMP 401 Fall 2014 Midterm 2

Introduction to Programming Using Java (98-388)

COMP 401 Fall 2014 Midterm 2

JAVA MOCK TEST JAVA MOCK TEST II

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

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

Java Object Oriented Design. CSC207 Fall 2014

Introduction to Java

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

Prelim 1. CS 2110, March 15, 2016, 5:30 PM Total Question Name True False. Short Answer

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

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

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

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

CS 1316 Exam 1 Summer 2009

ENCAPSULATION AND POLYMORPHISM

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

Programming II (CS300)

Programming II (CS300)

Simple Java Reference

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

Oracle 1Z Java SE 8 Programmer I. Download Full Version :

Sample Examination Paper Programming and Software Development

Chapter 15: Object Oriented Programming

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

Chapter 5 Object-Oriented Programming

Principles of Object Oriented Programming. Lecture 4

CS260 Intro to Java & Android 03.Java Language Basics

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

EECS168 Exam 3 Review

Index COPYRIGHTED MATERIAL

CSC Java Programming, Fall Java Data Types and Control Constructs

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Chapter 4 Defining Classes I

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

CSE 1223: Exam II Autumn 2016

CMSC 132: Object-Oriented Programming II

1 (5pts) 2 (20 pts) 3 (20 pts) 4 (10 pts) 5 (15 pts) 6 (30 pts) Total NAME: CU ID: Recitation instructor/time

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.

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

Midterm Exam CS 251, Intermediate Programming March 12, 2014

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

COP 3330 Final Exam Review

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

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

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

CMSC132 Summer 2018 Midterm 1. Solution

CS 1302 Chapter 9 (Review) Object & Classes

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

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

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

C++ Quick Guide. Advertisements

CS 1331 Exam 1 ANSWER KEY

APCS Semester #1 Final Exam Practice Problems

Encapsula)on, cont d. Polymorphism, Inheritance part 1. COMP 401, Spring 2015 Lecture 7 1/29/2015

CS211 Spring 2005 Prelim 1 March 10, Solutions. Instructions

CSCE 206: Structured Programming in C++

Objects and Classes. Basic OO Principles. Classes in Java. Mark Allen Weiss Copyright 2000

Sri Vidya College of Engineering & Technology

Oracle. Java SE 8 Programmer I. 1z Version: Demo. [ Total Questions: 10] Web:

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

Computer Programming, I. Laboratory Manual. Final Exam Solution

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

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

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

Objectives. Introduce the core C# language features class Main types variables basic input and output operators arrays control constructs comments

1B1b Classes in Java Part I

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

Lecture 2: Java & Javadoc

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

Spring 2013 COMP Midterm Exam Solutions March 07, 2013

More on Inheritance. Interfaces & Abstract Classes

Lecture 7: Classes and Objects CS2301

Distributed Systems Recitation 1. Tamim Jabban

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

Selected Questions from by Nageshwara Rao

Advanced MEIC. (Lesson #18)

Chapter 4: Writing Classes

Notes on Chapter Three

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

Last Name: Circle One: OCW Non-OCW

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

(Not Quite) Minijava

COMP 524 Spring 2018 Midterm Thursday, March 1

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

(800) Toll Free (804) Fax Introduction to Java and Enterprise Java using Eclipse IDE Duration: 5 days

Another IS-A Relationship

CS1004: Intro to CS in Java, Spring 2005

Comp Intermediate Programming EXAM #1 February 16, 2004 Rice University - Instructors: Cox & Nguyen

A final method is a method which cannot be overridden by subclasses. A class that is declared final cannot be inherited.

Java Fundamentals (II)

Topic 7: Algebraic Data Types

Prelim 1. CS 2110, 13 March 2018, 5:30 PM Total Question Name Short answer

Transcription:

COMP 401 Spring 2013 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 12

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 3 points. Selecting all of the choices or selecting none of the choices will result in no points awarded. 1) Which of these are valid built- in Java data types? * a) int b) unsigned long * c) String d) null * e) boolean 2) Which of these are reference types? * a) double[] b) byte * c) String d) char e) boolean 3) Which of these statements about Java arrays is true? a) Arrays are indexed numerically starting at 1. b) The number of elements in an array can change. * c) Each element of an array must be of the same type. * d) The "new" keyword is used to create an array. e) Negative index values access elements from the end of an array. 4) Which of these are legal Java variable names? * a) a_var * b) _b_var c) 1var * d) $var e) while 5) Which of these are NOT legal Java expressions? * a) false && (1-8) b) 4+3*2 c) Math.PI d) "hello" + 3 * e) "hello" * 3 Page 3 of 12

6) Which of these statements about Java statements and blocks are true? * a) A statement must end in a semicolon. b) A statement must result in a value. * c) A single statement can be placed anywhere a block of statements is expected. d) A block of statements is enclosed by parentheses. * e) A block of statements can be empty. 7) Which of these statements are correctly related to the principle of encapsulation? * a) Object state should be maintained in private instance fields. b) An object should provide direct access to its properties. c) Every object property must have both a getter and a setter. d) Java is a strongly- typed language. * e) Setters should validate values if possible. 8) Which of these statements about Java interfaces are true? a) Interfaces define a set of private methods that an object implements. b) Interface names are not valid data types for variables. c) Interfaces provide default implementations for specified methods. * d) Interfaces can be extended using inheritance. * e) Interfaces never specify constructors. 9) Which of these statements about polymorphism is true? * a) Constructor overloading is an example of polymorphism. b) Method overloading requires that different versions of the same method have the same method signature. c) An overloaded method can only invoke another version of the same method if it does so on the first line of code in the body of the method. d) Polymorphism hides object state in order to support abstraction. * e) Polymorphism allows methods to be context- specific. 10) Which of these statements about enumerations is true? * a) Enumerations define a new data type. b) Symbols can be added to an enumeration after it has been declared. c) The symbols of an enumeration can be used as integer values in expressions. * d) Enumeration symbols can be used in a "switch" statement. * e) One advantage of using an enumeration is type safety. Page 4 of 12

11) Which of these statements properly declares a named constant? a) public int A_CONSTANT; b) static public int A_CONSTANT; * c) static final public int A_CONSTANT = 3; * d) static final int A_CONSTANT = 3; e) static final public int A_CONSTANT; 12) Which of these statements about the iterator design pattern are true? * a) An iterable collection should provide a method that creates a new iterator instance. b) The state of an iteration is encapsulated within the iterable collection. c) If two iterators are created for the same iterable collection, advancing one will advance the other. * d) An iterator generally assumes that the underlying collection is not altered while being traversed. e) A single iterator can be used to traverse more than one collection. 13) Which of these statements is true of the String data type? a) String is equivalent to an array of char elements. * b) Strings are immutable. * c) Strings are reference types. d) The most appropriate way to compare to Strings is to use the == operator. e) An empty string (i.e., a string of length 0) is the same as the value null. 14) Which of the following statements is true of objects? * a) Every object is associated with a class. b) Every object is associated with at least one interface. * c) Two object references that have the same value refer to the same object. * d) The state of an object is determined by the current values of its instance fields. e) A new copy of an object is created when it is passed to a method as a parameter. 15) Which of the following are legal method signatures? a) public void int foo() * b) private static int foo() c) double foo(a, b) * d) String[] foo(string a, String b) * e) String foo(int a, String[] b) Page 5 of 12

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 d after the code below executes? Answer: 6 int a[] = new int[] {1, 2, 3; int b[] = new int[] {4, 5, 6; int c[] = new int[] {a[0], b[1], a[2]; b = a; a = c; c = b; int d = a[0] + b[1] + c[2]; 17) What is the value of bar after the code below executes? Answer: 25 int foo = 30; int bar = 15; if (foo < 90) { bar += 10; foo *= 3; else if (foo < 80) { bar += 5; else if (foo > 70) bar -= 5; Page 6 of 12

Questions 18 and 19 use the definitions of the functions foo and bar given below: int foo(string s) { if (s.equals("hello")) { bar(s); return s.length; else { return bar(s).length; String bar(string t) { String s = "good"; if (t.equals("bye")) { return s; else { return s+t; 18) What is the value of the expression foo("hello")? Answer: 5 19) What is the value of the expression foo("bye")? Answer: 4 20) What is the value of sum after the following code executes? Answer: 2 int sum = 0; for (int i=0; i<4; i++) { for (int j=i; j<i*2; j++) { if (j<4) { sum++; else { sum--; Page 7 of 12

Part III: Reading and understanding code (15 points total) Directions: All of the questions in this section concern the code for the Scoreboard class given below (note that the class definition continues on to the next page). This class is intended to be an abstraction for keeping track of the score of a basketball game. In this abstraction, teams can score either 2- point baskets or 3- point baskets that are recorded by using the methods scoretwo and scorethree respectively. Each question is worth 3 points. class Scoreboard { enum Team {HOME, VISITOR; private String home_name; private String visitor_name; private String home_mascot; private String visitor_mascot; private int home_score; private int visitor_score; Scoreboard(String home_name, String home_mascot, String visitor_name, String visitor_mascot) { this.home_name = home_name; this.home_mascot = home_mascot; this.visitor_name = visitor_name; this.vistor_mascot = visitor_mascot; home_score = 0; visitor_score = 0; public String getcurrentleader() { if (home_score > visitor_score) { return home_name; else { return visitor_name; public int gethomescore() { return home_score; Page 8 of 12

public int getvisitorscore() { return visitor_score; public int scoretwo(team who) { if (who == Team.HOME) { home_score += 2; return home_score; else { visitor_score += 2; return visitor_score; public int scorethree(team who) { if (who == Team.HOME) { home_score += 3; return home_score; else { visitor_score += 3; return visitor_score; 21) How many parameters does the Scoreboard constructor accept? 4 22) Which method(s), if any, act as getters following the JavaBeans convention? getcurrentleader, gethomescore, getvisitorscore 23) Which method(s) act as a getter for a derived property? getcurrentleader 24) Which method(s), if any, act as setters following the JavaBean convention? None 25) Is the following statement true or false: A Scoreboard object is immutable (i.e., has no state that changes after creation). False Page 9 of 12

Part IV: Writing Code (20 points total) Directions: In this section, you will create and use an abstraction for the immutable team information (i.e., does not change after creation) encapsulated by the Scoreboard class as a new class called Team. Question 26 is worth 3 points, Question 27 is worth 7 points, and Questions 28 is worth 10 points. 26) Identify which object fields originally part of Scoreboard should be encapsulated by the class Team. home_name, home_mascot, visitor_name, visitor_mascot 27) Write a definition for the class Team that includes an appropriate constructor and getter methods. public class Team { private String name; private String mascot; Team (String name, String mascot) { this.name = name; this.mascot = mascot; public String getname() { return name; public String getmascot() { return mascot; Page 10 of 12

28) Rewrite the Scoreboard class to use the new Team class you developed as the answer for Question 27. You are free to change the implementation of any or all of the existing methods, but some version of the existing methods must still be provided. Furthermore, be sure to include a new constructor that accepts two Team objects and to rewrite the existing constructor to chain from this new constructor. class Scoreboard { private Team home; private Team visitor; private int home_score; private int visitor_score; Scoreboard(Team home, Team visitor) { this.home = home; this.visitor = visitor; Scoreboard(String home_name, String home_mascot, String visitor_name, String visitor_mascot) { this(new Team(home_name, home_mascot), new Team(visitor_name, visitor_mascot)); home_score = 0; visitor_score = 0; public String getcurrentleader() { if (home_score > visitor_score) { return home.getname(); else { return visitor.getname(); public int gethomescore() { return home_score; public int getvisitorscore() { return visitor_score; public int scoretwo(team who) { Page 11 of 12

if (who == home) { home_score += 2; return home_score; else { visitor_score += 2; return visitor_score; public int scorethree(team who) { if (who == home) { home_score += 3; return home_score; else { visitor_score += 3; return visitor_score; Page 12 of 12