Java: Chapter 4
27) What will be the values of x and y as a result of the following code? int x = 12, y = 5; x += y--; A) x = 12, y = 5 B) x = 16, y = 4 C) x = 17, y = 5 D) x = 17, y = 4
Answer: D
5) If a loop does not contain within itself a way to terminate, it is called a(n): A) while loop B) do-while loop C) for loop D) infinite loop
Answer: D
1) The increment operator is: A) ++ B) -- C) *= D) -=
Answer: A
19) 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; A) 40 B) 25 C) 30 D) Invalid for statement
Answer: A
25) Which of the following will open a file named MyFile.txt and allow you to append data to its existing contents? A) FileWriter fwriter = new FileWriter("MyFile.txt", true); PrintWriter outFile = new PrintWriter(fwriter); B) FileWriter fwriter = new FileWriter("MyFile.txt"); PrintWriter outFile = new PrintWriter(fwriter); C) PrintWriter outfile = new PrintWriter("MyFile.txt", true); D) PrintWriter outfile = new PrintWriter(true, "MyFile.txt");
Answer: A
6) Each repetition of a loop is known as what? A) An iteration B) A cycle C) An execution D) A Lap
Answer: A
9) If you are using a block of statements, don't forget to enclose all of the statements in a set of: A) Braces B) Double quotes C) Semicolons D) Parentheses
Answer: A
10) What will be the value of x after the following code is executed? int x = 10; while (x < 100) { x += 10; } A) 90 B) 100 C) 110 D) This is an infinite loop.
Answer: B
12) ________ is the process of inspecting data given to the program by the user and determining if it is valid. A) Data parsing B) Input validation C) User authentication D) Defensive coding
Answer: B
16) How many times will the following do-while loop be executed? int x = 11; do { x += 20; } while (x > 100); A) 0 B) 1 C) 4 D) 5
Answer: B
21) Before entering a loop to compute a running total, the program should first do this. A) Read all the values into main memory B) Set the accumulator where the total will be kept to an initial value, usually zero C) Know exactly how many values there are to total D) Set all variables to zero
Answer: B
3) What will be the value of x after the following code is executed? int x, y = 4, z = 6; x = (y++) * (++z); A) 24 B) 28 C) 30 D) 35
Answer: B
4) This is a control structure that causes a statement or group of statements to repeat. A) Block B) Loop C) Prefix mode D) Body
Answer: B
7) This variable controls the number of times that the loop iterates. A) Counter variable B) Loop control variable C) Running total D) Decrement variable
Answer: B
8) This type of loop will always be executed at least once. A) pre-test loop B) post-test loop C) sentinel loop D) for loop
Answer: B
13) This type of loop allows the user to decide the number of iterations. A) Counter-controlled loop B) Dynamically executed loop C) User controlled loop D) Infinite loop
Answer: C
15) What will be the value of x after the following code is executed? int x = 10; do { x *= 20; } while (x > 5); A) 10 B) 200 C) This is an infinite loop. D) The loop will not be executed, the initial value of x > 5.
Answer: C
17) A loop that repeats a specific number of times is known as a(n): A) sentinel loop B) conditional loop C) counter-controlled loop D) infinite loop
Answer: C
18) How many times will the following for loop be executed? for (int count = 10; count <= 21; count++) System.out.println("Java is great!!!"); A) 1 B) 10 C) 12 D) 0
Answer: C
2) What will be the values of x and y as a result of the following code? int x = 25, y = 8; x += y++; A) x = 25, y = 8 B) x = 33, y = 8 C) x = 33, y = 9 D) x = 34, y = 9
Answer: C
22) This type of loop is ideal in situations where the exact number of iterations is known. A) while loop B) do-while loop C) for loop D) if statement
Answer: C
26) Assume that inputFile references a Scanner object that was used to open a file. Which of the following while loops shows the correct way to read data from the file until the end of the file is reached? A) while (inputFile != null) { ... } B) while (!inputFile.EOF) { ... } C) while (inputFile.hasNext()) { ... } D) while (inputFile.nextLine == " ") { ... }
Answer: C
11) What will be the value of x after the following code is executed? int x = 10, y = 20; while (y < 100) { x += y; } A) 90 B) 110 C) 210 D) This is an infinite loop.
Answer: D
14) 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(); } A) Numbers less than 100 or greater than 500 B) Numbers in the range 100 - 499 C) Numbers in the range 100 - 500 D) The boolean condition can never be true.
Answer: D
20) This is a value that signals when the end of a list of values has been reached. A) Terminal value B) Final value C) End value D) Sentinel
Answer: D
23) Given the following statement, which statement will write "Calvin" to the file DiskFile.txt? PrintWriter diskOut = new PrintWriter("DiskFile.txt"); A) System.out.println(diskOut, "Calvin"); B) DiskFile.println("Calvin"); C) PrintWriter.println("Calvin"); D) diskOut.println("Calvin");
Answer: D