CSE 110 Chapter 04a Review

Description

This is the first half of a selected set of review questions from Chapter 04 of the textbook Java For Everyone: Late Objects (ISBN: 9781118063316).
Branden Roper
Flashcards by Branden Roper, updated more than 1 year ago
Branden Roper
Created by Branden Roper over 6 years ago
1068
0

Resource summary

Question Answer
1) Which of the following loops executes the statements inside the loop before checking the condition? a) for b) while c) do d) do-for c
2) How many times will the following loop run? int i = 0; while (i < 10) { System.out.println(i); i++; } a) 0 b) 8 c) 9 d) 10 d
3) What is the output of the code snippet given below? int i = 0; while (i != 9) { System.out.println("" + i); i = i + 2; } a) No output b) 0 2 4 6 8 c) 10 12 14 16 18 …. (infinite loop) d) 0 2 4 6 8 10 12 14 …. (infinite loop) d
4) How many times does the code snippet given below display "Loop Execution"? int i = 1; while (i != 10) { System.out.println ("Loop Execution"); i++; } a) Infinite times b) 8 times c) 9 times d) 10 times c
5) What is the output of the code fragment given below? int i = 0; int j = 0; while (i < 27) { i = i + 2; j++; } System.out.println("j=" + j); a) j=27 b) j=12 c) j=13 d) j=14 d
6) What is the output of the following code snippet? int i = 1; while (i < 10) { System.out.print(i + " "); i = i + 2; if (i == 5) { i = 9; } } a) 1 3 5 b) 1 3 9 c) 1 3 5 7 9 d) 1 3 5 9 b
9) Which error type does the "off-by-one" error belong to? a) Syntax error b) Compile-time error c) Run-time error d) Infinite loop c
10) How many times does the following code fragment display "Hi"? int i = 10; while (i >= 0) { System.out.println("Hi"); i--; } a) 9 times b) 10 times c) 11 times d) 12 times c
11) What is the output of the following code fragment? int i = 1; int sum = 0; while (i <= 11) { sum = sum + i; i++; } System.out.println("The value of sum is " + sum); a) The value of sum is 65 b) The value of sum is 66 c) The value of sum is 55 d) The value of sum is 56 b
12) How many times does the loop execute in the following code fragment? int i; for (i = 0; i < 50; i = i + 4) { System.out.println(i); } a) 11 b) 12 c) 13 d) 14 c
15) Which loop does not check a condition at the beginning of the loop? I. The do loop II. The while loop III. The for loop a) I and II b) I and III c) I only d) III only c
20) What is the sentinel value in the following code snippet? public static void main(String[] args) { int age = 0; int sumOfAges = 0; int stop = 1; Scanner reader = new Scanner(System.in); System.out.println("Enter an age (-1 to stop): "); age = reader.nextInt(); while (age != -1) { sumOfAges = sumOfAges + age; System.out.println("Enter an age (-1 to stop): "); age = reader.nextInt(); } System.out.println("Sum of ages " + sumOfAges); return 0; } a) 0 b) 1 c) 2 d) -1 d
21) See text document. b
22) What will be the output of the following code snippet? boolean token = false; while (token) { System.out.println("Hello"); } a) “Hello” will be displayed infinite times. b) No output because of compilation error. c) No output after successful compilation. d) “Hello” will be displayed only once. c
23) What will be the output of the following code snippet? boolean token = false; do { System.out.println("Hello"); } while (token); a) “Hello” will be displayed infinite times. b) No output because of compilation error. c) No output after successful compilation. d) “Hello” will be displayed only once. d
24) What is the output of the following code snippet? int i = 1; while (i <= 10) { System.out.println("Inside the while loop"); i = i + 10; } a) No output because of compilation error. b) “Inside the while loop” will be displayed 10 times. c) No output after successful compilation. d) “Inside the while loop” will be displayed only once. d
25) What is the outcome of the following code snippet? boolean val1 = true; boolean val2 = false; while (val1) { if (val1) { System.out.println("Hello"); } val1 = val2; } a) No output will be displayed because of compilation error. b) “Hello” will be displayed only once. c) “Hello” will be displayed infinite times. d) No output will be displayed even after successful compilation of the code snippet. b
26) How many times does the code snippet below display "Hello"? int i = 0; while (i != 15) { System.out.println("Hello"); i++; } a) Infinite times b) 14 times c) 15 times d) 16 times c
29) What is the result when the following code is run? double x = 1; double y = 1; int i = 0; do { y = x / 2; x = x + y; i = i + 1; } while (x < 2.5); System.out.print(i + " "); a) 1 b) 2 c) 3 d) 4 c
30) What will be the range of the random numbers generated by the following code snippet? int r1 = (int) (Math.random() * 50) + 1; a) Between 1 and 49 b) Between 0 and 50 c) Between 0 and 49 d) Between 1 and 50 d
32) Given the following code snippet, what should we change to have 26 alphabet characters in the string str? String str = ""; for (char c = 'A'; c < 'Z'; c++) { str = str + c; } a) int c = 'A' b) str = c + str; c) c <= 'Z' d) Must change to use a do loop c
33) What is the output of the code snippet given below? String s = "abcde"; int i = 1; while (i < 5) { System.out.print(s.substring(i, i + 1)); i++; } a) No output b) abcd c) abcde d) bcde d
34) What is the output of the code snippet given below? String s = "abcde"; int i = 1; do { if (i > 1) { System.out.print(s.substring(i, i + 1)); } } while (i < 5); a) No output b) No output (infinite loop) c) abcde d) bcde b
35) What is the output of the code snippet given below? String s = "12345"; int i = 1; while (i < 5) { System.out.print(s.substring(i, i + 1)); i++; } a) No output b) 1234 c) 12345 d) 2345 d
36) What is the output of the code snippet given below? String s = "12345"; int i = 1; do { if (i > 1) { System.out.print(s.substring(i, i + 1)); } } while (i < 5); a) No output b) No output (infinite loop) c) 12345 d) 2345 b
37) Which of the following activities can be simulated using a computer? I. Waiting time in a line at a restaurant II. Tossing a coin III. Shuffling cards for a card game a) I only b) II only c) I and II only d) I, II, and III d
38) What is the data type of the number generated by the Math.random() function? a) double b) float c) int d) String a
39) What is the output of the code fragment given below? int i = 0; int j = 0; while (i < 125) { i = i + 2; j++; } System.out.println(j); a) 0 b) 62 c) 63 d) The code fragment displays no output because it does not compile. c
40) What is the output of the following code snippet? int i = 1; while (i < 20) { System.out.print(i + " "); i = i + 2; if (i == 15) { i = 19; } } a) 1 3 5 7 9 11 13 15 17 19 b) 1 3 5 7 9 11 13 19 c) 1 3 5 7 9 11 13 15 17 d) 1 3 5 7 9 11 13 17 19 b
42) What is the output of the following code fragment? int i = 1; int sum = 0; while (i <= 15) { sum = sum + i; i++; } System.out.println("The value of sum is " + sum); a) The value of sum is 0 b) The value of sum is 105 c) The value of sum is 120 d) The value of sum is 136 c
44) Which of the following is considered a loop with a bound that could be problematic? a) for (i = 1; i != n; i++) b) for (years = n; years < 0; years--) c) for (i = 1; i <= n; i++) d) for (int i = 1; i <= 10; i++) a
45) In the __________ loop header, you can include multiple update expressions, separated by commas, but it is not recommended. a) do b) while c) for d) do b
46) What values does counter variable i assume when this loop executes? for (int i = 20; i >= 2; i = i - 6) { System.out.print(i + ","); } a) 20, 14, 8, 2 b) 20, 14, 8, 2, –4 c) 20, 14, 8 d) 14, 8, 2 b
47) Insert a statement that will correctly terminate this loop when the end of input is reached. boolean done = false; while (!done) { String input = in.next(); if (input.equalsIgnoreCase("Q")) { __________ } else { double x = Double.parseDouble(input); data.add(x); } } a) stop; b) done = 1; c) exit; d) done = true; d
48) What is the output of the code snippet given below? String s = "abcdefghijkl"; int i = 1; do { if (i > 2) { System.out.print(s.substring (1, i)); } i++; } while (i < 5); a) abcd b) bcde c) bcbcd d) cdef c
50) What does the following code do? int sum = 0; final int count = 1000; for (int i = 1; i <= count; i++) { sum = sum + (int) (Math.random() * 101); } System.out.println(sum / count); a) It simulates the outcome of throwing a coin. b) It calculates the average of 1000 random numbers between 0 and 100. c) It performs Monte Carlo simulation of fluid dynamics. d) It calculates the average of 1000 random numbers between 1 and 101. b
51) See text document. d
52) See text document. a
53) What is the output of the following code snippet? int f1 = 0; int f2 = 1; int fRes; System.out.print(f1 + " "); System.out.print(f2 + " "); for (int i = 1; i < 10; i++) { fRes = f1 + f2; System.out.print(fRes + " "); f1 = f2; f2 = fRes; } System.out.println(); a) 0 1 5 7 9 11 13 15 17 19 b) 0 1 1 2 3 5 8 13 21 34 c) 0 1 4 6 8 10 12 14 16 18 d) 0 1 6 7 9 12 14 17 19 21 b
56) What is the output of the following code snippet? int counter = 1; for (double i = 0.01; i <= 1.0; i = i + 0.01) { counter++; } System.out.println(counter); a) 100 b) 49.50 c) 60.5 d) 10 a
60) Which of the following loops executes exactly 10 times? a) for (int i = 0; i <= 10; i++) { } b) int i = 0; boolean found = false; do { i++; if (i % 10 == 0) { found = true; } } while (i < 10 && !found); c) int i = 0; while (i <= 10) { i++; } d) for (int i = 1; i <= 10; i++) d
61) How many times does the following loop execute? for (double d = 1; d != 10; d++) { d = d / 3; System.out.print(d + " "); } a) 10 b) 9 c) 8 d) An infinite number of times d
64) Suppose that a program asks a user to enter multiple integers, either positive or negative, to do some calculation. The data entry will stop when the user enters a certain value to indicate the end of the data. What value should the code use as the sentinel? a) 0 b) -1 c) 999 d) An alphabetic character d
65) Which of the following statements expresses why the following code is considered bad form? for (rate = 5; years-- > 0; System.out.println(balance)) . . . I. Unrelated expressions in loop header II. Doesn't match expected for loop idiom III. Loop iteration is not clear a) II and III only b) I and II only c) I and III only d) I, II, and III d
66) Which of the following is considered a loop with a problematic condition? a) for(i = 1; i != n; i++) b) for (years = n; years > 0; years++) c) for(i = 1; i <= n; i++) d) for (int i = 20; i >= 10; i--) a
67) How do you fix this code snippet to make it print out the sum when the user enters Q? System.out.print("Enter a value, Q to quit: "); double sum = 0; Scanner in = new Scanner(System.in); boolean hasData = true; do { double value = in.nextDouble(); sum = sum + value; System.out.print("Enter a value, Q to quit: "); } while (in.hasNext()); System.out.println("sum " + sum); a) while (in.hasData()); b) while (!in.hasEnded()); c) while (in.hasNextDouble()); d) while (hasData); c
68) What is the output of this code snippet? String str = "ABCabc"; char ch; int i = 0; while (i < str.length()) { ch = str.charAt(i); if (Character.isLowerCase(ch)) { System.out.print(i + " "); } else { i++; } } a) 3 4 5 b) 3 c) 3 3 3 3 3 ... (infinite loop) d) 0 1 2 c
72) Which of the following loop(s) could possibly not enter the loop body at all? I. for loop II. while loop III. do loop a) I only b) I and II only c) II and III only d) I and III only b
73) Which of the following loop(s) should be used when you need to ask a user to enter one data item and repeat the prompt if the data is invalid? I. for loop II. while loop III. do loop a) I only b) I and II c) III only d) II only c
74) Which of the loop(s) test the condition after the loop body executes? I. for loop II. while loop III. do loop a) I only b) II only c) III only d) II and III c
75) When hand-tracing the loop in the code snippet below, which variables are important to evaluate? int i = 10; int j = 5; int k = -10; int sum = 0; while (i > 0) { sum = sum + i + j; i--; System.out.println("Iteration: " + i); } a) The variables i and j b) The variables i and sum c) The variables i, j, and k d) The variables j and k b
76. When hand tracing, drawing a line through the value stored in a variable means that a) The value stored there has changed to something new b) The variable is the wrong data type for the code being executed c) The expression being evaluated uses that variable d) The variable must be inside a loop a
77. When hand-tracing a portion of code, which statement about Boolean conditions is true? a) They typically are too complex to be evaluated. b) They do not need to be monitored because their result usually is not stored in a variable. c) It is rare to encounter a Boolean condition. d) They are crucial to evaluate since they determine if-statement conditions and looping. d
79. The process of hand-tracing code is valuable because a) It is usually faster than just running the code. b) It is the best way to design an algorithm. c) You must already have a working program in order to do it. d) It gives valuable insight that you do not get by running the code. d
84) What will be the result of running the following code fragment? int year = 0; double rate = 5; double principal = 10000; double interest = 0; while (year < 10) { interest = (principal * year * rate) / 100; System.out.println("Interest " + interest); } a) The code fragment will display the interest calculated for nine years. b) The code fragment will continue to display the calculated interest forever because the loop will never end. c) The code fragment will not display the calculated interest and halt abruptly. d) The code fragment will not display any output because it will not compile. b
85) See text document. b
87) What changes do you need to make in the following code snippet to display “Let us learn Java” exactly 10 times? int i = 0; while (i <= 10) { System.out.println("Let us learn Java"); i++; } a) while (i < 9) b) while (i < 11) c) while (i < 12) d) int i = 1; d
88) How many times is the text “Let's have fun with Java.” printed when this code snippet is run? int i = 0; do { System.out.println("Let's have fun with Java."); i++; if (i % 2 == 0) { i = 10; } } while (i <= 10); a) 1 b) 2 c) 3 d) 10 c
93) Which of the following statements is correct about a sentinel? a) A sentinel is a value that creates a bridge between a data set and unrelated input. b) A sentinel is a value that is part of the data to be processed by the program. c) A sentinel is a value that terminates a program. d) A sentinel is a value that indicates the end of an input sequence. d
Show full summary Hide full summary

Similar

Java Week 5 Object Oriented Programming
Troy Bowlin
Physics Review!
Nicholas Weiss
Java Practice 1
Ummm No
Using your memory's rhythms to review and remember
Isabel Liu
Parts of a Cell Membrane
Selam H
MCAT Biology Practice Test
Christine Sang
Accounting - Unit 1: Grade 11 Review
Claudia Voin
Biochemistry Final Review
Kaitlyn Emily Bi
Algebra Review II
Yasmin Bani
US History Review
Madison Martinez
Java Practice 2
Ummm No