CS 101 Fall 2006 Midterm 3 Name: ID:

Similar documents
CS 101 Spring 2007 Midterm 2 Name: ID:

CS 101 Fall 2006 Midterm 1 Name: ID:

CS 101 Fall 2005 Midterm 2 Name: ID:

This exam is open book. Each question is worth 3 points.

CS 101 Exam 1 Spring 200 Id Name

CS 101 Exam 2 Spring Id Name

CS 101 Spring 2006 Final Exam Name: ID:

Page 1 / 3. Page 2 / 18. Page 3 / 8. Page 4 / 21. Page 5 / 15. Page 6 / 20. Page 7 / 15. Total / 100. Pledge:

Controls Structure for Repetition

Fall CS 101: Test 2 Name UVA ID. Grading. Page 1 / 4. Page3 / 20. Page 4 / 13. Page 5 / 10. Page 6 / 26. Page 7 / 17.

Programming with Java

Getting started with Java

Chapter 6 Lab Classes and Objects

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

CIT 590 Homework 6 Fractions

Coding Standards for Java

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

Assignment 1 due Monday at 11:59pm

Java: Classes. An instance of a class is an object based on the class. Creation of an instance from a class is called instantiation.

COS 126 General Computer Science Spring Written Exam 1

H212 Introduction to Software Systems Honors

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

Computer Programming, I. Laboratory Manual. Final Exam Solution

University of Cape Town ~ Department of Computer Science Computer Science 1015F ~ June Exam

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: if Single-Selection Statement CSC 209 JAVA I. week 3- Control Statements: Part I

Practice Midterm 1. Problem Points Score TOTAL 50

APCS Semester #1 Final Exam Practice Problems

CS1083 Week 2: Arrays, ArrayList

Give one example where you might wish to use a three dimensional array

The newest, most different stuff is Java variable declaration, array syntax, class instance syntax. Expect those to be emphasized.

Course Outline. Introduction to java

CS111: PROGRAMMING LANGUAGE II

Final Exam Practice. Partial credit will be awarded.

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

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

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

Decisions in Java IF Statements

CS 302 Week 9. Jim Williams

CIS133J. Working with Numbers in Java

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

COSC 1010 Introduction to Computer Programming

What will this print?

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Math Modeling in Java: An S-I Compartment Model

Chapter 6 Lab Classes and Objects

First Java Program - Output to the Screen

Simple Java Reference

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Full file at

CS 1063 Introduction to Computer Programming Midterm Exam 2 Section 1 Sample Exam

CSE 1223: Exam II Autumn 2016

EECS168 Exam 3 Review

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

Practice Midterm 1 Answer Key

if (x == 0); System.out.println( x=0 ); if (x = 0) System.out.println( x=0 );

CS 177 Week 15 Recitation Slides. Review

Language Features. 1. The primitive types int, double, and boolean are part of the AP

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

Object Oriented Programming. Java-Lecture 1

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Control Structures: if and while A C S L E C T U R E 4

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2

Nested Loops ***** ***** ***** ***** ***** We know we can print out one line of this square as follows: System.out.

McGill University School of Computer Science COMP-202A Introduction to Computing 1

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

COS 126 General Computer Science Fall Exam 1

Overview. Lab 5 Methods and Parameters

CS111: PROGRAMMING LANGUAGE II

CS 113 MIDTERM EXAM 2 SPRING 2013

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

Final Exam. COMP Summer I June 26, points

CHAPTER 7 OBJECTS AND CLASSES

Some Sample AP Computer Science A Questions - Solutions

Com S 227 Spring 2018 Topics and review problems for Exam 1 Monday, February 19, 6:45 pm Exam locations by last name:

(the bubble footer is automatically inserted in this space)

Java Assignment 3: Loop Practice Ver 3.0 Last Updated: 12/1/2015 8:57 AM

Exam 2. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 40 Obtained Marks:

Robots. Byron Weber Becker. chapter 6

COS 126 Midterm 1 Written Exam Fall 2011

COS 126 Midterm 1 Written Exam, Fall 2009

Loops. CSE 114, Computer Science 1 Stony Brook University

CS 102/107 - Introduction to Programming Midterm Exam #2 - Prof. Reed Spring 2011

CSC 102 Lecture Notes Week 2 Introduction to Incremental Development and Systematic Testing More Jav a Basics

CSE 142 Su 04 Computer Programming 1 - Java. Objects

Do not start the test until instructed to do so!

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

4. Java Project Design, Input Methods

Lab 9: Creating a Reusable Class

1007 Imperative Programming Part II

More on Classes. The job of this method is to return a String representation of the object. Here is the tostring method from the Time class:

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

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

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

Birkbeck (University of London) Software and Programming 1 In-class Test Mar Answer ALL Questions

download instant at

1. An operation in which an overall value is computed incrementally, often using a loop.

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Fall 2009

Data Structures. Data structures. Data structures. What is a data structure? Simple answer: a collection of data equipped with some operations.

Prelim 1. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants

Transcription:

You only need to write your name and e-mail ID on the first page. This exam is CLOSED text book, closed-notes, closed-calculator, closed-neighbor, etc. Questions are worth different amounts, so be sure to look over all the questions and plan your time accordingly. Please sign the honor pledge here: Page 1 / 2 Page 2 / 15 Page 3 / 10 Page 4 / 8 Page 5 / 18 Page 6 / 16 Page 7 / 9 Page 8 / 22 Total / 100 Note: When an integer type is required use int, when a floating-point type is required use double. If we don t specify an aspect of the problem, you can choose it. Note: If you are still writing on the exam after pens down is called even if it is just to write your name then you will receive a zero on this exam. No exceptions! 1. [2 points] What lab section are you in? CS 101-E CS 101-2 (lab 7:00 8:30 p.m. Thu) CS 101-3 (lab noon 1:30 p.m. Fri) CS 101-4 (lab 2:00 3:30 p.m. Fri) CS 101-5 (lab 10:00 11:30 a.m. Fri) CS 101-6 (lab 2:00 3:30 p.m. Thu) 1

2. [3 points] Write a single Java statement that declares a variable called count to hold a value of type int. That variable should be initialized to 2006 at the end of the statement. 3. [3 points] Write the Java code needed to declare a public, static method called compute() that takes no parameters and returns a value of type int. Write your answer in the blank area. (The next question asks you to fill in code between the braces. Leave it blank for now.) { // this part filled in in the next question 4. [3 points] Complete the Java code for the last question to provide an implementation of the method, compute(), that always returns 0 (zero). Write your answer below: 5. [3 points] Write the Java code to assign the value returned by the method compute() to the variable count. We ll assume that the compute() method is in a class called Exam3, and that the code you are to write below is in a different class. 6. [3 points] Suppose that the int variables a, b and c have the values 1, 2 and 3, respectively and that the following Java statements are then executed. What are the values of a, b and c after these statements are executed? a = a + 1; b = a + b + 1; c = a + b + c + 1; / 15 2

7. [3 points] Suppose that the integer variable max holds a positive integer value (such as 3, for example). Complete the following code by filling in the blank with a for loop so that the code, when executed, prints out integer values starting with 0 (zero) and ending with the value of max. (If the value of max is 3, for example, then the code should print out 0 1 2 3.) Assume that the appropriate Java libraries have already been imported. { System.out.print(i + ); 8. [3 points] Complete the following code by filling in the blank with a while loop so that, when executed, the program prompts the user to enter an integer value and keeps on prompting the user to enter integer values until the user enters a 0 (zero), at which time the while loop should terminate. Assume that all the appropriate Java libraries have been imported and that the variable stdin holds a value of type Scanner for acquiring the user s inputs. boolean done = false; { System.out.print( Enter an integer value: ); int answer = stdin.nextint(); if (answer == 0) done = true; 9. [4 points] You can multiply two positive integers by starting with 0 (zero) and adding 1 (one) the correct number of times. For example, 3 * 4 = (1 + 1 + 1) + (1 + 1 + 1) + (1 + 1 + 1) + (1 + 1 + 1), which is just three four times, of course. Complete the following code by filling in the blank lines with nested for loops so that after the code runs the value of the variable product is m1 * m2 (i.e. m1 times m2). // at this point, m1 and m2 store positive integer values int product = 0; { { product++; // at this point, product stores the value m1*m2 / 10 3

10. [4 points] Suppose that you re writing a class, MyMath, that will provide methods for use by other classes for doing simple mathematical calculations. One method, multiply(), takes two positive integer values and returns an integer (namely, the product). You could, for example, use the code that you wrote for the last question to implement this method. Fill in the blank in the following code to declare the multiply() method. You do not need to provide an implementation. Note that this method should be public and static. class MyMath { { // some implementation would go here, but is not required for // this question 11. [4 points] Suppose that you re the chief programmer for the MyMath class. Your customers are complaining that some of the methods are too slow. Unfortunately, you have only a little time to make improvements. Your strategy is to improve the implementations of only the most used methods, but you don t know which methods they are. What you need to do is to count the number of times that each method in the class is called. Complete the code below by declaring an integer data member, multiplycount, that will keep track of the number of times the multiply() method is called. class MyMath { // declare count here <your multiply method declaration from the question 10 goes here> { // some implementation would go here count++; // increment the number of times method called / 8 4

12. [8 points] What value and type of value are produced by evaluating each of the following Java expressions? For example: for the expression 1 + 2, the correct answer is 3, int a. 1 + 3, b. 1 + 3.0, c. 2 + 3 * 4, d. 3 / 4, e. 5 / 4, f. 5.0 / 4, g. 1 == 1, h. 1 == 0, i. true && false, j. true false, k. true? true : false, l. false? false : true, m. 24 % 5, n. 24 % 5 / 2, 13. [10 points] In many programming situations, you have to produce an outcome that depends on a number of conditions. Suppose, for example, that you have two conditions, represented by the boolean variables pressurehigh and valvestuck. Based on these conditions, you have to set the value of an integer variable, action, as specified by the following table. You can assume the actions are represented by integer constants (e.g., NORMAL is 0 or some other integer value). pressurehigh valvestuck action false false NORMAL false true REPAIR true false RELEASE true true EVACUATE Write Java code using nested if statements to set the value of action based on the values of pressurehigh and valvestuck. The code should assign these values in the same order that they appear in the table (to ease the task of verifying that the code is correct). / 18 5

14. [4 points] For each of the following, indicate whether it is a class (C), a static data member (SDM), a non-static data member (NDM), a static method (SM), or a non-static method (NSM). Assume that we re using the standard Java libraries. a. System.out.println d. Integer.toString b. System.out e. Color.white c. System f. Math.PI 15. [4 points] Write a single Java statement to declare a variable iarray to be an array of 5 integers. We don t care what the initial values of the array elements are for this question. 16. [4 points] Write a for loop to initialize the elements of this array (iarray) so that the value of the first element is 1 (not 0), the second element 2, the third element 3, and so forth, through the last element. Do not use a literal 5 in your for loop. Instead, get the length of the array using the length property of the array. Be careful with your loop indices and array values! 17. [4 points] What output does the following program produce? class Exam3 { public static void main(string args[]) { int iarray[] = { 1, 2, 3 ; foo(iarray[2]); System.out.println(iarray[2]); bar(iarray); System.out.println(iarray[2]); public static void foo(int i) { i = 5; public static void bar(int i[]) { i[2] = 5; Answer: / 16 6

18. [17 points total] This problem has several parts. The overall goal is to produce a class called Rational for representing rational numbers (fractions with integer numerators and denominators). Here is an outline of the class. The comments indicate the tasks you are to perform. Give your answers under the appropriate subquestions, below. public class Rational { // declare and initialize data members: integer numerator and denominator // write a default constructor initializing value to 1/1 (one over one, // i.e., 1) // write a constructor taking non-negative integers n, d, initializing // value to n/d // write a method tostring() that returns a string to representing the // given value // write a method mult() taking a Rational and returning the product of // the two a. [3 points] Write the data member declarations (i.e. the instance variables). To make this test question easier, you can have them be public so that you don t have to use accessors and mutators. b. [3 points] Write the default constructor. It should initialize the value to 1/1. c. [3 points] Write the constructor taking integers n and d (which we require and assume to be nonnegative) and initializing the value to n/d. / 9 7

d. [4 points] Write the method tostring(). Given a Rational with value n/d, the method should return the string <n>/<d>, where <n> is the value of n and <d> is the value of d. So, for example, if the value of an integer is ¾, this method should return the String 3/4. e. [4 points] Write the method mult() taking a Rational object r as a parameter and returning a Rational whose value is the product of r and this. So, for example, if you have a Rational r1 with value ½ and a Rational r2 with value ¼, then the expression r1.mult(r2) must return a Rational whose value is 1/8. Don t worry about reducing fractions for this question (i.e. 2/3 * 1/2 can yield 2/6). 19. [7 points] What is the difference between a static variable and a non-static variable? Explain in about 25 words or less. 20. [7 points] What is the difference between public visibility and private visibility? Explain in about 25 words or less. / 22 8