Honors Computer Programming 1-2. Chapter 5 Exam Prep

Similar documents
Chapter 4: Control Structures I

In this chapter, you will:

Strings. Strings and their methods. Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

Programming with Java

Oct Decision Structures cont d

- Thus there is a String class (a large class)

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

CS1150 Principles of Computer Science Math Functions, Characters and Strings (Part II)

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

Conditional Execution

Logic & program control part 2: Simple selection structures

STUDENT LESSON A12 Iterations

Chapter Goals. 3.1 The if Statement. Contents 1/30/2013 DECISIONS

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4

Topics. Chapter 5. Equality Operators

Flow of Control. Chapter 3

COSC 123 Computer Creativity. Java Decisions and Loops. Dr. Ramon Lawrence University of British Columbia Okanagan

Building Java Programs

Programming: Java. Chapter Objectives. Control Structures. Chapter 4: Control Structures I. Program Design Including Data Structures

Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4

Selected Questions from by Nageshwara Rao

Java Coding 2. Decisions, decisions!

Introduction to Computer Science Unit 2. Exercises

Intro to Strings. Lecture 7 CGS 3416 Spring February 13, Lecture 7 CGS 3416 Spring 2017 Intro to Strings February 13, / 16

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Simple Control Flow: if-else statements

Flow of Control. Chapter 3

Full file at

Computer Programming I - Unit 5 Lecture page 1 of 14

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

CS Week 5. Jim Williams, PhD

The action of the program depends on the input We can create this program using an if statement

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

Building Java Programs

The if/else Statement

Advanced if/else & Cumulative Sum

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

Arrays: An array is a data structure that stores a sequence of values of the same type. The data type can be any of Java s primitive types:

Java Basic Programming Constructs

Chapter 3. Ch 1 Introduction to Computers and Java. Selections

Building Java Programs

Java Coding 3. Over & over again!

CS 101 Fall 2006 Midterm 1 Name: ID:

FLOW CONTROL. Author: Boaz Kantor The Interdisciplinary Center, Herzliya Introduction to Computer Science Winter Semester

Section 002 Spring CS 170 Exam 1. Name (print): Instructions:

Intro to Strings. Lecture 7 COP 3252 Summer May 23, 2017

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

CS302: Self Check Quiz 2

Introduction to Bioinformatics

CSE 114 Computer Science I

Term 1 Unit 1 Week 1 Worksheet: Output Solution

CS 1331 Exam 1 ANSWER KEY

Variables. Store information needed by the program

Full file at

Decision Making in C

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

Chapter 5 Decisions. Big Java by Cay Horstmann Copyright 2009 by John Wiley & Sons. All rights reserved.

Chapter 5. Selection Statements. Animated Version The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Key Points. COSC 123 Computer Creativity. Java Decisions and Loops. Making Decisions Performing Comparisons. Making Decisions

Computer Programming, I. Laboratory Manual. Experiment #6. Loops

4. If the following Java statements are executed, what will be displayed?

CS 112 Introduction to Programming

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

AP CS Unit 3: Control Structures Notes

Gaddis: Starting Out with Java: From Control Structures through Objects, 6/e

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Midterm Examination

Arrays. Weather Problem Array Declaration Accessing Elements Arrays and for Loops Array length field Quick Array Initialization Array Traversals

Advanced Object Concepts

10/30/2010. Introduction to Control Statements. The if and if-else Statements (cont.) Principal forms: JAVA CONTROL STATEMENTS SELECTION STATEMENTS

Expression statements are formed by adding a semicolon to the end of certain types of expressions.

Starting Out with Java: From Control Structures through Data Structures 3e (Gaddis and Muganda) Chapter 2 Java Fundamentals

COMP-202: Foundations of Programming. Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015

Key Differences Between Python and Java

Full file at

Question: Total Points: Score:

COE 212 Engineering Programming. Welcome to Exam I Tuesday November 11, 2014

AP Programming - Chapter 6 Lecture

Exam 2, Version 2. For the following code, mark True or False for statements 1.8 to 1.10.

Flow of Control. Chapter 3. Chapter 3 1

Arun chakravarthy Alagar samy. Technical Skill. Java Code Challenge - Basic Java

Midterm Examination (MTA)

Tutorial # 4. Q1. Evaluate the logical (Boolean) expression in the following exercise

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

STUDENT LESSON A10 The String Class

Section 004 Spring CS 170 Exam 1. Name (print): Instructions:

Wed. Sep 27th 7-8PM EE170

10 Control and Iteration

CS 302: Introduction to Programming in Java. Lecture 9

9 Working with the Java Class Library

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

Operators. Java operators are classified into three categories:

Faculty of Science COMP-202B - Introduction to Computing I (Winter 2009) Midterm Examination

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

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

CS 1331 Exam 1. Fall Failure to properly fill in the information on this page will result in a deduction of up to 5 points from your exam score.

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

Chapter 3: Program Statements

SELECTION. (Chapter 2)

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal

More on Strings. String methods and equality. Mairead Meagher Dr. Siobhán Drohan. Produced by: Department of Compu<ng and Mathema<cs h=p://

Transcription:

Honors Computer Programming 1-2 Chapter 5 Exam Prep

1. True or False: curly braces When writing an if statement, you MUST use if { }. false: you must use parenthesis if ( )

2. In Java, to implement a choice between alternatives use the if/ statement. or if (...)...... switch statement switch (...) { case 1:... case 2:...... default:... }

3. State what the following symbols in Java code translate to in English: A >= greater than or equal to C or B && and D = = is equal to E!= not equal to

4. Compare two primitive values: are both double values, then is the best way to test for equality. If distance1 and distance2 (distance1 = = distance2) true: primitive values can be compared using the = = operator.

5. Compare two String objects: Given the code shown, String title1 = "mineral"; String title2 = "mine"; String title3 = title1.substring(0, 4); (title2 = = title3) is the best way to test for equality. title1 title2 m i n e r a l 0 1 2 3 4 5 6 m i n e 0 1 2 3 title3 m i n e 0 1 2 3 false: you must use the equals method to test for equality of String objects (title2.equals(title3))

6. Compare two Rectangle objects: Rectangle objects, for equality. then (box1 = = box2) If box1 and box2 are both is the best way to test false: you must use the equals method to test for equality of Rectangle or Rectangle2D objects (box1.equals(box2))

7A 7B Give What an is the example output: of how to use the compareto method? String System.out.println("Hello".compareTo( Cowboy")); str1 = "car"; String str2 = "apple"; if (str1.compareto(str2) < 0) System.out.println(str1 + " " + str2); System.out.println(str2 + " " + str1); In the alphabet, the letter H which begins the word Hello comes 5 letters after the letter C which begins the word Cowboy. if (str1.compareto("pear") < 0) System.out.println(str1 + " pear"); System.out.println("pear " + str1); Therefore, the answer is 5 if ("apple".compareto(str2) = = 0) System.out.println("equal strings"); System.out.println("unequal strings"); str1.compareto("pear") "apple".compareto(str2) str1.compareto(str2) define returns two returns string -132 0 since objects str1 str2 comes is also before after "apple" str2 "pear" and since and -13 2 since > < 0 0 "apple 0 "car = = 0 pear" car" "equal is printed strings" (lexicographic is printed order)

8. Given the code fragment shown, what will be printed? boolean answer = 6 < 10; System.out.println("The result is " + answer); answer is set equal to true answer: The result is true

9. Given the code fragment shown, what will be printed? int count = 3; if(count > 2) System.out.println("The result is " + count); System.out.println(0); count is assigned a value of 3 since count = 3 and 3 > 2 the condition is true... answer: The result is 3

10. Use DeMorgan s Laws to restate each of the following:!(a && b) is equivalent to!a!b!(a b) is equivalent to!a &&!b A!(number < 5 && string1.equals("harry")) number >= 5!string1.equals("Harry") B!(month = = 3 month = = 4) month!= 3 && month!= 4

11. Give an example of a short circuit evaluation and explain what happens when the expression is evaluated. if (str!= null && str.equals(str2))... First the expression str!= null is evaluated. If str!= null, then the expression str.equals(str2) is evaluated. But if str = = null, then the test evaluates to false and the second condition is not evaluated. Note that str.equals(str2) would result in a run-time error if str were equal to null.

12. If your income was $20,000 then your bracket should be the 15% bracket. What percent is returned by each method for this income of $20,000: Method I 0.28 Method II 0.15 Method III 0.28 Method III only method II worked for the 15% bracket if (income <= > 51900) 21450) bracket = 0.15; 0.31; test test true true false bracket undefined bracket = = 0.15 if (income if (income <= 51900) 21450) <= 21450) test test true true bracket bracket = = 0.28 0.15 bracket = 0.28; 0.15; not executed if (income > <= 51900) bracket still 0.15 test test false true bracket = 0.28 bracket = 0.31; 0.28; bracket still 0.28 return return bracket; return return 0.28 0.28 0.15

12. Continued... If your income was $40,000 then your bracket should be the 28% bracket. What percent is returned by each method for this income of $40,000: Method I 0.28 Method II 0.15 Method III 0.28 methods I and III worked for the 28% bracket Method II Method III if (income <= 21450) 51900) if (income > 51900) bracket = 0.15; bracket = 0.31; if (income if (income <= 51900) <= 21450) if (income <= 21450) bracket = 0.28; bracket = 0.15; if (income > 51900) if (income <= 51900) bracket = 0.31; bracket = 0.28; test false true test false bracket undefined = 0.15 bracket undefined not test executed true test false bracket = still 0.28 0.15 bracket still undefined not test executed false test true bracket still 0.28 0.15 return bracket; return 0.28 return bracket; 0.15 return 0.28 bracket = 0.28

12. Continued... If your income was $80,000 then your bracket should be the 31% bracket. What percent is returned by each method for this income of $80,000: Method I 0.31 Method II 0.31 Method III 0.31 all methods worked for the 31% bracket Method III Method II if (income <= > 51900) 21450) if (income <= 51900) bracket = 0.15; 0.31; bracket = 0.15; if (income <= 51900) 21450) if (income 0.28; <= 21450) bracket = 0.15; bracket > = 51900) 0.28; if (income <= 51900) bracket 0.31; bracket = = 0.28; 0.31; test false true test false bracket undefined = 0.31 bracket undefined test false test false bracket undefined still 0.31 bracket undefined test true false bracket = 0.31 bracket still 0.31 executed bracket = 0.31 return bracket; return bracket; return return 0.31 0.31

12. Summary... Method I worked for the 28% and 31% brackets. Method II worked for the 15% and 31% brackets. worked for the 28% and 31% brackets. Method III None of these methods worked all the time. proper style but needs to fixed up. Method II is the original Method II if (income <= 51900) bracket = 0.15; if (income <= 21450) bracket = 0.28; bracket = 0.31; return bracket; corrected Method II if (income > 51900) bracket = 0.31; if (income <= 21450) bracket = 0.15; bracket = 0.28; return bracket;

13. Refer to the code at the right. Scanner console = new Scanner(System.in); System.out.print("Enter a month number: "); int month = console.nextint( ); String str = ""; // empty string B Write if- code that would correctly implement the intent of the switch statement shown. switch(month) { case 9: case 4: case 6: case 11: str = "30 days"; case 2: str = "28 days"; default: str = "31 days"; } System.out.println("Number of days: " + str); A What is the output for the switch statement shown if the input number is 6? If you are not sure of your answer, test the code. if(month = = 9 month = = 4 month = = 6 month = = 11) str = "30 days"; if(month = = 2) Since str there = "28 are days"; no break statements, str is first set to "30 days" then changed to "28 days" then finally changed to "31 days". str = "31 days"; answer Number of days: 31 days