Unformatted text preview:

Chapter 4 Study Guide This test is hard Therefore make sure you understand why the code does what it does You can test the code by doing cout statements You will have to write 2 small programs on the test One will be a program where you use a for loop to add users inputs The other will be a simple if then else statement 1 What will be the values of x and y as a result of the following code 2 What will be the value of x after the following code is executed int x 12 y 4 x y int x y 3 z 5 x y z 3 4 int x 10 while x 120 x 10 int x 10 y 20 while y 120 x y 5 What will be the value of x after the following code is executed 6 What will be the value of x after the following code is executed 7 In the following code what values could be read into number to terminate the while loop Scanner keyboard new Scanner System in System out print Enter a number int number keyboard nextInt while number 100 number 500 System out print Enter another number number keyboard nextInt 8 What will be the value of x after the following code is executed int x 10 do x 10 while x 5 int x 11 do x 20 while x 100 10 9 How many times will the following do while loop be executed 11 How many times will the following for loop be executed for int count 0 count 11 count System out println Java is great 12 What will be the value of x after the following code is executed int x 10 for int y 5 y 20 y 5 x y 13 14 15 18 19 16 Given the following statement which statement will write Calvin to the file DiskFile txt PrintWriter diskOut new PrintWriter DiskFile txt 17 When using the PrintWriter class which of the following import statements would you write near the top of your program 20 What will be the values of x and y as a result of the following code 21 What will be the value of x after the following code is executed int x 2 y 5 x y int x y 5 z 3 x y z a 3 b 4 c 5 d 6 24 What will be the value of x after the following code is executed 25 What will be the value of x after the following code is executed 22 23 int x 10 while x 100 x 10 int x 20 y 20 while y 100 x y y 20 26 In the following code what values could be read into number to terminate the while loop Scanner keyboard new Scanner System in System out print Enter a number int number keyboard nextInt while number 100 number 500 System out print Enter another number number keyboard nextInt 27 What will be the value of x after the following code is executed int x 30 do x 20 28 How many times will the following do while loop be executed while x 5 int x 1 do x 20 while x 100 31 What will be printed after the following code is executed for int number 5 number 15 number 3 System out print number a 5 6 7 8 9 10 11 12 13 14 15 b 5 8 11 14 17 c 5 8 11 14 d This is an invalid for statement 29 30 32 33 34 35 37 38 TRUE FALSE 1 The do while loop is a pre test loop 2 In the for loop the control variable cannot be initialized to a constant value and tested against a constant value 3 When the break statement is encountered in a loop all the statements in the body of the loop that appear after it are ignored and the loop prepares for the next iteration 4 You can use the PrintWriter class to open a file for writing and write data to it 5 The do while loop must be terminated with a semicolon 6 In a for statement the control variable can only be incremented 7 When the continue statement is encountered in a loop all the statements in the body of the loop that appear after it are ignored and the loop prepares for the next iteration 8 A file must always be opened before using it and closed when the program is finished using it 9 When you open a file with the PrintWriter class the class can potentially throw an IOException 10 When you pass the name of a fi le to the PrintWriter constructor and the file already exists it will be erased and a new empty file with the same name will be created 11 An infinite loop is a kind of a loop does not contain within itself a way to terminate 12 A pre test loop is a type of loop which will always be executed at least once 13 A loop that repeats a specific number of times is known as a counter controlled loop 14 A sentinel is a value that signals when the end of a list of values has been reached 15 Before entering a loop to compute a running total the program should first Set the accumulator where the total will be kept to an initial value usually zero 16 A for loop is ideal in situations where the exact number of iterations is known When using the PrintWriter class the following import statements must be written near 17 the top of your program import java io 18 The following statements will open a file named MyFile txt and allow you to append data to its existing contents FileWriter fwriter new FileWriter MyFile txt true PrintWriter outFile new PrintWriter fwriter 19 Assume that inputFile references a Scanner object that was used to open a file The following while loop shows the correct way to read data from the file until the end of the file is reached while inputFile hasNext 20 In all but rare cases loops must contain within themselves a way to terminate 21 The do while and for are pre test loops 22 A loop that executes as long as a particular condition exists is called a conditional loop 23 A for loop normally performs each of these steps a b initializes a control variable to a starting value tests the control variable by comparing it to a maximum minimum value and terminate when the variable reaches that value c updates the control variable during each iteration 24 Running total is a sum of numbers that accumulates with each iteration of a loop 25 A sentinel value is a special value that cannot be mistaken as a member of the listand signals that there are no more values to be entered 26 A do while loop is is ideal in situations where you always want the loop to iterate at least once 27 The following will open a file named MyFile txt and allow you to …


View Full Document

MGC CSCI 1302 - Chapter 4 Study Guide

Download Chapter 4 Study Guide
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Chapter 4 Study Guide and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Chapter 4 Study Guide and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?