asdf1234

Ace your homework & exams now with Quizwiz!

What is the output of the following code snippet? System.out.print(4 + 4); System.out.print(12);

812

8) What are the values of i and j after the following code fragment runs? int i = 60; int j = 50; int count = 0; while (count < 5) { i = i + i; i = i + 1; j = j - 1; j = j - j; count++; } System.out.println("i=" + i + ", j=" + j); a) i = 1951, j = 0 b) i = 1951, j = 45 c) i = 65, j = 1 d) i = 65, j = 45

Answer: a) i = 1951, j = 0

The programmer, not the compiler, is responsible for testing a program to identify

Run-time errors

Which of the following statements must you include in a Java class that can be executed by the virtual machine?

public static void main(String[] args)

Which of the following symbols is used to terminate a Java program statement?

semicolon

A Java virtual machine is

software

In order for the ENIAC computer to be re-programmed

wires needed to be plugged into a different wiring configuration

order of operations (python)

( ) parentheses. ** exponentiation - negation * multiplication / division, % remainder + addition subtraction

56) Consider the following code snippet: public class Vehicle { private String type; public String Vehicle(String type) { . . . } } What is wrong with this code? a) The class instance variable type must be initialized when it is declared. b) The constructor must not have any arguments. c) The constructor must not have a return type declared. d) The constructor's return type must be void.

) The constructor must not have a return type declared.

A default constructor takes _____ arguments

0

The first element of an array is located at index _____.

0

b

10) Which of the following operators is used as a relational operator? a) =< b) <= c) = d) !

d

100) Assuming that the user inputs "twenty" as the input, what is the output of the following code snippet? String numEmployeesStr; Scanner in = new Scanner(System.in); System.out.println("Please enter the number of your employees: "); numEmployeesStr = in.next(); int numEmployees = Integer.parseInt(numEmployeesStr); if (numEmployees < 10) { System.out.println("Very small business!"); } else { System.out.println("Small business"); if (numEmployees > 100) { System.out.println("Mid size business"); } else { System.out.println("Large business"); } } a) Very small business! b) Small business c) Mid size business d) Run-time error

b

15) A store provides 10 percent discount on all items with a price of at least $100. No discount is otherwise applicable. Which of the following DOES NOT correctly compute the discount? a) double discount = 0; if (price >= 100) { discount = 0.10 * price; } b) double discount = 0.10 * price; if (price <= 100) { discount = 0; } c) double discount; if (price < 100) { discount = 0; } else { discount = 0.10 * price; } d) double discount = 10; if (price >= 100) { discount = 0.1 * price; } else { discount = 0; }

III only

16) Which of the following statements is (are) true about an if statement? I. It guarantees that several statements are always executed in a specified order. II. It repeats a set of statements as long as the condition is true. III. It allows the program to carry out different actions depending on the value of a condition.

a

18) In Java, which of the following orderings is used to compare strings? a) Lexicographic b) Semantic c) Alphabetic d) Syntactic

In Java, a two dimensional array requires _____ indexes to access a specific element.

2

b

23) Which code snippet will output "Yes!" when two strings s1 and s2 are equal? a) if (s1 = s2) { System.out.println("Yes!"); } b) if (s1 == s2) { System.out.println("Yes!"); } c) if (s1.equals(s2)) { System.out.println("Yes!"); } d) if (s1.compareTo(s2) == 1) { System.out.println("Yes!"); }

her

26) What is the output of the following code snippet? String str1 = "her"; String str2 = "cart"; if (str1.compareTo(str2) < 0) { System.out.print(str2); } else { System.out.print(str1); }

"Aardvandermeer" is first, then "Aardvark"

28) The two strings "Aardvark" and "Aardvandermeer" are exactly the same up to the first six letters. What is their correct lexicographical ordering?

b

3) Which of the following statements is correct about an if statement? a) You must use braces if the body of an if statement contains only a single statement. b) You can omit an else statement if there is no task defined in the else branch. c) You cannot use braces if the body of an if statement contains only a single statement. d) The number of opening braces can be different from the number of closing braces.

a

32. The switch statement in Java a) is like a sequence of if statements that compares a single integer value against several constant alternatives. b) is a compound statement that tests all branches against different variables. c) makes the break statement optional. d) requires compound Boolean expressions as alternatives.

c

33) In a switch statement, if a break statement is missing a) The break happens at the end of each branch by default b) The statement will not compile c) Execution falls through the next branch until a break statement is reached d) The default case is automatically executed

c

37) Consider a situation where multiple if statements are combined into a chain to evaluate a complex condition. Which of the following reserved words is used to define the branch to be executed when none of the conditions are true? a) if b) else if c) else d) All of the above items

The index of the last element of an array with a size of 5 elements is _____.

4

What is the output of the following code snippet? public class PrintIt{ public static void main(String[] args){ System.out.println("4 * 4" + 12); } }

4 * 412

a

46) When drawing flowcharts, unconstrained branching and merging can lead to a) So-called "spaghetti code" b) A better design c) Intuitive understanding of the flow of control d) Clear visualization of the problem solution

d

47) The flow chart shows the order in which steps should be executed, and the diamond-shaped boxes indicate a) input b) algorithms c) tasks d) conditional tests

b

48) When testing code for correctness, it always makes sense to a) Test all cases b) Identify boundary cases and test them c) Check all cases by hand d) Assume invalid input will never occur

D

56) Assuming that a user enters 64 as his score, what is the output of the following code snippet? int score = 0; Scanner in = new Scanner(System.in); System.out.print("Enter your score: "); score = in.nextInt(); if (score < 40) { System.out.println("F"); } else if (score >= 40 || score < 50) { System.out.println("D"); } else if (score >= 50 || score < 60) { System.out.println("C"); } else if (score >= 60 || score < 70) { System.out.println("B"); } else if (score >= 70 || score < 80) { System.out.println("B+"); } else { System.out.println("A"); }

a

57) What is the output of the following code snippet? int num = 100; if (num > 100); { num = num - 10; } System.out.println(num); a) 90 b) 100 c) 99 d) 101

b

60) What is the output of the following code snippet? final int MIN_SPEED = 45; final int MAX_SPEED = 65; int speed = 55; if (!(speed < MAX_SPEED)) { speed = speed - 10; } if (!(speed > MIN_SPEED)) { speed = speed + 10; } System.out.println(speed); a) 45 b) 55 c) 65 d) 50

c

66) Which of the following options refers to the technique of simulating program execution on a sheet of paper? a) Compiling b) Prototyping c) Tracing d) Debugging

price - status 0 - "" 22 - "inexpensive" - "reasonable"

68) Assuming that a user enters 22 as the price of an object, which of the following hand-trace tables is valid for the given code snippet? int price = 0; String status = ""; Scanner in = new Scanner(System.in); System.out.print("Please enter object's price: "); price = in.nextInt(); if (price >= 50) { status = "reasonable"; if (price >= 75) { status = "costly"; } } else { status = "inexpensive"; if (price <= 25) { status = "reasonable"; } }

c

69) Which of the following operators compare using short-circuit evaluation? a) ++ b) - c) && d) ==

c

7) The following code snippet contains an error. What is the error? if (cost > 100); { cost = cost - 10; } System.out.println("Discount cost: " + cost); a) Syntax error (won't compile) b) Logical error: use of an uninitialized variable c) Logical error: if statement has do-nothing statement after if condition d) Logical error: assignment statement does not show equality

b

70) Consider the following code snippet: int score = 0; double price = 100; if (score > 0 && price < 200 && price / score > 10) { System.out.println("buy"); } Which of the following statements is true on the basis of this code snippet? a) The output is buy. b) The code snippet compiles and runs, but there is no output. c) The code snippet doesn't compile. d) The code snippet causes a divide-by-zero error.

b

72) Assuming that a user enters 45, 78, and then 12, what is the output of the following code snippet? int num1 = 0; int num2 = 0; int num3 = 0; Scanner in = new Scanner(System.in); System.out.print("Enter a number: "); num1 = in.nextInt(); System.out.print("Enter a number: "); num2 = in.nextInt(); System.out.print("Enter a number: "); num3 = in.nextInt(); if (!(num1 > num2 && num1 > num3)) { System.out.println(num1); } else if (!(num2 > num1 && num2 > num3)) { System.out.println(num2); } else if (!(num3 > num1 && num3 > num2)) { System.out.println(num3); } a) 12 b) 45 c) 78 d) There is no output due to compilation errors.

b

75) Which of the following conditions tests whether the user enters an integer value that will then be assigned to the floor variable? int floor = 0; Scanner in = new Scanner(System.in); System.out.print("Floor: "); ( . . . ) floor = in.nextInt(); a) if (in.nextInt(floor)) b) if (in.hasNextInt()) c) if (in.fail) d) if (in.nextInt())

b

77) Assuming that the user provides 49 as input, what is the output of the following code snippet? int x = 0; int y = 0; System.out.print("Please enter y: "); Scanner in = new Scanner(System.in); y = in.nextInt(); if (y > 50); { x = y; } System.out.println("x: " + x); a) x: 0 b) x: 49 c) x: 50 d) There is no output due to compilation errors.

a

8) What can be done to improve the following code fragment? if ((counter % 10) == 0) { System.out.println("Counter is divisible by ten: " + counter); counter++; } else { System.out.println("Counter is not divisible by ten: " + counter); counter++; } a) Move the duplicated code outside of the if statement b) Shorten variable names c) Move the brackets to save several lines of code d) Add semicolons after the if condition and the else reserved word

b

80) A store applies a 15 percent service charge on all items with a price of at least $150. No service charge is otherwise applicable. Which of the following DOES NOT correctly compute the service charge? a) double serviceCharge = 0; if (cost >= 150) { serviceCharge = 0.15 * cost; } b) double serviceCharge = 0.15 * cost; if (cost <= 150) { serviceCharge = 0; } c) double serviceCharge; if (cost < 150) { serviceCharge = 0; } else { serviceCharge = 0.15 * cost; } d) double serviceCharge = 15; if (cost >= 150) { serviceCharge = 0.15 * cost; } else { serviceCharge = 0; }

a

83) What is the output of the following code snippet? String someString1 = "his"; String someString2 = "cycle"; if (someString1.compareTo(someString2) < 0) { System.out.println(someString2); } else { System.out.println(someString1); } a) his b) hiscycle c) cycle d) There is no output due to compilation errors.

d

9) What kind of operator is the <= operator? a) Ternary b) Arithmetic c) Inequality d) Relational

str

A Python data type that holds a string of characters.

Algorithm

A description of how to solve a problem. A set up steps to solve a problem.

function

A function is a name that refers to a sequence of instructions in memory. Those instructions are executed when the function is called by its name.

float

A native type representing rational numbers to limited precision.

int

A native type representing the integers, which are positive whole numbers and their opposites.

Which of the following statements is valid with respect to the usage of a semicolon in Java?

A semicolon is used to denote the end of a statement

bool

A variable whose value can be either true or false is of this data type.

Data Types

A way to encode some type of data some computers can manipulate it- using binary numbers.

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

Answer: a) 100

52) Choose the loop that is equivalent to this loop. int n = 1; double x = 0; double s; do { s = 1.0 / (n * n); x = x + s; n++; } while (s > 0.01); a) double x = 0; double s = 1; for (int k = 1; s > 0.01; k++) { s = 1.0 / (k * k); x = x + s; } b) double x = 0; double s = 1; for (int k = 1; k < 100; k++) { s = 1.0 / (k * k); x = x + s; } c) double x = 0; double s = 1; int k = 10; while (s > 0.01) { s = 1.0 / (k * k); x = x + s; k++; } d) double x = 0; double s = 10; int k = 1; while (s > 0.01) { s = 1.0 / (k * k); x = x + s; k++;

Answer: a) double x = 0; double s = 1; for (int k = 1; s > 0.01; k++) { s = 1.0 / (k * k); x = x + s; }

71) Suppose that the chance to hit the jackpot in a lottery is one in one million. Which of the following code snippets simulates that random number? a) (long) (Math.random() * 1000000 + 1) b) (long) (Math.random() * 100000 + 1) c) (long) (Math.random() * 9999990 + 1) d) (long) (Math.random() * 1000001 + 1)

Answer: a) (long) (Math.random() * 1000000 + 1)

18) What is the first and last value of i to be displayed by the following code snippet? int n = 20; for (int i = 0; i <= n; i++) { for (int j = 0; j <= i; j++) { System.out.println("" + i); } } a) 0 and 20 b) 1 and 20 c) 0 and 19 d) 1 and 19

Answer: a) 0 and 20

106) What is the output of this code snippet? int s = 1; int n = 1; do { s = s + n; System.out.print(s + " "); n++; } while (s < 3 * n); a) 2 4 7 11 16 22 b) 1 3 5 7 9 c) 2 3 5 6 7 d) 2 4 6 8

Answer: a) 2 4 7 11 16 22

69) What is the output of the following loop? int s = 1; int n = 1; do { s = s + n; n++; } while (s < 10 * n); System.out.println(s); a) 211 b) 210 c) 120 d) 123

Answer: a) 211

98) How many times does the following loop execute? int upperCaseLetters = 0; String str = "abcdefgHIJKL"; boolean found = false; for (int i = str.length() - 1; i >= 0 && !found; i--) { char ch = str.charAt(i); if (Character.isUpperCase(ch)) { upperCaseLetters; } else { found = false; } } a) 6 times b) 8 times c) 9 times d) 12 times

Answer: a) 6 times

83) Which statement about storyboards is true? a) A storyboard can help prevent potential user confusion early in the design process. b) Storyboards are used primarily to understand how implemented programs work. c) The storyboard helps to train users about how to use software. d) Storyboards have no relationship to the structure of an actual working program.

Answer: a) A storyboard can help prevent potential user confusion early in the design process.

58) What does the following code snippet display? char ch1 = '\ u0000'; char ch2 = '\ uffff'; for (int i = 0; i < 1000; i++) { if (i % 50 == 0) { System.out.println(); } System.out.print((char)(ch1 + Math.random() * (ch2 - ch1 + 1))); } a) It displays random Unicode characters. b) It displays random ASCII characters. c) Nothing because it has compilation error. d) It displays the hexadecimal characters between '0' and 'F'

Answer: a) It displays random Unicode characters.

81. When designing storyboards, it is a good idea to use different colors to a) Make it easy to distinguish between user input and program output. b) Match the colors your program will use when it is finally designed. c) Emphasize the difference between numbers and words. d) Draw lines to divide up panels into different regions.

Answer: a) Make it easy to distinguish between user input and program output.

90) Which for loop prints data across each row in the following code snippet? int i; int j; for (i = 1; i <= 3; i++) { for (j = 1; j <= 3; j++) { System.out.print("X"); } System.out.println(""); } a) The inner for loop b) The outer for loop c) Both for loops d) Another missing for loop

Answer: a) The inner for loop

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

Answer: a) The value stored there has changed to something new

92) In the following code snippet, when does the execution of the program switch from the inner loop to the outer loop? int i; int j; for (i = 0; i <= 9; i++) { for (j = 1; j < 5; j++) { System.out.println("Hello"); } } a) When the value of j becomes 5 b) When the program executes completely c) When the condition for the outer loop is met d) When the value of i is incremented

Answer: a) When the value of j becomes 5

28) In the following code snippet, when does the execution of the program switch from the inner loop to the outer loop? int i; int j; for (i = 0; i <= 9; i++) { for (j = 1; j < 5; j++) { System.out.println("Hello"); if (j == 2) { j = 6; } } } a) When the value of j becomes 6 b) When the program executes completely c) When the condition for the outer loop is met d) When the value of i is incremented

Answer: a) When the value of j becomes 6

38) What is the data type of the number generated by the Math.random() function? a) double b) float c) int d) String

Answer: a) double

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++)

Answer: a) for (i = 1; i != n; i++)

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--)

Answer: a) for(i = 1; i != n; i++)

85) Which of the following code snippets displays the output exactly 10 times? a) int i = 0; while (i < 10); { System.out.println("This is example 1."); i++; } b) int i = 0; while (i < 10) { System.out.println("This is example 2."); i++; } c) int i = 0; while (i < 10) { System.out.println("This is example 3."); } d) int i = 1; while (i < 10) { System.out.println("This is example 4."); i++;

Answer: b) int i = 0; while (i < 10) { System.out.println("This is example 2."); i++; }

63) Which code snippet produces the sum of the first n even numbers? (0 is not considered an even number.) a) int sum = 0; for (int i = 1; i <= n; i++) { if (i % 2 == 0) { sum = sum + i; } } b) int sum = 0; for (int i = 1; i <= n; i++) { sum = sum + i * 2; } c) int sum = 0; for (int i = 0; i < n; i++) { if (i % 2 == 0) { sum = sum + i; } } d) int sum; for (int i = 1; i <= n; i++) { sum = sum + i * 2;

Answer: b) int sum = 0; for (int i = 1; i <= n; i++) { sum = sum + i * 2; }

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

Answer: b) 0 1 1 2 3 5 8 13 21 34

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

Answer: b) 1 3 5 7 9 11 13 19

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

Answer: b) 1 3 9

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

Answer: b) 20, 14, 8, 2, -4

43) What is the last output line of the code snippet given below? int i = 0; while (i < 10) { int num = 1; for (int j = i; j > 1; j--) { System.out.print(j + " "); num = num * 2; } System.out.println("***"); i++; } a) 3 2 *** b) 9 8 7 6 5 4 3 2 *** c) 8 7 6 5 4 3 2 *** d) 2 ***

Answer: b) 9 8 7 6 5 4 3 2 ***

17) A loop inside another loop is called: a) A sentinel loop b) A nested loop c) A parallel loop d) A do/while loop

Answer: b) A nested loop

95) What will be the output of the following code snippet? boolean token1 = true; while (token1) { for (int i = 0; i < 10; i++) { System.out.println("Hello"); } token1 = false; } a) No output. b) Hello will be displayed 10 times. c) Hello will be displayed 9 times. d) Hello will be displayed infinite times.

Answer: b) Hello will be displayed 10 times.

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

Answer: b) I and II only

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.

Answer: b) It calculates the average of 1000 random numbers between 0 and 100.

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

Answer: b) No output (infinite loop)

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

Answer: b) No output (infinite loop)

21) What will be the final output of the following code snippet when a user enters input values in the order 10, 20, 30, 40, 50, and -1? public static void main(String[] args) { double sum = 0; int count = 0; double salary = 0; double average = 0; Scanner reader = new Scanner(System.in); System.out.println("Enter salaries (-1 to stop): "); while (salary != -1) { salary = reader.nextInt(); if (salary != -1) { sum = sum + salary; count++; } } if (count > 0) { average = sum / count; System.out.println("The Average Salary: " + average); } else { System.out.println ("No data!"); } return 0; } a) The Average Salary: 0 b) The Average Salary: 30 c) The Average Salary: 24.83333 d) There will be no output as the code snippet will not compile.

Answer: b) The Average Salary: 30

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. Answer: B

Answer: b) The code fragment will continue to display the calculated interest forever because the loop will never end.

7) The code snippet below checks whether a given number is a prime number. What will be the result of executing it? public static void main(String[] args) { int j = 2; int result = 0; int number = 0; Scanner reader = new Scanner(System.in); System.out.println("Please enter a number: "); number = reader.nextInt(); while (j <= number / 2) { if (number % j == 0) { result = 1; } j++; } if (result == 1) { System.out.println("Number: " + number + " is Not Prime."); } else { System.out.println("Number: " + number + " is Prime. "); } } a) The code snippet will not compile. b) The code snippet will display the desired result. c) The code snippet will display an incorrect result. d) The code snippet will loop forever.

Answer: b) The code snippet will display the desired result.

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

Answer: b) The variables i and sum

89) Is the following code snippet legal? boolean b = false; do { System.out.println("Do you think in Java?"); } while (b != b); a) Yes, it is legal but does not print anything. b) Yes, it is legal and prints "Do you think in Java?" once. c) Yes, it is legal and prints "Do you think in Java?" twice. d) No, it is not legal and gives a compilation error.

Answer: b) Yes, it is legal and prints "Do you think in Java?" once.

59) Which of the following for loops is illegal? a) for (int i = 0; ; ) { } b) for (int i = 0) { } c) for (int i = 0, k = 1; ; i++) { } d) for ( ; ; )

Answer: b) for (int i = 0) { }

41) What are the values of i and j after the following code snippet is run? int i = 10; int j = 20; int count = 0; while (count < 5) { i = i + i; i = i + 1; j = j - 1; j = j - j; count++; } System.out.println("i = " + i + ", j = " + j); a) i = 45, j = 1 b) i = 351, j = 0 c) i = 351, j = 2 d) i = 1311, j = 35

Answer: b) i = 351, j = 0

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

Answer: b) while

104) Select the statement that correctly completes the loop in this code snippet. int years = 20; double balance = 10000; while (years > 0) { __________ double interest = balance * rate / 100; balance = balance + interest; } a) years++; b) years--; c) balance++; d) balance--;

Answer: b) years--;

31) Which of the following is the correct code snippet for throwing a pair of dice to get a sum of the numbers on two dice between 2 and 12 with different probabilities? a) (int) (Math.random() * 6 + 1) b) (int) (Math.random() * 12 + 1) c) (int) (Math.random() * 6 + 1) + (int) (Math.random() * 6 + 1) d) (int) (Math.random() * 12 - 2)

Answer: c) (int) (Math.random() * 6 + 1) + (int) (Math.random() * 6 + 1)

86) What is the output of the following code snippet? int i = 1; while (i != 9) { System.out.print(i + " "); i++; if (i == 9) { System.out.println("End"); } } a) 1 End b) 1 End (infinite loop) c) 1 2 3 4 5 6 7 8 End d) 1 2 3 4 5 6 7 8 End (infinite loop)

Answer: c) 1 2 3 4 5 6 7 8 End

62) How many times does the following loop execute? int i = 0; boolean found = false; while (i < 100 && !found) { i++; System.out.print(i + " "); int j = i * i; if ((i * i * i) % j == j) { found = true; } } a) 10 times b) 20 times c) 100 times d) An infinite number of times

Answer: c) 100 times

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

Answer: c) 15 times

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

Answer: c) 3

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

Answer: c) 3

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

Answer: c) 3 3 3 3 3 ... (infinite loop)

97) How many times does the following loop run? int i = 0; int j = 1; do { System.out.println("" + i + ";" + j); i++; if (i % 3 == 0) { j--; } } while (j >= 1); a) 1 time b) 2 times c) 3 times d) 4 times

Answer: c) 3 times

57) What does the following code snippet print? int a = 120; int b = 90; int n1 = Math.abs(a); int n2 = Math.abs(b); int result = 1; for (int k = 1; k <= n1 && k <= n2; k++) { if (n1 % k == 0 && n2 % k == 0) { result = k; } } System.out.println(result); a) 10 b) 20 c) 30 d) 40

Answer: c) 30

27) How many times does the following loop execute? int upperCaseLetters = 0; String str = "abcdEfghI"; boolean found = false; for (int i = 0; i < str.length() && !found; i++) { char ch = str.charAt(i); if (Character.isUpperCase(ch)) { found = true; } } a) 9 times b) 8 times c) 5 times d) 1 time

Answer: c) 5 times

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.

Answer: c) 63

82. Suppose you must design a program to calculate the roll-out (number of inches traveled in one revolution of the pedals of a bicycle based on its gear combinations). The user must provide the gear sizes, which must be converted into roll-out for all different gear combinations. How can the flow of user interaction for this problem be designed? a) Hand-tracing can confirm code that implements gear selection. b) Pseudocode can guide algorithm design through divide-and-conquer strategy. c) A storyboard can be used. d) The physical gears can lead to ideas for the correct algorithm to use.

Answer: c) A storyboard can be used.

54) How many times does the following loop execute? double d; double x = Math.random() * 100; do { d = Math.sqrt(x) * Math.sqrt(x) - x; System.out.println(d); x = Math.random() * 10001; } while (d != 0); a) Exactly once b) Exactly twice c) Can't be determined d) Always infinite loop

Answer: c) Can't be determined

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

Answer: c) III only

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

Answer: c) III only

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.

Answer: c) No output after successful compilation.

55) What is the output of this loop? int i = 0; boolean found; while (i < 20 && !found) { int sum = i * 2 + i * 3; System.out.print(sum + " "); if (sum > 50) { found = true; } i++; } a) 0 5 10 15 20 25 30 35 40 45 50 55 b) 0 c) No output, compilation error d) 0 5 10

Answer: c) No output, compilation error

101) Is the code snippet written below legal? String s = "1234"; for (int i = 0; i <= 5; i++) { System.out.print(s.substring(i, i + 1)); } a) Yes. b) No; there should be a semicolon at the end of line 2. c) No; for i = 3, s.substring(i, i + 1) will result in an StringIndexOutOfBounds error. d) No; line 4 should have no semicolon at the end.

Answer: c) No; for i = 3, s.substring(i, i + 1) will result in an StringIndexOutOfBounds error.

80. Storyboards are a helpful part of the design process because the storyboard develops a) A pseudocode description of the algorithm being designed b) The mathematical formulas required for computing a correct answer c) The information needed to solve the problem, and how to present that information d) The amount of time and space needed to find a solution

Answer: c) The information needed to solve the problem, and how to present that information

105) Which statement about this code snippet is accurate? int years = 50; double balance = 10000; double targetBalance = 20000; double rate = 3; for (int i = 1; i <= years; i++) { if (balance >= targetBalance) { i = years + 1; } else { double interest = balance * rate / 100; balance = balance + interest; } } a) The loop will run 50 times. b) The loop will never stop. c) The loop will run at most 50 times, but may stop earlier when balance exceeds or equals targetBalance. d) There is a compilation error.

Answer: c) The loop will run at most 50 times, but may stop earlier when balance exceeds or equals targetBalance.

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

Answer: c) The value of sum is 120

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

Answer: c) bcbcd

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

Answer: c) c <= 'Z

78. What are the values of i and j after the following code snippet executes? int i = 60; int j = 50; int count = 0; while (count < 5) { i = i + i; i = i + 1; j = j - 1; j = j - j; count++; } System.out.println(i); System.out.println(j); a) i = 65, j = 1 b) i = 65, j = 45 c) i = 1951, j = 0 d) i = 1951, j = 45

Answer: c) i = 1951, j = 0

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);

Answer: c) while (in.hasNextDouble());

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.

Answer: d) "Hello" will be displayed only once.

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.

Answer: d) "Inside the while loop" will be displayed only once.

70) Which of the following is correct for simulating the toss of a pair of coins to get 0 (head) or 1 (tail) with different probabilities? a) (int) (Math.random() * 0 + 1) b) (int) (Math.random() * 1 + 1) c) (int) (Math.random() * 2) d) (int) (Math.random() * 2) + (int) (Math.random() * 2)

Answer: d) (int) (Math.random() * 2) + (int) (Math.random() * 2)

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

Answer: d) -1

96) What is the output of the code snippet given below? int i = 0; while (i != 11) { System.out.print(" " + 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)

Answer: d) 0 2 4 6 8 10 12 14 .... (infinite loop)

102) What is the output of the code snippet given below? int i = 0; while (i != 11) { System.out.print(i + " "); i = i + 3; } a) No output b) 0 3 6 9 12 15 18 c) 0 1 3 5 7 9 d) 0 3 6 9 .... (infinite loop)

Answer: d) 0 3 6 9 .... (infinite loop)

103) What is the last output line of the code snippet given below? int i = 5; while (i >= 1) { int num = 1; for (int j = 1; j <= i; j++) { System.out.print(num + "**"); num = num * 2; } System.out.println(); i = i - 1; } a) No output b) 1**2 c) 1**2**3 d) 1**

Answer: d) 1**

49) What is the output of this code snippet if the user enters the numbers 1 2 3 4 -1 5? double total = 0; boolean hasValidNumber = true; Scanner in = new Scanner(System.in); while (in.hasNextDouble() && hasValidNumber) { double input = in.nextDouble(); if (input < 0) { hasValidNumber = false; } else { total = total + input; } } System.out.println(total); a) 15.0 b) 14.0 c) 12.0 d) 10.0

Answer: d) 10.0

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

Answer: d) 2345

51) What is the output of the following code snippet? double a = 2; int n = 16; double r = 1; double b = a; int i = n; while (i > 0) { if (i % 2 == 0) // n is even { b = b * b; i = i / 2; } else { r = r * b; i--; } } System.out.println("r = " + r); a) 16.0 b) 128.0 c) 4096.0 d) 65536.0

Answer: d) 65536.0

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.

Answer: d) A sentinel is a value that indicates the end of an input sequence.

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

Answer: d) An alphabetic character

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

Answer: d) An infinite number of times

91) What will be the output of the following code snippet? int i; int j; for (i = 0; i < 6; i++) { for (j = 7; j > i; j--) { System.out.print("*"); } System.out.println(""); } a) A rectangle with six rows and seven columns of asterisks. The number of rows increments by one on completion of one iteration of the inner loop. b) A right triangle with six rows and seven columns of asterisks. The number of columns increments by one on completion of one iteration of the inner loop. c) A rectangle with seven rows and six columns of asterisks. The number of rows increments by one on completion of one iteration of the inner loop. d) An inverted right triangle with six rows and seven columns of asterisks. The number of columns decrements by one on completion of one iteration of the inner loop.

Answer: d) An inverted right triangle with six rows and seven columns of asterisks. The number of columns decrements by one on completion of one iteration of the inner loop.

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

Answer: d) Between 1 and 50

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

Answer: d) I, II, and III

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

Answer: d) I, II, and III

107) When will the loop in the following code snippet stop? java.util.Scanner in = new java.util.Scanner(System.in); double sum = 0; int count = 0; System.out.print("Enter values, Q to quit: "); do { double value = in.nextDouble(); sum = sum + value; count++; System.out.print("Enter values, Q to quit: "); } while (in.hasNextDouble() && count < 100); I. When the user enters an integer II. When the user enters an alphabetic character III. After the user enters 100 numbers a) I or II b) II only c) III only d) II or III

Answer: d) II or III

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.

Answer: d) It gives valuable insight that you do not get by running the code.

94) Which statement is correct about the execution of the loop in the following code fragment? double num; int incr = 0; Scanner reader = new Scanner(System.in); do { incr = incr + 1; System.out.println("Please enter a number (0 when done): "); num = reader.nextDouble(); } while (num != 0); System.out.println("" + incr); a) The loop will execute only when 0 is entered. b) The execution of the loop is independent of user input. c) The program prints the count of positive inputs. d) The loop will execute at least once even if the user has entered the sentinel value.

Answer: d) The loop will execute at least once even if the user has entered the sentinel value.

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.

Answer: d) They are crucial to evaluate since they determine if-statement conditions and looping.

100) What is the output of the code snippet given below? String s = "aeiou"; int i = 0; do { System.out.print(s.substring(i, i + 1)); i++; if (i >= 3) { i = 5; } } while (i < 5); a) a b) ae c) aeiou d) aei

Answer: d) aei

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

Answer: d) bcde

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;

Answer: d) done = true;

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++)

Answer: d) for (int i = 1; i <= 10; i++)

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;

Answer: d) int i = 1;

99) Which of the following code snippets will generate a random number between 0 and 79? a) int val = (int) Math.random() % 80; b) int val = (int) Math.random() * 80 - 1; c) int val = (int) Math.random() % 79; d) int val = (int) Math.random() * 80;

Answer: d) int val = (int) Math.random() * 80;

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.

Answer:b) "Hello" will be displayed only once.

19) How many times will the output line be printed in the following code snippet? for (int num2 = 1; num2 <= 3; num2++) { for (int num1 = 0; num1 <= 2; num1++) { System.out.println("" + num2 + " " + num1); } } a) 3 times b) 6 times c) 9 times d) 12 times

Answer:c) 9 times

Which of the following expression would be a valid index for an array of 5 elements? Choose all that apply.

Answered: 2*2, 2+2 and 4

Which object oriented element is used to define "has a" relationship?

Answered: Data Hiding

Which object oriented element is used to define "is a" relationships?

Answered: Encapsulation, Message Passing, Polymorphism, Composition

Which object oriented element is best defined as "an object should have complete authority over its responsibilities"?

Answered: Inheritance, Encapsulation

Which of the following is a Java primitive data type?

Answered: String,float, int boolean, double,

During what activity do we directly analyze the language of the problem to identify objects, their attributes, and their behaviors?

Answered: class outlining, problem description

Creating an object of a class is called ________.

Answered: classification

Which object oriented element is best defined as "objects best viewed as packages of responsibility"?

Answered: composition

Which method can be called to get the size (number of elements) of an ArrayList?

Answered: count

Which method can be used to retrieve an element of an ArrayList? Choose all that apply.

Answered: index, at

The Java reference type equivalent of the primitive type int is _____.

Answered: integer, String

Which of the following are characteristics of the Java programming language?

Answered: interpreted and compiled

Which method can be used to replace an element in an ArrayList? Choose all that apply.

Answered: replace

Attributes are thing an object

Answered: resembles

Immediately after the object's variables are placed in memory _____.

Answered: the object is instantiated, the variables are destroyed, the object is destroyed

What kind of error is it when your program has a syntax error?

Compile-time error

compound expressions

Compound expressions are expressions composed of sub-expressions.A compound expression is a sequence of instructions for the computer to follow. The result is a value. Compound expressions are always evaluated according to the order of operations.

decision logic

Conditionals: if ______________ then ______________ elif . . . . then . . . . else . . . . fi

Which of the following are good analogies for classes an objects?

Cookie cutters and cookies Blueprints and buildings

describe an algorithm using flow charts

Don't stress! Flow charts are almost exactly like normal instructions, but you have to phrase them in ways or depict them in ways that suggest how a computer may process the instructions.

A reference of a primitive type can never be null

False

A value type variable can store a null reference in Java.

False

Arrays cannot be passed as arguments to methods in Java.

False

In Java, arrays can only store primitive types.

False

Java has a string multiplication operator just like Python.

False

Java uses the same operator for exponentiation as Python

False

Only an enhanced for loop can be used to iterate over an ArrayList

False

Only an enhanced for loop can be used to iterate over an ArrayList.

False

We can use square brackets and and index value to access ArrayLIst elements, just like arrays.

False

When we instantiate an ArrayList we must specify the initial size.

False

What is the output of the following code snippet? System.out.print("Hello"); System.out.println("Good Day!");

HelloGood Day!

Java is said to be a "safe" programming language. What does this mean?

Java programs can run within a browser without fear that they may attack your computer

Which object oriented element is best supports object collaboration?

Message Passing

Suppose you examine a simple Java program and the first line is ' public Class HelloPrinter ' . Is this the same thing in Java as the line ' public Class helloprinter ' ?

No, because Java is case-sensitive, these are considered to be completely distinct

operators

Operators are symbols such as +, -, × and / that represent operations such as addition, subtraction, multiplication, and division. Operators operate on operands.

Order of Operations (PEMDAS)

Parenthesis, Exponents, Multiplication, Division, Addition, Subtraction

Which object oriented element best allows us to designing and implementing families of related types that share outward similarities, but have specialized behaviors?

Polymorphism

To help us collect information from the user through the keyboard in Java, we use a

Scanner

Sequencing

Sequence is a fundamental concept in programming - the computer will execute instructions only in the order or sequence they are listed from top to bottom.

Binary system

The binary number system is base 2, using only bits 0 and 1.

Consider a scenario in which you develop a Java program on a computer that has a Pentium processor. What step should you take to run the same Java program on a computer that has a different processor?

The compiled Java machine language instructions can be run on any processor that has a Java Virtual Machine

When a compiler finds a syntax error in a program, what happens?

The compiler continues and may report about other errors but does not produce a Java class file

decisions pt 2

The condition for a decision is always an expression that evaluates to a boolean value: True or False. If the conditional expression evaluates to True, then the instructions that follow it will be executed. If the conditional expression evaluates to False, then the instructions that follow it will be skipped.

function call

The piece of code that you add to a program to indicate that the program should run the code inside a function at a certain time.

These two lines of code do not produce the same output. Why? System.out.println(7 + 3); System.out.println("7 + 3");

The quotes cause the second expression to be treated as a string

Here is a simple example, storing the value 3 in a variable named x : x = 3

This line of code, x = 3 is called an assignment statement because it instructs the computer to assign the value 3 to the variable named x . In Python, an assignment statement creates a variable (if it does not already exist), and assigns a value to that variable.

A name defined in an inner scope is not available outside that block

True

A name defined in an outer scope is also available in all blocks nested inside that scope

True

A reference type variable can store a null reference in Java

True

A variable of a primitive type can never be empty.

True

An advantage of composition is the fact that we can sometimes reuse classes that we have already written, instead of having to design and write new classes.

True

An object and the variable that refers to it are two different things.

True

An object is an instance of a class

True

An object is an instance of a class.

True

ArrayLists are reference types.

True

ArrayLists can only store reference types.

True

Data hiding can allow us to separate the information from its representation.

True

Data hiding helps make encapsulation work.

True

Encapsulation is about assigning responsibilities to objects.

True

In Java, arrays are reference types

True

In general, a setter method will take at least one argument

True

In general, data hiding means that an object keeps what it knows private.

True

In java, a two dimensional array in Java is really an "array of arrays."

True

Java ArrayList is a generic type.

True

Java ArrayLists are like re-sizable arrays

True

Java ArrayLists are like resizable arrays.

True

Java boolean literal values are expressed in all lowercase

True

Java has a reference type equivalent for each primitive type

True

Objects encapsulate state

True

Responsibilities that are very closely related can and should generally be assigned to a single object.

True

The Java String type is a reference type.

True

The ability to use variables as index expression for arrays gives us a way to generalize array access.

True

The differences between value types and reference types applies to parameters

True

We can implement a getter method without a setter method, thus creating a read-only value.

True

We can more easily debug a program when the responsibilities are well encapsulated

True

We can use a variable as an index expression to access an array element in Java

True

We cannot delete individual elements from an array in Java

True

We cannot delete individual elements from an array in Java.

True

When there are no remaining references to an object in your Java program, the object will be destroyed.

True

relational operators

Used to compare two values. Operators include =,<,>,<=,>=,<>, !(not)

Variable

Variables allow us to store a value for later use, and to retrieve that value whenever we need it. A variable is simply a place to store a value, and we use an assignment statement to store a value in a variable.<variable_name> = <expression>

what is the output of the following java program? Class Driver { public static void main(String[] args) { foo(3); bar(5); } static void foo(int a) { bar(a+1); System.out.print(a); } static void bar(int a) { System.out.print(a); } } a. 435 b. 653 c. 453 d. 563 e. 445 f. 365 g. none of these

a

what is the output of the following java program? Class Driver { public static void main(String[] args) { int a = 3; int b = 5; if (a>b || a * 2 > b) System.out.print(a); else System.out.print(b); } } a. 3 b. 5 c. 2 d. 8 e. none of these

a

which of the following would correctly declare and instantiate an array of 4 ints? a. int[] values = new int [4]; b. int[] values = new int [5]; c. int[] values = new int [6]; d. int[4] values = new int []; e. int[5] values = new int []; f. int[6] values = new int []; g. int[4] values = new int [4]; h. int[5] values = new int [5]; i. int[6] values = new int [6]; j. int[] values = int[4]; k. int[] values = int[5]; l. int[] values = int[6]; m. none of the above

a

A single silicon chip made from potentially millions of transistors is called

a Central Processing Unit (CPU)

The term "applet" refers to

a Java program that runs within a web browser

Hexadecimal Number System

a base-16 system, consisting of the 16 symbols 0 through 9 and A through F.

An example of an input device that interfaces between computers and humans is

a microphone

What structure is commonly used to iterate over all the elements of a two dimensional array?

a nested loop

If a reference type variable does not store a reference to an object, then it stores __________.

a null reference

A Java Virtual Machine is

a program that simulates a real CPU

69) Consider the following code snippet: public class Vehicle { private String model; private double rpm; . . . } Which of the following methods would correctly remove the values of the class instance variables? a) public void reset() { model = null; rpm = 0; } b) public void reset() { model.clear(); rpm = 0; } c) public void reset(String aModel, double theRpm) { model = aModel; rpm = theRpm; } d) public void reset("",0) { }

a) public void reset() { model = null; rpm = 0; }

Which operator is used to concatenate two or more strings? a) + b) % c) & d) ^

a) +

What is the value inside the value variable at the end of the given code snippet? public static void main(String[] args) { int value = 3; value = value - 2 * value; value++; } a) -2 b) 0 c) 2 d) 4

a) -2

What is the output of the following code snippet? public static void main(String[] args) { int num1 = 10; int num2 = 5; int num3 = 200; num3 = num3 % (num1 * num2); System.out.println(num3); } a) 0 b) 4 c) 10 d) 250

a) 0

40) What is the value of the count variable after the execution of the given code snippet? ArrayList<Integer> num = new ArrayList<Integer>(); num.add(1); num.add(2); num.add(1); int count = 0; for (int i = 0; i < num.size(); i++) { if (num.get(i) % 2 == 0) { count++; } } a) 1 b) 2 c) 0 d) 3

a) 1

78) What is the value of the cnt variable after the execution of the code snippet below? ArrayList<Integer> somenum = new ArrayList<Integer>(); somenum.add(1); somenum.add(2); somenum.add(1); int cnt = 0; for (int index = 0; index < somenum.size(); index++) { if (somenum.get(index) % 2 == 0) { cnt++; } } a) 1 b) 2 c) 3 d) 0

a) 1

99) What is the output of the following code? int[][] counts = { { 0, 0, 1 }, { 0, 1, 1, 2 }, { 0, 0, 1, 4, 5 }, { 0, 2 } }; System.out.println(counts[3].length); a) 2 b) 3 c) 4 d) 5

a) 2

What is result of evaluating the following expression? (45 / 6) % 5 a) 2 b) 7 c) 2.5 d) 3

a) 2

What is the output of this code snippet? int sum = 22; sum = sum + 2; System.out.print(sum); // sum = sum + 4; System.out.print(sum); a) 2424 b) 2425 c) 2428 d) 2528

a) 2424

What is the output of the following code snippet? public static void main(String[] args) { double x; x = Math.pow(3.0, 2.0) + Math.pow(4.0, 2.0); System.out.println(x); } a) 25.0 b) 34 c) 7.0 d) 14

a) 25.0

What is the output of the following code snippet? public static void main(String[] args) { int s; double f = 365.25; s = f / 10; System.out.println(s); } a) 36 b) 36.525 c) 37 d) No output because the code snippet generates compilation errors

a) 36

What does the following statement sequence print if the user input is 123? public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter a number "); int myInt = in.nextInt(); myInt += 456; System.out.println(myInt); } a) 579 b) Compile-time error c) Run-time error d) 123456

a) 579

88) Which operator should you use to test whether an object reference is null? a) == b) >= c) <= d) =

a) ==

79) Which one of the following code snippets accepts the integer input in an array list named num1 and stores the even integers of num1 in another array list named evennum? a) ArrayList<Integer> num1 = new ArrayList<Integer>(); ArrayList<Integer> evennum = new ArrayList<Integer>(); Scanner in = new Scanner(System.in); int data; for (int i = 0; i < 10; i++) { data = in.nextInt(); num1.add(data); if (num1.get(i) % 2 == 0) { evennum.add(num1.get(i)); } } b) ArrayList<Integer> num1 = new ArrayList<Integer>(); ArrayList<Integer> evennum = new ArrayList<Integer>(); Scanner in = new Scanner(System.in); int data; for (int i = 0; i < 10; i++) { data = in.nextInt(); num1.add(data); if (num1.get(i) % 2 != 0) { evennum.add(num1.get(i)); } } c) ArrayList<Integer> num1 = new ArrayList<Integer>(); ArrayList<Integer> evennum = new ArrayList<Integer>(); Scanner in = new Scanner(System.in); int data; for int i = 0; i < 10; i++) { data = in.nextInt(); num1.add(data); if (num1.get(i) % 2 != 0) { evennum[i] = num1[i]; } } d) ArrayList<Integer> num1 = new ArrayList<Integer>(); ArrayList<Integer> evennum = new ArrayList<Integer>(); Scanner in = new Scanner(System.in); int data; for (int i = 0; i < 10; i++) { data = in.nextInt(); num1.add(data); if (num1.get(i) % 2 == 0) { evennum[i] = num1[i]; } }

a) ArrayList<Integer> num1 = new ArrayList<Integer>(); ArrayList<Integer> evennum = new ArrayList<Integer>(); Scanner in = new Scanner(System.in); int data; for (int i = 0; i < 10; i++) { data = in.nextInt(); num1.add(data); if (num1.get(i) % 2 == 0) { evennum.add(num1.get(i)); } }

49) Which statement calls a constructor with no construction parameters? a) Circle c = new Circle(); b) Circle c = new Circle; c) Circle c = Circle() d) A call to a constructor must have construction parameters.

a) Circle c = new Circle();

21) Which of the following statements is true regarding classes? a) Each object of a class has a separate copy of each instance variable. b) All objects created from a class share a single set of instance variables. c) Private instance variables can be accessed by any user of the object. d) Public instance variables can be accessed only by the object itself.

a) Each object of a class has a separate copy of each instance variable.

What does the following statement sequence print? String str = "Java"; str += " is powerful"; System.out.println(str); a) Java is powerful b) Java + is powerful c) is powerful d) Compile-time error

a) Java is powerful

Which one of the following statements displays the output as (1.23e+02)? a) System.out.printf("%(5.2e", -123.0); b) System.out.printf("%5.2e", -123.0); c) System.out.printf("^5.2e", -123.0); d) System.out.printf("%5.2E", -123.0);

a) System.out.printf("%(5.2e", -123.0);

Which one of the following statements displays the output as 54321.00? a) System.out.printf("%8.2f", 54321.0); b) System.out.printf("%8,2f", 54321.0); c) System.out.printf(",8.2f", 54321.0); d) System.out.printf("%8.00f", 54321.0);

a) System.out.printf("%8.2f", 54321.0);

Which one of the following statements displays the output as +00321.00? a) System.out.printf("+%09.2f", 321.0); b) System.out.printf("%009,2f", 321.0); c) System.out.printf("+9.2f", 321.0); d) System.out.printf("%09.00f", 321.0);

a) System.out.printf("+%09.2f", 321.0);

Which of the given System.out.print statements generates the following output? ABCDE"\ a) System.out.println("ABCDE\"\\"); b) System.out.println("ABCDE"\"); c) System.out.println("ABCDE"\); d) System.out.println("ABCDE\"\")

a) System.out.println("ABCDE\"\\");

60) Which one of the following statements is correct for displaying the value in the second row and the third column of a two-dimensional, size 3 by 4 array? a) System.out.println(arr[1][2]); b) System.out.println(arr[2][3]); c) System.out.println(arr[2][1]); d) System.out.println(arr[3][2]);

a) System.out.println(arr[1][2]);

84) Which one of the following statements is correct for displaying the value in the third row and the fourth column of a two-dimensional 5 by 6 array? a) System.out.println(arr[2][3]); b) System.out.println(arr[3][4]); c) System.out.println(arr[3][2]); d) System.out.println(arr[4][3]);

a) System.out.println(arr[2][3]);

76) What should you check for when calculating the largest value in an array list? a) The array list contains at least one element. b) The array list contains at least two elements. c) The array list contains the minimum value in the first element. d) The array list contains the maximum value in the first element.

a) The array list contains at least one element.

96) What should you check for when calculating the smallest value in an array list? a) The array list contains at least one element. b) The array list contains at least two elements. c) The array list contains the maximum value in the first element. d) The array list contains the minimum value in the first element.

a) The array list contains at least one element.

68) Consider the following code snippet: public static void check(ArrayList<Integer> chknum1) { int cnt = 0; for(int i = 0; i < chknum1.size(); i++) { if(chknum1.get(i) == 0) { cnt++; } } System.out.println("The total 0 values in the list are: " + cnt); } Which one of the following is true about the check method in the given code snippet? a) The check method counts all the elements with value 0 in an array list passed as a parameter to the method. b) The check method removes all the elements with value 0 from an array list passed as a parameter to the method. c) The check method counts all the elements with value 0 in an array list passed as a parameter to a method and also returns the count. d) The check method adds 0 to the elements of an array list as a parameter to a method and also returns the array list.

a) The check method counts all the elements with value 0 in an array list passed as a parameter to the method.

88) Which statement is true about the code snippet below? ArrayList<String> names = new ArrayList<String>(); names.add("John"); names.add("Jerry"); ArrayList<String> friends = new ArrayList<String>(names); friends.add("Harry"); a) The final size of names is 2; the final size of friends is 3 b) The final size of names is 3; the final size of friends is 2 c) The final size of names is 3; the final size of friends is 3 d) Compilation error

a) The final size of names is 2; the final size of friends is 3

23) Consider the following code snippet: public class Coin { private String coinName; public String getCoinValue() { . . . } } Which of the following statements is correct? a) The getCoinValue method can be accessed by any user of a Coin object. b) The getCoinValue method can be accessed only by methods of the Coin class. c) The getCoinValue method can be accessed only by methods of another class. d) The getCoinValue method cannot be accessed at all.

a) The getCoinValue method can be accessed by any user of a Coin object.

25) Consider the following code snippet: public class Employee { private String empName; public String getEmpName() { . . . } } Which of the following statements is correct? a) The getEmpName method can be accessed by any user of an Employee object. b) The getEmpName method can be accessed only by methods of the Employee class. c) The getEmpName method can be accessed only by methods of another class. d) The getEmpName method cannot be accessed at all.

a) The getEmpName method can be accessed by any user of an Employee object.

Which of the following options is valid with reference to the code snippet? public static void main(String[] args) { double d = 45.326; double r = d % 9.0; System.out.println(r); } a) The value inside the variable r will be 0.326 b) The value inside the variable r will be 5.036 c) Variable r has to be defined as an integer because the % operator always returns an integer d) The initialization of variable r is wrong, because the % operator expects integer values as operands

a) The value inside the variable r will be 0.326

What is the output of this code snippet? double average; int grade1 = 87; int grade2 = 94; // System.out.print("The average is " + (grade1 + grade2) / 2.0); System.out.print("The average is " + average); a) Unpredictable result b) The average is 91.5 c) The average is 91.5 The average is 91.5 d) The average is 91.5 The average is 0.0

a) Unpredictable result

12) Which of the following statements about classes is correct? a) When programmers work with an object of a class, they do not need to know how the object stores its data or how its methods are implemented. b) When programmers work with an object of a class, they must understand how the object stores its data and how its methods are implemented. c) When programmers work with an object of a class, they must understand only how the object stores its data. b) When programmers work with an object of a class, they must understand only how the object's methods are implemented.

a) When programmers work with an object of a class, they do not need to know how the object stores its data or how its methods are implemented.

Which is the appropriate time to initialize a variable? a) When you define it b) When you use it c) At the end of the program d) Before the main function

a) When you define it

Which option represents the best choice for a variable name to represent the average grade of students on an exam? a) averageGrade b) $averageGrade c) avg d) AveGd

a) averageGrade

90) Consider the following code snippet: Coin coin1 = null; System.out.println("Value of coin = " + coin1.getValue()); What is wrong with this code? a) coin1 does not refer to an object and will result in an error at run time. b) You cannot concatenate a numeric value with a string value. c) The value variable in coin1 has not been set, and will print as 0. d) There is nothing wrong with this code.

a) coin1 does not refer to an object and will result in an error at run time

Which one of the following statements defines a constant with the value 123? a) final int MY_CONST = 123; b) const int MY_CONST = 123; c) final int MY_CONST; MY_CONST = 123; d) static int MY_CONST = 123;

a) final int MY_CONST = 123;

32) Which of the following is an accessor method of the CashRegister class used in the textbook? a) getTotal b) addItem c) clear d) The CashRegister class does not have any accessor methods

a) getTotal

Which one of the following is a correct method for defining and initializing an integer variable with name value? a) int value = 30; b) Int value = 30; c) int value = .30; d) Int value = .30;

a) int value = 30;

57) Which one of the following statements is the correct definition for a two-dimensional array of 20 rows and 2 columns of the type integer? a) int[][] num = new int[20][2]; b) int[][] num = new int[2][20]; c) int[][] num = new int[20,2]; d) int[][] num = new int[2,20];

a) int[][] num = new int[20][2];

8) The public constructors and methods of a class form the public _____ of the class. a) interface b) initialization c) implementation d) encapsulation

a) interface

35) The utility that formats program comments into a set of documents that you can view in a Web browser is called ____. a) javadoc b) javac c) javad d) java

a) javadoc

84) Insert the missing code in the following code fragment. This fragment is intended to implement a method to increase the speed of the motor by one step if possible. public class Motor { public static final STOPPED = 0; public static final SLOW = 1; public static final MEDIUM = 2; public static final FAST = 3; private int motorSpeed; . . . public void increaseMotorSpeed() { if(motorSpeed < FAST) { ________ } } } a) motorSpeed ++; b) motorSpeed = SLOW; c) motorSpeed = MEDIUM; d) motorSpeed = FAST;

a) motorSpeed ++;

31) A method in a class that modifies information about an object is called a/an ____ method. a) mutator b) accessor c) void d) constructor

a) mutator

3) Given the following class definition, which of the following are NOT considered part of the class's public interface? public class CashRegister { public static final double DIME_VALUE = 0.1; private static int objectCounter; public void updateDimes(int dimes) {...} private boolean updateCounter(int counter) {...} } a) objectCounter and updateCounter b) DIME_VALUE and objectCounter c) DIME_VALUE and updateDimes d) updateDimes and updateCounter

a) objectCounter and updateCounter

101) You have created an Employee class. You wish to have a unique, sequential employee number assigned to each new Employee object that is created. Which of the following declarations, if added to the Employee class, would allow you to determine which number was last assigned when you are creating a new Employee object? a) private static int lastAssignedEmpNum = 500; b) public int lastAssignedEmpNum = 500; c) private int lastAssignedEmpNum = 500; d) public static int lastAssignedEmpNum = 500;

a) private static int lastAssignedEmpNum = 500;

100) You have created a Student class. You wish to have a unique, sequential student number assigned to each new Student object that is created. Which of the following declarations, if added to the Student class, would allow you to determine which number was last assigned when you are creating a new Student object? a) private static int lastAssignedStudentNum = 1500; b) public int lastAssignedStudentNum = 1500; c) private int lastAssignedStudentNum = 1500; d) public static int lastAssignedStudentNum = 1500;

a) private static int lastAssignedStudentNum = 1500;

59) You are creating a class named Employee. Which statement correctly declares a constructor for this class? a) public Employee() b) public void Employee() c) void Employee() d) new Employee()

a) public Employee()

62) Complete the following code snippet to create a constructor that will initialize the class instance variables shown using data provided by the program which is creating an object from this class. public class Employee { private String empID; private boolean hourly; _______________________ { . . . } } Which of the following statements can be used to create an object of type Employee? a) public Employee(String employeeID, boolean isHourly) b) public Employee(employeeID, isHourly) c) public Employee(empID, Hourly) d) public Employee()

a) public Employee(String employeeID, boolean isHourly)

58) You are creating a class named Vessel. Which statement correctly declares a constructor for this class? a) public Vessel() b) public void Vessel() c) void Vessel() d) new Vessel()

a) public Vessel()

64) Which one of the following is a valid signature of a method with an integer two-dimensional array parameter of size 10 x 10? a) public static void func(int[][] arr) b) public static void func(int[10][] arr) c) public static void func(int[][10] arr) d) public static void func(int[10][10] arr)

a) public static void func(int[][] arr)

80) Insert the missing code in the following code fragment. This fragment is intended to implement a method to assign a value to an instance variable. public class Vehicle { private String model; private double rpm; . . . _______ { model = modelName; } } a) public void setModel(String modelName) b) public void getModel() c) public String getModel() d) public String setModel(String modelName)

a) public void setModel(String modelName)

4) Given the following class definition, which of the following are NOT considered part of the class's public interface? public class Motorcycle { public static final int WHEEL_COUNT = 2; private int rpmRating; public void updatePrice(double increase) {...} private String designCode() {...} } a) rpmRating and designCode b) WHEEL_COUNT and designCode c) WHEEL_COUNT and updatePrice d) updatePrice and designCode

a) rpmRating and designCode

28) Consider the following code snippet: public class Vehicle { . . . public void setVehicleAttributes(String attributes) {. . . } public String getVehicleAtrributes() {. . . } public String getModelName() {. . . } } Assuming that the names of the methods reflect their action, which of the following statements about these instance methods is NOT correct? a) setVehicleAttributes is an accessor method. b) setVehicleAttributes is a mutator method. c) getVehicleAttributes is an accessor method. d) getModelName is an accessor method

a) setVehicleAttributes is an accessor method.

Which one of the following statements can be used to extract the last 10 characters from the string variable str? a) str.substring(str.length() - 10, str.length()) b) str.substring(10, str.length()) c) str.substring(str.length() - 9, 10) d) str.substring(0, 10)

a) str.substring(str.length() - 10, str.length())

Which one of the following statements can be used to extract the last five characters from any string variable str? a) str.substring(str.length() - 5, str.length()) b) str.substring(5, 5) c) str.substring(str.length() - 4, 5) d) str.substring(str.length() - 5, 5)

a) str.substring(str.length() - 5, str.length())

20) Which of the following is NOT part of the declaration of an instance variable? a) the return type b) a modifier specifying the level of access c) the data type d) the name

a) the return type

38) The following statement gets an element from position 4 in an array: x = a[4]; What is the equivalent operation using an array list? a) x = a.get(4); b) x = a.get(); c) x = a.get[4]; d) x = a[4];

a) x = a.get(4);

Which of the following Java literals have the data type int? a. 123 b. 3f c. 3.14 d. -3 e. 'a' f. "3.0" g. '\n' h. 3.14f i. true j. false k. "true" l. 3.0

a, d

which of the following loops would correctly add 1 to all but the first element in values? a. for (int j = 1; j < values.length; j++) values[j]++; b. for (int j = 0; j < values.length; j++) values[j]++; c. for (int j = 1; j < values.length - 1; j++) values[j]++; d. for (int j = 0; j < values.length - 1; j++) values[j]++; e. for (int j = values.length - 1; j >0; j--) values[j]++; f. for (int j = values.length; j >0; j--) values[j]++; g. for (int j = values.length; j >=0; j--) values[j]++; h. for (int j = values.length - 1; j >=0; j--) values[j]++; i. none of these

a, e

The Java keywords "public" and "private" are called _____.

access modifiers

Which method can be used to insert an element into an ArrayList?

add

The Java statement counter++ will

add 1 to the variable count

Which of the following is/are true about arrays? Choose all that apply.

an array is a set of variables

The java expression like the one below will evaluate to ________. new Bunny("Flopsy", 8)

an object reference

Which method can be used to add an element to an ArrayList? Choose all that apply.

answered: at, none of these

The most widely used modeling tool for software design and construction is _____.

answered: flowcharts, pseudocode

The Java keyword used to declare that a method does not return a value is _____.

answered: null

Immediately after the object's variables are initialized _____.

answered: the object is ready for use

High-level programming languages

are independent of the underlying hardware

Which of the following is a major functional limitation of arrays?

arrays have a fixed size

what is the output of the following java program? Class Driver { public static void main(String[] args) { foo(); bar(); } static void foo() { System.out.print("foo"); bar(); } static void bar () { System.out.print("bar"); } } a. barfoobar b. foobarbar c. foofoobar d. foobarfoo e. barbarfoo f. barfoofoo g. none of these

b

what is the output of the following java program? Class Driver { public static void main(String[] args) { int a = 0; int b = 8; while (a < b) { a = a + 2; b = b - 1; System.out.print("buz"); } System.out.print(a); } } a. buzbuzbuzbuz4 b. buzbuzbuz6 c. buzbuzbuzbuz6 d. buzbuzbuz8 e. buzbuzbuz5 f. none of these

b

75) You have created a Coin class and a Purse class. Which of the following methods will correctly add a Coin object to an array list named coins in the Purse class? a) public void add(Coin aCoin) { coins.add(new Coin(aCoin)); } b) public void add(Coin aCoin) { coins.add(aCoin); } c) public void add(Coin aCoin) { coins.addItem(new Coin(aCoin)); } d) public void add(Coin aCoin) { coins.addItem(aCoin); }

b) public void add(Coin aCoin) { coins.add(aCoin); }

77) You have created a Fruit class and a ShoppingCart class. Which of the following methods will correctly add a Fruit object to an array list named fruits in the ShoppingCart class? a) public void add(Fruit aFruit) { fruits.add(new Fruit(aFruit)); } b) public void add(Fruit aFruit) { fruits.add(aFruit); } c) public void add(Fruit aFruit) { fruits.addItem(new Fruit(aFruit)); } d) public void add(Fruit aFruit) { fruits.addItem(aFruit); }

b) public void add(Fruit aFruit) { fruits.add(aFruit); }

71) Consider the following code snippet: public class Course { private int studentCount; . . . } Which of the following methods would correctly keep track of the total number of enrollees in a course? a) public void addEnrollees(int newEnrollees) { studentCount = newEnrollees } b) public void addEnrollees(int newEnrollees) { studentCount = studentCount + newEnrollees } c) public void addEnrollees(int newEnrollees) { newEnrollees = studentCount } d) public void addEnrollees(int newEnrollees) { newEnrollees = newEnrollees + studentCount }

b) public void addEnrollees(int newEnrollees) { studentCount = studentCount + newEnrollees }

70) Consider the following code snippet: public class Transaction { private int transactionCount; . . . } Which of the following methods would correctly keep track of the total number of transactions? a) public void addTransactions(int newTransactions) { transactionCount = newTransactions } b) public void addTransactions(int newTransactions) { transactionCount = transactionCount + newTransactions } c) public void addTransactions(int newTransactions) { newTransactions = transactionCount } d) public void addTransactions(int newTransactions) { newTransactions = newTransactions + transactionCount }

b) public void addTransactions(int newTransactions) { transactionCount = transactionCount + newTransactions }

73) Consider the following code snippet: public class AutoRace { private int lapCount; private double totalTime; . . . } Which of the following completeLap methods would correctly track how many laps occurred? a) public void completeLap(double lapTime) { totalTime = totalTime + lapTime; } b) public void completeLap(double lapTime) { totalTime = totalTime + lapTime; lapCount ++; } c) public void completeLap(double lapTime) { totalTime = totalTime + lapTime; lapCount = lapCount + lapTime; } d) public void completeLap(double lapTime) { totalTime = lapCount + lapTime; }

b) public void completeLap(double lapTime) { totalTime = totalTime + lapTime; lapCount ++; }

72) Consider the following code snippet: public class BankAccount { private int transactionCount; private double balance; . . . } Which of the following deposit methods would correctly track how many deposits occurred? a) public void deposit(double amount) { balance = balance + amount; } b) public void deposit(double amount) { balance = balance + amount; transactionCount ++; } c) public void deposit(double amount) { balance = balance + amount; transactionCount = transactionCount + amount; } d) public void deposit(double amount) { balance = transactionCount + amount; }

b) public void deposit(double amount) { balance = balance + amount; transactionCount ++; }

Which one of the following operators computes the remainder of an integer division? a) / b) % c) \ d) !

b) %

72) Consider the following code snippet: ArrayList<Double> somedata = new ArrayList<Double>(); somedata.add(10.5); What is the size of the array list somedata after the given code snippet is executed? a) 0 b) 1 c) 10 d) 2

b) 1

What will be the value stored in the variable x after the execution of the following code snippet? int a = 10; int b = 20; int c = 2; int x = b / a /*c*/; a) 1 b) 2 c) 4 d) The code has a syntax error

b) 2

55) Consider the following code snippet: int cnt = 0; int[][] numarray = new int[2][3]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 2; j++) { numarray[j][i] = cnt; cnt++; } } What is the value of numarray[1][2] after the code snippet is executed? a) 2 b) 5 c) 3 d) 4

b) 5

85) What is the output of the code snippet below? int[][] arr = { { 1, 2, 3, 0 }, { 4, 5, 6, 0 }, { 0, 0, 0, 0 } }; int val = arr[1][2] + arr[1][3]; System.out.println(val); a) 5 b) 6 c) 7 d) 9

b) 6

86) Consider the following code snippet: int[][] arr = { { 1, 2, 3, 0 }, { 4, 5, 6, 0 }, { 0, 0, 0, 0 } }; int[][] arr2 = arr; System.out.println(arr2[2][1] + arr2[1][2]); What is the output of the given code snippet on execution? a) 5 b) 6 c) 7 d) 9

b) 6

What is the result of the following expression? double d = 2.5 + 4 * -1.5 - (2.5 + 4) * -1.5; a) 24.375 b) 6.25 c) 12.375 d) 6

b) 6.25

81) What is the value of myArray[1][2] after this code snippet is executed? int count = 0; int[][] myArray = new int[4][5]; for (int i = 0; i < 5; i++) { for (int j = 0; j < 4; j++) { myArray[j][i] = count; count++; } } a) 8 b) 9 c) 19 d) 11

b) 9

What is the value of Math.pow(3, 2)? a) 6 b) 9 c) 8 d) 5

b) 9

74) Your program needs to store a sequence of integers of unknown length. Which of the following is most suitable to use? a) An array declared as int[] marks; b) A array list declared as ArrayList<Integer> marks = new ArrayList<Integer>(); c) An array declared as int marks[10000]; d) An array declared as int marks[10];

b) A array list declared as ArrayList<Integer> marks = new ArrayList<Integer>();

48) Which of the following statements about constructors is NOT correct? a) A constructor must have the same name as the class name. b) A call to a constructor must always have construction parameters. c) A constructor initializes the instance variables of an object. d) A class can have more than one constructor

b) A call to a constructor must always have construction parameters

9) Which of the following statements about classes is correct? a) A class is an object that can be manipulated by a program. b) A class describes a set of objects with the same behavior. c) Class is another name for a method. d) A class can contain only methods

b) A class describes a set of objects with the same behavior.

Which of the following guidelines will make code more explanatory for others? a) Use more statements in source code. b) Add comments to source code. c) Avoid usage of complex calculations in source code. d) Always enclose the statements in curly braces in source code.

b) Add comments to source code.

5) Which of the following are considered members of a class? a) Private instance variables and methods. b) All instance variables and methods. c) Non-static instance variables and methods. d) Public instance variables and methods.

b) All instance variables and methods.

99) Consider the following code snippet: public class Student { private static int lastAssignedStudentNum = 1500; { . . . } } Which of the following statements about this code is correct? a) Each object created from this class will have its own lastAssignedStudentNum variable. b) All objects created from this class will share the lastAssignedStudentNum variable. c) The lastAssignedStudentNum variable's value cannot be changed. d) You cannot assign a value to a static variable.

b) All objects created from this class will share the lastAssignedStudentNum variable

41) Which one of the following code snippets accepts the integer input in an array named num1 and stores the odd integers of num1 in another array named oddnum? a) ArrayList<Integer> num1 = new ArrayList<Integer>(); ArrayList<Integer> oddnum = new ArrayList<Integer>(); int data; Scanner in = new Scanner(System.in); for (int i = 0; i < 10; i++) { data = in.nextInt(); num1.add(data); if (num1.get(i) % 2 == 0) { oddnum.add(num1.get(i)); } } b) ArrayList<Integer> num1 = new ArrayList<Integer>(); ArrayList<Integer> oddnum = new ArrayList<Integer>(); int data; Scanner in = new Scanner(System.in); for (int i = 0; i < 10; i++) { data = in.nextInt(); num1.add(data); if (num1.get(i) % 2 != 0) { oddnum.add(num1.get(i)); } } c) ArrayList<Integer> num1 = new ArrayList<Integer>(); ArrayList<Integer> oddnum = new ArrayList<Integer>(); int data; Scanner in = new Scanner(System.in); for (int i = 0; i < 10; i++) { data = in.nextInt(); num1.add(data); if (num1.get(i) % 2 == 0) { oddnum.add(num1[i]); } } d) ArrayList<Integer> num1; ArrayList<Integer> oddnum = new ArrayList<Integer>(); int data; Scanner in = new Scanner(System.in); for (int i = 0; i < 10; i++) { data = in.nextInt(); num1.add(data); if (num1[i] % 2 != 0) { oddnum.add(num1[i]); } }

b) ArrayList<Integer> num1 = new ArrayList<Integer>(); ArrayList<Integer> oddnum = new ArrayList<Integer>(); int data; Scanner in = new Scanner(System.in); for (int i = 0; i < 10; i++) { data = in.nextInt(); num1.add(data); if (num1.get(i) % 2 != 0) { oddnum.add(num1.get(i)); } }

At what point in the problem-solving process should one write pseudocode? a) After writing Java code, as a way to summarize the code's algorithm b) Before writing Java code, as a guide for a general solution c) After defining Java variables so that the pseudocode and data types make sense d) Before working out examples by hand in order to guide those examples

b) Before writing Java code, as a guide for a general solution

What is the output of the following statement sequence? public static void main(String[] args) { int x = 100.0 % 6.0; System.out.println(x); } a) 4 b) Compile-time error c) Run-time error d) 16

b) Compile-time error

6) Which of the following statements about objects is correct? a) An object defines only the methods for a class. b) Every object has its own set of data and a set of methods to manipulate the data. c) An object is a sequence of instructions that performs a task. d) All entities, even numbers, are objects.

b) Every object has its own set of data and a set of methods to manipulate the data.

What does the following statement sequence print? String str = "Java Is Good"; int n = str.length(); String mystery = str.substring(n - 4, n) + str.charAt(4) + str.substring(0, 4); System.out.println(mystery); a) Java b) Good Java c) Good d) Is Good

b) Good Java

39) Which statements are true regarding the differences between arrays and array lists? I. Arrays are better if the size of a collection will not change II. Array lists are more efficient than arrays III. Array lists are easier to use than arrays a) I, II b) I, III c) II, III d) I, II, III

b) I, III

Which statements about numeric types in Java are true? I. There is more than one integer type II. The data type float uses twice the storage of double III. The numeric range of the Java integer type is related to powers of two a) I, II b) I, III c) II, III d) I, II, III

b) I, III

40) Which statement about instance variables is correct? a) Instance variables must be declared within a method. b) Instance variables must be declared inside the class, but outside any method. c) Instance variables may be declared inside the class, or inside a method. d) Instance variables must be declared outside the class

b) Instance variables must be declared inside the class, but outside any method

What is the meaning of x = 0; in Java? a) It checks whether x equals 0. b) It sets the variable x to zero. c) It defines a variable named x and initializes it with 0. d) It is a syntax error because x is not always 0.

b) It sets the variable x to zero.

Assuming that the user inputs "Joe" at the prompt, what is the output of the following code snippet? public static void main(String[] args) { System.out.print("Enter your name "); String name; Scanner in = new Scanner(System.in); name = in.next(); name += ", Good morning"; System.out.print(name); } a) The code snippet does not compile because the += operator cannot be used in this context. b) Joe, Good morning c) , Good morning d) Joe

b) Joe, Good morning

Which one of the following statements gives the absolute value of the floating-point number x = -25.50? a) abs(x); b) Math.abs(x); c) x.abs(); d) x.absolute();

b) Math.abs(x);

13) You have created a Motorcycle class which has a constructor with no parameters. Which of the following statements will construct an object of this class? a) Motorcycle myBike; b) Motorcycle myBike = new Motorcycle(); c) myBike.new(Motorcycle); d) Motorcycle.new(myBike);

b) Motorcycle myBike = new Motorcycle();

Consider the following division statements: I. 22 / 7 II. 22.0 / 7 III. 22 / 7.0 Which of the following is correct? a) All three statements will return an integer value. b) Only I will return an integer value. c) Only I, II will return an integer value. d) Only I and III will return an integer value.

b) Only I will return an integer value.

14) You have created a Rocket class which has a constructor with no parameters. Which of the following statements will construct an object of this class? a) Rocket myRocket; b) Rocket myRocket = new Rocket(); c) myRocket.new(Rocket); d) Rocket.new(myRocket);

b) Rocket myRocket = new Rocket();

What (if any) type of error occurs with the following code if the user input is ABC? public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter a number: "); String str = in.next(); int count = Integer.parseInt(str); System.out.println("Input is " + count); } a) Compile-time error b) Run-time error c) Overflow error d) Illegal expression

b) Run-time error

Which of the given statements generates the following output? \\\"/// a) System.out.println("\\\"///"); b) System.out.println("\\\\\\\"///"); c) System.out.println("\\\\\\""//////"); d) System.out.println("\\\"///");

b) System.out.println("\\\\\\\"///");

What is wrong with the following code snippet? int size = 42; cost = 9.99; System.out.println("size = " + size); System.out.println(" cost = " + cost); a) The code snippet uses a variable that has not yet been initialized. b) The code snippet uses a variable that has not been declared. c) The code snippet attempts to assign a decimal value to an integer variable. d) The code snippet attempts to assign an integer value to a decimal variable.

b) The code snippet uses a variable that has not been declared.

What is wrong with the following code snippet? public class Area { public static void main(String[] args) { int width = 10; height = 20.00; System.out.println("area = " + (width * height)); } } a) The code snippet uses an uninitialized variable. b) The code snippet uses an undeclared variable. c) The code snippet attempts to assign a decimal value to an integer variable. d) The code snippet attempts to add a number to a string variable.

b) The code snippet uses an undeclared variable.

22) Consider the following code snippet: public class Coin { private String coinName; . . . } Which of the following statements is correct? a) The coinName variable can be accessed by any user of a Coin object. b) The coinName variable can be accessed only by methods of the Coin class. c) The coinName variable can be accessed only by methods of another class. d) The coinName variable cannot be accessed at all because it is private.

b) The coinName variable can be accessed only by methods of the Coin class.

54) If you do not provide a constructor in a class, which of the following is correct? a) The compiler will generate a constructor with arguments for each class instance variable and will initialize all of the class instance variables. b) The compiler will generate a constructor with no arguments. c) The compiler will generate a constructor with arguments for each class instance variable. d) The class code will not compile.

b) The compiler will generate a constructor with no arguments.

24) Consider the following code snippet: public class Employee { private String empName; . . . } Which of the following statements is correct? a) The empName variable can be accessed by any user of an Employee object. b) The empName variable can be accessed only by methods of the Employee class. c) The empName variable can be accessed only by methods of another class. d) The empName variable cannot be accessed at all because it is private.

b) The empName variable can be accessed only by methods of the Employee class.

91) Which statement is true about the code snippet below? ArrayList<String> names = new ArrayList<String>(); names.add("John"); names.add("Jerry"); ArrayList<String> friends = new ArrayList<String>(names); names.add("Harry"); a) The final size of names is 2; the final size of friends is 3 b) The final size of names is 3; the final size of friends is 2 c) The final size of names is 3; the final size of friends is 3 d) Compilation error

b) The final size of names is 3; the final size of friends is 2

What happens to the fractional part when a division is performed on two integer variables? a) The fractional part is rounded off to the nearest integer value. b) The fractional part is discarded. c) Two integers cannot be used in division; at least one of the operands should be a floating-point number. d) Instead of using an integer division, you should use the modulus operator to perform floating-point division.

b) The fractional part is discarded.

Which statement is true about variable names in Java? a) They can contain the percent sign (%) b) They can contain an underscore symbol ("_") c) They can contain spaces d) They must make sense as a word

b) They can contain an underscore symbol ("_")

The first step in problem solving is a) To write the expression that calculates the answer b) To understand the problem and its inputs and outputs c) To do examples by hand that confirm the solution will work d) To write Java code that can be executed and tested

b) To understand the problem and its inputs and outputs

Which one of the following is an assignment statement? a) int a = 20; b) a = 20; c) assign a = 20; d) assign 20 to a;

b) a = 20;

30) A method in a class that returns information about an object but does not change the object is called a/an ____ method. a) mutator b) accessor c) void d) constructor

b) accessor

19) An instance variable declaration consists of ____. a) the return type, the name of the method, and a list of the parameters (if any). b) an access specifier, the type of the instance variable, and the name of the instance variable. c) an access specifier, a list of the parameters (if any), and the body of the method. d) the type of the instance variable, an access specifier, a list of the parameters (if any), and the body of the method.

b) an access specifier, the type of the instance variable, and the name of the instance variable

37) Private instance variables ___. a) can only be accessed by methods of a different class b) can only be accessed by methods of the same class c) cannot be accessed by methods of the same class d) can only be accessed by the constructor of the class

b) can only be accessed by methods of the same class

Which one of the following statements can be used to get the fifth character from a string str? a) char c = str.charAt(5); b) char c = str.charAt(4); c) char c = str[5]; d) char c = str[4];

b) char c = str.charAt(4);

45) Consider the following code snippet: public int getCoinValue(String coinName) { . . . } Which of the following statements is correct? a) coinName is an implicit parameter. b) coinName is an explicit parameter. c) coinName is the object on which this method is invoked. d) coinName is an instance variable

b) coinName is an explicit parameter.

97) Which statement illustrates the invocation of a static method? a) mySavings.deposit(100); b) double s = Math.sqrt(100); c) deposit(100).mySavings; d) double s = 100.sqrt();

b) double s = Math.sqrt(100);

44) Consider the following code snippet: public int getSalary(String empNum) { . . . } Which of the following statements is correct? a) empNum is an implicit parameter. b) empNum is an explicit parameter. c) empNum is the object on which this method is invoked. d) empNum is an instance variable

b) empNum is an explicit parameter.

7) The process of hiding object data and providing methods for data access is called ____. a) documentation b) encapsulation c) implementation d) initialization

b) encapsulation

18) Each object of a class has a separate copy of each ___. a) method b) instance variable c) constructor d) class

b) instance variable

17) Data required for an object's use are stored in ____. a) local variables b) instance variables c) parameters d) methods

b) instance variables

Which one of the following reserved words is used in Java to represent a value without a fractional part? a) integer b) int c) Int d) Float

b) int

97) Which one of the following is the correct code snippet for calculating the largest value in an integer array list aList? a) int max = 0; for (int count = 1; count < aList.size(); count++) { if (aList.get(count) > max) { max = aList.get(count); } } b) int max = aList.get(0); for (int count = 1; count < aList.size(); count++) { if (aList.get(count) > max) { max = aList.get(count); } } c) int max = aList[1]; for (int count = 1; count < aList.size();count++) { if (aList.get(count) > max) { max = aList.get(count); } } d) int max = aList.get(0); for (int count = 1; count > aList.size();count++) { if (aList.get(count) >= max) { max = aList.get(count); } }

b) int max = aList.get(0); for (int count = 1; count < aList.size(); count++) { if (aList.get(count) > max) { max = aList.get(count); } }

77) Which one of the following is the correct code snippet for calculating the largest value in an integer array list arrList of size 10? a) int maximum = 0; for (int counter = 1; counter < arrList.size(); counter++) { if (arrList.get(counter) > maximum) { maximum = arrList.get(counter); } } b) int maximum = arrList.get(0); for (int counter = 1; counter < arrList.size(); counter++) { if (arrList.get(counter) > maximum) { maximum = arrList.get(counter); } } c) int maximum = arrList.get(1); for (int counter = 1; counter < arrList.size(); counter++) { if (arrList.get(counter) > maximum) { maximum = arrList.get(counter); } } d) int maximum = arrList.get(0); for (int counter = 1; counter > arrList.size(); counter++) { if (arrList.get(counter) < maximum) { maximum = arrList.get(counter); } }

b) int maximum = arrList.get(0); for (int counter = 1; counter < arrList.size(); counter++) { if (arrList.get(counter) > maximum) { maximum = arrList.get(counter); } }

Which of the following statements places input into the variable value given this line of code? Scanner in = new Scanner(System.in); a) int value = in(); b) int value = in.nextInt(); c) int value = in.next(); d) int value = in.nextFloat();

b) int value = in.nextInt();

59) Which one of the following is the correct definition for initializing data in a two-dimensional array of three rows and two columns? a) int[][] arr = { { 1, 1, 1 }, { 2, 2, 2 }, }; b) int[][] arr = { { 1, 1 }, { 2, 2 }, { 3, 3 } }; c) int[][] arr = { { 1, 1 } { 2, 2 } { 3, 3 } }; d) int[][] arr = { { 1, 1, 1 } { 2, 2, 2 } { 3, 3, 3 } };

b) int[][] arr = { { 1, 1 }, { 2, 2 }, { 3, 3 } };

83) Insert the missing code in the following code fragment. This fragment is intended to implement a method to set the state of the object. public class Motor { public static final STOPPED = 0; public static final PAUSED = 1; public static final RUNNING = 2; private int motorState; . . . public void stopMotor() { ________ } } a) motorState++; b) motorState = STOPPED; c) motorState = PAUSED; d) motorState = RUNNING;

b) motorState = STOPPED;

What are the values of num1 and num2 after this snippet executes? double num1 = 4.20; double num2 = num1 * 10 + 5.0; a) num1 = 4.20 and num2 = 42.0 b) num1 = 4.20 and num2 = 47.0 c) num1 = 42.0 and num2 = 42.0 d) num1 = 42.0 and num2 = 47.0

b) num1 = 4.20 and num2 = 47.0

What are the values of num1 and num2 after this snippet executes? double num1 = 4.20; double num2 = num1 * 10 + 5.0; a) num1 = 4.20 and num2 = 63.0 b) num1 = 4.20 and num2 = 47.0 c) num1 = 42.0 and num2 = 42.0 d) num1 = 42.0 and num2 = 47.0

b) num1 = 4.20 and num2 = 47.0

The assignment operator a) denotes mathematical equality b) places a new value into a variable c) means the same as the equals sign used in algebra d) makes it illegal to write a statement like sum = sum + 4;

b) places a new value into a variable

39) Which of the following declares a rpmRating instance variable for a Motor class that stores an integer value? a) private integer rpmRating; b) private int rpmRating; c) public integer rpmRating; d) public int rpmRating;

b) private int rpmRating;

38) Which of the following declares a sideLength instance variable for a Square class that stores an integer value? a) private integer sideLength; b) private int sideLength; c) public integer sideLength; d) public int sideLength;

b) private int sideLength;

29) Consider the following code snippet: public class Coin { . . . public void setCoinName(String name) {. . . } public String getCoinName() {. . . } public String getCoinValue() {. . . } } Assuming that the names of the methods reflect their action, which of the following statements about this class is correct? a) setCoinName is an accessor method. b) setCoinName is a mutator method. c) getCoinName is a mutator method. d) getCoinValue is a mutator method.

b) setCoinName is a mutator method.

27) Consider the following code snippet: public class Vehicle { . . . public void setVehicleAttributes(String attributes) {. . . } public String getVehicleAtrributes() {. . . } public String getModelName() {. . . } } Assuming that the names of the methods reflect their action, which of the following statements about this class is correct? a) setVehicleAttributes is an accessor method. b) setVehicleAttributes is a mutator method. c) getVehicleAttributes is a mutator method. d) getModelName is a mutator method.

b) setVehicleAttributes is a mutator method.

96) What type of method does NOT operate on an object? a) private b) static c) final d) instance

b) static

36) Babbage's machine for automatically producing printed tables was called a) the slide rule. b) the difference engine. c) the mechanical calculator. d) the cog wheel.

b) the difference engine.

94) Insert the missing code in the following code fragment. This fragment is intended to initialize an instance variable. public class Motor { int motorSpeed; public Motor(int speed) { motorSpeed = speed; } public Motor() { _________; } } Which of the following lines of code will allow the second constructor to call the first constructor? a) this.motorspeed(0); b) this(0); c) this(speed); d) You cannot call a constructor from within another constructor.

b) this(0);

95) Insert the missing code in the following code fragment. This fragment is intended to initialize an instance variable. public class Vehicle { int numAxles; public Vehicle(int axles) { numAxles = axles; } public Vehicle() { _________; } } Which of the following lines of code will allow the second constructor to call the first constructor? a) this. numAxles(0); b) this(0); c) this(axles); d) this(numAxles);

b) this(0);

What will be the value inside the variables x and y after the given set of assignments? int x = 20; int y = 10; x = (x - y) * 2; y = x / 2; a) x = 40, y = 20 b) x = 20, y = 10 c) x = 10, y = 20 d) x = 20, y = 20

b) x = 20, y = 10

which of the following correctly declares a variable of type int in Java? a. int a = new int(); b. int a; c. int a = 0 d. a = int(3); e. int[] a = 0 f. int [] a = new int [3]; g. none of these

b, c

Which of the following are Java primitive data types? a. String b. float c. Random d. boolean e. char f. int g. Array h. math i. double j. class k. none of these

b, d, e, f, i

suppose you are given the code below. what should the output be? String str1 = "abc"; String str2 = "abc"; if (str1 == str2) { System.out.println("True"); } else{ System.out.println("False"); } a. true b. false c. run time error

b, for strings the .equals() method must be used

suppose you are given the code below. Are there any issues? int i = 1; while (i <= 3) { System.out.println(i); } a. no issues b. infinite loop c. run time error due to integers not being allowed in while loop conditions

b, i was never incremented so 1 will always be printed

given the following declaration: String s1 = "banana"; String s2 = "apple"; evaluate the expression: s2.compareTo(s1)>0 a. true b. false c. none of these

b, if first string is greater in length then second string then it is 1, if the second string is smaller in length then the first one then it is -1, same length is 0

given the following declaration: String s = "Apples and bananas are yummy."; Evaluate the expression: s.indexOf("an") a. -1 b. 0 c. 7 d. 8 e. 9 f. 10 g. 11 h. 12 i. none of these

c

given two String variables, s1 and s2, to determine if s1 is longer than s2, which of the following conditions would you use? a. (! s1.equals(s2)) b. (! s1.length().equals(s2)) c. (s1.length() > s2.length()) d. (s1.length() >= s2.length()) e. length(s1) != length(s2) f. none of these

c

what is the output of the following java program? Class Driver { public static void main(String[] args) { int a = 5; int b = 8; if (a<b) if (a * 2 > b) System.out.print("foo"); else System.out.print("bar"); else System.out.print("buz"); } } a. buz b. bar c. foo d. foobar e. foobuz f. barbuz e. none of these

c

when the following expression is evaluated, the result will be what Java data type? 3+5.0 a. int b. float c. double d. boolean e. char f. String g. none of the above

c

66) Consider the following class: public class Auto { private String make; private String model; private String year; public Auto(String aMake, String aModel, String aYear) { make = aMake; model = aModel; year = aYear; } public String getInfo() { return year + " " + make + " " + model; } } Which of the following code snippets will correctly create an object of this class and display its information? a) myAuto = new Auto(); System.out.println(myAuto.getInfo()); b) Auto myAuto = new Auto(); System.out.println(myAuto.getInfo()); c) Auto myAuto = new Auto("Ford", "Focus", "2011"); System.out.println(myAuto.getInfo()); d) Auto myAuto = new Auto("2011", "Ford", "Focus"); System.out.println(myAuto.getInfo());

c) Auto myAuto = new Auto("Ford", "Focus", "2011"); System.out.println(myAuto.getInfo());

98) Consider the following code snippet: public static class Triangle { public static double getTriangleArea(double heightLen, double baseLen) { . . . } } You are writing a program that needs to use this method. Which of the following statements correctly calls this method? a) Triangle myTriangle = new Triangle(); double area = myTriangle.getTriangleArea(4.0,7.0); b) double area = new Triangle().getTriangleArea(4.0,7.0); c) double area = Triangle.getTriangleArea(4.0, 7.0); d) This method cannot be used outside of the Triangle class.

c) double area = Triangle.getTriangleArea(4.0, 7.0);

74) You have created a Coin class and a Purse class. Which of the following will correctly allow the Purse class to collect Coin objects? a) public Purse { private Coin ArrayList coins; } b) public Purse { private ArrayList coins; } c) public Purse { private ArrayList<Coin> coins; } d) public Purse { public ArrayList<Coin> coins; }

c) public Purse { private ArrayList<Coin> coins; }

76) You have created a ShoppingCart class and a Fruit class. Which of the following will correctly declare an array list of Fruit objects in the ShoppingCart class? a) public ShoppingCart { private Fruit ArrayList fruits; } b) public ShoppingCart { private ArrayList fruits; } c) public ShoppingCart { private ArrayList<Fruit> fruits; } d) public ShoppingCart { public ArrayList<Fruit> fruits; }

c) public ShoppingCart { private ArrayList<Fruit> fruits; }

What is the value of Math.abs(-2)? a) -2 b) 0 c) 2 d) 4

c) 2

58) Consider the following code snippet: int[][] numarray = { { 3, 2, 3 }, { 0, 0, 0 } }; System.out.print(numarray[0][0]); System.out.print(numarray[1][0]); What is the output of the given code snippet? a) 00 b) 31 c) 30 d) 03

c) 30

What is the output of the following code snippet? public static void main(String[] args) { int value = 3; value++; System.out.println(value); } a) 2 b) 3 c) 4 d) No output due to syntax error

c) 4

What is the output of the following code snippet? public static void main(String[] args) { int value = 25; value = value * 2; value--; System.out.println(value); } a) 25 b) 50 c) 49 d) 26

c) 49

What is the output of the following code snippet? public static void main(String[] args) { double a; a = Math.sqrt(9.0) + Math.sqrt(16.0); System.out.println(a); } a) 25.0 b) 337.0 c) 7.0 d) 19.0

c) 7.0

What is the value of Math.pow(2, 3)? a) 5 b) 6 c) 8 d) 9

c) 8

83) What is the output of the code snippet below? int[][] numarray = { { 8, 7, 6 }, { 0, 0, 0 } }; System.out.print(numarray[0][0]); System.out.print(numarray[1][0]); a) 00 b) 87 c) 80 d) 08

c) 80

63) Consider the following code snippet: int[][] arr = { { 1, 2, 3 }, { 4, 5, 6 } }; int val = arr[0][2] + arr[1][2]; System.out.println(val); What is the output of the given code snippet on execution? a) 5 b) 7 c) 9 d) 10

c) 9

82) Which of the following statements about class properties is correct? a) A setter method is used to retrieve the value of a class property. b) A getter method is used to assign the value of a class property. c) A setter method is used to assign the value of a class property. d) You cannot assign values to class properties.

c) A setter method is used to assign the value of a class property.

65) Which of the following statements about testing a class is correct? a) A unit test verifies that only one method within a class works correctly. b) A unit test verifies that a class works correctly when it is in a complete program. c) A unit test verifies that a class works correctly in isolation, outside a complete program. d) A unit test does not need to know what results are expected.

c) A unit test verifies that a class works correctly in isolation, outside a complete program.

93) What is the output of the following statements? ArrayList<String> names = new ArrayList<String>(); names.add("Bob"); names.add(0, "Ann"); names.remove(1); names.add("Cal"); names.set(1, "Tony"); for (String s : names) { System.out.print(s + ", "); } a) Cal, Bob, Ann b) Ann, Bob c) Ann, Tony d) Cal, Bob, Tony

c) Ann, Tony

Which one of the following types of statements is an instruction to replace the existing value of a variable with another value? a) Update b) Declaration c) Assignment d) Initialization

c) Assignment

What is the output of the following code snippet? String firstname = "William"; String lastname; System.out.println("First: " + first); System.out.println("Last: " + lastname); a) First: William Last: b) First: William Last: lastname c) Code will not compile d) Unpredictable output

c) Code will not compile

1) Given the following class definition, which of the following are considered part of the class's public interface? public class CashRegister { public static final double DIME_VALUE = 0.1; private static int objectCounter; public void updateDimes(int dimes) {. . .} private boolean updateCounter(int counter) {. . .} } a) objectCounter and updateCounter b) DIME_VALUE and objectCounter c) DIME_VALUE and updateDimes d) updateDimes and updateCounter

c) DIME_VALUE and updateDimes

61) Consider the following code snippet: public class Employee { private String empID; private boolean hourly; public Employee(String employeeID, boolean isHourly) { . . . } } Which of the following statements can be used to create an object of type Employee? a) Employee anAuto = new Employee(); b) Employee anAuto = new Employee(10548, true); c) Employee anAuto = new Employee("10548", true); d) Employee anAuto = new Employee(10548, "true");

c) Employee anAuto = new Employee("10548", true);

11) Which of the following statements about encapsulation is correct? a) Encapsulation enables changes to the public interface without affecting users of the class. b) Encapsulation prohibits changes to the implementation details to ensure that users of the class will not be affected. c) Encapsulation enables changes to the implementation details without affecting users of the class. d) Encapsulation provides an easier means of transporting class information.

c) Encapsulation enables changes to the implementation details without affecting users of the class.

Which of the following statements about constants in Java are true? I. Although not required, constants are commonly named using uppercase letters II. Only integer values can appear as constants III. A variable can be defined with an initial value, but the reserved word final prevents it from being changed IV. A named constant makes computations that use it clearer a) I, II, III b) II, III, IV c) I, III, IV d) I, II, IV

c) I, III, IV

Which of the following statements with comments is(are) valid? I. int cnt = 0; /* Set count to 0 II. int cnt = 0; /* Set count to 0 */ III. int cnt = 0; // Set count to 0 a) Only I is valid b) I and II are valid c) II and III are valid d) Only III is valid

c) II and III are valid

What is wrong with the following code? int count = 2000 * 3000 * 4000; a) Wrong data type b) Variable is undefined c) Integer overflow d) Illegal expression

c) Integer overflow

Which statement about number literals in Java is false? a) Numbers in exponential notation always have type double b) Zero is an integer c) Integers must be positive d) An integer with fractional part of .0 has type double.

c) Integers must be positive

Which one of the following refers to a number constant that appears in code without explanation? a) Constant b) Variable c) Magic number d) String literal

c) Magic number

One way to avoid round-off errors is to use: a) Math.sqrt() b) Math.pow() c) Math.round() d) Math.truncate()

c) Math.round()

26) Which of the following statements about a class is correct? a) A class should not have private instance variables. b) Private instance variables are the interface to the class. c) Private instance variables hide the implementation of a class from the class user. d) Private instance variables should rarely be used in classes.

c) Private instance variables hide the implementation of a class from the class user

What is the difference between the result of the following two Java statements? I. int cents = (int)(100 * price + 0.5); II. int cents = (100 * price + 0.5); a) Statement I causes truncation, but II does not b) Statement II causes truncation, but I does not c) Statement I compiles, but II does not d) Statement II compiles, but I does not

c) Statement I compiles, but II does not

Which of the following statements displays price = 20.00 a) System.out.print("price = "); System.out.printf(price); b) System.out.print("price = "); System.out.printf("%f", price); c) System.out.print("price = "); System.out.printf("%10.2f", price); d) System.out.print("price = "); System.out.printf("%2.10f", price);

c) System.out.print("price = "); System.out.printf("%10.2f", price);

61) Consider the following code snippet: int[][] arr = { { 13, 23, 33 }, { 14, 24, 34 } }; Identify the appropriate statement to display the value 24 from the given array? a) System.out.println(arr[1][2]); b) System.out.println(arr[2][2]); c) System.out.println(arr[1][1]); d) System.out.println(arr[2][1]);

c) System.out.println(arr[1][1]);

98) Consider the following code snippet, where the array lists contain elements that are stored in ascending order: ArrayList<Integer> list1 = new ArrayList<Integer>(); ArrayList<Integer> list2 = new ArrayList<Integer>(); . . . int count = 0; for (int i = 0; i < list1.size() && i < list2.size(); i++) { if (list1.get(i) == list2.get(i)) { count++; } } Which one of the following descriptions is correct for the given code snippet? a) The code snippet finds the highest value out of the two array lists. b) The code snippet finds the lowest value out of the two array lists. c) The code snippet compares the values of two array lists and stores the count of total matches found. d) The code snippet adds the values of the two array lists.

c) The code snippet compares the values of two array lists and stores the count of total matches found.

What is the output of the following code snippet? int counter = 0; counter++; System.out.print("The initial value of the counter is "); System.out.println(count); a) The initial value of the counter is 0 b) The initial value of the counter is 1 c) The code will not compile d) The initial value of the counter is

c) The code will not compile

57) Consider the following code snippet: public class Vessel { private String type; public String Vessel(String type) { . . . } } What is wrong with this code? a) The class instance variable type must be initialized when it is declared. b) The constructor must not have any arguments. c) The constructor must not have a return type declared. d) The constructor's return type must be void.

c) The constructor must not have a return type declared.

87) Which statement is true about the code snippet below? ArrayList<String> names = new ArrayList<String>(); names.add("John"); names.add("Jerry"); ArrayList<String> friends = names; friends.add("Harry"); a) The final size of names is 2; the final size of friends is 3 b) The final size of names is 3; the final size of friends is 2 c) The final size of names is 3; the final size of friends is 3 d) Compilation error

c) The final size of names is 3; the final size of friends is 3

Assuming that the user inputs a value of 25000 for the pay and 10 for the bonus rate in the following code snippet, what is the output? public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter the pay: "); double pay = in.nextDouble(); System.out.print("Enter the bonus rate: "); double bonus = in.nextDouble(); System.out.println("The new pay is " + (pay + pay * (bonus / 100.0))); } a) The new pay is 25000 b) The new pay is 25100 c) The new pay is 27500 d) The new pay is 30000

c) The new pay is 27500

Assuming that the user inputs a value of 25 for the price and 10 for the discount rate in the following code snippet, what is the output? public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter the price: "); double price = in.nextDouble(); System.out.print("Enter the discount rate: "); double discount = in.nextDouble(); System.out.println("The new price is " + price - price * (discount / 100.0)); } a) The new price is 25 b) The new price is 15 c) The new price is 22.5 d) The new price is 20.0

c) The new price is 22.5

Assuming that the user inputs a value of 30 for the price and 10 for the discount rate in the following code snippet, what is the output? Scanner in = new Scanner(System.in); System.out.print("Enter the price: "); double price = in.nextDouble(); System.out.print("Enter the discount rate: "); double discount = in.nextDouble(); System.out.print("The new price is "); System.out.println(price - price * (discount / 100.0)); a) The new price is 30 b) The new price is 20 c) The new price is 27.0 d) The new price is 33.0

c) The new price is 27.0

62) Consider the following code snippet: int val = arr[0][2]; Which value of arr is stored in the val variable? a) The value in the first row and the second column b) The value in the first row and the first column c) The value in the first row and the third column d) The value in the third row and the second column

c) The value in the first row and the third column

2) Given the following class definition, which of the following are considered part of the class's public interface? public class Motorcycle { public static final int WHEEL_COUNT = 2; private int rpmRating; public void updatePrice(double increase) {...} private String designCode() {...} } a) rpmRating and designCode b) WHEEL_COUNT and designCode c) WHEEL_COUNT and updatePrice d) updatePrice and designCode

c) WHEEL_COUNT and updatePrice

Which of the following statements is correct about constants? a) Constants are written using capital letters because the compiler ignores constants declared in small letters. b) The data stored inside a constant can be changed using an assignment statement. c) You can make a variable constant by using the final reserved word when declaring it. d) Constant variables can only be changed through the Math library.

c) You can make a variable constant by using the final reserved word when declaring it.

What will be the value inside the variables a and b after the given set of assignments? int a = 20; int b = 10; a = (a + b) / 2; b = a; a++; a) a = 15, b = 16 b) a = 16, b = 16 c) a = 16, b = 15 d) a = 15, b = 15

c) a = 16, b = 15

92) When an integer literal is added to an array list declared as ArrayList<Integer>, Java performs: a) casting b) trimming c) auto-boxing d) auto-fixing

c) auto-boxing

Which is the Java equivalent of the following mathematical expression? c = √(a2 + b2) a) c = Math.sqrt(a * 2 + b * 2); b) c = Math.sqrt(a * 2) + Math.sqrt(b * 2); c) c = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2)); d) c = Math.sqrt(Math.pow(a, 2)) + Math.sqrt(Math.pow(b, 2));

c) c = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));

Given the definition final double PI = 3.14159; which of the following is the Java equivalent of the mathematical expression c = radius2 a) c = PI * (radius * 2); b) c = PI * Math.pow(2, radius); c) c = PI * Math.pow(radius, 2); d) c = Math.pow(PI * radius, 2);

c) c = PI * Math.pow(radius, 2);

33) Which of the following is a mutator method of the CashRegister class used in the textbook? a) getTotal b) getCount c) clear d) The CashRegister class does not have any mutator methods.

c) clear

87) Given the following class: public class Coin { private String coinName; private double coinValue; . . . } Consider the following code snippet that utilizes the Coin class: Coin coin1 = new Coin("dime", 0.10); Coin coin2 = coin1; coin2.setValue(0.15); Which of the following statements is correct? a) coin1 has a value of 0.10 and coin2 has a value of 0.15. b) coin1 and coin2 both have the value of 0.10. c) coin1 and coin2 both have the value of 0.15. d) coin2 does not have any data in the coinName variable

c) coin1 and coin2 both have the value of 0.15.

37) Java 7 introduced enhanced syntax for declaring array lists, which is termed a) angle brackets. b) method lists. c) diamond syntax. d) symmetric slants.

c) diamond syntax.

In an airline reservation system, the cost of an airline ticket is required. Which data type should be used to store this value? a) int b) byte c) double d) short

c) double

Which one of the following statements can be used to convert a string str to a double? a) double n = str.parseDouble(); b) double n = Integer.parseDouble(str); c) double n = Double.parseDouble(str); d) double n = double.parseDouble(str);

c) double n = Double.parseDouble(str);

Which one of the following variables is assigned with valid literals? a) int salary = 0; salary = 5000.50; b) int salary1 = 0; salary1 = 1.2E6; c) double salary2 = 0; salary2 = 2.96E-2; d) long salary3 = 0; salary3 = 1E-6;

c) double salary2 = 0; salary2 = 2.96E-2;

42) Input to a method enclosed in parentheses after the method name is known as ____. a) implicit parameters b) interfaces c) explicit parameters d) return values

c) explicit parameters

Which of the following options declares a float variable? a) Float age; b) flt age; c) float age; d) age: float;

c) float age;

43) The object on which a method is invoked is called the ____. a) interface b) procedure c) implicit parameter d) explicit parameter

c) implicit parameter

16) When an object is created from a class, the object is called a/an ____ of the class. a) child b) constructor c) instance d) subclass

c) instance

In an airline reservation system, the number of available seats in an airplane is required. Which data type should be used to store this value? a) double b) float c) int d) long

c) int

Which of the following options defines an integer variable? a) char age; b) integer age; c) int age; d) age: int;

c) int age;

15) The ____ operator is used to construct an object from a class. a) add b) create c) new d) construct

c) new

89) Which choice indicates that a string variable refers to no string? a) nil b) "" c) null d) empty

c) null

Which of the following is the Java equivalent of the following mathematical expression? p = 2 (radius)3 a) p = 2 * Math.PI * (radius * 3); b) p = Math.PI * Math.pow(3, radius); c) p = 2 * Math.PI * Math.pow(radius, 3); d) p = 2 * Math.pow(Math.PI * radius, 3);

c) p = 2 * Math.PI * Math.pow(radius, 3);

79) Insert the missing code in the following code fragment. This fragment is intended to implement a method to get the value stored in an instance variable. public class Employee { private String empID; private boolean hourly; . . . _______ { return hourly; } } a) public void setHourly(String isHourly) b) public void getHourly() c) public boolean getHourly() d) public boolean setHourly(boolean isHourly

c) public boolean getHourly()

69) Consider the following line of code for calling a method named func1: func1(listData, varData); Which one of the following method headers is valid for func1, where listData is an integer array list and varData is an integer variable? a) public static void func1(int[] listData, int varData) b) public static void func1(ArrayList<Integer> listData, varData) c) public static void func1(ArrayList<Integer> ldata, int vdata) d) public static void func1(int vdata, ArrayList<Integer> ldata)

c) public static void func1(ArrayList<Integer> ldata, int vdata)

80) Which one of the following is a correct declaration for a method named passAList that has as arguments an array list myList of size 10 and an integer array intArr of size 20, and that modifies the contents of myList and intArr? a) public static void passAList(ArrayList<Integer> myList(10), int[] intArr) b) public static void passAList(ArrayList<Integer> myList, int[20] intArr) c) public static void passAList(ArrayList<Integer> myList, int[] intArr) d) public static void passAList(ArryaList<Integer> myList, int intArr)

c) public static void passAList(ArrayList<Integer> myList, int[] intArr)

67) Which one of the following is a correct declaration for a method named passAList with the array list num of size 5 as a parameter? a) public static void passAList(ArrayList<Integer> num[5]) b) public static void passAList(ArrayList<Integer> num, 5) c) public static void passAList(ArrayList<Integer> num) d) public static void passAList(num)

c) public static void passAList(ArrayList<Integer> num)

65) Consider the following code snippet: ArrayList<Integer> num1 = new ArrayList<Integer>(); int data; Scanner in = new Scanner(System.in); for (int i = 0; i < 5; i++) { data = in.nextInt(); num1.add(data); if (data == 0 && num1.size() > 3) { num1.remove(num1.size() - 1); } } System.out.println("size is : " + num1.size()); What is the output of the given code snippet if the user enters 1,2,0,0,1 as the input? a) size is : 1 b) size is : 2 c) size is : 4 d) size is : 0

c) size is : 4

50) A constructor is invoked when ___ to create an object. a) the class keyword is used b) the class is defined as public c) the new keyword is used d) the class is defined as private

c) the new keyword is used

93) Insert the missing code in the following code fragment. This fragment is intended to initialize an instance variable. public class Employee { private String empName; . . . public Employee(String empName) { ________ } } a) Employee.empName = empName; b) empName = this.empName; c) this.empName = empName; d) The constructor parameter variable cannot have the same name as an instance variable.

c) this.empName = empName;

92) Insert the missing code in the following code fragment. This fragment is intended to initialize an instance variable. public class Motor { private int motorSpeed; . . . public Motor(int motorSpeed) { ________ } } a) Motor.motorSpeed = motorSpeed; b) motorSpeed = this.motorSpeed; c) this.motorSpeed = motorSpeed; d) The constructor parameter variable cannot have the same name as the instance variable.

c) this.motorSpeed = motorSpeed;

Which keyword is used in Java to define a class?

class

Every Java program consists of at least one

class definition

Every Java program consists of one or more

classes

Which method can be called to remove all elements from an ArrayList at once?

clear

Which of the following are characteristics of the python programming language

compiled interpreted

string(str) operators

concatenation (+) for adding strings together.

given the following declaration: int[] values = {2, 6, 0, 1, 4, 3, 5}; evaluate the expression: values[0]; a. -1 b. 0 c. 1 d. 2 e. 3 f. 4 g. 5 h. 6 i. 7 j. none of these

d

67) Consider the following class: public class Auto { private String make; private String model; private String year; public Auto(String aMake, String aModel, String aYear) { make = aMake; model = aModel; year = aYear; } public String calcMileage(double milesDriven, double gasUsed) { double milage = milesDriven/gasUsed; return mileage.toString(); } } Which of the following code snippets will correctly create an object of type Auto and display its mileage? a) myAuto = new Auto(); System.out.println(myAuto.calcMileage(246.0, 15.8)); b) Auto myAuto = new Auto(); System.out.println(myAuto.calcMileage(246.0, 15.8)); c) Auto myAuto = new Auto("2011", "Ford", "Focus"); System.out.println(myAuto.calcMileage(246.0, 15.8)); d) Auto myAuto = new Auto("Ford", "Focus", "2011"); System.out.println(myAuto.calcMileage(246.0, 15.8));

d) Auto myAuto = new Auto("Ford", "Focus", "2011"); System.out.println(myAuto.calcMileage(246.0, 15.8));

Which one of the following is a correct representation of the given mathematical expression in Java? a) a - b / 2 % 2 b) a - b / 2 c) a - (b / 2) / 2 d) (a - b / 2) / 2

d) (a - b / 2) / 2

Which one of the following is a correct representation of the given mathematical expression in Java? a + b ----- 2 a) a + b % 2 b) a + b / 2 c) a + (b / 2) d) (a + b) / 2

d) (a+b) / 2

What output is produced by these statements? String name = "Joanne Hunt"; System.out.println(name.length()); a) 8 b) 10 c) 9 d) 11

d) 11

What does the following statement sequence print if the user input is 123? public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter a number: "); String str = in.next(); str += 456; System.out.println(str); } a) 579 b) Compile-time error c) Run-time error d) 123456

d) 123456

What is the output of the following code snippet? System.out.printf("%5.3f", 20.0); a) 20 b) 20.0 c) 20.00 d) 20.000

d) 20.000

Assuming that the user enters 23 and 45 as inputs for num1 and num2, respectively, what is the output of the following code snippet? public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter a number: "); String num1 = in.next(); System.out.print("Enter another number: "); String num2 = in.next(); System.out.println(num1 + num2); } a) 23 b) 4523 c) 68 d) 2345

d) 2345

82) How many elements can be stored in a two-dimensional 5 by 6 array? a) 5 b) 6 c) 11 d) 30

d) 30

What is the value inside the var variable at the end of the given code snippet? public static void main(String[] args) { int var = 30; var = var + 2 / var; var++; } a) 0 b) 1 c) 30 d) 31

d) 31

Assuming that the user enters 45 and 62 as inputs for n1 and n2, respectively, what is the output of the following code snippet? public static void main(String[] args) { System.out.print("Enter a number: "); Scanner in = new Scanner(System.in); String n1 = in.next(); System.out.print("Enter another number: "); String n2 = in.next(); String result = n1 + n2; System.out.print(result); } a) 46 b) 4662 c) 107 d) 4562

d) 4562

56) How many elements can be stored in an array of dimension 2 by 3? a) 2 b) 3 c) 5 d) 6

d) 6

70) What is the output of the following code snippet? public static int check(ArrayList<Integer> listData) { int sum = 0; for (int i = 0; i < listData.size(); i++) { sum = sum + listData.get(i); } return sum; } public static void main(String[] args) { ArrayList<Integer> vdata = new ArrayList<Integer>(); int rsum; for (int cnt = 0; cnt < 3; cnt++) { vdata.add(cnt + 1); } rsum = check(vdata); System.out.println(rsum); } a) 4 b) 2 c) 3 d) 6

d) 6

95) What is the output of the following statements? ArrayList<String> cityList = new ArrayList<String>(); cityList.add("London"); cityList.add("New York"); cityList.add("Paris"); cityList.add("Toronto"); cityList.add("Hong Kong"); cityList.add("Singapore"); System.out.print(cityList.size()); System.out.print(" " + cityList.contains("Toronto")); System.out.print(" " + cityList.indexOf("New York")); System.out.println(" " + cityList.isEmpty()); a) 5 true 1 false b) 6 true 2 false c) 5 false 1 false d) 6 true 1 false

d) 6 true 1 false

51) Which of the following statements about constructors is correct? a) A class can have only one constructor. b) A constructor should always return a value. c) A class must have at least one constructor supplied by the programmer. d) A constructor must have the same name as the class.

d) A constructor must have the same name as the class.

46) Which of the following statements about classes is correct? a) All instance variables and most instance methods should be declared as public. b) All instance variables and most instance methods should be declared as private. c) All instance variables should be declared as public and most instance methods should be declared as private. d) All instance variables should be declared as private and most instance methods should be declared as public.

d) All instance variables should be declared as private and most instance methods should be declared as public.

66) Which one of the following statements about declaring an array list as a method parameter is true? a) An array list declared as a method parameter should be accompanied with its size. b) An array list declared as a method parameter is a constant value by default. c) An array list value cannot be modified in a method when the array list is declared as a parameter. d) An array list value can be modified inside the method.

d) An array list value can be modified inside the method.

47) Which of the following statements about instance methods is correct? a) An instance method should always be declared as public. b) An instance method should always be declared as private. c) An instance method should be declared as public only if it is a helper method. d) An instance method should be declared as private only if it is a helper method.

d) An instance method should be declared as private only if it is a helper method.

94) What is the output of the following statements? ArrayList<String> names = new ArrayList<String>(); names.add("Bob"); names.add(0, "Ann"); names.remove(1); names.add("Cal"); names.set(2, "Tony"); for (String s : names) { System.out.print(s + ", "); } a) Cal, Bob, Ann b) Ann, Bob c) Ann, Cal, Tony d) Array list bound error

d) Array list bound error

89) Consider the following code snippet: public static void main(String[] args) { ArrayList<String> names = new ArrayList<String>(); names.add("John"); names.add("Jerry"); names.add("Janet"); ArrayList<String> names = reverse(names); } public static ArrayList<String> reverse(ArrayList<String> names) { ArrayList<String> result = new ArrayList<String>(); for (int i = names.size() - 1; i >= 0; i--) { result.add(names.get(i)); } return <String>result; } Which statement is true after the main method is executed? a) names contains "Janet", "Jerry", "John" in this order b) names contains "John", "Jerry", "Janet" in this order c) reverse method has a bound error d) Compilation error due to the return statement in reverse method

d) Compilation error due to the return statement in reverse method

What is the result of the following code snippet? double bottles; double bottleVolume = bottles * 2; System.out.println(bottleVolume); a) 0 b) 1 c) 2 d) Does not compile

d) Does not compile

73) What is the output of the following code snippet? ArrayList<Integer> num; num.add(4); System.out.println(num.size()); a) 1 b) 0 c) 4 d) Error because num is not initialized

d) Error because num is not initialized

What does the following statement sequence print? String str = "Harry"; int n = str.length(); String mystery = str.substring(0, 1) + str.substring(n - 2, n); System.out.println(mystery); a) Ha b) Har c) Hy d) Hry

d) Hry

What is the correct way to invoke methods on variables in Java that are strings? a) Methods can only be invoked on string constants, not on variables. b) For each method there is a special operator that must be used. c) There are no methods available in Java for string variables. d) Invoke them using the variable name and the dot (.) notation.

d) Invoke them using the variable name and the dot (.) notation.

Which statement is true? a) Variables cannot be assigned and declared in the same statement b) Variable names must contain at least one dollar sign c) Variable names can be no more than 8 characters long d) It is incorrect to initialize a string variable with a number

d) It is incorrect to initialize a string variable with a number

34) Which type of method modifies the object on which it is invoked? a) Constructor method. b) Static method. c) Accessor method. d) Mutator method.

d) Mutator method.

75) Consider the following code snippet: ArrayList<Integer> arrList = new ArrayList<Integer>(); for (int i = 0; i < arrList.size(); i++) { arrList.add(i + 3); } What value is stored in the element of the array list at index 0? a) 0 b) 3 c) 6 d) None

d) None

What does the following statement sequence print? final String str = "Java"; str += " is powerful"; System.out.println(str); a) Java is powerful b) Java + is powerful c) is powerful d) Nothing; compile-time error

d) Nothing; compile-time error

Which of the methods below are static methods? I. length II. Substring III. Pow IV. sqrt a) All the methods are static b) Only I, II and III c) Only II and IV d) Only III and IV

d) Only III and IV

Consider the following Java variable names: I. 1stInstance II. basicInt% III. empName_ IV. addressLine1 V. DISCOUNT Which of the following options is correct? a) Only IV is a valid Java variable name. b) Only I and IV are valid Java variable names. c) Only I, IV, and V are valid Java variable names. d) Only III, IV, and V are valid Java variable names.

d) Only III, IV, and V are valid Java variable names.

The typical ranges for integers may seem strange but are derived from a) Base 10 floating-point precision b) Field requirements for typical usage and limits c) Overflows d) Powers of two because of base 2 representation within the computer

d) Powers of two because of base 2 representation within the computer

Suppose a phone number, stored as a ten-character string (of digits only) called phoneNumber, must be converted into a string that has parentheses around the area code. Which statement below will do that? a) String newNumber = "(" + phoneNumber.substring(3, 0) + ")"; b) String newNumber = "(" + ")" + phoneNumber; c) String newNumber = "(" + phoneNumber.substring(1, 3) + ")" + phoneNumber.substring(3, 7); d) String newNumber = "(" + phoneNumber.substring(0, 3) + ")" + phoneNumber.substring(3, 10);

d) String newNumber = "(" + phoneNumber.substring(0, 3) + ")" + phoneNumber.substring(3, 10);

What is wrong with the following code snippet? int average; average = 78A; a) The average variable is never initialized. b) The data type for the average variable is not specified. c) The average variable is never assigned a value. d) The average variable is assigned a non-numeric value.

d) The average variable is assigned a non-numeric value.

90) Which statement is true about the code snippet below? public static void main(String[] args) { Scanner in = new Scanner(System.in); ArrayList<Double> inputs = new ArrayList<Double>(); int ind = 0; while (in.hasNextDouble()) { inputs.set(ind, in.nextDouble()); ind++; } } a) The code adds all input numbers to the array list b) The code has compile-time error c) The array list is full after 100 numbers are entered d) The code has a run-time error

d) The code has a run-time error

63) Consider the following code snippet: public class Vehicle { private String type; private int numAxles; public void Vehicle(String vehicleType, int VehicleAxles) { . . . } } What is wrong with this code? a) There must be a default constructor with no arguments. b) The class instance variables must be initialized in the declaration statements. c) The constructor declaration must have a return type of String. d) The constructor declaration must not have any return type.

d) The constructor declaration must not have any return type.

What is wrong with the following code snippet? int price; price = 9.42; a) The price variable is never initialized. b) The data type for the price variable is not specified. c) The price variable is never assigned a value. d) The price variable is assigned a decimal value.

d) The price variable is assigned a decimal value.

71) What is the result of the following definition of an array list? ArrayList<Double> points; a) The statement causes a compile time error as the size of the array list is not defined. b) The statement creates an array list of size 0. c) The statement creates an array list with unlimited size. d) The statement defines an array list reference but leaves its value as null.

d) The statement defines an array list reference but leaves its value as null.

What is the result of the following statement? String s = "You" + "had" + "me" + "at" + "hello"; a) The string s has the following value: "You had me at "hello" b) The statement results in an error because the + operator can be used only with numbers c) The statement results in an error because the + operation cannot be performed on string literals d) The string s has the following value: "Youhadmeathello"

d) The string s has the following value: "Youhadmeathello"

53) Consider the following code snippet: public class Coin { private String coinName; private int coinValue; public Coin() { } . . . } Which statement reflects the action performed when the constructor to Coin is called? a) The variable coinName will be initialized to "" and the variable coinValue will be initialized to 0. b) The variable coinName will be initialized to "null" and the variable coinValue will be initialized to 0. c) The variable coinName will not be initialized but the variable coinValue will be initialized to 0. d) The variable coinName will be initialized to null and the variable coinValue will be initialized to 0.

d) The variable coinName will be initialized to null and the variable coinValue will be initialized to 0.

55) Consider the following code snippet: public class Employee { private String empID; private boolean hourly; public Employee() { } . . . } Which statement reflects the action performed when the constructor to Employee is called? a) The variable empID will be initialized to "" and the variable hourly will be initialized to false. b) The variable empID will be initialized to "null" and the variable hourly will be initialized to true. c) The variable empID will not be initialized but the variable hourly will be initialized to false. d) The variable empID will be initialized to null and the variable hourly will be initialized to false.

d) The variable empID will be initialized to null and the variable hourly will be initialized to false.

52) Consider the following code snippet: public class Vehicle { private String type; private int numAxles; public Vehicle() { } . . . } Which statement reflects the action performed when the constructor to Vehicle is called? a) The variable type will be initialized to "" and the variable numAxles will be initialized to 0. b) The variable type will be initialized to "null" and the variable numAxles will be initialized to 0. c) The variable type will not be initialized but the variable numAxles will be initialized to 0. d) The variable type will be initialized to null and the variable numAxles will be initialized to 0.

d) The variable type will be initialized to null and the variable numAxles will be initialized to 0.

64) Consider the following code snippet: public class Vehicle { private String type; private int numAxles; public Vehicle(String vehicleType, int VehicleAxles) { . . . } public Vehicle(String vehicleType) { . . . } } What is wrong with this code? a) There must be a default constructor with no arguments. b) A class cannot have more than one constructor. c) If a class has more than one constructor, each one should have a unique name. d) There is nothing wrong with this code.

d) There is nothing wrong with this code.

What is the output of the following code snippet? public static void main(String[] args) { int var1 = 10; int var2 = 2; int var3 = 20; var3 = var3 / (var1 % var2); System.out.println(var3); } a) 0 b) 4 c) 20 d) There will be no output due to a run-time error.

d) There will be no output due to a run-time error.

10) Which of the following statements about encapsulation is correct? a) To use encapsulation, you must provide a set of private methods in the class. b) To use encapsulation, the class must contain only public methods in the class. c) To use encapsulation, the implementation details must be public. d) To use encapsulation, you must provide a set of public methods in the class and hide the implementation details.

d) To use encapsulation, you must provide a set of public methods in the class and hide the implementation details.

What is the output of the following code snippet? public static void main(String[] args){ { String str1; str1 = "I LOVE MY COUNTRY"; String str2 = str1.substring(4, 9); System.out.println(str2); } a) I LOV b) OVE M c) V d) VE MY

d) VE MY

Which of the following statements is correct about constants? a) Constants are written using uppercase letters because the compiler ignores constants declared in lowercase letters. b) The data stored inside a final variable can be changed using an assignment statement. c) You can make a variable constant by using the constant reserved word while declaring the variable. d) Variables defined using final make a code snippet more readable and easier to maintain.

d) Variables defined using final make a code snippet more readable and easier to maintain.

60) Consider the following code snippet: public class Vehicle { private String type; private int numAxles; public Vehicle(String vehicleType, int VehicleAxles) { . . . } } Which of the following statements can be used to create an object of type Vehicle? a) Vehicle anAuto = new Vehicle(); b) Vehicle anAuto = new Vehicle(2, "SUV"); c) Vehicle anAuto = new Vehicle("SUV", "2"); d) Vehicle anAuto = new Vehicle("SUV", 2);

d) Vehicle anAuto = new Vehicle("SUV", 2);

68) Which of the following statements using data values in a class is NOT correct? a) You can use an instance variable's data value in an instance method. b) You can use the argument of an instance method in that method. c) You can use a global constant's value in an instance method. d) You can use the argument of any instance method in any other method.

d) You can use the argument of any instance method in any other method.

Which of the following is the Java equivalent of the following mathematical expression? c = 2 radius a) c = 2 * Math.PI * radius * 2; b) c = 2 * Math.PI * Math.pow(2, radius); c) c = 2 * Math.PI * Math.pow(radius, 2); d) c = 2 * Math.PI * radius;

d) c = 2 * Math.PI * radius;

Which is the Java equivalent of the following mathematical expression? c = (√a + √b)2 a) c = Math.sqrt(a * 2 + b * 2); b) c = Math.sqrt(a * 2) + Math.sqrt(b * 2); c) c = Math.sqrt(pow(a, 2) + Math.pow(b, 2)); d) c = Math.pow((Math.sqrt(a) + Math.sqrt(b)), 2);

d) c = Math.pow((Math.sqrt(a) + Math.sqrt(b)), 2);

86) Consider the following code snippet: Coin coin1 = new Coin("dime", 0.10); Coin coin2 = new Coin("dime", 0.10); Which of the following statements is correct? a) coin1 and coin2 contain references to the same object. b) coin1 and coin2 refer to the same object. c) coin1 and coin2 contain the same memory location. d) coin1 and coin2 each have their own set of instance variables.

d) coin1 and coin2 each have their own set of instance variables

85) Consider the following code snippet: Coin coin1 = new Coin("dime", 0.10); Coin coin2 = coin1; Which of the following statements is NOT correct? a) coin1 and coin2 are object references. b) coin1 and coin2 refer to the same object. c) coin1 and coin2 contain the same memory location. d) coin1 and coin2 each have their own set of instance variables.

d) coin1 and coin2 each have their own set of instance variables.

What is the result of the following code snippet? public static void main(String[] args { double circleRadius; double circleVolume = 22 / 7 * circleRadius * circleRadius; System.out.println(circleVolume); } a) 0 b) 3.14 c) 6.28 d) compile-time error

d) compile-time error

Which of the following is the mathematical equivalent of the following Java expression? h = (4.0 * a * b - Math.pow(b, 2)) / c; a) h = 4ab - 2b / c b) h = (4ab - 2b) / c c) h = 4ab - b2 / c d) h = (4ab - b2) / c

d) h = (4ab - b2) / c

The problem solving process emphasizes a "first, do-it-by-hand" approach because a) Pseudocode is not able to capture the subtleties of complex problems. b) it is faster to do computations by hand than to do them by computer. c) this guarantees that programs will be correct. d) if programmers cannot compute a solution by hand, it is unlikely they will be able to write a program that can do it.

d) if programmers cannot compute a solution by hand, it is unlikely they will be able to write a program that can do it.

54) Consider the following 2-dimensional array. Select the statement that gives the number of columns in the third row. int[][] counts = { { 0, 0, 1 }, { 0, 1, 1, 2 }, { 0, 0, 1, 4, 5 }, { 0, 2 } }; a) int cols = counts[2].size(); b) int cols = counts.length[2]; c) int cols = counts.length; d) int cols = counts[2].length;

d) int cols = counts[2].length;

41) Which of the following lists the correct order of items in an instance method declaration? a) the return type, the name of the method, and a list of the parameters (if any) b) modifiers, the type of the instance variable, and the name of the instance variable c) the type of the instance variable, modifiers, and a list of the parameters (if any) d) modifiers, a return type, a method name, and a list of the parameters (if any)

d) modifiers, a return type, a method name, and a list of the parameters (if any)

36) You should declare all instance variables as ___. a) protected b) class c) public d) private

d) private

78) Insert the missing code in the following code fragment. This fragment is intended to implement a method to get the value stored in an instance variable. public class Vehicle { private String model; private double rpm; . . . _______ { return model; } } a) public void setModel(String modelName) b) public void getModel() c) public String getModel() d) public String setModel(String modelName)

d) public String setModel(String modelName)

81) Insert the missing code in the following code fragment. This fragment is intended to implement a method to set the value stored in an instance variable. public class Employee { private String empID; private boolean hourly; . . . _______ { hourly = isHourly; } } a) public void setHourly(String isHourly) b) public void getHourly() c) public boolean getHourly() d) public boolean setHourly(boolean isHourly)

d) public boolean setHourly(boolean isHourly)

How do you compute the length of the string str? a) length(str) b) length.str c) str.length d) str.length()

d) str.length()

How do you extract the first 5 characters from the string str? a) substring(str, 5) b) substring.str(0, 5) c) str.substring(5) d) str.substring(0, 5)

d) str.substring(0, 5)

91) The this reference refers to ____. a) the explicit parameter b) the method that is running c) the constructor of the object d) the implicit parameter

d) the implicit parameter

Method overloading distinguishes between methods based on

data types of arguments and number of arguments

When there are no remaining references to an object, the object will be _____.

destroyed

The first last in the object life cycle is _____.

destruction

Variable Scope

dictates what portions of the code can "see" or use a variable, typically derived from where the variable was first created. (See Global v. Local)

Operation are things an object

does

given the following declaration: String s = "Arizona_State_University"; Evaluate the expression: s.charAt(3) a. 'Ar' b. 'Ari' c. 'r' d. 'i' e. 'z' f. none of these

e

what is the output of the following java program? Class Driver { public static void main(String[] args) { int a = bar(3); int b = foo(a); System.out.print(b); } static int foo(int a) { System.out.print(a); a = bar (a+2); return a; } static int bar(int a) { System.out.print(a); return a+5; } } a. 3801015 b. 49914 c. 381515 d. 291414 e. 381015 f. 279914 g. none of these

e

Expressions

expressions always evaluate to a value. pieces of code that produce a value. Computers take in expressions and convert them into data the computer can understand,(binary). Allows humans to use less binary and understand code easier. The literal expression 123 is evaluated by Python to the int value one-hundred and twenty-three, which is represented in Binary by the computer as 1110112. An expression is instructions for the computer to follow. The result is a value.

which of the following would be the best data type for a variable to store a phone number? a. int b. float c. double d. boolean e. char f. String g. none of these

f

given the following declaration: String s = "Apples and bananas are yummy."; evaluate the expression: s.substring(1,4) a. "A" b. "Ap" c. "p" d. "pp" e. "Apple" f. "Apple and" g. none of these

g

given the following declaration: int[] values = {2, 6, 0, 1, 4, 3, 5}; evaluate the expression: values[values[1]]; a. -1 b. 0 c. 1 d. 2 e. 3 f. 4 g. 5 h. 6 i. 7 j. none of these

g

Methods used to access an object's private data are called _____.

getters and setters

what is the output for this java program? Class Driver { public static void main(String[] args) { int a = 1; int b = 0; for (int c = 1; c < 6; c++) { b = 1 while (b < c) { a = a + 1; b = b + a; } System.out.print(a); } System.out.print(b); } } a. 1234566 b. 234567 c. 012344 d. 12344 e. 12345 f. 001234 g. 012345 h. none of these

h

Programs that are not running are usually stored

in secondary storage

Which of the following step comes earliest in the object life cycle?

initialization

The purpose of a constructor is to _____.

initialize an object to a valid state

The purpose of a constructor is to __________

initialize an object to a valid state

The first step in the object life cycle is _____.

instantiation

In java, when you divide an int by another int, the result will be an

int

Which of the following would declare and instantiate an array of 5 integers in Java?

int[] my_array = new int[5];

Which of the following correctly demonstrates the use of an array initializer list? Choose all that apply.

int[] my_array = {1, 2, 3, 4, 5};

Which of the following would declare an array of integers in Java?

int[] my_array;

Programmers have embraced Java over its closest rival, C++, mainly because

it is easier to use

Iteration(loops)

iteration (or loops) allow us to execute a set of instructions more than once.

Java arrays have an attribute named _____ that can be accessed to get the size (number of elements) of the array.

length

The idea of looping through an array (element-by-element) looking for some element that meets our criteria, is called a(n) _____.

linear search

logical operators

logical operators are used to combine conditional statements. & [and] || [or] ! [not]

In java, functions are called

methods

Which keyword is used in Java to create an object?

new

which of the following would instantiate an array of 5 integers in Java? Choose all that apply.

new int[5]

Which of the following are characteristics of the Java programming language?

none of these

Decisions

one fo three programming structures,(including iteration and sequence). It is important to understand that decision structures are still just instructions, and they can be used anywhere any other instruction can be used (e.g. inside of functions, before and after other decisions, and even in the body of another decision).

Which statement starts the declaration of a class in Java?

public class Classname

A reference type variable stores a _____________

reference

All non-primitive types in Java are __________

reference types

Which method can be used to remove an element from an ArrayList?

remove

Java uses the keyword ________ to return a value from a method

return

Which methods can be used to replace an element in an ArrayList?

set

In Java class members are declared with the keyword

static

decimal system

system of numbers based on 10. basically each numeric position increases by a power of 10.

output

the result of something, like a function or equation. use a print string and input function to get an outlet on the console

In Java what keyword does an object use to refer to itself?

this

are Strings immutable in java? a. true b. false

true, once their declared you can't modify their contents

To declare or instantiate a specialized ArrayList, we must include a(n) _____ in the declaration or instantiation.

type parameter

Which of the following is not a kind of loop in Java? Choose all that apply.

until

The language developed by Sun Microsystems that became the Java programming language was originally designed to be simple, secure, and

usable for many different processor types

When doing an object oriented analysis of a problem, we call the ways that various kinds of users will use the system _____.

use cases

The two kinds of types in the Java programming language are ___________.

value and reference types

All of the Java primitive types are _______.

value types

An object's attributes are implemented as _____.

varaibles

An object's attributes are implemented as

variables

When developing a program, the programmer adds the discount amount to the total instead of subtracting it. What type of an error is this?

A logic error

What is the difference between an editor and a compiler?

An editor allows program files to be written and stored; a compiler converts program files into an executable program

An integrated development environment (IDE) bundles tools for programming into a unified application. What kinds of tools are usually included?

An editor and a compiler

What is a logic error?

An error that occurs when a program is running because, for example, the wrong operator was used

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

Answer: b) The value of sum is 66

14) What is the output of the code snippet given below? int i; int j = 0; for (i = 0; i < 5; i++) { if (i % 2 == 0) { i = i + 2; j++; } else { i++; j = j + 2; } j++; } System.out.println("i=" + i + ", j=" + j); a) i=7, j =7 b) i =7, j =6 c) i =6, j =7 d) i =5, j =5

Answer: d) i =5, j =5

Which pointers about backing up your Java projects are important? I. Check your backups once in a while II. Rely on the Java programming language's built-in back up system III. Back up often

I, III

Consider the following statements regarding computers: I. Computers can execute a large number of instructions in a fraction of a second. II. Computer application areas mainly target the research and scientific communities. III. The physical components of a computer constitute its hardware. IV. Unlike humans, a computer never gets bored or exhausted when performing repetitive tasks. Which are true?

I, III, IV

Which one of the following typically provides data persistence without electricity? I. The CPU's memory II. The hard disk III. Secondary storage

II, III

A Java class with the name Printer has to be saved using the source file name:

Printer.java

Who or what is responsible for inspecting and testing the program to guard against logic errors?

Programmer

Which Java statement does not contain an error?

System.out.println();

Which Java statement prints a blank line?

System.out.println();

How do programmers find exceptions and run-time errors?

Testing by running the program with a variety of input values

In order for Java to achieve protability

compiled Java programs contain instructions for a virtual machine

When a program begins to run

it is moved to the CPU's memory

The Central Processing Unit is primarily responsible for

performing program control and data processing

Writing a computer game in Java that has graphics, motion, and sound effects

requires a team of skilled programmers

The source code for a Java program is stored in a file

that ends with a.java suffix

Computer programming is

the act of designing and implementing a computer program

Which of the following statements is true with respect to the main method in Java?

Every Java application must have a main method

Which statements are true? I. In Java, a file can contain at most one public class II. The word public denotes that the class is usable by the "public" III. Every Java program must have a main method

I, II, III

Which of these are true about writing algorithms in pseudo code? I. The exact wording is not important II. The sequence of steps should be ambiguous III. The algoirthm should terminate

I, III

What is one of the benefits of using a high-level programming language like Java?

Problems solved in a high-level language are independent of the underlying computer

Which one of the following methodologies is a sequence of steps formulated in English for solving a program?

Pseudocode

Which of the following refers to a collection of programs that a computer executes?

Software

Every statement in Java must be terminated with

the semi-colon character ;

Which of the following are reasons why Java is good, but not perfect, for beginners? I. A certain amount of technical machinery is necessary to write basic, simple programs II. There are many extensions III. There are a large number of extensive libraries in Java

I, II, III

Which of the following statements regarding backup strategies for Java files are true? I. You should back up your projects often to prevent loss of valuable work II. You should check your backups only in case of loss of data III. You should pay attention to the backup direction

I, III

What kind of error is created by the following code snippet? System.out.print("The sum of 8 and 12 is "); System.out.println(8*12);

Logic error: the program does not produce the desired result

The Java statement public static void main(String[] args) declares a

method

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

Answer: c) 11 times

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

Answer: c) 13

16) How many times does the following loop run? int i = 0; int j = 1; do { System.out.println("" + i + ";" + j); i++; if (i % 2 == 0) { j--; } } while (j >= 1); a) 0 times b) 1 times c) 2 times d) 4 times

Answer: c) 2 times

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

Answer: c) 9 times

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

Answer: c) I only

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

Answer: c) Run-time error

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

Answer: c) do

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)

Answer: d) 0 2 4 6 8 10 12 14 .... (infinite loop)

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

Answer: d) 10

13) How many times does the following code snippet display "Loop Execution"? for (int i = 0; i < 10; i++); { System.out.println("Loop Execution"); } a) Ten times. b) The code snippet does not run because of a compile error. c) Infinite loop. d) Only one time.

Answer: d) Only one time.

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

Answer: d) j=14

The ENIAC computer burned out transistors at the rate of

none -- it used vacuum tubes

The technical term for the values that a method needs in order to carry out its task is an argument. When there is more than one argument needed by a method, they are

separated by commas

Characters that are grouped together between double quotes (quotation marks) in Java are called

strings

Sometimes errors throw the compiler off track because

the compiler does not give up when it find the first error

During program development, errors are

unavoidable

In order to run Java programs on a computer, the computer needs to have software called a(n)

virtual machine

In Java, if you want the compiler to interpret what you type as program instructions, you must

write correct Java statements separated by the semicolon

Because Java was designed for the Internet, which two of its attributes make it suitable for beginning programmers?

Safety and portability

What kind of error is created by the following code snippet? System.outt.println("Hello");

Syntax error: the program will not compile

Which one of the following errors represents a part of a program that is incorrect according to the rules of the programming language?

Syntax errors

Which one of the following code snippets compiles without errors and displays the output "Hello Good Day!" on the screen?

System.out.print("Hello "); System.out.println("Good Day!");

What is the output from this code snippet? System.out.print("The sum is "); System.out.println("7 + 3");

The sum is 7 + 3

An example of an output device that interfaces between computers and humans is

a speaker

A sequence of steps that is unambiguous, executable, and terminating is called

an algorithm

Some run-time errors are so severe that they generate

an exception

Small applications written in the Java programming language that can be located on the Internet are called

applets

In order to translate a Java program to a class file, the computer needs to have software called a(n)

compiler

Which one of the following translates high-level descriptions into machine code?

compiler

A Java "class" file

contains instructions to the Java virtual machine

The first step in describing an algorithm in pseudo code is

determine the inputs and outputs

Computers are machines that

execute programs

Computer scientists have devised something that allows programmers to describe tasks in words that are closer to the syntax of the problems being solved. This is called (an)

high-level programming language

In Java, the statement System.out.print("hello");;;

is a legal statement

Consider the following statements about folders and your integrated development environment (IDE): I. Hierarchical folders help to organize a project II. Folders are a way to visualize the layout of a file system III. Folders make it impossible to lose or accidentally delete a file

I, II

Which option correctly completes this statement? Programs can repeat simple instructions very quickly to I. help human users to perceive images and sound II. remove the drudgery of repeating instructions by hand III. overcome the need for connectivity to the computer network

I, II

If you misspell a word in your Java program it may be true that I. the program will not compile II. the program may compile, but not run III. the program may compile and run but still have a logic error

I, II, III

What statements about the integrated development environment (IDE) are true? I. You may run Java class files even after exiting the IDE II. The IDE can invoke a virtual machine, which is required to run a Java program III. Translating Java source code into class files is enough to then actually run the program

I, II, III

Consider the following statements about computer programs: I. Computer programs can be written by someone who has a basic knowledge of operating a computer. II. Computer programs can complete complex tasks quickly. III. Large and complex computer programs are generally written by a group of programmers. IV. Computer programs are composed of extremely primitive operations. Which are true?

II, III, IV

The Java programming language is itself relatively simple, but also contains a vast set of

library packages


Related study sets

Business Organizations and Corporate Law

View Set

Consumer Behavior Chapter 1 MKT 4305

View Set

10年文法不白學49-was和were的疑問句

View Set

Algebra Statistics - Representing Data

View Set

Chapter 22 - Reproductive System

View Set

Chapter 6 compound and complex sentences

View Set