CMPS 12A - Winter 2002 Midterm 2 March 5, Name: ID:

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

CMPS 12A - Winter 2002 Final Exam A March 16, Name: ID:

CIS 1068 Program Design and Abstraction Spring2016 Midterm Exam 1. Name SOLUTION

Section 003 Fall CS 170 Exam 2. Name (print): Instructions:

Lara Technologies Special-Six Test

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

Selenium Class 9 - Java Operators

I. True/False: (2 points each)

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

Prof. Navrati Saxena TA: Rochak Sachan

Motivating Examples (1.1) Selections. Motivating Examples (1.2) Learning Outcomes. EECS1022: Programming for Mobile Computing Winter 2018

COS 126 Midterm 1 Written Exam Fall 2011

JAVA OPERATORS GENERAL

I. True/False: (2 points each)

CSEN202: Introduction to Computer Science Spring Semester 2017 Midterm Exam

Place your name tag here

CS 113 MIDTERM EXAM 2 SPRING 2013

COMP-202B - Introduction to Computing I (Winter 2011) - All Sections Example Questions for In-Class Quiz

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

CS 101 Spring 2007 Midterm 2 Name: ID:

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

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

University of Palestine. Mid Exam Total Grade: 100

CMPS 11 Intermediate Programming Midterm 2 Review Problems

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

CSE 142, Spring 2009, Sample Final Exam #2. Good luck!

INTRODUCTION TO C++ FUNCTIONS. Dept. of Electronic Engineering, NCHU. Original slides are from

Question: Total Points: Score:

New York University Introduction to Computer Science Midterm2 Sample Problems 2013 Andrew I. Case. Instructions:

CS 455 Midterm Exam 1 Fall 2016 [Bono] Thursday, Sept. 29, 2016

University of Massachusetts Amherst, Electrical and Computer Engineering

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

CSE 142, Summer 2013 Midterm Exam, Friday, July 26, 2013

CMPS 11 Introduction to Programming Midterm 1 Review Problems

CS 101 Exam 2 Spring Id Name

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

CS 110 Practice Final Exam originally from Winter, Instructions: closed books, closed notes, open minds, 3 hour time limit.

CS212 Midterm. 1. Read the following code fragments and answer the questions.

Spring 2013 COMP Midterm Exam Solutions March 07, 2013

Question: Total Points: Score:

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

Name CIS 201 Midterm II: Chapters 1-8

Announcements. PS 3 is due Thursday, 10/6. Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am

CS S-08 Arrays and Midterm Review 1

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

Loops and Expression Types

CMPS 12A Introduction to Programming Midterm 2 Review Problems

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

Name Section Number. CS210 Exam #3 *** PLEASE TURN OFF ALL CELL PHONES*** Practice

INTRODUCTION TO SOFTWARE SYSTEMS (COMP1110/COMP1140/COMP1510/COMP6710)

AP COMPUTER SCIENCE A

More types, Methods, Conditionals. ARCS Lab.

Top-down programming design

CS 113 PRACTICE FINAL

Operators Questions

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

Question: Total Points: Score:

CSE 142 Sample Midterm Exam #3

a. a * c - 10 = b. a % b + (a * d) + 7 =

Java Simple Data Types

I. True/False: (2 points each) On your bubble form fill out a for true and b for false.

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Two hours QUESTION PAPER MUST NOT BE REMOVED FROM THE EXAM ROOM UNIVERSITY OF MANCHESTER SCHOOL OF COMPUTER SCIENCE

CIS 110 Introduction to Computer Programming Spring 2016 Midterm

We have written lots of code so far It has all been inside of the main() method What about a big program? The main() method is going to get really

CSC 240 Computer Science III Spring 2018 Midterm Exam. Name

Day 2 : Intermediate Concepts 1 Examples

Warm up Exercise. What are the types and values of the following expressions: * (3 + 1) 3 / / 2.0 (int)1.0 / 2

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

Chapter 6 Methods. Dr. Hikmat Jaber

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

Java Programming for Selenium

Q1 Q2 Q3 Q4 Q5 Total 1 * 7 1 * 5 20 * * Final marks Marks First Question

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Some Practice Midterm Problems

CS 177 Spring 2010 Exam I

CS 101 Fall 2006 Midterm 3 Name: ID:

Contents. 8-1 Copyright (c) N. Afshartous

COS 126 General Computer Science Spring Written Exam 1

CSC 273 Data Structures

I. True/False: (2 points each)

2 What are the differences between constructors and methods?

Submit your answers to these questions to the Curator under Quizzes as HW08 by the posted due date and time. No late submissions will be accepted.

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

Final Exam COMP Fall 2004 Dec 16, 2004

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

bitwise inclusive OR Logical logical AND && logical OR Ternary ternary? : Assignment assignment = += -= *= /= %= &= ^= = <<= >>= >>>=

CIS Fall 2012 Midterm, 7 June 2012, Answer Key. Miscellaneous

CSCI 135 Midterm Fundamentals of Computer Science I Fall 2011

CS115 Principles of Computer Science

COP 3223 Introduction to Programming with C - Study Union - Spring 2018

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

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

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class.

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

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

Lecture #6-7 Methods

CS111: PROGRAMMING LANGUAGE II

CSci 1113 Midterm 1. Name: Student ID:

Key Java Simple Data Types

Transcription:

CMPS 12A - Winter 2002 Midterm 2 March 5, 2002 Name: ID: This is a closed note, closed book exam. Any place where you are asked to write code, you must declare all variables that you use. However, I just want code fragments, you must not write extra code such as a class specification or extraneous print statements. 1. [10 points] Given the following javadoc specification, show how to call and get a return value from the function pow(). Let a equal 5, b equal 3, and store the result in c. Print out the value that is returned. static double pow(double a, double b) Returns of value of the first argument raised to the power of the second argument. double a, b, c; a = 5; b = 3; c = pow(a, b); System.out.println(c); 1 point 1 point 4 points 2. [10 points] Write a method called circlearea() that takes as a parameter the radius of a circle and returns its area. 3 points (return type, name, parame- 3 points public static double circlearea(double radius) { ter) double area; area = Math.PI * radius * radius; return area; 1

3. [10 points] Given the following numbered lines of code 1 class Foo { 2 3 public static void main(string[] args) { 4 int a = 5; 5 6 for(int b = 0; b < 100; b++) { 7 b = a*b; 8 9 10 int c; 11 12 c = foo(a); 13 14 System.out.println(c); 15 16 17 public static double foo(int d) { 18 double e = 1/d; 19 return e; 20 21 Write the line numbers of the lines that constitute the scope of each variable: a: 5-15 b: 6-8 c: 10-15 d:17-20 e: 18-20 2

4. [10 points] What does this program print out? Why? class TestProgram{ public static void main(string[] args){ int a = 1, b = 2; System.out.println(a); System.out.println(b); swap(a, b); System.out.println(a); System.out.println(b); static void swap(int x, int y) { int temp; System.out.println(x); System.out.println(y); temp = x; x = y; y = temp; System.out.println(x); System.out.println(y); It prints out: The reason it does this is that in swap, the values of x and y are swapped, but in main the values of a and b are not swapped because x and y are merely copies of a and b. 3

5. Recursion a) [10 points] Write a non-recursive implementation of the pow() function from problem 1 public static int pow(int a, int b) { (correct method header) int result = 1; (variable declaration) for(int i = 0; i < b; i++) { (for loop construction) result = result * a; (calculation) return result; (return statement) b) [10 points] Write a recursive implementation of the same function public static int pow(int a, int b) { (method header) if(b == 0) (test) return 1; (base case) else return a*pow(a, b-1); 4 points (recursive case) 4

6. Arrays a) [5 points] Declare and create storage for an array of 10 integers called foo int[] foo; foo = new int[10]; 3 points b) [5 points] Write a method called bar() that takes an array of integers and adds 1 to each element public static void bar(int[] thearray) { for(int i = 0; i < thearray.length; i++) { thearray[i]++; 1 point c) [5 points] Show how you would call bar() with the array you created as a parameter bar(foo); 5 points d) [5 points] After the call to bar(), is the original array changed in any way? Why? Yes. Because when you pass an array to a method, you are passing a reference to the array, so the formal parameter refers to the same storage as the actual parameter. 5 points 7. [10 points] Write a method called arraymin() that takes an array of doubles as a parameter and returns the index of the smallest element of the array. public static int arraymin(double[] thearray) { int min = 0; for(int i = 0; i < thearray.length; i++) { if(thearray[i] < thearray[min]) { min = i; return min; (method header) 1 point (initializing min) (looping through the array) 3 points (setting min) (returning min) 5

8. [10 points] Conway s Life program simulates cell life. It is played on a 2D array of elements that represent cells. It has three basic rules: Rule 1.If a cell is dead and it has exactly three neighbors that are alive, it comes to life in the next generation. Rule 2.If a cell is alive and it has fewer than 2 or more than 3 neighbors that are alive, it dies in the next generation. Rule 3.Otherwise, the cell will be the same in the next generation as it is in the current generation. Suppose that we have a life game board that is a 2D array of booleans, where the boolean value false means that a cell is dead and true means that a cell is alive. Write a method called live() that takes the board as a parameter and returns an updated board that shows what it looks like after exactly one generation. public static void live(boolean[][] board) { boolean[][] boardcopy = board.clone(); for(int i = 0; i < board.length; i++) { for(int j = 0; j < board[i].length; j++) { int n = neighbors(boardcopy, i, j); 1 point (passing 2D array) 1 point (copying board) (traversing board) if(board[i][j] == false && n == 3) (finding state of cells) board[i][j] = true; else if(board[i][j] == true && n!= 2 && n!= 3) board[i][j] = false; public static int neighbors(boolean[][] board, int i, int j) { (counting neighbors) int n = 0; if(i >0 && j > 0 && board[i-1][j-1]) n++; (dealing with edges) if(i > 0 && board[i-1][j]) n++; if(i > 0 && j < board[i].length-1 && board[i-1][j+1]) n++; if(i < board.length-1 && j > 0 && board[i+1][j-1]) n++; if(i < board.length-1 && board[i+1][j]) n++; if(i < board.length-1 && j < board[i].length-1 && board[i+1][j+1]) n++; if(j > 0 && board[i][j-1]) n++; if(j < board.length[i]-1 && board[i][j+1]) n++; return n; 6