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

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

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];

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

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

VARIABLES AND TYPES CITS1001

Object Oriented Programming in C#

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

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

A Foundation for Programming

CS 177 Week 15 Recitation Slides. Review

ITI Introduction to Computing II

ITI Introduction to Computing II

Final Exam. COMP Summer I June 26, points

Object-Oriented Programming Concepts

COS 126 General Computer Science Fall Written Exam 1

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

Topics. Java arrays. Definition. Data Structures and Information Systems Part 1: Data Structures. Lecture 3: Arrays (1)

Introduction to Programming Using Java (98-388)

COS 126 General Computer Science Spring Written Exam 1

CLASSES AND OBJECTS. Fundamentals of Computer Science I

Objectives. Introduce static keyword examine syntax describe common uses

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

Objects and Classes Lecture 2

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

Common Misunderstandings from Exam 1 Material

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

JAVA GUI PROGRAMMING REVISION TOUR III

COS 126 Midterm 1 Written Exam Fall 2012

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

All classes in a package can be imported by using only one import statement. If the postcondition of a method is not met, blame its implementer

Exam 1 - (20 points)

CS18000: Problem Solving And Object-Oriented Programming

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

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

Array. Prepared By - Rifat Shahriyar

Lecture 9: Arrays. Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp. Copyright (c) Pearson All rights reserved.

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

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

Arrays. myints = new int[15];

Declarations and Access Control SCJP tips

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

Topic 7: Algebraic Data Types

COS 226 Midterm Exam, Spring 2009

Simple Java Reference

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

MACS 261J Final Exam. Question: Total Points: Score:

CSE 143 Lecture 20. Circle

3. Convert 2E from hexadecimal to decimal. 4. Convert from binary to hexadecimal

Making New instances of Classes

AP CS Fall Semester Final

Practice Questions for Chapter 9

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

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

Lab5. Wooseok Kim

index.pdf January 21,

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

5.12 EXERCISES Exercises 263

CSE331 Winter 2014, Midterm Examination February 12, 2014

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

University of Palestine. Mid Exam Total Grade: 100

Functions. x y z. f (x, y, z) Take in input arguments (zero or more) Perform some computation - May have side-effects (such as drawing)

Introduction to Java Applications

NATIONAL UNIVERSITY OF SINGAPORE

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

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

CS 1316 Exam 1 Summer 2009

Computer Science II (20082) Week 1: Review and Inheritance

(Not Quite) Minijava

Language Reference Manual

ESC101 : Fundamental of Computing

CS150 Sample Final Solution

AP Computer Science Unit 1. Programs

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

Birkbeck (University of London) Software and Programming 1 In-class Test Mar 2018

Java Primer. CITS2200 Data Structures and Algorithms. Topic 2

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

This page intentionally left blank

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

EXPRESSIONS AND ASSIGNMENT CITS1001

DO NOT. UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N.

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

CISC-124. This week we continued to look at some aspects of Java and how they relate to building reliable software.

A foundation for programming. Classes and objects. Overview. Java primitive types. Primitive types Creating your own data types

Sorting. Weiss chapter , 8.6

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Unit 1 Lesson 4. Introduction to Control Statements

1B1b Classes in Java Part I

CSE 143. More ArrayIntList; pre/post; exceptions; debugging. Computer Programming II. CSE 143: Computer Programming II

Spring 2013 COMP Midterm Exam Solutions March 07, 2013

Adam Blank Lecture 3 Autumn 2016 CSE 143. Computer Programming II

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

Practical Questions CSCA48 Week 6

2. The object-oriented paradigm!

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

Getting started with Java

Use the template below and fill in the areas in Red to complete it.

COMPUTER APPLICATIONS

CS150 Sample Final. Name: Section: A / B

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

Transcription:

1. What does mystery(3) return? public int mystery (int n) { int m = 0; while (n > 1) {if (n % 2 == 0) n = n / 2; else n = 3 * n + 1; m = m + 1;} return m; } (a) 0 (b) 1 (c) 6 (d) (*) 7 (e) 8 2. What are the values of a and b after this loop? int a; int b = 3; for (a = 0; a < 8 b < 20; a++) {a = a + b; b = b + a;} (a) a is 9 and b is 15 (b) a is 11 and b is 16 (c) a is 24 and b is 24 (d) a is 24 and b is 39 (e) (*) a is 28 and b is 43 1

3. What are the respective values of these expressions? 18 / 8-3.0 / 2 18 / 8.0-3 / 2 18 / (8.0-3) / 2 (a) 0.5, 0.75, and 1.8 (b) (*) 0.5, 1.25, and 1.8 (c) 0.5, 1.25, and 7.2 (d) 1.0, 0.75, and 7.2 (e) 1.0, 1.25, and 1.8 4. Which sentence best describes the collective effect of these statements on the int variables a and b? a = a + b; b = a - b; a = a - b; (a) (*) The values of the variables are swapped. (b) The values of the variables are unchanged. (c) Both variables hold the initial value of a. (d) Both variables hold the initial value of b. (e) One variable holds the sum of the initial values, and the other holds the difference. 5. What is the effect of this declaration? EtchASketch e; (a) It causes a compile-time error, as it is not permitted to declare a variable of reference type without creating the associated object. (b) It creates a variable e with an unspecified initial value. (c) It creates a variable e with initial value 0. (d) (*) It creates a variable e with initial value null. (e) It creates a variable e that points to an EtchASketch object. 2

6. What does mysteryflag(700) draw on the screen? public void mysteryflag (int n) { SimpleCanvas sc = new SimpleCanvas("What am I?", n, n); sc.setforegroundcolour(java.awt.color.red); for (int i = 0; i < n; i++) sc.drawline(0, i, i, 0); sc.setforegroundcolour(java.awt.color.blue); for (int i = 0; i < n; i++) sc.drawline(i, 0, 0, i); } (a) A square split diagonally into red and blue. (b) (*) A square split diagonally into blue and white. (c) A square split diagonally into red and white. (d) A square uniformly in one colour. (e) Nothing. 7. Which of these situations causes a NullPointerException? (a) Attempting to illegally access a private instance variable. (b) Attempting to invoke a method with an argument of the wrong type. (c) Attempting to use a variable of reference type that hasn t been declared. (d) (*) Attempting to use a variable of reference type that doesn t point at an object. (e) None of the above. 3

8. How many constructors is a class allowed to define? (a) 0 (b) 0 or 1 (c) 1 (d) 1 or more (e) (*) Any number 9. What is the value of a[99] after these statements? int[] a = new int[100]; a[0] = 1; for (int i = 1; i < a.length; i++) a[i] = 1 - a[i-1]; (a) -2 (b) -1 (c) (*) 0 (d) 1 (e) 2 10. If I want to create and populate an array of n BankAccounts, how many calls to new do I need? (a) 1 (b) n (c) (*) n+1 (d) 2n (e) 2n+1 4

11. Why can t 0.2 be represented exactly in Java? (a) It is an irrational number. (b) It can t be represented exactly in 32 bits. (c) (*) It can t be represented exactly in binary. (d) Floating-point numbers are represented as objects. (e) Floating-point numbers are stored on the heap. 12. Which sentence best describes what this method does? public void mystery (int[] a) { for (int i = 1; i < a.length; i = i+2) a[i] = a[i-1]; } (a) (*) It duplicates the even-indexed elements of a. (b) It duplicates the odd-indexed elements of a. (c) It copies the first element of a into every other slot. (d) It swaps adjacent elements of a. (e) It copies the front half of a into the back half. 5

13. Which of these expressions return false if x and y are both true, and return true in all other cases?!x!y!x &&!y!x x!= y (a) None of them (b) 1 and 2 (c) (*) 1 and 3 (d) 2 and 3 (e) All of them 14. Which of these literals is not of primitive type? (a) 7 (b) 7 (c) (*) "7" (d) 7.7 (e) 7e7 6

The next six questions refer to the following source code, which defines a class that represents circles. public class Circle { private double centrex, centrey, 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;} // halve() returns a concentric circle with half the area public Circle halve() {return new Circle(centreX, centrey, radius / Math.sqrt(2));} } // the mystery method does something unclear public boolean mystery(circle c) {double x = this.centrex - c.centrex; double y = this.centrey - c.centrey; return this.radius >= c.radius + Math.sqrt(x*x + y*y);} 7

15. What are the instance variables of Circle? (a) (*) centrex, centrey, and radius (b) x, y, and r (c) factor, other, and c (d) other and c (e) bigger and halve 16. How many of these statements correctly construct a Circle? 1. Circle c = new Circle(1e-4); 2. Circle c = Circle(); 3. Circle c = new Circle(Math.PI, Math.E, -1); 4. Circle c = Circle(3, 2, 1).new; (a) 0 (b) 1 (c) (*) 2 (d) 3 (e) 4 17. How many Circles does this sequence construct? Circle c1, c2, c3, c4; c1 = new Circle(0); c2 = c1; c3 = c2.halve(); c4 = c3.bigger(c1).halve().halve(); (a) 1 (b) 2 (c) 3 (d) (*) 4 (e) 5 8

18. Which sentence best describes the conditions under which mystery(c) returns true? (a) When the centre of the circle lies inside c. (b) When the centre of c lies inside the circle. (c) When c and the circle touch. (d) (*) When the whole of c is contained inside the circle. (e) When the whole of the circle is contained inside c. 19. What value is stored in c_area after this code (rounded to the nearest integer)? Circle c = new Circle(10); c.scale(1 / Math.sqrt(Math.PI)); double c_area = c.halve().area(); (a) 16 (b) 31 (c) (*) 50 (d) 100 (e) 493 20. If c1 is a circle with centre (2, 4) and radius 2, and c2 is a circle with centre (4, 2) and radius 4, what is the value of c1.bigger(c2) (a) An object reference equal to c1. (b) (*) An object reference equal to c2. (c) A reference to a new Circle object representing a circle with centre (4, 2) and radius 4. (d) A reference to a new Circle object representing a circle with centre (4, 4) and radius 4. (e) It causes an error. 9

Blank Page for Rough Work 10

Blank Page for Rough Work 11

Blank Page for Rough Work 12