Practice Midterm 1 Answer Key

Similar documents
Practice Midterm 1. Problem Points Score TOTAL 50

AP CS Unit 3: Control Structures Notes

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 24, Name: KEY 1

Please show all of your work.

Introduction to Computer Science Unit 2. Exercises

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 9, Name: KEY

Inheritance Systems. Merchandise. Television Camcorder Shirt Shoe Dress 9.1.1

Introduction to Computer Science Unit 2. Notes

Introduction to Computer Science Unit 2. Notes

CS 101 Fall 2005 Midterm 2 Name: ID:

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

CS111: PROGRAMMING LANGUAGE II

CS/120 Final Exam. Name

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 26, Name: Key

Name CS/120 Sample Exam #1 -- Riley. a) Every program has syntax, which refers to the form of the code, and, which refers to the meaning of the code.

Problem Points Score Grader Total 100

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 7, Name:

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

I. True/False: (2 points each)

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

Programming with Java

1. [3 pts] What is your section number, the period your discussion meets, and the name of your discussion leader?

Computing Science 114 Solutions to Midterm Examination Tuesday October 19, In Questions 1 20, Circle EXACTLY ONE choice as the best answer

CS111: PROGRAMMING LANGUAGE II

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

Peer Instruction 1. Elementary Programming

AP CS Unit 12: Drawing and Mouse Events

Problem Grade Total

CSC 1051 Data Structures and Algorithms I

Exam 1 - (20 points)

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

AP Computer Science Unit 1. Writing Programs Using BlueJ

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

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

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

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

Unit 4: Classes and Objects Notes

Term 1 Unit 1 Week 1 Worksheet: Output Solution

Chapter 3. Selections

Supplementary Test 1

I. True/False: (2 points each)

Objectives of CS 230. Java portability. Why ADTs? 8/18/14

H212 Introduction to Software Systems Honors

APCS Semester #1 Final Exam Practice Problems

COMP 202. Programming With Iterations. CONTENT: The WHILE, DO and FOR Statements. COMP Loops 1

CIS 110 Introduction to Computer Programming Spring 2016 Midterm

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

BLOCK STRUCTURE. class block main method block do-while statement block if statement block. if statement block. Block Structure Page 1

CS Week 5. Jim Williams, PhD

First Java Program - Output to the Screen

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

CS 170 Exam 1. Version: B Fall Name (as on OPUS):

Introduction to Java Applications

CSC 1051 Data Structures and Algorithms I

AP Computer Science Unit 1. Programs

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

Midterm Examination (MTA)

CS141 Programming Assignment #6

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

CS 101 Fall 2006 Midterm 3 Name: ID:

AYBUKE BUYUKCAYLI KORAY OZUYAR MUSTAFA SOYLU. Week 21/02/ /02/2007 Lecture Notes: ASCII

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

CS 101 Exam 2 Spring Id Name

Algorithms and Conditionals

CS 170 Exam 1. Version: C Fall Name (as on OPUS):

Chapter 2: Basic Elements of Java

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

CS 101 Spring 2007 Midterm 2 Name: ID:

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

CS 101 Fall 2006 Midterm 1 Name: ID:

2.8. Decision Making: Equality and Relational Operators

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

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

CSC 1051 Algorithms and Data Structures I. Midterm Examination March 2, Name:

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

CIS 110 Introduction to Computer Programming 8 October 2013 Midterm

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

CS 177 Week 15 Recitation Slides. Review

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

Mr. Monroe s Guide to Mastering Java Syntax

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

Intro to Programming in Java Practice Midterm

CMPT-166: Sample Midterm

CSC 1051 Algorithms and Data Structures I. Final Examination May 2, Name:

Selection Statements and operators

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 6, Name:

CIS 110 Introduction to Computer Programming Summer 2014 Midterm. Name:

Final Examination CS 125 Introduction to Computer Science Fall Hours

Final Exam Practice. Partial credit will be awarded.

COS 126 Midterm 1 Written Exam Fall 2011

University of Cape Town ~ Department of Computer Science Computer Science 1015F ~ Test 2. Question Max Mark Internal External

Using Java Classes Fall 2018 Margaret Reid-Miller

Chapter. Let's explore some other fundamental programming concepts

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

download instant at

1.00 Introduction to Computers and Engineering Problem Solving Quiz 1 March 4, 2005

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

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

CIS 110 Fall 2014 Introduction to Computer Programming 8 Oct 2014 Midterm Exam Name:

CIS Introduction to Computer Programming. 5 October 2012 Midterm

Transcription:

CS 120 Software Design I Fall 2018 Practice Midterm 1 Answer Key University of Wisconsin - La Crosse Due Date: October 5 NAME: Do not turn the page until instructed to do so. This booklet contains 10 pages including the cover page. This is a closed-book exam. All you need is the exam and a writing utensil. (You may use a calculator if you wish.) You have exactly 55 minutes. The maximum possible score is 50. Problem Points Score 1 8 2 8 3 10 4 4 5 10 6 10 TOTAL.

1. (8 pts.) TRUE/FALSE. For each of the following, indicate whether the statement is true or false. You do not need to explain your answers. (Note: I am including some explanations for a few of these; this is just for the purpose of the answer key, and you will not be expected to do the same on the actual exam.) a. If you do not import a class for use in your code, then you cannot create or otherwise use objects of that class. False: you can also use the complete path address, or place the class code in a default location, such as the local source folder. b. Within a single code block, no object can have two different identifiers. False. c. Within a single code block, no two objects can be declared with the same identifier. True. d. In a while loop with index variable i, the loop body must always include a line that increments (adds 1 to) the value of i. False: while you must update the index variable, it isn t always incremented. e. Variables can be named anything you like, without exception. False: not only must names be unique, but there are rules about what sorts of characters you can use (e.g., you cannot start the name with a number). f. The following line of code will not compile: int x = (int) 20 * 3.3; True: the (int) cast binds to just the 20, and so the right-hand side is actually a double value, which will not auto-convert and lose precision. g. The following line of code will not compile: int x = 20 * (int) 3.3; False: the cast here binds to 3.3, and so x == 60 here. h. The following code will always print some result, no matter what value the integer x has: if( x < 0 ) System.out.println( "One" ); else if ( x!= 0 ) System.out.println( "Two" );

False: the statement if-clause executes and prints One if the number is less than 0, and the else if-clause executes and prints Two if the number is greater than 0, but if it is equal to 0, then nothing happens.

2. (8 pts.) SHORT ANSWER. a. (2 pts.) How many times will the following loop run, assuming it is in a correct program? (This is the same as the number of lines of output each produces.) int num = 0; while ( num < 11 ) System.out.println( num ); num = num + 1; Answer: 11 times (for num == 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10). b. (2 pts.) When we want to convert a more-precise primitive type to a less-precise type in Java, we must use a cast command. An example is: int x = (int)( 10 / 3.5 * 2 ); c. (3 pts.) Give three examples of errors in your code that will prevent the code from compiling. 1. Not putting a semicolon at the end of a line of code 2. Putting a space between the two characters of >= 3. Using system.in rather than System.in when creating a Scanner

3. (10 pts.) CODE EVALUATION (I). For each of the following, give the value of the variable x after each line executes. If the line produces an error, then write ERROR. If the variable can have different values (as when using a random number generator), then indicate those values by writing, e.g., 1 <= x <= 5. a. int x = 3 / 2 * 4 + 6; x == 10 b. int x = (int)( 3 / 2.0 * 4 + 6 ); x == 12 c. int x = 3 / (int)( 2.0 * 4 + 6 ); x == 0 d. double x = 10 / 4 + 11; x == 13.0 e. double x = 10.0 / 4 + 11; x == 13.5 f. String x = "num = " + (3 + 6); x == "num = 9" g. String x = "num = " + 3 + 6; x == "num = 36" h. int x = (int)( Math.random() * 1 + 100); x == 100 i. int x = (int)( Math.random() * 100 ) + 1; 1 <= x <= 100 j. boolean x = ( 2!= 2.0 ); x == false

4. (4 pts.) CODE EVALUATION (II). Consider the following code: Oval o1, o2, o3, o4; o1 = new Oval( 50, 50, 100, 100 ); o2 = new Oval( 100, 100, 200, 200 ); o3 = null; o4 = new Oval( 200, 200, 300, 300 ); o1 = o2; o2 = o3; o3 = o4; o4 = o1; o1.setbackground( Color.blue ); o2.setbackground( Color.red ); o3.setbackground( Color.green ); o4.setbackground( Color.magenta ); a. When this code is complete, two of the Oval variable identifiers refer to the same object in memory. What are those two identifiers? Answer: o4 and o1 (due to the last assignment: o4 = o1;). b. When this code completes, one of the Oval objects will no longer have an identifier. Write down the code line that has this effect. Answer: o1 = o2; (this orphans the object to which o1 originally referred).

5. (10 pts.) CODE COMPLETION (I). On the next page, fill in the class given so that it contains a main() method that: a. Asks the user for an integer value via System.out, and reads it in from System.in, using a Scanner. b. Displays the absolute value of that input, so that if the user enters a negative number, it displays it in positive form. (See below for required format.) c. Displays the cube of the value, so that if the user enters a number n, it will display the value of n 3. d. Treats the required input value as zero if it is in incorrect form. Thus, three different runs of the program the first two with correct input, and the third with incorrect input could be: Please enter an integer value: -5 Absolute value: 5 Cube: -125 Please enter an integer value: 5 Absolute value: 5 Cube: 125 Please enter an integer value: banana Absolute value: 0 Cube: 0

import java.util.scanner; public class Q5 public static void main( String[] args ) Scanner scan = new Scanner( System.in ); System.out.print( "Please enter an integer value: " ); int i = 0; if ( scan.hasnextint() ) i = scan.nextint(); int abs = i; if ( abs < 0 ) abs = -1 * abs; System.out.println( "Absolute value: " + abs ); int cube = i * i * i; System.out.println ( "Cube: " + cube ); scan.close(); Note: to get the absolute value, you can also use the following (to replace the code inside the second if-clause: abs = Math.abs( abs ); Furthermore, you can also get the cube of a number using the Math.pow() method (although you must cast to an integer to get the proper output format, since otherwise you get a double-format value): cube = (int) Math.pow( i, 3 );

6. (10 pts.) CODE COMPLETION (II). Complete the given class so that it can execute the following steps (use the back of the page if you run out of room): a. Create two different random integer values that are either 1 or 2. b. If the first of the two number is less than the other, then a circle with diameter of 50 pixels is placed in the window, centered vertically and horizontally. c. If the first of the two number is greater than the other, then a square with sides of 50 pixels is placed in the window, centered vertically and horizontally. d. If the two numbers are the same, the background of the window is turned black. Note: class diagrams for required graphical classes appear on the last page of the exam. import java.awt.color; public class Q6 public static void main( String[] args ) Window win = new Window(); int winsize = 300; win.setsize( winsize, winsize ); int shapesize = 50; int shapeloc = ( winsize / 2 ) - ( shapesize / 2 ); int val1 = (int)( Math.random() * 2 ) + 1; int val2 = (int)( Math.random() * 2 ) + 1; if ( val1 < val2 ) Oval o = new Oval( shapeloc, shapeloc, shapesize, shapesize ); win.add( o ); else if ( val1 > val2 ) Rectangle r = new Rectangle( shapeloc, shapeloc, shapesize, shapesize ); win.add( r ); else

win.setbackground( Color.black );

Oval! << constructor >>! Oval( int, int, int, int )! << update >>! void repaint()! void setbackground( java.awt.color )! void setlocation( int, int )! void setsize( int, int )! << query >>! java.awt.color getbackground()! Rectangle! << constructor >>! Rectangle( int, int, int, int )! << update >>! void repaint()! void setbackground( java.awt.color )! void setlocation( int, int )! void setsize( int, int )! Triangle! << constructor >>! Triangle( int, int, int, int, int )! << update >>! void repaint()! void setbackground( java.awt.color )! void setlocation( int, int )! void setsize( int, int )! Window! << constructor >>! Window()! << update >>! void add( JComponent )! void repaint()! void setbackground( java.awt.color )! void setlocation( int, int )! void setsize( int, int )! void settitle( String )!