Midterm Review Session

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

CSE 142 Sample Midterm Exam #3

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

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

Topic 12 more if/else, cumulative algorithms, printf

CSE 142 Sample Midterm Exam #3

CS 112 Introduction to Programming

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

Java. Programming: Chapter Objectives. Why Is Repetition Needed? Chapter 5: Control Structures II. Program Design Including Data Structures

Building Java Programs

Control Structures in Java if-else and switch

CS 112 Introduction to Programming

Flow of Control of Program Statements CS 112 Introduction to Programming. Basic if Conditional Statement Basic Test: Relational Operators

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

Building Java Programs

Building Java Programs

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

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

Topic 12 more if/else, cumulative algorithms, printf

Building Java Programs

Building Java Programs. Chapter 2: Primitive Data and Definite Loops

Sample Midterm Exam #2

CSE142 Sample Midterm Spring Name UW NetId (e.g. whitab) Section (e.g., AA) TA

Building Java Programs A Back to Basics Approach 4th Edition Reges TEST BANK Full download at:

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

CSE 142 Sample Midterm Exam #2

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

Building Java Programs

Building Java Programs

Building Java Programs

assertion: A statement that is either true or false.

b. Suppose you enter input from the console, when you run the program. What is the output?

Advanced if/else & Cumulative Sum

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

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

1 Short Answer (10 Points Each)

NoSuchElementException 5. Name of the Exception that occurs when you try to read past the end of the input data in a file.

Building Java Programs

Chapter 5 Control Statements: Part 2 Section 5.2 Essentials of Counter-Controlled Repetition

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

Menu Driven Systems. While loops, menus and the switch statement. Mairead Meagher Dr. Siobhán Drohan. Produced by:

Building Java Programs

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

CMPT 125: Lecture 4 Conditionals and Loops

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

Building Java Programs Chapter 2

CSCI 136 Data Structures & Advanced Programming. Fall 2018 Instructors Bill Lenhart & Bill Jannen

Topic 14 while loops and loop patterns

Topic 4 Expressions and variables

Building Java Programs

CSE142 Sample Midterm, Winter 2018

Control Structures in Java if-else and switch

CSE 142, Autumn 2007 Midterm Exam, Friday, November 2, 2007

YEAH 2: Simple Java! Avery Wang Jared Bitz 7/6/2018

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

Structured Program Development in C

CSEN202: Introduction to Computer Science Spring Semester 2017 Midterm Exam

Chapter 4: Control structures. Repetition

Building Java Programs Chapter 2. bug. Primitive Data and Definite Loops. Copyright (c) Pearson All rights reserved. Software Flaw.

Building Java Programs Chapter 2

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

-Alfred North Whitehead. Copyright Pearson Education, 2010 Based on slides by Marty Stepp and Stuart Reges from

Getting started with Java

Introduction to Computer Programming

Oct Decision Structures cont d

Chapter 4: Control structures

Name CIS 201 Midterm II: Chapters 1-8

CSCI 131, Midterm Exam 1 Review Questions This sheet is intended to help you prepare for the first exam in this course. The following topics have

Control Statements: Part 1

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

CS 112 Introduction to Programming

CS 112 Introduction to Programming

Course Outline. Introduction to java

Type boolean. Building Java Programs. Recap: Type boolean. "Short-circuit" evaluation. De Morgan's Law. Boolean practice questions.

Final Exam. COMP Summer I June 26, points

CONDITIONAL EXECUTION

Repetition, Looping. While Loop

CompSci 125 Lecture 09. Chapter 5: while, break and continue statements Iterators and the ArrayList Class

CompSci 125 Lecture 11

Lecture 2: Operations and Data Types

Conditionals and Loops

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

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Outline. Overview. Control statements. Classes and methods. history and advantage how to: program, compile and execute 8 data types 3 types of errors

CSC 1051 Data Structures and Algorithms I

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Spring 2010

Building Java Programs

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

Midterm Exam 2 CS 455, Fall 2013

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

CSC 1051 Villanova University. CSC 1051 Data Structures and Algorithms I. Course website:

Chapter 3 Structured Program Development

COE 211/COE 212 Computer/Engineering Programming. Welcome to Exam II Thursday December 20, 2012

Islamic University of Gaza Computer Engineering Dept. C++ Programming. For Industrial And Electrical Engineering By Instructor: Ruba A.

Introduction. C provides two styles of flow control:

CS 112 Introduction to Programming. Exercise: MatchDB. match1. Removing break. Solution I: Using break. Loop Patterns: break; Fencepost.

CS110D: PROGRAMMING LANGUAGE I

Java Coding 3. Over & over again!

CS 102 / CS Introduction to Programming Midterm Exam #1 - Prof. Reed Fall 2010

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Transcription:

Midterm Review Session Programming Problems For more practice: http://webster.cs.washington.edu:8080/practiceit/ Copyright 2009 by Pearson Education

Midterm Logistics Bring your UW Student ID card!! Will check your ID while you re leaving Bring your stuff with you while you exit (don t go back to seat) 60 minute exam -10 if you start early or try to write after time Any paper notes allowed, but NO ELECTRONICS Book OK, section handouts OK, past exams + solutions OK, etc. NO CALCULATORS! NO LAPTOPS! Aiming for a median of 80 Will be a curve if the exam is too hard, will not have a hurtful curve if exam is too easy Copyright 2009 by Pearson Education 2

About the Exam 100 pts, 20% of course grade Non-programming 61 pts Expressions (10), parameter mystery (12), if/else simulation (12), while loop simulation (12), assertions (15) Easy Programming 15 pts Medium Programming 15 pts Really Hard Programming 9 pts Topics covered: Chapters 1-5 Topics not covered: see website! Includes Graphics, printf, do/while, break Copyright 2009 by Pearson Education 3

More Exam Info Non-programming questions You do not need to show your work You are not allowed to use calculators (e.g. on expressions) Programming questions Are not graded on style, only external correctness Must write valid Java code that would compile Will not grade comments or pseudo-code Do NOT use abbreviations Do not have to write import statements Substantial partial credit is given for partially working solutions Write as much as you can: even a method header and the beginnings of a while loop could earn you partial points! More info on course website! Copyright 2009 by Pearson Education 4

General Tips BE AFRAID. Success on HW is not an indication of being prepared for the exam. Watch the clock! The hardest part about this exam will be the time limit. PRACTICE Q1-Q5 so that you can go through them fast and accurately! You want as much time as possible for the programming questions. Write as much as you can! A partial answer is better than none at all. Writing the correct method header will earn at least 1 point. Writing a partial loop, if statements, returns, etc. will points too Copyright 2009 by Pearson Education 5

General Tips Read the instructions now and learn the format so you won't be surprised tomorrow The format of the midterm will be the same as the samples For more practice, use Marty Stepp's Practice-it tool: http://webster.cs.washington.edu:8080/practiceit/ Copyright 2009 by Pearson Education 6

Concepts Review Some essential topics from Ch1-5 Copyright 2009 by Pearson Education

For-loops Use to repeat stuff when you know exactly* how much you want to repeat * Could also have a variable or expression that tells you exactly how much you want to repeat Nested for-loops: loops within a loop When you want to do task multiple times, and that task has repetition to it E.g. multiplication table Cumulative sum: variable that keeps a sum in progress and is updated repeatedly until summing is finished In general: think about bounds, possible fencepost issues Copyright 2009 by Pearson Education 8

Conditionals and Tests If/else statement: Decide between several logical choices While loop: Repeat unknown number of times Sentinel loops: repeat until a certain signal is seen (e.g. prompt until you see a -1) boolean: primitive type with a true or false value When to return?? Boolean flag is helpful Copyright 2009 by Pearson Education 9

Strings length() to get length of a String indexof/contains to search for substring within String equals/equalsignorecase to test equality startswith/endswith to test beginning/end of word substring(index1, index2) to get a piece of a String index1 inclusive, index2 exclusive Index 0 for first letter, index length() -1 for last letter + to concatenate Strings together Copyright 2009 by Pearson Education 10

Other miscellaneous stuff Scanner to prompt for input hasnext, hasnextint, hasnextdouble, hasnextline Random to get a random value To generate random number between [min, max]: r.nextint(range) + min Where range: max min + 1 r.nextint(51) + 50; // produces random value 50-100 Expressions stuff: % 10 and / 10 to get digits % 2 == 0 to test for even (!= for odd) Copyright 2009 by Pearson Education 11

Programming Practice Practice problems from previous exams Copyright 2009 by Pearson Education

Programming Tips Read the problems carefully: Does it want you to print a result, or return it? What values does the method use for computation? Are these values parameters, are they read from a Scanner, etc.? What type of value (if any) does the method return? Have you handled all special cases? What if the integer is 0, or negative? What if the string has no letters? What if there is only one word in the string? Many words? Copyright 2009 by Pearson Education 13

Practice Problem 1: Print Multiples Write a static method named printmultiples Takes two integers n and m as parameters and prints the first m multiples of n Assume m >= 1 Multiples are separated by commas printmultiples(3, 5); The first 5 multiples of 3 are 3, 6, 9, 12, 15 printmultiples(7, 3); The first 3 multiples of 7 are 7, 14, 21 Copyright 2009 by Pearson Education 14

Print Multiples Solution public static void printmultiples(int n, int times) { System.out.print("The first " + times + " multiples of " + n + " are " + n); for (int i = 2; i <= times; i++) { System.out.print(", " + i * n); System.out.println(); Copyright 2009 by Pearson Education 15

Practice Problem 2: Count Even Digits Write a static method named countevendigits Accepts an integer as its parameter and returns the number of even-valued digits in that number. Even digits: 0, 2, 4, 6, or 8 Assume value passed to your method is non-negative // 4 even digits: two 8s, the 4, and 6 int x = countevendigits(8346387) ; System.out.println("x is " + x); // x is 4 Copyright 2009 by Pearson Education 16

Count Even Digits Solution public static int countevendigits(int n) { int count = 0; while (n!= 0) { int digit = n % 10; n = n / 10; if (digit % 2 == 0) { count++; return count; Copyright 2009 by Pearson Education 17

Practice Problem 3: Cheerleader Write a static method named cheerleader Takes two params: number of lines of output number of "cheers" per line Cheer structure: 1 cheer: Go 2 cheers: Go Team Go 3 cheers: Go Team Go Team Go Each line indented by 3 spaces; first line at 0 spaces cheerleader(2, 1); Go Go cheerleader(4, 3); Go Team Go Team Go Go Team Go Team Go Go Team Go Team Go Go Team Go Team Go cheerleader(2, 4); Go Team Go Team Go Team Go Go Team Go Team Go Team Go Copyright 2009 by Pearson Education 18

Cheerleader Solution public static void cheerleader(int lines, int cheers) { for (int line = 1; line <= lines; line++) { for (int space = 1; space <= line - 1; space++) { System.out.print(" "); for (int cheer = 1; cheer <= cheers - 1; cheer++) { System.out.print("Go Team "); System.out.println("Go"); Copyright 2009 by Pearson Education 19

Practice Problem 4: Favorite Letter Write a static method named faveletter Accepts two parameters: Scanner for the console Favorite letter represented as a one-letter String Repeatedly prompt the user until two consecutive words are entered that start with that letter. Case sensitive (assume a Scanner named console was made earlier in code) faveletter(console, "y"); Looking for two "y" words. Type a word: hi Type a word: bye Type a word: yes Type a word: what? Type a word: yellow Type a word: yippee "y" is for "yippee" Print a message showing the last word typed. Copyright 2009 by Pearson Education 20

Favorite Letter Solution public static void faveletter(scanner console, String letter) { System.out.println("Looking for two \"" + letter + "\" words."); int count = 0; String word = ""; while (count < 2) { System.out.print("Type a word: "); word = console.next(); if (word.startswith(letter)) { count++; else { count = 0; System.out.println("\"" + letter + "\" is for \"" + word + "\""); Copyright 2009 by Pearson Education 21

Practice Problem 5: Random Rects Write a static method named randomrects Calculates and displays the area of randomly-generated rectangles. Each rectangle has random width and height btwn 1 and 10 inclusive Keep generating rectangles until an increasing sequence of four areas is printed. Stop when the last four rectangles generated have areas of a1, a2, a3 and a4 such that a1 < a2 < a3 < a4 randomrects(); w: 5, h: 6, area: 30 w: 10, h: 5, area: 50 w: 2, h: 8, area: 16 w: 4, h: 4, area: 16 w: 2, h: 9, area: 18 w: 8, h: 3, area: 24 w: 7, h: 2, area: 14 w: 3, h: 10, area: 30 w: 7, h: 9, area: 63 w: 9, h: 8, area: 72 End random rectangles. Copyright 2009 by Pearson Education 22

Random Rectangles Solution public static void randomrects() { Random r = new Random(); int last = 0; int count = 0; while (count!= 4) { int w = r.nextint(10) + 1; int h = r.nextint(10) + 1; int area = w * h; if (area <= last) { count = 1; // need to count first rect in sequence else { count++; System.out.println("w: " + w + ", h: " + h + ", area: " + area); last = area; System.out.println("End random rectangles."); Copyright 2009 by Pearson Education 23

Non-Programming Practice Tips on Questions 1-4 (For assertions help, see lecture 14) Copyright 2009 by Pearson Education

Expressions integer division and mod: quotient and remainder 14 / 3 is 4, 14 % 3 is 2 7 / 10 is 0, 7 % 10 is 7 precedence: ( ) before * / % before + - 5 + 2 * 6 - (1 + 7) % 3 5 + 2 * 6-8 % 3 5 + 12-8 % 3 5 + 12-2 17-2 15 Copyright 2009 by Pearson Education 25

Expressions 2 String concatenation: same precedence as integer + -, evaluated left-to-right with other + - operations 1 + 2 + "3" + 4 + 5 3 + "3" + 4 + 5 "33" + 4 + 5 "334" + 5 "3345" type promotion: done as needed when int and double are mixed 50 / 6 / 5.0 8 / 5.0 1.6 Copyright 2009 by Pearson Education 26

Expression questions Evaluate the following expressions: 16 / 3 + 3.2 * 2 8 / 7 + "5 / 4" + 8 / 3 88 % 10 % 3 * 16 / 10 29 / 3 / 2 / 4.0 + 3.6 * 2 1.4 + (3 + 2 * 6) / (8-14 / 3) * 2.2 Copyright 2009 by Pearson Education 27

Expression answers Correct answers: 16 / 3 + 3.2 * 2 11.4 8 / 7 + "5 / 4" + 8 / 3 "15 / 42" 88 % 10 % 3 * 16 / 10 3 29 / 3 / 2 / 4.0 + 3.6 * 2 8.2 1.4 + (3 + 2 * 6) / (8-14 / 3) * 2.2 8.0 Copyright 2009 by Pearson Education 28

Parameter mystery question What is the output of the following program? public class Mystery { public static void main(string[] args) { String john = "skip"; String mary = "george"; String george = "mary"; String fun = "drive"; String work = "john"; speak(mary, john, fun); speak(george, work, john); speak(fun, "george", "work"); speak(george, mary, john); speak(george, "john", "dance"); public static void speak(string mary, String john, String fun) { System.out.println(john + " likes to " + fun + " with " + mary); Copyright 2009 by Pearson Education 29

Parameter mystery tips Try making a table of the parameters passed to each call: public class Mystery { public static void main(string[] args) { String john = "skip"; String mary = "george"; String george = "mary"; String fun = "drive"; String work = "john"; speak(mary, john, fun); speak(george, work, john); speak(fun, "george", "work"); speak(george, mary, john); speak(george, "john", "dance"); george skip drive mary john skip drive george work mary george skip mary john dance public static void speak(string mary, String john, String fun) { System.out.println(john + " likes to " + fun + " with " + mary); Copyright 2009 by Pearson Education 30

Parameter mystery answer What is the output of the following program? public class Mystery { public static void main(string[] args) { String john = "skip"; String mary = "george"; String george = "mary"; String fun = "drive"; String work = "john"; speak(mary, john, fun); speak(george, work, john); speak(fun, "george", "work"); speak(george, mary, john); speak(george, "john", "dance"); skip likes to drive with george john likes to skip with mary george likes to work with drive george likes to skip with mary john likes to dance with mary public static void speak(string mary, String john, String fun) { System.out.println(john + " likes to " + fun + " with " + mary); Copyright 2009 by Pearson Education 31

While loop mystery question Given the following program, public static void mystery(int y) { int x = 0; int z = 0; while (y > 0) { System.out.print(x + " " + z + " "); x++; z = z + y % 10; y = y / 10; System.out.println(x + " " + z); What is the output of the following sequence of calls? mystery(0); mystery(8); mystery(32); mystery(72); mystery(184); mystery(8239); Copyright 2009 by Pearson Education 32

While loop mystery tips Keep track of each variable's value as it changes: public static void mystery(int y) { int x = 0; int z = 0; while (y > 0) { System.out.print(x + " " + z + " "); x++; z = z + y % 10; y = y / 10; System.out.println(x + " " + z); What is the output of the following call? mystery(184); Sometimes, these problems are performing real computations in disguise. What is this problem really doing? x y z 0 184 0 1 18 4 2 1 12 3 0 13 Copyright 2009 by Pearson Education 33

While loop mystery answers Given the following program, public static void mystery(int y) { int x = 0; int z = 0; while (y > 0) { System.out.print(x + " " + z + " "); x++; z = z + y % 10; y = y / 10; System.out.println(x + " " + z); What is the output of the following sequence of calls? mystery(0); 0 0 mystery(8); 0 0 1 8 mystery(32); 0 0 1 2 2 5 mystery(72); 0 0 1 2 2 9 mystery(184); 0 0 1 4 2 12 3 13 mystery(8239); 0 0 1 9 2 12 3 14 4 22 Copyright 2009 by Pearson Education 34