CS 312 Exam 1 Fall KG Kris Megan Roman Sonika

Similar documents
CS 312 Midterm 1 Spring 2013

CS 312 Exam 2 Fall KG Kris Megan Roman Sonika

CS 312 Midterm 1 Spring 2013

CS 312 Midterm 2 Fall 2013

CS 305j Midterm 1 Fall 2007

CS 305j Midterm 1 Fall 2008

CS 305j Midterm 1 Fall 2006

CS 312 Exam 2 Fall Circle your TA's Name: Aish Anthony Bri Carla Chris

CS 314 Exam 2 Fall 2017

CS 312 Final Fall 2015

Points off Total off Net Score. CS 314 Final Exam Fall 2016

Lecture 6: While Loops and the Math Class

CS 312 Final Fall 2013

CS 307 Midterm 2 Spring 2008

CS 314 Exam 2 Spring

CS 305j Midterm 2 Fall 2006

Practice Midterm 1 Answer Key

CS 307 Midterm 1 Fall 2007

CS 307 Midterm 2 Spring 2011

Points off Total off Net Score. CS 314 Final Exam Spring 2016

CS111: PROGRAMMING LANGUAGE II

CS 312 Midterm 2 Fall Jose Stas Aaron

AP CS Unit 3: Control Structures Notes

CS110: PROGRAMMING LANGUAGE I

CS 307 Midterm 2 Fall 2008

CS 312 Midterm 2 Fall 2013

The Math Class. Using various math class methods. Formatting the values.

CS 312 Final Fall 2016

Chapter 6 Methods. Dr. Hikmat Jaber

CS 314 Final Spring 2013 SOLUTION - SOLUTION - SOLUTION - SOLUTION - SOLUTION - SOLUTION - SOLUTION

Building Java Programs

Building Java Programs

Chapter 5 Methods. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

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

1 Short Answer (10 Points Each)

CS 307 Midterm 2 Fall 2010

Points off A 4B 5 Total off Net Score. CS 314 Final Exam Spring 2015

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

CS 314 Exam 2 Spring 2018

Definite Loops. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Using a Variable for Counting

Practice Midterm 1. Problem Points Score TOTAL 50

Points off Total off Net Score. CS 314 Final Exam Spring 2017

CS 314 Exam 1 Fall 2017

CONDITIONAL EXECUTION

Lecture 5: Methods CS2301

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

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

Computer Science 1 Honors

Topic 14 while loops and loop patterns

CSE 142, Autumn 2011 Midterm Exam: Friday, November 4, 2011

Introduction to Computer Science Unit 2. Notes

CS 314 Final Fall 2012

CIS 110 Introduction To Computer Programming. October 5th, 2011 Exam 1. Answer key for review problems

CS 314 Midterm 2 Spring 2013

CS 314 Midterm 1 Fall 2011

Midterm Review Session

CS 314 Final Fall 2011

CSEN202: Introduction to Computer Science Spring Semester 2017 Midterm Exam

Points off Total off Net Score. CS 314 Final Exam Spring Your Name Your UTEID

CS 314 Midterm 1 Fall 2012

Chapter 5 Methods / Functions

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

Introduction to Computer Science Unit 2. Notes

Building Java Programs

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

Topic 6 Nested for Loops

CS 312 Final Fall Your Name SOLUTION SOLUTION SOLUTION SOLUTION SOLUTION. Your UTEID SOLUTION SOLUTION SOLUTION SOLUTION SOLUTION _

Exam Percentage: / 55 = %

Problem Grade Total

Programming Language. Functions. Eng. Anis Nazer First Semester

To define methods, invoke methods, and pass arguments to a method ( ). To develop reusable code that is modular, easy-toread, easy-to-debug,

CS 314 Exam 2 Spring 2016

Topic 5 for loops and nested loops

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

COE 212 Engineering Programming. Welcome to Exam II Thursday April 21, Instructors: Dr. Salim Haddad Dr. Joe Tekli Dr. Wissam F.

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

Activity 4: Methods. Content Learning Objectives. Process Skill Goals

CS 111X - Fall Test 1

Building Java Programs

Sample Midterm Exam #2

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

Building Java Programs

Table of Contents Date(s) Title/Topic Page #s. Chapter 4: Writing Classes 4.1 Objects Revisited

CIS 110 Introduction To Computer Programming. October 5th, 2011 Exam 1. Answer key

Computer Science is...

CS 307 Midterm 2 Fall 2009

CS 307 Midterm 1[corrected] Spring 2008

Following is the general form of a typical decision making structure found in most of the programming languages:

Term 1 Unit 1 Week 1 Worksheet: Output Solution

Building Java Programs

(c) ((!(a && b)) == (!a!b)) TRUE / FALSE. (f) ((!(a b)) == (!a &&!b)) TRUE / FALSE. (g) (!(!a) && (c-d > 0) && (b!b))

Final Exam. COMP Summer I June 26, points

CS 314 Exam 1 Spring 2018

CS 111X - Fall Test 1 - KEY KEY KEY KEY KEY KEY KEY

CS111: PROGRAMMING LANGUAGE II

Welcome to the Primitives and Expressions Lab!

Programming Exercise 7: Static Methods

Chapter 5 Methods. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Question: Total Points: Score:

1. Expressions - 1 point each. -1 for missing or extra " OR.0. differences in capitalization of true and false okay. No limit on points off.

Transcription:

CS 312 Exam 1 Fall 2015 Name: SOLUTION SOLUTION SOLUTION UTEID: SOLUTION SOLUTION SOLUTION _ Circle your TA's Name: Aila CK Jialin Katherine B KG Kris Megan Roman Sonika Problem Number Topic Points Possible 1 Expressions 18 2 Code Tracing 16 3 Method Trace 14 4 Programming 10 5 Programming 10 6 Programming 15 7 Graphics 15 TOTAL POINTS OFF: SCORE OUT OF 98: Points Off Instructions: 1. Please turn off your cell phones 2. There are 7 questions on this test. 3. You have 2 hours to complete the test. 4. Place your answers on this test, not scratch paper. 5. You may not use a calculator. 6. When code is required, write Java code. You may use only features that we discussed up to topics 1-12, including those covered in the textbook for that material (Chapters 1-4). 7. Style is not evaluated when grading. 8. The proctors will not answer questions. If you believe a question has an error or is ambiguous, state your assumption and answer based on your assumption. 9. If you finish early bring your exam and scratch paper to the proctor and show them your UTID. CS 312 Exam 1 Fall 2015 1

1. Expressions - 1 point each, 18 points total. For each Java expression in the left hand column, indicate the result of the expression in the right hand column. You must show a value of the appropriate type. For example, 7.0 rather than 7 for a double and "7" instead of 7 for a String. Answers that do not indicate the data type correctly are wrong. A. 3 * 5 + 2-6 * 5-13 B. 2 + 3 * 6 / (4 + 2) * 5 17 C. 20 / 6 + 1 / 5 + 7 / 2 6 D. 1.0 / 4.0 + 6 / 4 + 2.6 / 2 2.55 E. 2.5 * 4 + 15.0 / 3.0 15.0 F. 375 % 100 / 7 * 2 20 G. 4 * 6 % 51 + 12 % 4 24 H. "UT" + "horns" + 3 "UThorns3" I. 3 + 2 + "C!" + 1 + 15 "5C!115" J. 2 + "RE" + 2 * 10 "2RE20" K. (double) (25 / 10 * 3) 6.0 L. (int) (25 / 10.0 * 3 + 0.25) 7 M. (int) (-7.0 / 2 * 3.1) -10 The Math methods ceil, floor, pow, and abs all return doubles. N. Math.pow(2.0, 3.0) 8.0 O. Math.max(20 / 10, 10 % 20) // returns an int 10 P. Math.floor(3.99 * 4) 15.0 Q. Math.ceil(-3.011) -3.0 R. Math.abs( Math.ceil(-3.5) * Math.floor(6.8 / 2 * 3)) 30.0 CS 312 Exam 1 Fall 2015 2

2. Code tracing - 2 points each, 14 points total. Place you answer in the box to the right of the code. If the code results in a syntax or runtime error, state the kind of error that occurs. A. What is output by the following code when it is run? int xa = 5; 13 5 int ya = xa; xa = xa + 3 + ya; System.out.print(xa + " " + ya); B. What is output by the following code when it is run? int xb = 2; int yb = 5; 3-6 11 int zb = xb * yb + 1; yb -= zb; xb++; System.out.print(xb + " " + yb + " " + zb); C. What is output by the following code when it is run? int xc = 4; int yc = 3; -4 18 yc *= 2 + xc; xc -= xc * 2; System.out.print(xc + " " + yc); D. What is output by the following code when it is run? double ad = 1.5; int xd = 4; 1.5-0.5 int yd = 5; double bd = yd / xd + ad - 3; System.out.print(ad + " " + bd); E. What is output by the following code when it is run? int xe = -4; 6 30 int ye = 5; for(int i = 0; i <= 4; i++) { xe += 2; ye = 5 * xe; System.out.print(xe + " " + ye); CS 312 Exam 1 Fall 2015 3

F. How many asterisks does the following code print out? Don't show the output. Simply state the number of asterisks that are printed out when the code runs. for(int i = 10; i <= 35; i++) { 26 System.out.print("*"); G. How many asterisks does the following code print out? Don't show the output. Simply state the number of asterisks that are printed out when the code runs. 400 for(int ig = 0; ig < 8; ig++) { for(int jg = 0; jg < 5; jg++) { for(int kg = 0; kg < 10; kg++) { System.out.print("*"); H. How many asterisks does the following code print out? Don't show the output. Simply state the number of asterisks that are printed out when the code runs. for(int ih = 2; ih < 6; ih++) { for(int jh = 0; jh < ih; jh++) { System.out.print("*"); 62 System.out.print("*"); for(int jh = 10; jh >= 0 ; jh--) { System.out.print("*"); CS 312 Exam 1 Fall 2015 4

3. Method Tracing and Parameters Simulation. 2 points each, 14 points total. For each part write what the output to the screen will be when the code is run. Consider the following methods that are all part of the same program: public static void a(int x, int y) { x = x * 3; x--; y = y - 3; System.out.print(x + " " + y + " "); public static int b(int x, int y) { x++; y *= 3; return x + y; public static void c(int x) { if (x < 5) { x *= 3; else { x = x / 3; if(x % 2 == 1) { x += 2; System.out.print(x); public static int d(int i) { i++; i++; i = b(i + 1, i - 2); System.out.print(i); return i; A. Given the methods above, what is output by the following code? int xa = 3; int ya = 2; 8-1 a(xa, ya); B. Given the methods above, what is output by the following code? int xb = 4; int yb = -2; 11-5 4-2 a(xb, yb); System.out.print(xb + " " + yb); CS 312 Exam 1 Fall 2015 5

C. Given the methods above, what is output by the following code? int xc = -3; int yc = 4; -3 10 yc = b(xc, yc); System.out.print(xc + " " + yc); D. Given the methods on the previous page, what is output by the following code? int xd = 45; 17 c(xd); E. Given the methods on the previous page, what is output by the following code? int xe = -3; 3 int ye = 7; c(ye + xe + 1); F. Given the methods on the previous page, what is output by the following code? int xf = 4; 20 d(xf); G. Given the methods on the previous page, what is output by the following code? int xg = 3; 16x is 16! System.out.print("x is " + d(xg) + "!"); CS 312 Exam 1 Fall 2015 6

4. Programming - Write a Java method name printfigure that prints out a figure based on a single integer parameter, size. When the parameter size is 1 the output is: * When the parameter size is 2 the output is:...*..**.*** **** When the parameter size is 3 the output is:...*...**...***...****...*****...******..*******.******** ********* When the parameter size is 4 the output is:...*...**...***...****...*****...******...*******...********...*********...**********...***********...************...*************..**************.*************** **************** Complete your printfigure method on the next page, including the method header. You cannot use any String variables or String methods on this question. Of course you can use String literals such as "." and "*". CS 312 Exam 1 Fall 2015 7

public static void printfigure(int size) { final int NUM_LINES = size * size; for(int line = 1; line <= NUM_LINES; line++) { final int DOTS = NUM_LINES - line; for(int i = 0; i < DOTS; i++) { System.out.print("."); for(int i = 0; i < line; i++) { System.out.print("*"); System.out.println(); Criteria: header, 1 point number of lines correct, 1 point outer loop correct: 2 points prints correct number of dots: 3 points prints correct number of stars: 2 points println: 1 point CS 312 Exam 1 Fall 2015 8

5. Programming - Write a static method named printmultiples that accepts an integer value and an integer num as parameters. The method prints a complete line of output reporting the first num multiples of values. Consider the following examples: printmultiples(8, 2); prints: The first 2 multiples of 8 are 8, 16 printmultiples(4, 6); prints: The first 6 multiples of 4 are 4, 8, 12, 16, 20, 24 Assume the parameter num is >= 2. value can be any integer value. public static void printmultiples(int value, int num) { System.out.print("The first " + num + " multiples of " + value + " are "); for(int i = 1; i < num; i++) { System.out.print(value * i + ", "); System.out.println(value * num); // or print header with parameters: 1 point print first part, "The first... ": 1 point for loop: 3 points print out multiples in correct order: 3 points multiples of value calculated correctly: 1 point no comma after last value handled correctly: 1 point return statement in loop: -4 CS 312 Exam 1 Fall 2015 9

6. Programming - Write a method, timesallsame that accepts a single parameter, an integer that specifies the number of times to flip a set of 3 coins. The method returns the number of times the 3 coins were flipped and were all the same, either all heads or all tails. Assume the integer parameter is greater than or equal to 0. To simulate flipping a coin, use the Math.random() method: public static double random() Returns a double value with a, greater than or equal to 0.0 and less than 1.0. Returned values are chosen with (approximately) uniform distribution from that range. Assume values < 0.5 are heads, and values >= 0.5 are tails. Complete the timesallsame below including the method header. int total = 0; for(int i = 0; i < numflips; i++) { double c1 = Math.random(); double c2 = Math.random(); double c3 = Math.random(); if(c1 < 0.5 && c2 < 0.5 && c3 < 0.5) { total++; else if(c1 >= 0.5 && c2 >= 0.5 && c3 >= 0.5) { total++; return total; header: 1 point total variable: 1 point loop correct: 3 points call Math.random correctly 3 times: 3 points correctly determine if all flips the same for a given set: 5 points increment total correctly if appropriate: 1 point return correct value: 1 point CS 312 Exam 1 Fall 2015 10

7. Graphics Programming. Complete a method to produce the following output of concentric circles. The parameters for the method are The Graphics object for the DrawingPanel. Color has already been set to BLACK The width and height of the drawing panel given as integers. The drawing panel has already been created. The number of circles to draw. Here is the output of the method call drawcircles(g, 700, 400, 3); Here is the output of the method call drawcircles(g, 400, 600, 8); Complete the method on the next page. CS 312 Exam 1 Fall 2015 11

public static void drawcircles(graphics g, int width, int height, int numovals) { int x = 0; int y = 0; int ovalwidth = width; int ovalheight = height; int widthfraction = width / (numovals * 2); int heightfraction = height / (numovals * 2); for(int i = 0; i < numovals; i++) { g.drawoval(x, y, ovalwidth, ovalheight); x += widthfraction; y += heightfraction; ovalwidth -= widthfraction * 2; ovalheight -= heightfraction * 2; loop correct, correct number of ovals: 3 points draw first oval correctly: 2 points calculate x and y coordinates of each oval correctly: 4 points calculate width and height of each oval correctly: 4 points calls to draw oval correct, with correct parameters: 2 points CS 312 Exam 1 Fall 2015 12

CS 312 Exam 1 Fall 2015 13