CS110 Introduction to Computing Fall 2016 Practice Exam 1 -- Solutions

Similar documents
CS110 Introduction to Computing Fall 2016 Practice Exam 1

Question 1 (10 points) Write the correct answer in each of the following: a) Write a Processing command to create a canvas of 400x300 pixels:

Pick a number. Conditionals. Boolean Logic Relational Expressions Logical Operators Numerical Representation Binary. CS Conditionals 1

Basic Computer Programming (Processing)

The way I feel about music is that there is no right and wrong. Only true and false. Fiona Apple. true false false

A B C D CS105 03a Interaction

CISC 1600, Lab 3.2: Interactivity in Processing

1 Getting started with Processing

CISC 1600 Lecture 3.1 Introduction to Processing

The Processing language

Introduction to Processing. Sally Kong

CISC 1600, Lab 2.2: Interactivity in Processing

All program statements you write should be syntactically correct. Partial credit is not guaranteed with incorrect use of syntax.

mith College Computer Science CSC103 How Computers Work Week 6 Fall 2017 Dominique Thiébaut

CISC 1600, Lab 3.1: Processing

Class #1. introduction, functions, variables, conditionals

mith College Computer Science CSC103 How Computers Work Week 7 Fall 2017 Dominique Thiébaut

Using Methods. Methods that handle events. Mairead Meagher Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

Interaction Design A.A. 2017/2018

CISC 1600, Lab 2.1: Processing

Watch the following for more announcements

CMPT-166: Sample Midterm

2D Shapes. Creative Coding & Generative Art in Processing 2 Ira Greenberg, Dianna Xu, Deepak Kumar

CSE120 Wi18 Final Review

Kimberly Nguyen Professor Oliehoek Introduction to Programming 8 September 2013

Interaction Design A.A. 2017/2018

Real Time Data Plotting

University of Cincinnati. P5.JS: Getting Started. p5.js

INTRODUCTION TO PROCESSING. Alark Joshi, Amit Jain, Jyh-haw Yeh and Tim Andersen

CS110 Introduction to Computing Fall 2006 Midterm Exam

CMPT-166: Sample Final Exam Answer Key

+ Inheritance. Sometimes we need to create new more specialized types that are similar to types we have already created.

We will start our journey into Processing with creating static images using commands available in Processing:

Solution Notes. COMP 151: Terms Test

Getting Started in Java CIS 110

Final Exam Winter 2013

Computer Graphics. Interaction

EXAMINATIONS 2017 TRIMESTER 2

Name CMPS 5J Final March 17, 2010 This is a closed notes, closed book exam. Each problem is worth 1 point unless indicated otherwise.

What is a variable? A named locajon in the computer s memory. A variable stores values

Conditional Events. Mouse events and Operators. Dr. Siobhán Drohan Mairead Meagher. Produced by:

Interaction Design A.A. 2017/2018

COMP Summer 2015 (A01) Jim (James) Young jimyoung.ca

Variables One More (but not the last) Time with feeling

1. Complete these exercises to practice creating user functions in small sketches.

Chapter 12: Functions Returning Booleans and Collision Detection

EXAMINATIONS 2016 TRIMESTER 2

COMP Summer 2015 (A01) Jim (James) Young jimyoung.ca

Old 257 Exam #2s for Practice

Loops. Variable Scope Remapping Nested Loops. Donald Judd. CS Loops 1. CS Loops 2

Iteration in Programming

void mouseclicked() { // Called when the mouse is pressed and released // at the same mouse position }

Exploring Processing

Variables. location where in memory is the information stored type what sort of information is stored in that memory

CPSC Fall L01 Final Exam

What can we do with Processing? Let s check. Natural Language and Dialogue Systems Lab Guest Image. Remember how colors work.

AP Computer Science Principles Python Programming Using Processing

void setup() { void draw() { draw a rectangle or ellipse wherever the mouse cursor is }

if / if else statements

Class Notes CN19 Class PImage Page

CISC 1600 Lecture 2.2 Interactivity&animation in Processing

for(int i = 0; i < numbers.length; i++) {

CS 139 Practice Midterm Questions #2

Recall that creating or declaring a variable can be done as follows:

Variables and Control Structures. CS 110 Eric Eaton

Basic Input and Output

Functions. Functions. nofill(); point(20, 30); float angle = map(i, 0, 10, -2, 2); parameters return values

EP486 Microcontroller Applications

Braitenberg code. Page 1

CISC 1600 Lecture 2.2 Interactivity&animation in Processing

Methods (cont.) Chapter 7

The Processing language. Arduino and Processing.

This unit introduces the basics of trigonometry and how to utilize it for generating form.

Repetition is the reality and the seriousness of life. Soren Kierkegaard

COPYRIGHTED MATERIAL. Elements of the Language. C h a p t e r

Introduction to Processing

Practice Written Examination, Fall 2016 Roger B. Dannenberg, instructor

cs6964 March TOOLKITS Miriah Meyer University of Utah

This Week Assignment 1 Arrays Time Casting Reading code Project 1

Module 05 User Interfaces. CS 106 Winter 2018

CST112 Variables Page 1

What is a variable? a named location in the computer s memory. mousex mousey. width height. fontcolor. username

GRAPHICS PROGRAMMING. LAB #3 Starting a Simple Vector Animation

Basic Input and Output

Khan Academy JavaScript Study Guide

CMSC427 Interac/ve programs in Processing: Polyline editor

Reviewing all Topics this term

Module 01 Processing Recap. CS 106 Winter 2018

CST112 Looping Statements Page 1

Lecture 6: Processing

[ the academy_of_code] Senior Beginners

5.1. Examples: Going beyond Sequence

Exam I Review Questions Fall 2010

CSC 120 Introduction to Creative Graphical Coding, Fall 2015

Basic Input and Output

Name CMPS 5J Final March 17, 2009 This is a closed notes, closed book exam.

Evaluating Logical Expressions

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

1 Getting started with Processing

CS 177 Week 15 Recitation Slides. Review

Transcription:

CS110 Introduction to Computing Fall 2016 Practice Exam 1 -- Solutions The exam will be closed-note and closed-book; please consider this fact before using your notes on this practice version. Please see the abbreviated Processing reference at the back of this exam. This exam is intended to be taken in 80 minutes. Assume that any given Processing statements do not have typographical errors. Aim for all code to be syntactically correct, but missing semi-colons (for example) will not be penalized. 1

Question 1 (10 points) Circle/write the correct answer in each of the following: a) Which of the following is not one of the shape drawing commands in Processing: 1. quad( ) 2. triangle( ) 3. circle( ) 4. point( ) b) What would Processing do as a result of this statement? line(width, 0, 0, height); sketch Draw a line from the top-right corner to the bottom-left corner of the c) Consider the following function: void enigma(int k) int answer = 0; if (k < 10) if (k < 15) answer = 1; else answer = 2; else if (k < 15) answer = 3; else answer = 4; println(answer); For what argument values k does enigma(k) print the value 2? 1. enigma(k) will never print 2 for any value of k 2. k < 10 3. 10 <= k < 15 4. k >=15 2

d) How many times would this code print the text Hello? for (int i = 10; i >= 2; i-=2) println( Hello ); 5 e) What is the value of x after the following statements are evaluated? int y = 0, z = 1; boolean x = (y >= 0 && z < 0); false f) Suppose that the variable x currently has the value 10. What statement would cause the value of x to become 13? 1. x + 3; 2. y = x + 3; 3. x = x + 3; 4. None. The value of x can't be changed. g) What is the result of evaluating 9/2? 4 h) What is the result of evaluating 9%2? 1 i) What does the following code print? float x = 2/9; println(x); 0.0 j) What does the following code print? float x = 9/2.0; println(x); 4.5 3

Question 2 (5 points) Write Processing statements to draw a red rectangle covering the right half of the sketch. Your statements should work for any sketch size. fill(255, 0, 0); rect(width / 2, 0, width / 2, height); Question 3 (5 points) Write a conditional expression that would print teenager whenever an integer variable age has a value between 13 and 19, inclusive. int age = ; // write your conditional here: if(age >= 13 && age <= 19) println("teenager"); // or the following: if(age >= 13) if(age <= 19) println("teenager"); 4

Question 4 (15 points) The following Processing program is supposed to draw a blue circle of random diameter in the range 20 to 50 pixels centered on every mouse click. Complete the program by writing the appropriate commands needed to accomplish the task. void setup() size(400, 400); background(255); void draw() void mousepressed() float size = random(20, 50); ellipse(mousex, mousey, size, size); 5

Question 5 (15 points) a) Rewrite the following code, using a while-loop instead of a for-loop. The results of executing the two code fragments should be identical. int num = 6; for(int i=10; i > 0; i-=2) println(num); num -= i; int num = 6; int i = 10; while(i > 0) println(num); num -= i; i -= 2; b) What is printed by the code fragment in 5(a) above? 6-4 -12-18 -22 6

Question 6 (15 points) Write a Processing function that plays a simplified version of the Bizz/Buzz game. To do this, your code should print out consecutive numbers (starting at the parameter start and ending at end). Whenever a number is divisible by 5, it should print "Bizz" instead of the number. Whenever a number is divisible by 7, it should print "Buzz" instead of the number. Whenever a number is divisible by both 5 and 7, it should print "BizzBuzz" instead of the number. (Note: The non-simplified version of the game also has special behavior when the digit 5 or 7 appears in the number. You do not have to worry about this part of the game.) Remember that the % operator in Processing computes the modulus that is, the remainder after dividing one integer by another. void bizzbuzz(int start, int end) for(int i = start; i <= end; i++) if(i % 5 == 0 && i % 7 == 0) println("bizzbuzz"); else if(i % 5 == 0) println("bizz"); else if(i % 7 == 0) println("buzz"); else println(i); 7

Question 7 (15 points) Write a function named average that takes two integers x and y as parameters and use a loop to compute and print the average of all integers starting at x and ending at y, inclusive. For example, average(2,5) should print 3.5, which equals (2+3+4+5)/4. You may assume that x < y. void average(int x, int y) float sum = 0; for(int i = x; i <= y; i++) sum += i; float avg = sum / (y - x + 1); println(avg); 8

Question 8 (20 points) Consider the following code segment. Fill out the table that traces the function call to mystery. int a = 1; int b = 2; int c = 3; int d = 4; void setup() d = 6; mystery(d); void mystery(int e) int f = 1; int result = 1; for (int i = 2; i < e; i++) result = a + f; a = f; f = result; Trace of mystery(d) a e f result i 1 6 1 1 2 1 2 2 3 2 3 3 4 3 5 5 5 5 8 8 Directly after execution of the statement mystery(d); in setup(), what are the values of the following variables? a b c d 5 2 3 6 9

Extra Credit (20 points) Write a complete Processing program to draw a clock face as shown below on the left. Partial credit for only drawing the hour (longer) ticks as shown on the right. void setup() size(500, 500); translate(250, 250); ellipse(0, 0, width, height); for(int i = 0; i < 60; i++) line(240, 0, 250, 0); rotate(pi / 30); stroke(255, 0, 0); for(int i = 0; i < 12; i++) line(230, 0, 250, 0); rotate(pi / 6); 10

PROCESSING QUICK REFERENCE Variables/Constants: width, height, mousex, mousey, HALF_PI, PI, QUARTER_PI, TWO_PI Data Types boolean, int, float Conversion Functions int(x), float(x), double(x), degrees(rad), radians(deg) Relational and Logical Operators!= (inequality) < (less than) <= (less than or equal to), == (equality) > (greater than) >= (greater than or equal to)! (logical NOT) && (logical AND) (logical OR) Loops for (init; test; update) while (expression) statements statements Conditionals if (test) if (expression) if (expression) statements statements statements else else if (expression) statements statements else statements 2D Primitives arc(x,y,width,height,startrad,endrad) ellipse(x,y,width,height) line(x1,y1,x2,y2) point(x,y) quad(x1,y1,x2,y2,x3,y3,x4,y4) rect(x,y,width,height) triangle(x1,y1,x2,y2,x3,y3) text(message,x,y) 11

PROCESSING QUICK REFERENCE Attributes ellipsemode(mode), rectmode(mode) Vertex beginshape(), endshape(), vertex(x,y), curvevertex(x,y) Mouse mouseclicked(), mousedragged(), mousepressed(), mousereleased(), mousex, mousey Text Output print(message), println(message) Setting background(r,g,b), fill(r,g,b), nofill(), nostroke(), stroke(r,g,b) Trigonometry cos(rad), sin(rad), tan(rad) Random random(lower,upper) 12