int[] a; a = new int[8]; a[0] = 1; a[1] = 1; for (int i = 2; i < 8; i++) a[i] = a[i-1] - a[i-2];

Similar documents
int b = 2; for (int a = 0; a < 10 b < 20; a++) { a = a + b; b = b + a; }

int a; int b = 3; for (a = 0; a < 8 b < 20; a++) {a = a + b; b = b + a;}

SEMESTER 1, 2011 EXAMINATIONS. CITS1200 Java Programming FAMILY NAME: GIVEN NAMES:

THE UNIVERSITY OF WESTERN AUSTRALIA. School of Computer Science & Software Engineering CITS1001 OBJECT-ORIENTED PROGRAMMING AND SOFTWARE ENGINEERING

THE UNIVERSITY OF WESTERN AUSTRALIA. School of Computer Science & Software Engineering CITS1001 OBJECT-ORIENTED PROGRAMMING AND SOFTWARE ENGINEERING

VARIABLES AND TYPES CITS1001

Object-oriented Programming and Software Engineering CITS1001. Multiple-choice Mid-semester Test

Practice Midterm 1. Problem Points Score TOTAL 50

ITI Introduction to Computing II

ITI Introduction to Computing II

Classes. Classes. Classes. Class Circle with methods. Class Circle with fields. Classes and Objects in Java. Introduce to classes and objects in Java.

Solution Notes. COMP 151: Terms Test

Computer Science and Software Engineering SEMESTER 1, 2016 EXAMINATIONS. CITS1001 Object-oriented Programming and Software Engineering

Practice Midterm 1 Answer Key

index.pdf January 21,

CS 177 Week 15 Recitation Slides. Review

Making New instances of Classes

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

EXPRESSIONS AND ASSIGNMENT CITS1001

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Introduction to Programming Using Java (98-388)

Simple Java Reference

Scope of this lecture. Repetition For loops While loops

OOP: Key Concepts 09/28/2001 1

ASSIGNMENT 2. COMP-202A, Fall 2013, All Sections. Due: October 20 th, 2013 (23:59)

Exam 1 - (20 points)

MORE ARRAYS: THE GAME OF LIFE CITS1001

AP CS Fall Semester Final

Lecture 10 Declarations and Scope

Software Design and Analysis for Engineers

COMPUTER APPLICATIONS

BASIC ELEMENTS OF A COMPUTER PROGRAM

Bits and Bytes. How do computers compute?

An introduction to Java II

Creating and Using Objects

Chapter 8 Objects and Classes Dr. Essam Halim Date: Page 1

ITEC1620 Object-Based Programming. Lecture 13

Declarations and Access Control SCJP tips

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

CSE 143 SAMPLE MIDTERM

2. The object-oriented paradigm!

Lab 9: Creating a Reusable Class

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

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

Array. Prepared By - Rifat Shahriyar

Java Review: Objects

CS 61B Discussion Quiz 1. Questions

COMP 102: Test August, 2017

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

A Foundation for Programming

CS/ENGRD 2110 FALL Lecture 7: Interfaces and Abstract Classes

Principles of Computer Science

Computer Science E-119 Fall Problem Set 1. Due before lecture on Wednesday, September 26

Lesson 15..Classes and Objects

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

Practice Questions for Chapter 9

Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B

CS112 Lecture: Defining Instantiable Classes

Ticket Machine Project(s)

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

Introduction to Typed Racket. The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples

STUDENT LESSON A5 Designing and Using Classes

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

Inf1-OOP. Data Types. Defining Data Types in Java. type value set operations. Overview. Circle Class. Creating Data Types 1.

OBJECT ORİENTATİON ENCAPSULATİON

Midterm 1 Written Exam

Chapter 4 Defining Classes I

5.12 EXERCISES Exercises 263

Java Object Oriented Design. CSC207 Fall 2014

Name CIS 201 Midterm II: Chapters 1-8

Arrays. myints = new int[15];

CSCI 135 Midterm Fundamentals of Computer Science I Fall 2011

Chapter 4. Defining Classes I

CS2401 QUIZ 5 February 26, questions / 20 points / 20 minutes NAME:..

Chapter 6 Introduction to Defining Classes

CSE 142 Su 04 Computer Programming 1 - Java. Objects

Object-Oriented Programming and Software Engineering CITS1001 MID-SEMESTER TEST

Defensive Programming

Name: Tutor s

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Spring 2010

CIS 110 Introduction to Computer Programming Summer 2018 Final. Recitation # (e.g., 201):

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario

Object Oriented Modeling

COMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed

CS112 Lecture: Working with Numbers

1st Semester Examinations CITS1001 3

Selections. EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG

Topic 7: Algebraic Data Types

CSCI-1200 Data Structures Fall 2017 Lecture 25 C++ Inheritance and Polymorphism

CSCI 2101 Java Style Guide

Assertions, pre/postconditions

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

CSCI 136 Programming Exam #2 Fundamentals of Computer Science II Spring 2012

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

School of Computer Science & Software Engineering SEMESTER 2, 2008 EXAMINATIONS 2008 JAVA PROGRAMMING 1200 (CITS1200)

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

Basic Object-Oriented Concepts. 5-Oct-17

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Principles of Computer Science

APCS Semester #1 Final Exam Practice Problems

Transcription:

1. The source code for a class (a) must explicitly define exactly one constructor. (b) can explicitly define zero, one or more constructors. (c) must explicitly define at least one constructor. (d) can explicitly define at most one constructor. (e) should never contain any explicitly defined constructors. 2. What is the value of a[7] after the following statements? int[] a; a = new int[8]; a[0] = 1; a[1] = 1; for (int i = 2; i < 8; i++) a[i] = a[i-1] - a[i-2]; (a) -2 (b) -1 (c) 0 (d) 1 (e) 2 3. The variable int runtime contains the running time of a movie in minutes. You wish to convert it to the normal hours-and-minutes notation (e.g. 133 minutes = 2 hours and 13 minutes). Which piece of code is correct? (a) int hours = runtime/60; int minutes = (runtime - hours*60)/60; (b) int hours = runtime%60; int minutes = runtime; (c) int minutes = runtime%60; int hours = runtime/60; (d) int minutes = runtime%60; int hours = runtime-60*minutes; (e) int hours = runtime-60; int minutes = runtime/60; 1

4. A method in the class BankAccount (defined as in the lectures) returns true if and only if the target object has a strictly higher balance than the argument object (strictly means that the method should return false if the balances are equal). Which of these definitions work correctly? (All of them are legal Java.) // Method 1 public boolean higherbalance(bankaccount other) { return balance > other.balance; // Method 2 public boolean higherbalance(bankaccount other) { if (getbalance() > other.getbalance()) return this; else return other; // Method 3 public boolean higherbalance(bankaccount other) { if (balance <= other.getbalance()) return false; else return true; (a) All three (b) The 3rd only (c) The 1st and 2nd only (d) The 1st and 3rd only (e) The 2nd and 3rd only 2

5. Consider this class with a single method. public class Doubler { public void doubleit(int[] a) { a[0] = 2*a[0]; a = null; What happens when the class is compiled, and the following sequence of statements (which is in a method of another class) is compiled and executed? int[] x = {5,6,7; Doubler d = new Doubler(); d.doubleit(x); System.out.println(x[0]); (a) The code for Doubler doesn t compile, because the class has no constructors. (b) The code for Doubler doesn t compile, because a parameter variable is read-only and cannot be assigned a value. (c) The program crashes with a NullPointerException. (d) The value 5 is printed in the terminal window. (e) The value 10 is printed in the terminal window. 3

6. Assume BankAccount is defined as in the lectures, and consider the following sequence of statements. BankAccount b1 = new BankAccount("gfr",1,0); BankAccount b2 = new BankAccount("rlw",2,0); BankAccount temp = b1; b1.deposit(1000); b2.deposit(2000); temp.withdraw(500); temp = b2; temp.deposit(1000); What are the final balances of the accounts owned by gfr and rlw, respectively? (a) 1000 and 2000 (b) 500 and 3000 (c) 1000 and 3000 (d) 500 and 2500 (e) 1500 and 2000 7. One approximation of π uses the equation 1 4 π 2 = 1 + 1 3 1 5 1 7 + 1 9 + 1 11... Which of these methods correctly calculate the approximation of π to a specified number of terms? (All of them are legal Java.) // Method 1 public double pi(int numterms) { double approx = 0; for (int i=0; i < numterms; i++) { double term = 1/(double)(2*i+1); if(i%4==0 i%4==1) approx += term; else approx -= term; return approx*4/math.sqrt(2); 4

// Method 2 public double pi(int numterms) { double approx = 0; int mult = 1; for (int i=0; i < numterms; i=i+2) { double term1 = 1d/(2*i+1); double term2 = 1d/(2*(i+1)+1); approx += mult*(term1 + term2); mult = -mult; return approx*4/math.sqrt(2); // Method 3 public double pi(int numterms) { double approx = 0; for (int i=0; i < numterms; i++) { double term = 1d/(2*i+1); switch(i%4) { case 0: case 1: approx += term; break; case 2: case 3: approx -= term; break; default: System.out.println("ERROR"); return approx*4/math.sqrt(2); (a) Method 1 only (b) Method 2 only (c) Method 3 only (d) Methods 1 and 3 only (e) All three 5

8. What does mystery(9870) return? public int mystery(int n) { int m = 0; while (n > 0) { m = 10*m + n%10; n = n/10; return m; (a) 987 (b) 0 (c) 7890 (d) 789 (e) 9870 9. What are the values of a and b after the following loop? int a = 1; int b = 0; for (a = 0; b < 10 a < 20; a = a + 2) { a = a + b; b = b + a + 1; (a) a is 54, and b is 53 (b) a is 10, and b is 16 (c) a is 28, and b is 45 (d) a is 12, and b is 16 (e) a is 30, and b is 45 6

10. What are the values of these three expressions, respectively? 12 / 8 * 3 12 / 8d * 3 12 / 8 * (double)3 (a) 4, 4.5, and 3.0 (b) 4.5, 4.5, and 4.5 (c) 3, 3.0, and 3.0 (d) 3.0, 3.0, and 4.5 (e) 3, 4.5, and 3.0 11. Which expression evaluates to true whenever a has the value true? (All of the expressions are legal Java.) (a) a &&!b (b) a &&!a (c) a == b!b (d) 0.15 == 0.2-0.05 == a (e) a ==!!false 12. This declaration: Java.awt.Color w; (a) creates a variable w with initial value null. (b) creates a variable w referring to a newly created java.awt.color object, which by default represents the colour black. (c) creates a variable w with initial value 0. (d) creates a variable w with an unspecified initial value. (e) causes a syntax error, as it is not permitted to declare a variable of reference type without creating the associated object. 7

13. What does mysteryflag() draw on the screen? public void mysteryflag() { int z = 300; SimpleCanvas sc = new SimpleCanvas("What am I?",z,z); sc.setforegroundcolour(new java.awt.color(0,255,0)); for (int i = 0; i < z; i++) sc.drawline(0,0,i,z-i); sc.setforegroundcolour(new java.awt.color(0,0,255)); for (int i = 0; i < z; i++) sc.drawline(i,z-i,z,z); (a) A square split horizontally into two coloured rectangles. (b) A square split vertically into two coloured rectangles. (c) A square split diagonally into two coloured triangles, one in the top-left half and one in the bottom-right half. (d) A square split diagonally into two coloured triangles, one in the top-right half and one in the bottom-left half. (e) Nothing. 14. What is the value of red == xxx after these two statements, and why? java.awt.color red = new java.awt.color(255,0,0); java.awt.color xxx = new java.awt.color(255,0,0); (a) false: they are references to different objects, even though the objects contain identical data. (b) true: they both represent the colour red. (c) A reference with the same value as xxx: an assignment expression has the same value as the expression on the right hand side of the assignment. (d) We cannot tell: it depends on details of the implementation that are hidden from the users. (e) A runtime error occurs: == can only be used for primitive types. 8

The next six questions refer to the following source code, which defines a class that represents circles. public class Circle { // Represents a circle with its centre at (centrex, centrey) // and with the specified radius private double centrex; private double centrey; private double radius; public Circle(double x, double y, double r) { centrex = x; centrey = y; radius = r; public Circle(double r) {this(0,0,r); public Circle() {this(1d); // area() returns the area of the circle public double area() {return Math.PI * radius * radius; // scale() enlarges/shrinks the circle public void scale(double factor) {radius = radius * factor; // bigger() returns the circle with the larger area public Circle bigger(circle other) {if (this.area() > other.area()) return this; else return other; // duplicate() returns a circle identical to this one public Circle duplicate() {return new Circle(centreX, centrey, radius); 9

// the mystery method does something unclear public boolean mystery(double x, double y) { double z = (x-centrex)*(x-centrex); z = z + (y-centrey)*(y-centrey); return z > radius*radius; 15. What are the instance variables of Circle? (a) x, y, and r (b) area, scale, bigger, duplicate, and mystery (c) centrex, centrey, and radius (d) z (e) x, y, r, factor, and other 16. Which of these statements correctly construct a Circle? 1. Circle c = Circle(5); 2. Circle c = new Circle(1,2,1); 3. Circle c = new Circle(); 4. Circle c = Circle(1,2,1).new(); (a) 1, 2, and 3 only (b) 2 only (c) 3 only (d) 4 only (e) 2 and 3 only 10

17. During the following sequence of statements, how many Circle objects are constructed? Circle c1; Circle c2; Circle c3; Circle c4; c1 = new Circle(1); c2 = c1; c3 = c1.duplicate(); c4 = c3.bigger(c1); (a) 1 (b) 2 (c) 3 (d) 4 (e) 5 18. What does the mystery method do? (a) It returns the distance between the circle and the point (x,y). (b) It tests if (x,y) is outside the circle. (c) It tests if the area of the circle is greater than the length of the line joining (0,0) to (x,y). (d) It returns the radius of the smallest circle containing both (x,y) and (centrex,centrey). (e) It tests if (x,y) is exactly on the edge of the circle. 11

19. What value is stored in c_area after the following code (rounded to six decimal places)? Circle c = new Circle(2); c.scale(2); double c_area = c.area(); (a) 3.141593 (b) 4.000000 (c) 16.000000 (d) 12.566371 (e) 50.265482 20. If c1 is a circle with centre (2, 2) and radius 1, and c2 is a circle with centre (1, 1) and radius 2, what is the value of c1.bigger(c2) (a) An object reference equal to c2. (b) A reference to a new Circle object representing a circle with centre (2, 2) and radius 1. (c) A reference to a new Circle object representing a circle with centre (2, 2) and radius 2. (d) An object reference equal to c1. (e) A reference to a new Circle object representing a circle with centre (1, 1) and radius 2. 12

Blank Page for Rough Work 13

Blank Page for Rough Work 14

Blank Page for Rough Work 15