Java Accel Midterm Review January 29th, 2016

¡Supera tus tareas y exámenes ahora con Quizwiz!

The _______ operator takes a single condition and evaluates to true if that condition is false and false if that condition is true.

!

Insert the missing statement in the following code fragment. The loop is intended to keep prompting until the user enters a positive number. boolean done = false; while(__________) { System.out.print("Please enter a positive number: "); value = in.nextDouble(); if (value > 0) done = true; }

!done

What is the length of an empty string?

0

93. In the following code segment, the value d is set to a random integer between __________ (inclusive). Random generator = new Random(); int d = 1 + generator.nextInt(10);

1 and 10 inclusive.

40. List three rules imposed by Java on identifiers.

1) No numbers at the beginning. 2) Case sensitive. 3) No Spaces. 4) No special characters.

66. Why would you get a StringIndexOutOfBoundsException?

1) The index is negative. 2) The index is larger than the size of the array.

87. The following loop: for(x = 0; x <= 10; x++) is executed __________ times.

11

52. What is the output of the following code? public class Test { public static void main(String[] args) { int[][] matrix = {{1, 2, 3, 4}, {4, 5, 6, 7}, {8, 9, 10, 11}, {12, 13, 14, 15}}; for (int i = 0; i < 4; i++) System.out.print(matrix[i][1] + " "); } }

2 5 9 13

89. How many times does the following loop execute? for (i = 0; i <= 25; i++) System.out.println( i * i);

26

51. What will be displayed by the following program? public class Test { public static void main(String[] args) { int[][] values = {{3, 4, 5, 1}, {33, 6, 1, 2}}; int v = values[0][0]; for (int row = 0; row < values.length; row++) for (int column = 0; column < values[row].length; column++) if (v < values[row][column]) v = values[row][column]; System.out.print(v); } }

33

What is the decimal equivalent value of (int)520.55?

520

How many relational operators does the Java language have?

6: >, >=, <, <=,==, !=

1. Which of the following code fragments converts a floating-point number to the nearest integer? A) double f = 4.65; int n = (int) Math.round(100 * f); B) double f = 4.65; int n = (int) Math.round(f); C) double f = 4.65; int n = Math.round(f); D) double f = 4.65; int n = (int) f;

A

24. Which of the following statements is equivalent to balance = balance + amount; ? A) balance += amount; B) balance =+ amount; C) balance == amount; D) balance +== amount;

A

36. Which of the following statements is correct? A) Identifiers can be made up of letters, digits, and the underscore (_) character. B) Identifiers can use symbols such as ? or %. C) Spaces are permitted inside identifiers. D) Identifiers are not case sensitive.

A

37. Analyze the following code: public class Test { public static void main(String[] args) { int[] oldList = {1, 2, 3, 4, 5}; reverse(oldList); for (int i = 0; i < oldList.length; i++) System.out.print(oldList[i] + " "); } public static void reverse(int[] list) { int[] newList = new int[list.length]; for (int i = 0; i < list.length; i++) newList[i] = list[list.length - 1 - i]; list = newList; } } A. The program displays 1 2 3 4 5. B. The program displays 1 2 3 4 5 and then raises an ArrayIndexOutOfBoundsException. C. The program displays 5 4 3 2 1. D. The program displays 5 4 3 2 1 and then raises an ArrayIndexOutOfBoundsException.

A

4. Suppose a Scanner object is created as follows: Scanner input = new Scanner(System.in); What method do you use to read an int value? A. input.nextInt(); B. input.nextInteger(); C. input.int(); D. input.integer();

A

41. Analyze the following code: public class Test { public static void main(String[] args) { int[] oldList = {1, 2, 3, 4, 5}; reverse(oldList); for (int i = 0; i < oldList.length; i++) System.out.print(oldList[i] + " "); } public static void reverse(int[] list) { int[] newList = new int[list.length]; for (int i = 0; i < list.length; i++) newList[i] = list[list.length - 1 - i]; list = newList; } } A. The program displays 5 4 3 2 1. B. The program displays 1 2 3 4 5 and then raises an ArrayIndexOutOfBoundsException. C. The program displays 1 2 3 4 5. D. The program displays 5 4 3 2 1 and then raises an ArrayIndexOutOfBoundsException.

A

42. Which of the following counts the number of characters in a string? String greeting = "Hello, World!"; ______________ A) int n = greeting.length(); B) int n = greeting.count(); C) int n = greeting.size(); D) int n = greeting.number();

A

76. Which of the following statements is correct? A) It is a good idea to write the first test cases before the program is written completely. B) It is not a good idea to write the first test cases before the program is written completely. C) It is not a good idea to write the first test cases after the program is written completely. D) It is a good idea to write the first test cases after the program is written completely.

A

81. Which of the following is considered a loop with a hidden danger? 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++) System.out.println(i * i) ;

A

86. The following loop alters the control variable i. What values does i assume? for (int i = 20; i >= 2; i = i - 6) . . . a) 20, 14, 8, 2, −4 b) 20, 14, 8 c) Infinitely many values d) 14, 8, 2 e) 20, 14, 8, 2

A

96. Which column represents the code given? double aRandomNum = Math.random(); System.out.print( aRandomNum ); A) B) C) D) 0.23775390042651734 1.9510156017060694 0 3 0.6679636169820509 3.6718544679282035 2 5 0.10599088269978185 1.4239635307991274 0 3 0.5589477785393575 3.23579111415743 2 5 0.7959125921331364 4.183650368532545 3 6 0.426394893526862 2.705579574107448 1 4

A

When an array is first created, what are all the elements initialized with?

A fixed value that depends on the element type.

53. Pick the right output for the program below: /** * Asterisks patterns * */ public class Pattern { public static void main(String [] args) { for (int i = 0; i < 10 ; i++) { for ( int j = 0; j < i; j++) System.out.print("*"); System.out.println(); } } }

A right triangle with the right angle in the lower left-hand corner.

The Integer class in an example of what?

ADT

What is the difference between a fence post error and an off-by-one error?

An off-by-one error is an avoidable error in which a loop iterates one too many or one too few times than desired. Usually this problem arises when a programmer fails to take into account that a sequence starts at zero rather than one, or makes mistakes such as using "is less than" where "is less than or equal to" should have been used in a comparison. A fence post error occurs when you forget to count the last iteration of a "<=" loop. Thus, it is a special case of an off-by-one error.

What is a sequence of values of the same type?

Array

Why do arrays suffer from a significant limitation?

Arrays suffer from a significant limitation because their length is fixed.

100. Which of the following is used as a strategy to recognize bugs and their causes? a) Executing the program b) Divide and conquer c) Program tracing d) Logging

B

16. Based on the code below, which of the following correctly iterates over the elements of the array and prints them? int[] arrayOfInts = { 32, 87, 3, 589, 12, 1076, 2000, 8, 622, 127 }; A) for (int i = 0; i < arrayOfInts.length(); i++) System.out.print(arrayOfInts[i] + " "); B) for (int i = 0; i < arrayOfInts.length; i++) System.out.print(arrayOfInts[i] + " "); C) for (int i = 0; i < arrayOfInts.size(); i--) System.out.print(arrayOfInts[i] + " "); D) for (int i = 0; i < arrayOfInts.size; i++) System.out.print(arrayOfInts[i] + " ");

B

22. Which of the following code fragments will compile without error? A) int balance = (double) 100; B) int dollars = 100; double balance = dollars; C) double balance = 13.75; int dollars = balance; D) (int) balance = 13.75

B

26. What is the error in this code fragment? double[] data; data[0] = 15.25; A) A cast is required B) storage has not been allocated for data C) A two-dimensional array is required D) Out-of-bounds error

B

35. Consider the following statement: String greeting = 13; Which of the following is true? A) The statement yields neither a compile-time error nor a runtime error. B) The statement yields a compile-time error C) The statement yields a runtime error. D) The statement yields both a compile-time error and a runtime error.

B

44. What is result after executing the following statements? int[] list1 = {1, 2, 3, 4, 5, 6}; int[] result = new int[list1.length]; for (int i = 0, j = result.length - 1; i < list.length; i++, j--) { result[j] = list[i]; A. result is 1 2 3 4 5 6 B. result is 6 5 4 3 2 1 C. result is 0 0 0 0 0 0 D. result is 6 6 6 6 6 6

B

49. Which column represents the code given? double aRandomNum = Math.random(); System.out.print(4 * aRandomNum + 1) ); A) B) C) D) 0.23775390042651734 1.9510156017060694 0 3 0.6679636169820509 3.6718544679282035 2 5 0.10599088269978185 1.4239635307991274 0 3 0.5589477785393575 3.23579111415743 2 5 0.7959125921331364 4.183650368532545 3 6 0.426394893526862 2.705579574107448 1 4

B

50. Assume double[][] x = new double[4][5], what are x.length and x[2].length? A. 4 and 4 B. 4 and 5 C. 5 and 4 D. 5 and 5

B

79. Which of the following code fragments is considered bad style? a) public int getYears() { return years; } b) for(int i = 1; i <= years; i++) { if(balance >= targetBalance) i = years + 1; else { double interest = balance * rate / 100; balance = balance + interest; } } c) public double getBalance() { return balance; } d) for (int i = 1; i <= n; i++) { double interest = balance * rate / 100; balance = balance + interest; }

B

32. A(n) __________ statement groups several statements together.

Block

45. Based on the code below, move the rectangle 25 units to the left and 40 units down. Rectangle box = new Rectangle(5, 10, 20, 30);

Box.translate(-25, 40);

10. Based on the following code, which of the following statements correctly prints the third value of the array, that is, the value 9? int[] squares = { 1, 4, 9, 16, 25, 36 }; A) System.out.println(squares.get(2)); B) System.out.println(squares.get(3)); C) System.out.println(squares[2]); D) System.out.println(squares[3]);

C

14. Analyze the following code: public class Test { public static void main(String[] args) { int n = 10000 * 10000 * 10000; System.out.println("n is " + n); } } A. The program displays n is 1000000000000 B. The result of 10000 * 10000 * 10000 is too large to be stored in an int variable n. This causes an overflow and the program is aborted. C. The result of 10000 * 10000 * 10000 is too large to be stored in an int variable n. This causes an overflow and the program continues to execute because Java does not report errors on overflow. D. The result of 10000 * 10000 * 10000 is too large to be stored in an int variable n. This causes an underflow and the program is aborted.

C

21. Which of the following is a relational operator? A) && B) || C) != D) /

C

27. Which of the following conditions does not test whether x is between 1 and 10 (inclusive)? A) 1 <= x && x <= 10 B) !(x < 1 || 10 < x) C) !(x <= 1 || x >= 10) D) 10 >= x && x >= 1

C

29. Analyze the following code. public class Test { public static void main(String[] args) { int[] x = new int[3]; System.out.println("x[0] is " + x[0]); } } A. The program has a compile error because the size of the array wasn't specified when declaring the array. B. The program has a runtime error because the array elements are not initialized. C. The program runs fine and displays x[0] is 0. D. The program has a runtime error because the array element x[0] is not defined.

C

5. What is the exact output of the following code? double area = 3.5; System.out.print("area"); System.out.print(area); A. 3.53.5 B. 3.5 3.5 C. area3.5 D. area 3.5

C

8. Which of the following correctly defines a constant in a method? A) final double NICKEL_VALUE == 0.05; B) public static final double LITERS_PER_GALLON = 3.785; C) final double NICKEL_VALUE = 0.05; D) public static final double LITERS_PER_GALLON == 3.785

C

84. Insert the missing statement in the following code fragment. for(years = 1; (balance = balance + balance * rate / 100) < targetBalance; years++) __________ a) : b) years--; c) balance++; d) ;

C

94. Assume that generator has been initialized to an instance of class Random. Which of the following calls returns a random integer between 0 and n inclusive? a) (int) (n * generator.nextDouble()) b) generator.nextInt(n) c) generator.nextInt(n + 1) d) generator.nextInt(n) + 1 e) generator.nextInt(n + 1) - 1

C

97. Which column represents the code given? double aRandomNum = Math.random(); System.out.print( (int)(4 * aRandomNum)); A) B) C) D) 0.23775390042651734 1.9510156017060694 0 3 0.6679636169820509 3.6718544679282035 2 5 0.10599088269978185 1.4239635307991274 0 3 0.5589477785393575 3.23579111415743 2 5 0.7959125921331364 4.183650368532545 3 6 0.426394893526862 2.705579574107448 1 4

C

What is a string a sequence of?

Characters

13. The following code is an example of a(n) ____________________ statement. if(balance >= amount) balance = balance - amount;

Compound

30. Consider this statement for computing the average of x, y, and z. double average = x + y + z / 3; Which of the following statements is correct? A) The code always computes the correct average. B) The code works correctly provided x, y, and z are variables of type double C) The code works correctly provided x, y, and z are variables of type int D) The code only gives the right answer when x = -y

D

39. Which of the following code fragments will cause an error? A) String greeting1 = "Hello, Dave!"; B) String greeting2 = "Hello, Crystal!"; C) String greeting = "Hello, World!"; int n = greeting.length(); D) int luckyNumber; System.out.println(luckyNumber);

D

48. Which of the following statements are correct? A. char[][] charArray = {'a', 'b'}; B. char[2][2] charArray = {{'a', 'b'}, {'c', 'd'}}; C. char[2][] charArray = {{'a', 'b'}, {'c', 'd'}}; D. char[][] charArray = {{'a', 'b'}, {'c', 'd'}};

D

80. Why is the following code considered bad style? for (rate = 5; years-- > 0; System.out.println(balance)) . . . a) Unrelated expressions in loop header b) Expressions with side effect combine tests and modifications c) One or more magic numbers are used d) All of the above e) None of the above

D

98. Which column represents the code given? double aRandomNum = Math.random(); System.out.print(((int)(4 * aRandomNum)+3)); A) B) C) D) 0.23775390042651734 1.9510156017060694 0 3 0.6679636169820509 3.6718544679282035 2 5 0.10599088269978185 1.4239635307991274 0 3 0.5589477785393575 3.23579111415743 2 5 0.7959125921331364 4.183650368532545 3 6 0.426394893526862 2.705579574107448 1 4

D

Which of the following primitive types has a size of two bytes? A) int B) byte C) long D) char

D

In Java, a common error involving a compound statement with two if and one else is called the _____ else.

Dangling

What is the process of identifying errors in your program and removing them?

Debugging

33. When you are done testing your program, you need to remove all print statements that produce ____________________ messages.

Diagnostics

46. The statement System.out.printf("%3.1f", 1234.56) outputs ___________. A. 123.4 B. 123.5 C. 1234.5 D. 1234.56 E. 1234.6

E

6. Which of the following is a valid identifier? A. $343 B. class C. 9X D. 8+9 E. radius

E

The items in an array are called what?

Elements

What are boolean variables sometimes called because they can only have two states: "up'' and "down"?

Flags

What is the dangling else problem?

It is an if/if/else statement in which the else clause may match with a different if than the programmer intended.

Write the code to find the absolute value of variable x.

Math.abs(x);

Write the code to find the value of x to the power of y.

Math.pow(x,y);

When casting what types do you lose information and have to use the cast to tell the compiler that you agree to the information loss?

Number

47. Which of the following terms denotes the memory location of an object? A) implicit parameter B) mutator method C) object reference D) encapsulation

Object Reference

11. The && and || operators in Java are computed using ____ evaluation.

Short

31. A statement such as balance = balance - amount; is called a(n) ____________________ statement.

Simple

34. Why are the following statements considered bad programming practice? if ((d = b * b - 4 * a * c) >= 0) r = Math.sqrt(d); if (n-- > 0) . . .

This is considered bad programming practice because there are no expressions inside the parentheses.

What is the purpose of the Scanner class?

To take any type of data input from the keyboard.

A program ________ consists of messages that show the path of execution.

Trace

What program consists of messages that show the path of execution?

Trace

38. It is an error to use the value of a variable that has never had a value assigned to it. T rue or False.

True

The _______ character is used as an escape character in Java.

\

The opposite of < is what>

__>=_____>=____<_________

91. The __________ loop is executed at least once.

do while

92. Insert a statement to terminate the 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); } }

done = true;

64. Convert the following string to its floating-point value as price: String input = "75.23";

double num = Double.parseDouble(input);

85. If a loop is executed for a definite number of times, a(n) __________ loop is usually appropriate.

for

83. In the __________ loop header, you can declare multiple variables, as long as they are of the same type and you can include multiple update expressions, separated by commas.

for loop

68. Write the Java version of the following pseudocode: If the student's grade is between 60 and 70 (inclusive), print "C"

if ( grade <= 70 && grade > 60) System.out.println("C");

Write the code snippet to find the maximum value in an array of integers, intNum.

int max = 0; for (int i = 0; i < intNum.length; i++) { if (intNum[i] > max) { max = intNum[i]; } } System.out.println(max);

82. Using a for loop, compute the value of an when n is an integer greater than or equal to 0. The factorial of a number n is the product 1 · 2 · ... n.

int power = 1; for (int i = 1; i <= n; i++) power = power * i;

Write the code snippet to find the average in an array of integers, intNum.

int sum = 0; for (int i = 0; i < intNum.length; i++) { sum += intNum[i]; } double average = sum/ intNum.length; System.out.println(average);

90. A loop which requires you to go halfway into it before knowing whether or not to terminate is often called a(n) __________.

loop and a half

Look at study guide #104

public class Pattern { public static void main(String [] args) { for (int i = 0; i < 10 ; i++) { for ( int j = 0; j < i; j++) System.out.print("*"); System.out.println(); } System.out.println(); for (int i = 10; i > 0 ; i--) { for ( int j = 0; j < i; j++) System.out.print("*"); System.out.println(); } System.out.println(); for (int i = 10; i > 0 ; i--) { for ( int j = 0; j < 10 - i; j++) System.out.print(" "); for ( int j = 0; j < i; j++) System.out.print("*"); System.out.println(); } System.out.println(); for (int i = 0; i < 11 ; i++) { for ( int j = 0; j < 10 - i; j++) System.out.print(" "); for ( int j = 0; j < i; j++) System.out.print("*"); System.out.println(); } } }

20. Program: Twenty students were asked to rate on a scale of 1 to 5 the quality of the food in the student cafeteria, with 1 being "awful" and 5 being "excellent." Randomly assign 20 integers from 1 to 5 to an integer array and determine the frequency of each rating. Using what you learned about the elements of an array as counters, display the rating and their frequency in two well-formatted columns.

public class YI_FoodSurvey { public static void main(String[] args) { int a[]; a = new int[20]; for (int i = 0; i < a.length; i++) { a[i] = 1 + (int) (Math.random()*5); } int b[]; b = new int[20]; for (int j = 0; j < a.length; j++) { b[a[j]]++; } System.out.printf("%5s: ", "Score"); System.out.printf("%9s: ", "Frequency"); System.out.println(); for (int l = 1; l < 6; l++) { System.out.printf("%2d", l); System.out.printf("%8d", b[l]); System.out.println(); } // Output: // Score: Frequency: // 1 3 // 2 4 // 3 4 // 4 6 // 5 3 } }

The value of x after the following sequence of statements is_______ x--; x++;

x

How to you assign a value of 1 to variable x?

x = 1;

78. Insert the missing statement in the following code fragment. int years = 20; while(years >0) { __________ double interest = balance * rate / 100; balance = balance + interest; }

years-;


Conjuntos de estudio relacionados

6 components of health and wellness

View Set

Module 7 - LinkedIn - Excel Essential Training (Office/Microsoft 365)

View Set

Introduction to Construction Management Unit 1

View Set

Chapter 7 - Virtualization and Cloud Computing

View Set

cyber section 4 and 5 practice questions

View Set