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

Similar documents
CIS 110 Introduction To Computer Programming. October 5th, 2011 Exam 1. Review problems

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

CIS 110 Introduction To Computer Programming. November 21st, 2011 Exam 2

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

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

CIS 110 Introduction To Computer Programming. December 19th, 2011 Final. Answer key for review problems

CIS 110: Introduction to Computer Programming

CIS 110 Introduction To Computer Programming. December 19th, 2011 Final. Answer key

CIS 110 Introduction to Computer Programming. 12 February 2013 Midterm

CIS 110 Introduction to Computer Programming. 13 February 2013 Make-Up Midterm Midterm

CIS 110 Introduction to Computer Programming. 7 May 2012 Final Exam

CIS Introduction to Computer Programming. 5 October 2012 Midterm

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

Using Graphics. Building Java Programs Supplement 3G

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

Building Java Programs

TWO-DIMENSIONAL FIGURES

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

CIS 110 Introduction to Computer Programming 8 October 2013 Midterm

CIS 110 Fall 2016 Introduction to Computer Programming 13 Oct 2016 Midterm Exam

CIS 110 Introduction to Computer Programming. 17 December 2012 Final Exam

Building Java Programs

CS 305j Midterm 1 Fall 2007

CSE 131 Introduction to Computer Science Fall Exam II

Programs, Statements, Variables

CIS 110 Introduction to Computer Programming Spring 2016 Midterm

CIS Introduction to Computer Programming Spring Exam 1

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

CSE 142, Autumn 2008 Midterm Exam, Friday, October 31, 2008

CSE 143: Computer Programming II Spring 2015 Midterm Exam Solutions

CIS 110: Introduction to Computer Programming

CS 305j Midterm 1 Fall 2008

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

CIS 110: Introduction to Computer Programming

Mid Term Exam 1. Programming I (CPCS 202) Instructor: M. G. Abbas Malik Date: Sunday November 3, 2013 Total Marks: 50 Obtained Marks:

Garfield AP CS. Graphics

Building Java Programs

CIS 110 Fall 2015 Introduction to Computer Programming 7 Oct 2015 Makeup Midterm Exam

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

Topic 8 graphics. -mgrimes, Graphics problem report 134

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

Syntax of Variable Declarations. Variables Usages as Expressions. Self-assignment Statements. Assignment. Scoping and Naming Rules

CSE 142, Autumn 2010 Midterm Exam, Friday, November 5, 2010

Topic 8 Graphics. Margaret Hamilton

Building Java Programs

CS 305j Midterm 1 Fall 2006

CSE 142, Summer 2010 Midterm Exam, Friday, July 30, 2010

Topic 6 Nested for Loops

CSE 131 Introduction to Computer Science Fall Exam II

Computer Programming, I. Laboratory Manual. Final Exam Solution

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

Prelim One Solution. CS211 Fall Name. NetID

Lecture 19: Recursion

Parameters. Repetitive figures. A solution? Parameterization. Declaring parameterized methods. Generalizing methods. Readings: 3.1

CS 312 Midterm 1 Spring 2013

CIS 162 Project 1 Business Card Section 04 (Kurmas)

CSCI 1226 A Test #1. Wednesday, 10 October, 2018 Name: Student #: General Instructions Read and follow all directions carefully.

Repetition with for loops

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

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

Without fully opening the exam, check that you have pages 1 through 11.

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

CSEN202: Introduction to Computer Science Spring Semester 2017 Midterm Exam

CSCI 135 Exam #0 Fundamentals of Computer Science I Fall 2012

Repetition. We might start out with the following program that draws only the left most object.

First Exam Computer Programming 326 Dr. St. John Lehman College City University of New York Thursday, 7 October 2010

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

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

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

ASSIGNMENT 2. COMP-202A, Fall 2013, All Sections. Due: October 20 th, 2013 (23:59)

Title. Java Just in Time. John Latham. October 25, October 25, 2017 Java Just in Time - John Latham Page 1(0/0)

1 Getting started with Processing

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

CSE 332 Spring 2014: Midterm Exam (closed book, closed notes, no calculators)

CSCI 135 Midterm Fundamentals of Computer Science I Fall 2011

CS 161: Object Oriented Problem Solving

AP Computer Science Unit 1. Writing Programs Using BlueJ

Prelim 1. CS 2110, 14 March 2017, 7:30 PM Total Question Name Short answer. OO Recursion Loop invariants Max Score Grader

Introduction to Java Applications

CSE 131 Introduction to Computer Science Fall Exam II

Java and OOP. Part 3 Extending classes. OOP in Java : W. Milner 2005 : Slide 1

Final Examination CS 125 Introduction to Computer Science Fall Hours

Control Structures (Deitel chapter 4,5)

CS 106A Midterm Review. Rishi Bedi, adapted from slides by Kate Rydberg and Nick Troccoli Summer 2017

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

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

CIS Introduction to Computer Programming Spring Exam 2

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

Place your name tag here

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario

CSE 131 Introduction to Computer Science Fall Final Exam

S.E. Sem. III [CMPN] Object Oriented Programming Methodology

1 Getting started with Processing

CS 051 Homework Laboratory #2

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

CS 170 Section 3, Spring 2015 Programming in Java Midterm Exam 1. Name (print):

CS100J April 17, 2001

CS100B: Prelim 3 Solutions November 16, :30 PM 9:00 PM

Fall 2013 Program/Homework Assignment #2 (100 points) -(Corrected Version)

Introduction to Java Unit 1. Using BlueJ to Write Programs

Transcription:

CIS 110 Introduction To Computer Programming October 5th, 2011 Exam 1 Answer key for review problems Scores: 1 2 3 4 5 6 Total (100 max)

CIS 110 Exam 1 Instructions You have 50 minutes to finish this exam. Time will begin when called by a proctor and end precisely 50 minutes after that time. If you continue writing after the time is called, you will receive a zero for the exam. This exam is closed-book, closed-notes, and closed-computational devices except for a one page sheet (8.5 by 11 ) of double-sided notes. When writing code, the only abbreviations you may use are for System.out.println and System.out.print as follows: System.out.println S.O.PLN System.out.print S.O.P Otherwise all code must be written out as normal. Please do not separate the pages of the exam. If a page becomes loose, please make sure to write your name on it so that we don t lose it, and use the provided staplers to reattach the sheet when you turn in your exam. If you require extra paper, please use the backs of the exam pages or the extra sheet paper provided at the end of the exam. Clearly indicate on question page where the graders can find the remainder of your work (e.g., back of page or on extra sheet ). If you have any questions, please raise your hand and an exam proctor will come to answer them. When you turn in your exam, you will be required to show ID. If you forgot to bring your ID, please talk to an exam proctor immediately. Good luck, have fun! 1

CIS 110 Exam 1 Cheat Sheet /** 1. For syntax, look at the format of the code presented in the questions * (unless otherwise stated, it is all syntactically correct code). */ // class declarations // method declarations public class <name> { public static <type> <name>(<params>) { <methods> <statements> /** 2. Useful methods for String objects */ charat(index) endswith(text) indexof(character) length() startswith(text) substring(start, stop) tolowercase() touppercase() // Returns the character at the given (zero-based) index. // Returns true if the string ends with the given text. // Returns the (zero-based) index of the given character. // Returns the length of the string. // Returns true if the string starts with the given text. // Returns the characters from the start index to just // before the stop index // Returns a new string with all lower case characters. // Returns a new string with all upper case characters. /** 3. Useful methods for Graphics objects */ drawline(x1, y1, x2, y2) // Draws a line between (x1, y1) and (x2, y2) drawoval(x, y, width, height) // Draws the outline of the largest oval that // fits in the specified rectangle. drawrect(x, y, width, height) // Draws the outline of the specified rectangle. drawstring(msg, x, y) // Draws the given text with its lower-left // corner at (x, y) filloval(x, y, width, height) // Fills the largest oval that fits in the // specified rectangle. fillrect(x, y, width, height) // Fills the outline of the given rectangle. setcolor(color) // Sets the graphics context to use the color. 2

Notes about this exam: Exam #1 covers all content covered so far in class from lectures, labs, homeworks, and the text (chapters 1 3 including 3G). The purpose of this exam is to test your algorithmic thinking skills rather than have you regurgitate facts about computer programming. To prepare for this exam, you should practice those skills by reviewing homeworks and doing practice problems discussed in section or found in the text. The exam is closed book, closed notes, and closed electronic devices. You may bring a single 8.5 11 sheet of double-sided notes to help jog your memory. In addition to your note sheet, we will include a sheet of commonly-used APIs so that you do not have to write them down. This cheat sheet appeared on the previous page. This practice exam introduces you to the types of problems that will be on the real exam. That way you can spend more time solving problems instead of trying to understand what the problem asks of you. That being said, while my goal is not to stray far from this format, the format of the actual exam may change slightly if necessary. I strongly recommend that you attempt this practice exam without looking at the answers. Afterwards, check your work, and then use the usual channels (e.g., Piazza, your TA, or myself) to resolve any lingering questions that you may have. 3

Expression Evaluating Machines 1. (10 points) Evaluate these Java expressions to their final values. (a) 10 * 3 + 4 / 2 32 (b) 3 / (double) 2 + 5 / 7 1.5 (c) 12 % 8-3 % 4 1 (d) "super" + 3 * 3 + "duper" + (8-5) super9duper3 (e) String s = "hello world!"; s.substring(2, 3) + ": " + (s.length() + 1) l: 13 4

Trace You A Loop 2. (10 points) Trace the execution of the following loops by filling in the provided tables. Each row of the table corresponds to the state of the program after executing the line marked HERE. You may skip any cases of the variables that do not reach execution of the line marked HERE. (a) for (int i = 0; i < 8; i += 2) { for (int j = 0; j < i / 2; j++) { System.out.println("*"); // HERE i j Output (each line) 2 0 * 4 0 * 4 1 * 6 0 * 6 1 * 6 2 * (b) for (int i = -1; i < 1; i++) { for (int j = 2; j > i; j--) { for (int k = 0; k < 1; k++) { System.out.println((i + j) * k); // HERE i j k Output (each line) -1 2 0 0-1 1 0 0-1 0 0 0 0 2 0 0 0 1 0 0 5

Method Mystery 3. (15 points) Write the output of this program in the space below. public static void f(int x, String y, int z) { System.out.println(y + " ; " + z + " ; " + x); public static int g(int n) { n--; n *= n + 2; return n; public static void main(string[] args) { int x = 0; String y = "forty-two"; int z = 1; f(z, y, x); f(x, "zero", z); f(z, y, z); f(x, "hello", g(5)); f(g(1), "bye", g(g(2))); forty-two ; 0 ; 1 zero ; 1 ; 0 forty-two ; 1 ; 1 hello ; 24 ; 0 bye ; 8 ; 0 6

Patterns, Patterns, Patterns 4. (20 points) Consider the following ASCII picture: *- +=*-- +=+=*--- +=+=+=*---- (a) What is the form of each line (i.e., what is the pattern of characters on each line)? spaces, +=, *, - (b) For each chunk of characters you identify, give a formula for the number of chunks on each line in terms of the row number i. You may choose to number the first row either 0 or 1. spaces = 6 - i * 2 += = i * = 1 - = i + 1 (c) Finally, write a method, drawshape() that draws the above diagram in terms of the formulae that you derived. public static void drawshape() { for (int i = 0; i < 4; i++) { for (int j = 0; j < 6 - i * 2; j++) { System.out.print(" "); for (int j = 0; j < i; j++ ) { System.out.print("+="); System.out.print("*"); for (int j = 0; j < i + 1; j++ ) { System.out.print("-"); System.out.println(); 7

Parameterization 5. (20 points) (a) Write a method, drawfigure, that takes a Graphics object as input and draws the following figure to that Graphics object. The figure consists of three parts: A circle of radius 50. Its anchor point is (0, 0). A square of width 100. Its anchor point is (50, 0). A horizontal line of length 150. Its left endpoint is at (0, 50). public static void drawfigure(graphics g) { g.drawoval(0, 0, 100, 100); g.drawrect(50, 0, 100, 100); g.drawline(0, 50, 150, 50); (b) Parameterize drawfigure further so that you can draw the figure anywhere on the screen. The figure should remain the same size, but the caller of drawfigure should be able to pass in an (x, y) pair to move the anchor of the figure (which is its upper left-hand corner). You may produce the new drawfigure method below or simply change your answer to the previous part to become parameterized. public static void drawfigure(graphics g, int x, int y) { g.drawoval(x, y, 100, 100); g.drawrect(x + 50, y, 100, 100); g.drawline(x, y + 50, x + 150, y + 50); 8

Gonna Fly Now 6. (25 points) Write a method sillyprint that takes a parameter n and writes out the numbers from 1 to n with the following format: ][1][][2][][][3][... ][n][ Where the numbers are surrounded by square brackets in a pattern. For reference, here is the execution of sillyprint(7). ][1][][2][][][3][][][][4][][][][][5][][][][][][6][][][][][][][7][ Hint: Decompose the problem, writing a separate, well-named method, for each subproblem. And then combine all those methods in the definition of sillyprint to arrive at the final result. public static void printpattern(int n) { for (int i = 0; i < n; i++) { System.out.print("]["); public static void sillyprint(int n) { for (int i = 1; i <= n; i++) { printpattern(i); System.out.print(i); printpattern(1); 9