CSE 142, Spring 2009 Midterm Exam Answer Key

Similar documents
Topic 11 Scanner object, conditional execution

Building Java Programs

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Building Java Programs

Sample CSE8A midterm Multiple Choice (circle one)

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

Building Java Programs

Building Java Programs

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Introduction to Java

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Chapter 2 Introduction to Java programming

Example of a Java program

J a v a Quiz (Unit 3, Test 0 Practice)

Arrays. Introduction. Chapter 7

Object Oriented Software Design

Unit 6. Loop statements

Part I. Multiple Choice Questions (2 points each):

While and Do-While Loops Summer 2010 Margaret Reid-Miller

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

AP Computer Science Java Mr. Clausen Program 9A, 9B

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

JAVA ARRAY EXAMPLE PDF

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

CS 121 Intro to Programming:Java - Lecture 11 Announcements

Using Files as Input/Output in Java 5.0 Applications

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

Building Java Programs

CS1020 Data Structures and Algorithms I Lecture Note #1. Introduction to Java

CS170 Lab 11 Abstract Data Types & Objects

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

Arrays in Java. Working with Arrays

Chapter 2: Elements of Java

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

5.2 Q2 The control variable of a counter-controlled loop should be declared as: a.int. b.float. c.double. d.any of the above. ANS: a. int.

Software Testing. Definition: Testing is a process of executing a program with data, with the sole intention of finding errors in the program.

Building Java Programs

Introduction to Java. CS 3: Computer Programming in Java

MIDTERM 1 REVIEW WRITING CODE POSSIBLE SOLUTION

Java Basics: Data Types, Variables, and Loops

LOOPS CHAPTER CHAPTER GOALS

In this Chapter you ll learn:

Statements and Control Flow

Topic 16 boolean logic

(Eng. Hayam Reda Seireg) Sheet Java

AP Computer Science Java Subset

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

The following program is aiming to extract from a simple text file an analysis of the content such as:

Introduction to Programming

Iteration CHAPTER 6. Topic Summary

Homework/Program #5 Solutions

AP Computer Science File Input with Scanner

This loop prints out the numbers from 1 through 10 on separate lines. How does it work? Output:

Mobile App Design Project #1 Java Boot Camp: Design Model for Chutes and Ladders Board Game

Object Oriented Software Design

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

AP Computer Science Static Methods, Strings, User Input

Two-Dimensional Arrays. Multi-dimensional Arrays. Two-Dimensional Array Indexing

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

Introduction to Java Lecture Notes. Ryan Dougherty

Conditionals (with solutions)

Comp 248 Introduction to Programming

Introduction to Computer Programming, Spring Term 2014 Practice Assignment 3 Discussion

Line-based file processing

Basics of Java Programming Input and the Scanner class

Collections.sort(population); // Método de ordenamiento

The While Loop. Objectives. Textbook. WHILE Loops

IRA EXAMPLES. This topic has two examples showing the calculation of the future value an IRA (Individual Retirement Account).

Course Intro Instructor Intro Java Intro, Continued

Java Interview Questions and Answers

Explain the relationship between a class and an object. Which is general and which is specific?

2009 Tutorial (DB4O and Visual Studio 2008 Express)

Moving from C++ to VBA

Building Java Programs

Introduction to Object-Oriented Programming

6. Control Structures

Moving from CS 61A Scheme to CS 61B Java

Handout 3 cs180 - Programming Fundamentals Spring 15 Page 1 of 6. Handout 3. Strings and String Class. Input/Output with JOptionPane.

Chapter 2. println Versus print. Formatting Output withprintf. System.out.println for console output. console output. Console Input and Output

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

Conditional Statements Summer 2010 Margaret Reid-Miller

12-6 Write a recursive definition of a valid Java identifier (see chapter 2).

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

CS114: Introduction to Java

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Scanner sc = new Scanner(System.in); // scanner for the keyboard. Scanner sc = new Scanner(System.in); // scanner for the keyboard

Week 1: Review of Java Programming Basics

CSE 8B Midterm Fall 2015

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

1.00/ Session 2 Fall Basic Java Data Types, Control Structures. Java Data Types. 8 primitive or built-in data types

Building Java Programs

Transcription:

CSE 142, Spring 2009 Midterm Exam Answer Key 1 of 9

1. Expressions For each expression in the left-hand column, indicate its value in the right-hand column. Be sure to list a constant of appropriate type and capitalization. e.g., 7 for an int, 7.0 for a double, "hello" for a String, true or false for a boolean. Expression 3 * (7 + 1) - 3-7 + 2 9 % 5 + 20 % 5 + 20 % 3 2.3 * 2.0 + 1.5 * 10 / 3 1 + 2 + "3 + 4" + 5 + 6 / 4 5 % 2 + 10 / 5 / 2 + 4.2 Value 16 6 9.6 33 + 451 6.2 2. Parameter Mystery What output is produced by the following program? public class ParameterMystery { public static void main(string[] args) { String head = "shoulders"; String knees = "toes"; String elbow = "head"; String eye = "eyes and ears"; String ear = "eye"; touch(ear, elbow); touch(elbow, ear); touch(head, "elbow"); touch(eye, eye); touch(knees, "Toes"); touch(head, "knees " + knees); public static void touch(string elbow, String ear) { System.out.println("touch your " + elbow + " to your " + ear); touch your eye to your head touch your head to your eye touch your shoulders to your elbow touch your eyes and ears to your eyes and ears touch your toes to your Toes touch your shoulders to your knees toes 2 of 9

3. If/Else Simulation For each call below to the following method, write the output that is produced, as it would appear on the console: public static void ifelsemystery(int x, int y) { int z = 4; if (z <= x) { z = x + 1; else { z = z + 9; if (z <= y) { y++; System.out.println(z + " " + y); Method Call ifelsemystery(3, 20); ifelsemystery(4, 5); ifelsemystery(5, 5); ifelsemystery(6, 10); Output 13 21 5 6 6 5 7 11 3 of 9

4. While Loop Simulation For each call below to the following method, write the output that is produced, as it would appear on the console: public static void whilemystery(int n) { int x = 0; int y = 1; while(n > x && n > y) { n--; x = x + 2; y = y + x; System.out.println(x); Method Call whilemystery(5); whilemystery(10); whilemystery(4); Output 4 6 2 4 of 9

5. Assertions For each of the five points labeled by comments, identify each of the assertions in the table below as either being always true, never true, or sometimes true / sometimes false. public static int assertionmystery(scanner console) { int a = -1; int b = 0; int next = console.nextint(); // Point A while(next!= 0) { // Point B b = Math.max(a, next); a = next; // Point C if(a == 42) { // Point D a++; next = console.nextint(); // Point E return b; Fill in each box below with one of ALWAYS, NEVER or SOMETIMES. (You may abbreviate them as A, N, or S.) next == 0 a > 0 b >= next Point A Point B Point C Point D Point E SOMETIMES NEVER SOMETIMES NEVER SOMETIMES SOMETIMES NEVER SOMETIMES ALWAYS NEVER ALWAYS ALWAYS ALWAYS SOMETIMES SOMETIMES 5 of 9

6. Programming Write a program that simulates an ant trying to crawl up a building of height 6 steps. The ant starts on the ground, at height 0. Each iteration, the ant either crawls up one step, or slips off and falls all the way back to the ground. There is a 50% chance on each iteration that the ant will slip. The program should keep going until the ant gets to the top of the building. It should then print out the number of falls that the ant took before it finally reached the top. Here is a sample execution: number of falls: 8 // this program includes the class declaration etc so that it can run this isn t // needed for a correct answer import java.util.*; public class Ant { public static void main(string[] args) { Random rand = new Random(); int height = 0; int falls = 0; while (height<6) { int r = rand.nextint(2); // 50% chance the ant will fall... if (r==0) { // ant fell height = 0; falls++; else { // ant climbs a step height++; System.out.println("number of falls: " + falls); Note: this solution assumes that if the ant slips even when height==0 that counts as a fall. (Maybe the ant gets partway up the wall?) We also counted as correct solutions that didn t count this case as a fall. 6 of 9

7. Programming Write a static method ispoweroftwo that takes an integer n as an argument, and that returns true if n is a power of two, and otherwise false. If n is zero or negative, return false. Note that ispoweroftwo(1) should return true, since 2 0 =1. Here are some example calls to the method and the value it returns: Call ispoweroftwo(0) ispoweroftwo(1) ispoweroftwo(5) ispoweroftwo(8) Result false true false true // this program includes the class declaration etc so that it can run this isn t // needed for a correct answer import java.util.*; public class PowerOfTwo { // main not required for the answer on the test public static void main(string[] args) { for (int i=0; i<100; i++) { if (ispoweroftwo(i)) { System.out.println(i); if (ispoweroftwoa(i)) { System.out.println(i); // a couple of possible solutions public static boolean ispoweroftwo (int n) { int reduced = n; if (n<1) { return false; while (reduced>1) { if (reduced%2 == 0) { reduced = reduced/2; else { return false; return true; public static boolean ispoweroftwoa (int n) { int pow = 1; while (pow<=n) { if (n==pow) { return true; pow = 2*pow; return false; 7 of 9

8. Programming A perfect number is a positive integer that is the sum of all its proper factors (that is, factors including 1 but not the number itself). The first two perfect numbers are 6 and 28, since 1+2+3=6 and 1+2+4+7+14=28. Write a static method findallperfectupto that takes an integer max as an argument and prints out all perfect numbers that are less than or equal to max. Here is the console output from a call to findallperfectupto(6): Perfect numbers up to 6: 6 Here is the console output from a call to findallperfectupto(500): Perfect numbers up to 500: 6 28 496 // this program includes the class declaration etc so that it can run this isn t // needed for a correct answer import java.util.*; public class Perfect { public static void main(string[] args) { Scanner console = new Scanner(System.in); int n = 1; // any number other than 0 to start the loop while (n!=0) { System.out.print("Enter an integer: "); n = console.nextint(); findallperfectupto(n); System.out.println("bye"); public static void findallperfectupto(int n) { System.out.print("Perfect numbers up to " + n + ":"); for (int current = 1; current <= n; current++) { int sum = 0; // for (int factor = 1; factor < current; factor++) { if (current % factor==0) { sum = sum+factor; if (sum == current) { System.out.print(" " + current); System.out.println(); 8 of 9

X. Extra Credit (+1 point) Make up an English word for a monster that is part Alan, part Dan. Use only the letters A, L, D, and N. Example: Landalad. Do not use this example. (Any other word you write will get the +1 extra point.) 9 of 9