Chapter 3: Program Statements

Similar documents
CS111: PROGRAMMING LANGUAGE II

Program Development. Chapter 3: Program Statements. Program Statements. Requirements. Java Software Solutions for AP* Computer Science A 2nd Edition

Chapter 3: Program Statements

Chapter 2: Objects and Primitive Data

Conditionals and Loops

Program Development. Java Program Statements. Design. Requirements. Testing. Implementation

Chapter 4: Control structures. Repetition

Chapter 4: Control structures

L o o p s. for(initializing expression; control expression; step expression) { one or more statements }

Conditionals. For exercises 1 to 27, indicate the output that will be produced. Assume the following declarations:

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

Chapter 4: Conditionals and Loops

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Repetition, Looping. While Loop

CMPT 125: Lecture 4 Conditionals and Loops

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

CS1004: Intro to CS in Java, Spring 2005

Chapter 4: Conditionals and Loops

CSC 1051 Algorithms and Data Structures I. Midterm Examination March 1, Name: KEY A

1007 Imperative Programming Part II

Java Flow of Control

AYBUKE BUYUKCAYLI KORAY OZUYAR MUSTAFA SOYLU. Week 21/02/ /02/2007 Lecture Notes: ASCII

Term 1 Unit 1 Week 1 Worksheet: Output Solution

Programming Projects: 2.1, 2.3, 2.4, 2.7, 2.8, 2.13

Repetition, Looping CS101

Building Java Programs

Java Coding 3. Over & over again!

Practice Midterm 1 Answer Key

Chapter 3. Selections

SAMPLE QUESTIONS FOR DIPLOMA IN INFORMATION TECHNOLOGY; YEAR 1

CS 112 Introduction to Programming

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

CS302: Self Check Quiz 2

Algorithms and Conditionals

Control Structures II. Repetition (Loops)

Comparing Data. Comparing Floating Point Values. Comparing Float Values. CS257 Computer Science I Kevin Sahr, PhD

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

Repetition CSC 121 Fall 2014 Howard Rosenthal

Introduction. C provides two styles of flow control:

Examples of Using the ARGV Array. Loop Control Operators. Next Operator. Last Operator. Perl has three loop control operators.

Lab 8: IF statement. Conditionals and Loops. Copyright 2012 Pearson Education, Inc.

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

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

Midterm Practice Problems

Building Java Programs

CS Week 5. Jim Williams, PhD

Conditional Programming

CS111: PROGRAMMING LANGUAGE II

Computing Science 114 Solutions to Midterm Examination Tuesday October 19, In Questions 1 20, Circle EXACTLY ONE choice as the best answer

Simple Java Programming Constructs 4

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

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

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

COMP 202. Programming With Iterations. CONTENT: The WHILE, DO and FOR Statements. COMP Loops 1

Control Structures in Java if-else and switch

CS150 Intro to CS I. Fall Fall 2017 CS150 - Intro to CS I 1

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

e) Implicit and Explicit Type Conversion Pg 328 j) Types of errors Pg 371

CSC 1051 Algorithms and Data Structures I. Midterm Examination March 2, Name:

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 25, Name: KEY A

Chapter 2 Exercise Solutions

Chapter Goals. Contents LOOPS

Exam 1. CSC 121 Spring Lecturer: Howard Rosenthal. March 1, 2017

Assignment 2.4: Loops

Introduction to Software Development (ISD) Week 3

Control Statements: Part 1

CompSci 125 Lecture 11

Review for Test 1 (Chapter 1-5)

Flow of Control. Chapter 3

Conditionals and Loops Chapter 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

CMPT 125: Lecture 3 Data and Expressions

Topic 14 while loops and loop patterns

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

Top-Down Program Development

Chapter 4: Conditionals and Loops

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

CS 106 Introduction to Computer Science I

Final Exam. COMP Summer I June 26, points

Program Elements -- Introduction

COMP 202 Java in one week

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

Activity 6: Loops. Content Learning Objectives. Process Skill Goals

Chapter 4 Loops. int x = 0; while ( x <= 3 ) { x++; } System.out.println( x );

COMP 202 Recursion. CONTENTS: Recursion. COMP Recursion 1

CS 302: INTRODUCTION TO PROGRAMMING. Lectures 7&8

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

CS 106 Introduction to Computer Science I

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

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

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

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

STUDENT LESSON A12 Iterations

Midterm Practice Problems - answer key Answers appear in boldface.

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

4 WORKING WITH DATA TYPES AND OPERATIONS

REPETITION CONTROL STRUCTURE LOGO

Topics. Introduction to Repetition Structures Often have to write code that performs the same task multiple times. Controlled Loop

Comp 170 Test 1 SAMPLE June 8, 2000

Flow Control. Key Notion. Statement Categories. 28-Oct-10

Transcription:

Chapter 3: Program Statements Multiple Choice 1 e 2 d 3 e 4 d 5 c 6 a 7 b 8 c 9 d 10 a True/False 1 T 2 F 3 F 4 F 5 T 6 F 7 T 8 T 9 F Short Answer 31 What happens in the MinOfThree program if two or more of the values are equal? The program still prints the lowest value Because only less than comparisons are made, the comparison of two equal values produces a false result If two values are equal, and lower than the third value, then one of the two lower but equal values is printed If all three values are equal, then this value is printed Which version of the equal value is irrelevant If exactly two of the values are equal, does it matter whether the equal values are lower or higher than the third? The correct result is determined in either case If the two equal values are lower than the third, then one of the two lower but equal values is printed If the two equal values are higher than the third, then the third value is printed 32 What is wrong with the following code fragment? Rewrite it so that it produces correct output if (total == MAX) if (total < sum) Systemoutprintln ("total == MAX and is < sum"); else Systemoutprintln ("total is not equal to MAX"); Despite the indentation, the else clause is associated with the immediately preceding if rather than the first if The program will produce the correct output if it is rewritten as: if (total == MAX) if (total < sum) Systemoutprintln ( total == MAX and is < sum ); else Systemoutprintln ( total is not equal to MAX ); S 21

S 22 Lewis/Loftus/Cocking, 3/e: Chapter 3 33 What is wrong with the following code fragment? Will this code compile if it is part of an otherwise valid program? Explain if (length = MIN_LENGTH) Systemoutprintln ("The length is minimal"); The assignment operator (=) is used erroneously in place of the equality operator (==) Hence, it will not compile in an otherwise valid program 34 What output is produced by the following code fragment? int num = 87, max = 25; if (num >= max*2) Systemoutprintln ("apple"); Systemoutprintln ("orange"); Systemoutprintln ("pear"); apple orange pear The second println statement is improperly indented 35 What output is produced by the following code fragment? int limit = 100, num1 = 15, num2 = 40; if (limit <= limit) if (num1 == num2) Systemoutprintln ("lemon"); Systemoutprintln ("lime"); Systemoutprintln ("grape"); The output is: lime grape 36 Put the following list of strings in lexicographic order as if determined by the compareto method of the String class Consult the Unicode chart in Appendix C "fred" "Ethel" "?-?-?-?" "([])" "Lucy" "ricky" "book" "******" "12345" " " "HEPHALUMP" "bookkeeper" "6789" ";+<?"

Lewis/Loftus/Cocking, 3/e: Chapter 3 S 23 "^^^^^^^^^^" "hephalump" The strings in lexicographic order: " " "******" "12345" "6789" ";+<?" "?-?-?-?" "Ethel" "HEPHALUMP" "Lucy" "^^^^^^^^^^" "book" "bookkeeper" "fred" "hephalump" "ricky" "([])" 37 What output is produced by the following code fragment? int num = 1, max = 20; while (num < max) if (num%2 == 0) Systemoutprintln (num); num++; 2 4 6 8 10 12 14 16 18 38 What output is produced by the following code fragment? for (int num = 0; num <= 200; num += 2) Systemoutprintln (num); 0 2

S 24 Lewis/Loftus/Cocking, 3/e: Chapter 3 4 6 198 200 39 What output is produced by the following code fragment? for (int val = 200; val >= 0; val -= 1) if (val % 4!= 0) Systemoutprintln (val); 199 198 197 195 5 3 2 1 310 Transform the following while loop into a for loop (make sure it produces the same output) int num = 1; while (num < 20) num++; Systemoutprintln (num); This code can be written using a for loop as follows: for (int num = 1; num < 20;) num++; Systemoutprintln (num); 311 What is wrong with the following code fragment? What are three distinct ways it could be changed to remove the flaw? count = 50; while (count >= 0) Systemoutprintln (count); count = count + 1; The loop is infinite because count initially is greater than zero, and continues to increase in value The flaw can be removed by (1) decrementing rather than incrementing count, (2) initializing count to 0 and using, as the condition of the

Lewis/Loftus/Cocking, 3/e: Chapter 3 S 25 while loop, count <= 50, and (3) picking an upper limit and using, as the condition of the while loop, count <= upperlimit 313 Write a while loop that verifies that the user enters a positive integer value Systemoutprint ( Enter a positive integer: ); number = scannextint(); while (number <= 0) Systemoutprint ( Enter a positive integer: ); number = scannextint(); 314 Write a code fragment that reads and prints integer values entered by a user until a particular sentinel value (stored in SENTINEL) is entered Do not print the sentinel value Systemoutprint ( Enter an integer ( + SENTINEL + to quit): ); number = scannextint(); while (number!= SENTINEL) Systemoutprintln (number); number = scannextint(); 315 Write a for loop to print the odd numbers from 1 to 99 (inclusive) for (int value = 1; value <= 99; value +=2) Systemoutprintln (value); 316 Write a for loop to print the multiples of 3 from 300 down to 3 for (int value = 300; value >= 3, value -= 3) Systemoutprintln (value); 317 Write a foreach loop that prints all possible values of an enumerated type called Month for (Month m : Monthvalues()) Systemoutprintln (m); 318 Write a code fragment that reads 10 integer values from the user and prints the highest value entered int max, number; Systemoutprint ("Enter an integer: "); max = scannextint(); for (int count = 2; count <= 10; count++) Systemoutprint ("Enter another integer: "); number = scannextint(); if (number > max) max = number; Systemoutprintln ("The highest value is :" + max); 319 Write a code fragment that determines and prints the number of times the character 'a' appears in a String object called name int count = 0; for (int position = 0; position < namelength(); position++) if (namecharat(position) == 'a') count++; Systemoutprintln ("The character \'a\' appears "

S 26 Lewis/Loftus/Cocking, 3/e: Chapter 3 + count + " time(s)"); 320 Write a code fragment that prints the characters stored in a String object called str backwards for (int position = strlength()-1; position >= 0; position--) Systemoutprint (strcharat(position)); Systemoutprintln(); 321 Write a code fragment that prints every other character in a String object called word starting with the first character for (int position = 0; position < wordlength(); position +=2) Systemoutprintln(wordcharAt(position)); AP-Style Multiple Choice 1 D 2 A 3 A 4 E 5 D 6 C