computer science final

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

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? if else if else All of the above items

else

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++; } } 1 2 0 3

1

What is the output of the following code snippet? int[] values = { 1, 2, 3, 4}; values[2] = 24; values[values[0]] = 86; for (int i = 0; i < values.length; i++) { System.out.print (values[i] + " "); } 86 2 24 4 86 24 3 4 1 86 24 4 1 2 24 86

1 86 24 4

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); 2 3 4 5

2

What is the output of the statements below? int a = 10; int b = 20; int count = 0; if (a > 5) { count ++; if (b > 5) { count ++; } } if (a > 10) { count ++; if (b > 10) { count ++; } } System.out.print (count); 1 2 3 4

2

What is the output of the following code snippet? int x = 25; if (x < 100) { x = x + 5; } if (x < 500) { x = x - 2; } if (x > 10) { x++; } else { x--; } System.out.println(x); 27 28 29 30

29

What is the output of the statements below? int a = 10; int b = 20; int count = 0; if (a > 5) { count ++; } if (b > 5) { count ++; } if (a > 10) { count ++; } if (b > 10) { count ++; } System.out.print (count); 1 2 3 4

3

What will be printed by the statements below? int[] values = { 10, 24, 3, 64}; int position = 0; for (int i = 1; i < values.length; i++) { if (values[i] > values[position]) { position = i; } } System.out.print (position); 0 1 2 3

3

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); } 12 45 78 There is no output due to compilation errors.

45

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? 5 6 7 9

6

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? 5 7 9 10

9

The method findLargest should return the largest of any number of double values. For example, the call findLargest (3.4, -2.6, 2.9) should return 3.4 and the call findLargest (9.2, 17.6, 3.4, -2.6, 2.9) should return 17.6. Partial code to do this is given below: double findLargest (double... values) { double max = values[0]; // New code goes here return max; } What code will complete this method? A. for (double val: values) { if (val > max) { max = val; } } B. for (int i = 1; i < values.length; i++) { if (i > max) { max = i; } } C. for (double val:values) { if (values[val] > max) { max = values[val]; } } D. for (int i = 1; i < values.length; i++) { if (values[i] > max) { max = i; } }

A. for (double val: values) { if (val > max) { max = val; } }

Which of the following performs the same way as the switch statement below? switch (value) { case 1: System.out.print ("Small"); break; case 10: System.out.print ("Large"); break; default: System.out.print ("Other"); break; } A. if (value == 1) System.out.print("Small"); else if (value == 10) System.out.print("Large"); else System.out.print("Other"); B. if (value == 1) System.out.print("Small"); if (value == 10) System.out.print("Large"); else System.out.print("Other"); C. if (value >= 10) System.out.print("Large"); else System.out.print("Other"); D. if (value >= 10) System.out.print("Large"); else System.out.print("Other");

A. if (value == 1) System.out.print("Small"); else if (value == 10) System.out.print("Large"); else System.out.print("Other");

Which code snippet calculates the sum of all the even elements in an array values? A. int sum = 0; for (int i = 0; i < values.length; i++) { if ((values[i] % 2) == 0) { sum += values[i]; } } B. int sum = 0; for (int i = 0; i < values.length; i++) { if ((values[i] % 2) == 0) { sum++; } } C. int sum = 0; for (int i = 0; i < values.length; i++) { if ((values[i] / 2) == 0) { sum += values[i]; } } D. int sum = 0; for (int i = 0; i < values.length; i++) { if ((values[i] / 2) == 0) { sum++; } }

A. int sum = 0; for (int i = 0; i < values.length; i++) { if ((values[i] % 2) == 0) { sum += values[i]; } }

Consider the following code snippet. What is the output? int score = 68; if (score < 50) { System.out.print("You need to practice!"); } if (score < 100) { System.out.print("Almost respectable!"); } if (score < 150) { System.out.print("You hit triple digits!"); } if (score < 250) { System.out.print("Impressive!"); } You need to practice! Almost respectable!You hit triple digits! You hit triple digits!Impressive! Almost respectable!You hit triple digits!Impressive!

Almost respectable!You hit triple digits!Impressive!

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> names2 = 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? names contains "Janet", "Jerry", "John" in this order. names contains "John", "Jerry", "Janet" in this order. reverse method has a bound error. Compilation error due to the return statement in reverse method.

Compilation error due to the return statement in reverse method.

Which statement(s) about the size of a Java array, array list, and string are true? I. The syntax for determining the size of an array, an array list, and a string in Java is consistent among the three. II. The string uses s.size(), while the array list uses a.length(). III. The array uses a.length, which is not a method call. I only II only III only II, III only

III only

Which statement correctly describes the enhanced for loop? In the enhanced for loop, the element variable is assigned. In the basic for loop, the index variable is assigned. In the enhanced for loop, the index variable is assigned. In the basic for loop, the element variable is assigned. The enhanced for loop has a very specific purpose: modifying the contents of the array. The enhanced for loop has a very specific purpose: initializing the contents of the array.

In the enhanced for loop, the element variable is assigned. In the basic for loop, the index variable is assigned.

What is the output of the following code snippet? double income = 45000; double cutoff = 55000; double minIncome = 30000; if (minIncome > income) { System.out.println("Minimum income requirement is not met."); } if (cutoff < income) { System.out.println("Maximum income limit is exceeded."); } else { System.out.println("Income requirement is met."); } Minimum income requirement is not met. Maximum income limit is exceeded. Income requirement is met. There is no output.

Income requirement is met.

Which of the following statements is true about the "nested if" structure? It cannot have any else branches at all. It allows multiple else branches in a single if statement. It allows one if statement within another if statement. It does not allow more than one if statement nested within another if statement.

It allows one if statement within another if statement.

Consider using a deck of cards as a way to visualize a shuffle algorithm. When two cards shuffle their position, what has to happen to the size of the array holding them? It increases by one. It decreases by one. It stays the same. It increases by two.

It stays the same.

Assume the method doSomething has been defined as follows: public static void doSomething (int[] values, int p1, int p2) { int temp = values[p1]; values[p1] = values[p2]; values[p2] = values[p1]; } What does the method do? It copies the integer array values to a new array. It swaps the integer values stored in positions p1 and p2 of the array values. It moves each element of the array values to a higher index position. It overwrites the value stored in the integer array values at position p1 with the value stored in position p2.

It swaps the integer values stored in positions p1 and p2 of the array values.

When the order of the elements is unimportant, what is the most efficient way to remove an element from an array? Delete the element and move each element after that one to a lower index. Replace the element to be deleted with the last element in the array. Replace the element to be deleted with the first element in the array. Replace the element with the next element.

Replace the element to be deleted with the last element in the array.

The code snippet below is intended to perform a linear search on the array values to find the location of the value 42. What is the error in the code snippet? int searchedValue = 42; int pos = 0; boolean found = true; while (pos < values.length && !found) { if (values[pos] == searchedValue) { found = true; } else { pos++; } } The boolean variable found should be initialized to false. The condition in the while loop should be (pos <= values.length && !found). The variable pos should be initialized to 1. The condition in the if statement should be (values[pos] <= searchedValue).

The boolean variable found should be initialized to false.

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? The code snippet finds the highest value out of the two array lists. The code snippet finds the lowest value out of the two array lists. The code snippet compares the values of two array lists and stores the count of total matches found. The code snippet adds the values of the two array lists.

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

Which statement about an if statement is true? The condition in an if statement using relational operators will evaluate to a Boolean result The condition in an if statement should make exact comparisons to floating-point numbers The condition in an if statement should always evaluate to true The condition in an if statement should never include integer variables

The condition in an if statement using relational operators will evaluate to a Boolean result

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"); The final size of names is 2; the final size of friends is 3. The final size of names is 3; the final size of friends is 2. The final size of names is 3; the final size of friends is 3. compilation error

The final size of names is 2; the final size of friends is 3.

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"); The final size of names is 2; the final size of friends is 3. The final size of names is 3; the final size of friends is 2. The final size of names is 3; the final size of friends is 3. compilation error

The final size of names is 3; the final size of friends is 3.

Which of the following operators is used to invert a conditional statement? ! != || ?

!

Assuming that a user enters 5 as the value for num, what is the output of the following code snippet? int num = 0; Scanner in = new Scanner(System.in); System.out.print("Enter a number: "); num = in.nextInt(); if (num < 50) { num = num + 5; } if (num < 10) { num = num - 2; } if (num > 5) { num++; } else { num--; } System.out.println(num); 0 9 5 11

11

What is the output of the following code snippet? final int COST = 583; int digit = COST % 10; if (digit != 500) { System.out.println("500"); } else { System.out.println("Not 500"); } A. There is no output due to compilation errors. B. 500 C. Not 500 D. 500 Not 500

Not 500

Assume the array of integers values has been created. Which condition must be used in the indicated area so the loop below will assign max the largest value in values? int max = values[0]; for (int current = 1; current < values.length; current++) { if (/* Put condition here */) max = values[current]; } current > max max > current max > values[current] values[current] > max

values[current] > max

What is the output of the following code snippet? int x = 50; if (x > 100) { x++; } else { x--; } System.out.println(x); 49 50 51 52

49

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

B. double discount = 0.10 * price; if (price <= 100) { discount = 0; }

Which of the following variables is used to store a condition that can be either true or false? Algebraic Logical Boolean Conditional

Boolean

Assuming that a user enters 64 as the 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"); } D C B A

D

What is the output of the following code snippet? int golfScore = 64; if (golfScore < 60) { System.out.println("Astounding!"); } if (golfScore >= 60 && golfScore < 70) { System.out.println("Professional!"); } if (golfScore >= 70 && golfScore < 80) { System.out.println("Pretty good!"); } if (golfScore >= 80 && golfScore < 90) { System.out.println("Not so hot!"); } if (golfScore >= 90) { System.out.println("Keep your day job!"); } Astounding! Professional! Pretty good! Keep your day job!

Professional!

What is the output of the following code snippet? double salary = 55000; double cutOff = 65000; double minSalary = 40000; if (minSalary > salary) { System.out.println("Minimum salary requirement is not met."); } if (cutOff < salary) { System.out.println("Maximum salary limit is exceeded."); } else { System.out.println("Salary requirement is met."); } Minimum salary requirement is not met. Maximum salary limit is exceeded. Salary requirement is met. There is no output.

Salary requirement is met.

Which of the following conditions can be added to the code below so it will assign the larger value of two integer variables a and b to the integer variable maximum? if (/* put condition here */) { maximum = a; } else { maximum = b; } a == b b > a a > b a.compareTo (b) > 0

a > b

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

conditional tests

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); } his hiscycle cycle There is no output due to compilation errors.

his

Consider the following code snippet: int number = 0; Scanner in = new Scanner(System.in); System.out.print("Enter a number: "); number = in.nextInt(); if (number > 30) { . . . } else if (number > 20) { . . .. } else if (number > 10) { . . . } else { . . . } Assuming that the user input is 40, which block of statements is executed? if (number > 30) { . . .} else if (number > 20) { . . .} else if (number > 10) { . . .} else { . . .}

if (number > 30) { . . .}

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

public void func(int[][] arr)

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? size is : 1 size is : 2 size is : 4 size is : 0

size is : 4

The binary search is more efficient than the linear search, providing the size of the array is a power of two. the elements of the array are ordered. the elements of the array are unordered. the element being searched for is actually in the array.

the elements of the array are ordered.

Which of the following operators is NOT a relational operator? <= += != ==

+=

What is the valid range of index values for an array of size 10? 0 to 9 0 to 10 1 to 9 1 to 10

0 to 9

What is the output of the statements below? int a = 10; int b = 20; int count = 0; if (a > 10) { count ++; if (b > 5) { count ++; } if (a > 10) { count ++; } } if (b > 10) { count ++; } System.out.print (count); 1 2 3 4

1

Assume the method doSomething has been defined as follows: public static int [] doSomething (int[] values) { int [] result = new int[values.length - 1]; for (int i = 0; i < result.length; i++) { result[i] = values[i] + values[i + 1]; } return result; } What is printed by the statements below? int [] nums = {3, 18, 29, -2} ; System.out.print(Arrays.toString(doSomething(nums))); [4, 19, 30, -1] [21, 47, 27] [21, 50, 48] [3, 21, 47, 27]

[21, 47, 27]

What will be printed by the statements below? ArrayList<String> names = new ArrayList<String>(); names.add("Annie"); names.add("Bob"); names.add("Charles"); for (int i = 0; i < 3; i++) { String extra = names.get(i); names.add (extra); } System.out.print (names); [Annie, Bob, Charles, Annie, Bob, Charles] [Annie, Bob, Charles, Charles, Bob, Annie] [Annie, Annie, Bob, Bob, Charles, Charles] [Annie, Bob, Charles, Bob, Charles]

[Annie, Bob, Charles, Annie, Bob, Charles]

Consider the following code snippet: String[] data = { "abc", "def", "ghi", "jkl" }; Using Java 6 or later, which statement grows the data array to twice its size? data = Arrays.copyOf(data, 2 * data.length); data = Arrays.copyOf(data, 2); data = Arrays.copyOf(data, 2 * data); data = Arrays.copyOf(data, 2 * data.size());

data = Arrays.copyOf(data, 2 * data.length);

Java 7 introduced enhanced syntax for declaring array lists, which is termed angle brackets. method lists. diamond syntax. symmetric slants.

diamond syntax.

If currLength is an integer variable that gives the number of elements currently in the array myArray, which code snippet prints out the elements in the partially filled array of integers? A. for (int i = 0; i < myArray.length(); i++) { System.out.print(myArray[i]); } B. for (int i = 0; i < myArray.currLength(); i++) { System.out.print(myArray[i]); } C. for (int i = 0; i < currLength; i++) { System.out.print(myArray[i]); } D. for (int i = 0; i < myArray.length(); i++) { System.out.print(myArray[currLength]); }

for (int i = 0; i < currLength; i++) { System.out.print(myArray[i]); }

Which of the following options checks that character ch is neither a letter nor a white space? if (!Character.isLetter(ch) || !Character.isWhiteSpace(ch)) if (!(Character.isLetter(ch) || Character.isWhiteSpace(ch))) if (!(Character.isLetter(ch) && Character.isWhiteSpace(ch))) if (!Character.isLetter(ch) || Character.isWhiteSpace(ch))

if (!(Character.isLetter(ch) || Character.isWhiteSpace(ch)))

What is the conditional required to check whether the length of a string s1 is odd? if ((s1.length() % 2) == 0) if ((s1.length() % 2) != 0) if ((s1.length() / 2)) if ((s1.length() * 2))

if ((s1.length() % 2) != 0)

Which of the following conditions will correctly check if the String variable early comes before "middle" alphabetically? if (greeting <= "middle") if (greeting.compareTo("middle") > 0) if (greeting.compareTo("middle") == 0) if (early.compareTo("middle") < 0)

if (early.compareTo("middle") < 0)

Which of the following statements can be used to validate that the user input for the floor variable is between 0 and 20 inclusive? if (floor >= 0 && floor <= 20) if (floor >= 0 || floor <= 20) if (floor <= 0 && floor >= 20) if (floor <= 0 || floor >= 20)

if (floor >= 0 && floor <= 20)

Consider the following code snippet: boolean married = true; boolean engaged = false; Which of the following if statements includes a condition that evaluates to true? if (married == "true") { . . . } if (married == true) { . . . } if (engaged == "false") { . . . } if (married == engaged) { . . . }

if (married == true) { . . . }

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? public void passAList(ArrayList<Integer> myList(10), int[] intArr) public void passAList(ArrayList<Integer> myList, int[20] intArr) public void passAList(ArrayList<Integer> myList, int[] intArr) public void passAList(ArryaList<Integer> myList, int intArr)

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

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

the value in the first row and the third column


संबंधित स्टडी सेट्स

SDSU FA 17 - Homework_CH_02 - B A 370

View Set

Drugs, Discovery and Development Final

View Set

US - Hechos importantes de España

View Set