Compsci Term 1 Mistakes

Ace your homework & exams now with Quizwiz!

int[] arr = {4, 3, 2, 1, 0}; int total = 0; for (int k = 0; k <= total; k++) { if (arr[k] % 2 == 0) { total += arr[k]; } else { total -= arr[k]; } } System.out.print(total); What, if anything, is printed as a result of executing the code segment?

1

public static void changeIt(int[] arr, int val, String word){ arr= new int[5]; val=0; word = word.substring(0,5); for(int k=0;k<arr.length;k++){ arr[k]=0;} } public static void start(){ int[] nums={1,2,3,4,5}; int value = 6; String name = "blackboard"; changeIt(nums, value, name); for(int k=0; k<nums.length;k++){ SOP(nums[k] + " "); SOP(value + " " ); SOP(name); } Evaluate

1 2 3 4 5 6 blackboard

ArrayList<Integer> myList = new ArrayList<Integer>(); for (int i = 0; i < 4; i++) { myList.add(i + 1); } for (int i = 0; i < 4; i++) { if (i % 2 == 0) System.out.print(myList.get(i) + " "); } What output is produced as a result of executing the code segment?

1 3

int a = 5; int b = 2; double c = 3.0; System.out.println(5 + a / b * c - 1); What is printed when the code segment is executed?

10.0

What is printed as a result of executing the following statement? 404 / 10 * 10 + 1

401

ArrayList<String> arrList = new ArrayList<String>(); arrList.add("A"); arrList.add("B"); arrList.add("C"); arrList.add("D"); for (int i = 0; i < arrList.size(); i++) { System.out.print(arrList.remove(i)); } What, if anything, is printed as a result of executing the code segment? A)AC B)BD C)ABC D)ABCD E)Nothing is printed.

A

Consider the following code segment. String one = "ABC123"; String two = "C"; String three = "3"; System.out.println(one.indexOf(two)); System.out.println(one.indexOf(three)); System.out.println(two.indexOf(one)); What is printed when the code segment is executed? A)2 5 -1 B)2 5 2 C)2 6 -1 D)3 6 -1 E)-1 -1 2

A

Consider the following code segment. System.out.print(*); // Line 1 System.out.print("*"); // Line 2 System.out.println(); // Line 3 System.out.println("*"); // Line 4 The code segment is intended to produce the following output, but may not work as intended. ** * Which line of code, if any, causes an error? A)Line 1 B)Line 2 C)Line 3 D)Line 4 E)The code segment works as intended.

A

Consider the following incomplete code segment, which is intended to print the sum of the digits in num. For example, when num is 12345, the code segment should print 15, which represents the sum 1 + 2 + 3 + 4 + 5. int num = 12345; int sum = 0; /* missing loop header */ { sum += num % 10; num /= 10; } System.out.println(sum); Which of the following should replace /* missing loop header */ so that the code segment will work as intended? A)while (num > 0) B)while (num >= 0) C)while (num > 1) D)while (num > 2) E)while (num > sum)

A

Consider the following incomplete method. // returns index of first occurrence of val in arr // after position start; // returns arr.length if val is not found public int findNext(int[] arr, int val, int start) { int pos = start + 1; while ( /* condition */ ) pos++; return pos; } For example, consider the following code segment. int[] arr = {11, 22, 100, 33, 100, 11, 44, 100}; System.out.println(findNext(arr, 100, 2)); The execution of the code segment should result in the value 4 being printed. Which of the following expressions could be used to replace /* condition */ so that findNext will work as intended? A) (pos < arr.length) && (arr[pos] != val) B) (arr[pos] != val) && (pos < arr.length) C) (pos < arr.length) || (arr[pos] != val) D) (arr[pos] == val) && (pos < arr.length) E) (pos < arr.length) || (arr[pos] == val)

A

In the code segment below, assume that the int array numArr has been properly declared and initialized. The code segment is intended to reverse the order of the elements in numArr. For example, if numArr initially contains {1, 3, 5, 7, 9}, it should contain {9, 7, 5, 3, 1} after the code segment executes. /* missing loop header */ { int temp = numArr[k]; numArr[k] = numArr[numArr.length - k - 1]; numArr[numArr.length - k - 1] = temp; } Which of the following can be used to replace /* missing loop header */ so that the code segment works as intended? A)for (int k = 0; k < numArr.length / 2; k++) B)for (int k = 0; k < numArr.length; k++) C)for (int k = 0; k < numArr.length / 2; k--) D)for (int k = numArr.length - 1; k >= 0; k--) E)for (int k = numArr.length - 1; k >= 0; k++)

A

int w = 1; int x = w / 2; double y = 3; int z = (int) (x + y); Which of the following best describes the results of compiling the code segment? A)The code segment compiles without error. B)The code segment does not compile, because the int variable x cannot be assigned the result of the operation w / 2. C)The code segment does not compile, because the integer value 3 cannot be assigned to the double variable y. D)The code segment does not compile, because the operands of the addition operator cannot be of different types int and double. E)The code segment does not compile because the result of the addition operation is of type double and cannot be cast to an int.

A

Code Segment I for (int i = 0; i < 10; i++) { System.out.print( "*" ); } Code Segment II for (int i = 1; i <= 10; i++) { System.out.print( "*" ); } Which of the following best explains how the difference in the two loop headers affects the output? A)The output of the code segments is the same because the loops in each code segment terminate when i is 10. B)The output of the code segments is the same because the loops in both code segments iterate 10 times. C)The output of the code segments is different because code segment I iterates from i = 0 to i = 9 and code segment II iterates from i = 1 to i = 10. D)The output of the code segments is different because code segment I iterates from i = 0 to i = 10 and code segment II iterates from i = 1 to i = 11. E)Neither code segment produces output because both loop conditions are initially false.

B

Consider the following class. The method getTotalSalaryAndBonus is intended to return an employee's total salary with the bonus added. The bonus should be doubled when the employee has 10 or more years of service. public class Employee{ private String name; private double salary; private int yearsOfService; public Employee(String n, double sal, int years) { name = n; salary = sal; yearsOfService = years; } public double getTotalSalaryAndBonus(double bonus) { /* missing code */ } } Which of the following could replace /* missing code */ so that method getTotalSalaryAndBonus will work as intended? A if (years >= 10) bonus *= 2; return salary + bonus; B if (yearsOfService >= 10) bonus *= 2; return salary + bonus; C return salary + bonus; D if (years >= 10) bonus *= 2; return sal + bonus; E if (yearsOfService >= 10) bonus *= 2; return sal + bonus;

B

Consider the following code segment, which is intended to display 6.0. double fact1 = 1 / 2; double fact2 = 3 * 4; double product = fact1 * fact2; System.out.println(product); Which of the following best describes the error, if any, in the code segment? A)There are no errors and the code works as intended. B)Either the numerator or the denominator of the fraction 1 / 2 should be cast as double. C)The expression fact1 * fact2 should be cast as double. D)The expressions 1 / 2 and 3 * 4 should both be cast as double. E) The variables fact1 and fact2 should both be declared as int.

B

Consider the following code segment. int x = 4; int y = 6; x -= y; y += x; Which of the following best describes the behavior of the code segment? A) The value of x and the value of y have not been changed. B) Both the value of x and the value of y have been decreased. C) Both the value of x and the value of y have been increased. D) The value of x has been decreased and the value of y has been increased. E) The value of x has been increased and the value of y has been decreased.

B

In the Toy class below, the raisePrice method is intended to increase the value of the instance variable price by the value of the parameter surcharge. The method does not work as intended. public class Toy { private String name; private double price; public Toy(String n, double p) { name = n; price = p; } public void raisePrice(double surcharge) // Line 12 { return price + surcharge; // Line 14 } Which of the following changes should be made so that the class definition compiles without error and the method raisePrice works as intended? A)Replace line 14 with surcharge += price;. B)Replace line 14 with price += surcharge;. C)Replace line 14 with return price += surcharge;. D)Replace line 12 with public raisePrice (double surcharge).

B

The Thing class below will contain a String attribute, a constructor, and the helper method, which will be kept internal to the class. Which of the following replacements for /* missing code */ is the most appropriate implementation of the class? A private String str; private Thing(String s){ } private void helper(){} B private String str; public Thing(String s){ } private void helper(){ } C private String str; public Thing(String s){} public void helper(){} D public String str; private Thing(String s){} public void helper(){} E public String str; public Thing(String s){} public void helper(){}

B

double num = 9 / 4; System.out.print(num); System.out.print(" "); System.out.print((int) num); What is printed as a result of executing the code segment? A)2 2 B)2.0 2 C)2.0 2.0 D)2.25 2 E)2.25 2.0

B

Consider the following code segment, which is intended to display 0.5. int num1 = 5; int num2 = 10; double ans = num1 / num2; System.out.print(ans); Which of the following best describes the error, if any, in the code segment? A)There is no error and the code works as intended. B) The code should have cast the expression num1 / num2 to double. C)The code should have cast either num1 or num2 in the expression num1 / num2 to double. D) The code should have declared ans as an int. E) The code should have initialized num1 to 5.0 and num2 to 10.0.

C

Consider the following descriptions of two methods, which appear in the same class. public void methodA(int arg) - Calls methodB with the value of arg * 10 public void methodB(int arg) - Displays the value of arg + 10 Consider the call methodA(4), which appears in a method in the same class. What, if anything, is printed as a result of the call methodA(4)? A)14 B)40 C)50 D)140 E)Nothing is printed.

C

String s1 = "ABCDEFGHI"; String s2 = s1.substring(6, 7); String s3 = new String("abcdefghi"); String s4 = s3.substring(4, 5); String s5 = s3.substring(2, 3); System.out.print(s2 + s4 + s5); What, if anything, is printed when the code segment is executed? A)Fdb B)FGdebc C)Gec D)GHefcd E)There is no output due to a compilation error.

C

The Car class will contain two string attributes for a car's make and model. The class will also contain a constructor. public class Car { /* missing code */ } Which of the following replacements for /* missing code */ is the most appropriate implementation of the class? A public String make; public String model; public Car(String myMake, String myModel) { /* implementation not shown */ } B public String make; public String model; private Car(String myMake, String myModel) { /* implementation not shown */ } C private String make; private String model; public Car(String myMake, String myModel) { /* implementation not shown */ } D public String make; private String model; private Car(String myMake, String myModel) ( /* implementation not shown */ } E private String make; private String model; private Car(String myMake, String myModel) { /* implementation not shown */ }

C

int a = 100; while (a > 1) { System.out.println("$"); a /= 2; } How many $'s are printed as a result of executing the code segment? A)0 B)5 C)6 D)7 E)50

C

public static void selectionSort(int[] elements) { for (int j = 0; j < elements.length - 1; j++) { int minIndex = j; for (int k = j + 1; k < elements.length; k++) { if (elements[k] < elements[minIndex]) { minIndex = k; } } if (j != minIndex) { int temp = elements[j]; elements[j] = elements[minIndex]; elements[minIndex] = temp; // line 19 } } } The following declaration and method call appear in a method in the same class as selectionSort. int[] arr = {30, 40, 10, 50, 20}; selectionSort(arr); How many times is the statement elements[minIndex] = temp; in line 19 of the method executed as a result of the call to selectionSort ? A)1 B)2 C)3 D)4 E)5

C

Assume that an array of integer values has been declared as follows and has been initialized. int[] arr = new int[10]; Which of the following code segments correctly interchanges the value of arr[0] and arr[5] ? A arr[0] = 5; arr[5] = 0; B arr[0] = arr[5]; arr[5] = arr[0]; C int k = arr[5]; arr[0] = arr[5]; arr[5] = k; D int k = arr[0]; arr[0] = arr[5]; arr[5] = k; E int k = arr[5]; arr[5] = arr[0]; arr[0] = arr[5];

D

Consider the following code segment. num += num; num *= num; Assume that num has been previously declared and initialized to contain an integer value. Which of the following best describes the behavior of the code segment? A)The value of num is two times its original value. B)The value of num is the square its original value. C)The value of num is two times the square of its original value. D)The value of num is the square of twice its original value. E)It cannot be determined without knowing the initial value of num.

D

Consider the following method, which is intended to return the number of strings of length greater than or equal to 3 in an array of String objects. public static int checkString(String[] arr) { int count = 0; for (int k = 0; k < arr.length; k++) { if (arr[k].length() >= 3) { count++; } } return count; } Which of the following code segments compile without error? I.checkString(new String[]); II.checkString(new String[0]); III.String[] str = {"cat", "dog"};checkString(str); A)II only B)III only C)I and III only D)II and III only E)I, II, and III

D

String str = "AP"; str += "CS " + 1 + 2; System.out.println(str); What is printed as a result of executing the code segment? A)CS AP12 B)AP CS3 C)CSAP 12 D)APCS 12 E)APCS 3

D

The code segment below is intended to randomly print one of the values 2, 4, 6, or 8 with equal probability. int val = /* missing code */ ; val *= 2; System.out.print(val); Which of the following can be used to replace /* missing code */ so that the code segment works as intended? A)Math.random() * 4 + 1 B)Math.random() * 8 C)(int) (Math.random() * 4) D)(int) (Math.random() * 4 + 1) E)(int) (Math.random() * 8 + 1)

D

for (int k = 0; k < 4; k++) { /* missing loop header */ { System.out.print(k); } System.out.println(); } The code segment is intended to produce the following output. 0 11 222 3333 Which of the following can be used to replace /* missing loop header */ so that the code segment will work as intended? A)for (int h = 0; h < k; h++) B)for (int h = 1; h < k + 1; h++) C)for (int h = 0; h < 3; h++) D)for (int h = k; h >= 0; h--) E)for (int h = k; h <= 0; h--)

D

int x; int y; int sum = x + y; double average = (double) (sum / 2); Which of the following best describes the error, if any, in the code segment? A) There is no error, and the code works as intended. B)In the expression (double) (sum / 2), the cast to double is applied too late, so the average will be less than the expected result for even values of sum. C)In the expression (double) (sum / 2), the cast to double is applied too late, so the average will be greater than the expected result for even values of sum. D)In the expression (double) (sum / 2), the cast to double is applied too late, so the average will be less than the expected result for odd values of sum. E)In the expression (double) (sum / 2), the cast to double is applied too late, so the average will be greater than the expected result for odd values

D

public static void insertionSort(int[] elements) { for (int j = 1; j < elements.length; j++) { int temp = elements[j]; int possibleIndex = j; while (possibleIndex > 0 && temp < elements[possibleIndex - 1]) { elements[possibleIndex] = elements[possibleIndex - 1]; possibleIndex--; // line 10 } elements[possibleIndex] = temp; } } The following declaration and method call appear in a method in the same class as insertionSort. int[] arr = {10, 8, 3, 4}; insertionSort(arr); How many times is the statement possibleIndex--; in line 10 of the method executed as a result of the call to insertionSort ? A) 0 B) 1 C) 4 D) 5 E) 6

D

String word = "September"; String str1 = word.substring(0, 3); String str2 = word.substring(word.length() - 3); System.out.println(str1 + str2); What is printed when the code segment is executed? A)epbe B)epber C)Sepbe D)Seper E)Sepber

E

The method is intended to return a string from the array words that would be last alphabetically. private String[] words; Which of the following code segments can be used so that findLastWord will work as intended? A int maxIndex = 0; for (int k = 0; k < words.length; k++) if (words[k].compareTo(maxIndex) > 0) maxIndex = k; return words[maxIndex]; B int maxIndex = 0; for (int k = 1; k <= words.length; k++) if (words[k].compareTo(words[maxIndex]) > 0) maxIndex = k; return words[maxIndex]; C int maxIndex = 0; for (int k = 1; k < words.length; k++) if (words[k].compareTo(words[maxIndex]) > 0) maxIndex = k; return maxIndex; D String maxWord = words[0]; for (int k = 1; k < words.length; k++) if (words[k].compareTo(maxWord) > 0) maxWord = k; return maxWord; E String maxWord = words[0]; for (int k = 1; k < words.length; k++) if (words[k].compareTo(maxWord)>0) maxWord= words[k]; return maxWord;

E

Which of the following code segments can be used to set the value of the string str to "Good morning, sunshine!" ? I.String str = "Good " + "morning," + " sunshine!"; II.String str = "Good"; str += " morning, sunshine!"; III.String str = " morning, "; str = "Good" + str + "sunshine!"; A)I only B)II only C)III only D)II and III only E)I, II, and III

E

for (int j = 0; j < 4; j++) { for (int k = 0; k < j; k++) { System.out.println("hello"); } } Which of the following best explains how changing the inner for loop header to for (int k = j; k < 4; k++) will affect the output of the code segment? A)The output of the code segment will be unchanged. B)The string "hello" will be printed three fewer times because the inner loop will iterate one fewer time for each iteration of the outer loop. C)The string "hello" will be printed four fewer times because the inner loop will iterate one fewer time for each iteration of the outer loop. D)The string "hello" will be printed three additional times because the inner loop will iterate one additional time for each iteration of the outer loop. E)The string "hello" will be printed four additional times because the inner loop will iterate one additional time for each iteration of the outer loop.

E

public class Element { public static int max_value = 0; private int value; public Element (int v) { value = v; if (value > max_value) { max_value = value; } } } The following code segment appears in a class other than Element. for (int i = 0; i < 5; i++) { int k = (int) (Math.random() * 10 + 1); if (k >= Element.max_value) { Element e = new Element(k); } } Which of the following best describes the behavior of the code segment? A)Exactly 5 Element objects are created. B)Exactly 10 Element objects are created. C)Between 0 and 5 Element objects are created, and Element.max_value is increased only for the first object created. D)Between 1 and 5 Element objects are created, and Element.max_value is increased for every object created. E)Between 1 and 5 Element objects are created, and Element.max_value is increased for at least one object created.

E

public class Beverage { private int numOunces; private static int numSold = 0; public Beverage(int numOz) { numOunces = numOz; } public static void sell(int n) { /* implementation not shown */ } } Which of the following best describes the sell method's level of access to the numOunces and numSold variables? A)Both numOunces and numSold can be accessed and updated. B)Both numOunces and numSold can be accessed, but only numOunces can be updated. C)Both numOunces and numSold can be accessed, but only numSold can be updated. D)numSold can be accessed but not updated; numOunces cannot be accessed or updated. E)numSold can be accessed and updated; numOunces cannot be accessed or updated.

E; Static methods can access and update static variables like numSold but not instance variables like numOunces.

Consider an integer array nums, which has been properly declared and initialized with one or more values. Which of the following code segments counts the number of negative values found in nums and stores the count in counter ? I. int counter = 0; int i = -1; while (i <= nums.length - 2) { i++; if (nums[i] < 0) { counter++; }} II. int counter = 0; for (int i = 1; i < nums.length; i++) { if (nums[i] < 0) { counter++; }} III. int counter = 0; for (int i : nums) { if (nums[i] < 0) { counter++; }}

I only

isSorted is intended to return true if an array of integers is sorted in nondecreasing order and to return false otherwise. I. for(int k=1; k< data.length; k++) if(data[k-1]>data[k]) return false; return true; II. for(int k =0; k<data.length;k++) if(data[k]>data[k+1] return false; return true; III. for(int k=0; k<data.length-1;k++) if(data[k]>data[k+1]) return false; else return true; return true;

I only

Consider the following incomplete method, which is intended to return the sum of all the elements in its array parameter. /** Precondition: data.length > 0 */ public static int total(int[] data) { int result = 0; /* missing code */ return result; } The following code segments have been proposed to replace /* missing code */. I.for (int k = 0; k < data.length; k++){ result += k;} II.for (int d : data){ result += d;} III.for (int k = 0; k < data.length; k++){ result += data[k];} Which of the replacements for /* missing code */ could be used so that total will work as intended?

II and III

Consider the following class definition. public class Something { private static int count = 0; public Something() { count += 5; } public static void increment() { count++; } } The following code segment appears in a method in a class other than Something. Something s = new Something(); Something.increment(); Which of the following best describes the behavior of the code segment?

The code segment creates a Something object s. The class Something's static variable count is initially 0, then increased by 5, then increased by 1.

public class Student { private String firstName; private String lastName; private int age; public Student(String firstName, String lastName, int age) { firstName = firstName; lastName = lastName; age = age; } public String toString() { return firstName + " " + lastName; } } The following code segment appears in a method in a class other than Student. It is intended to create a Student object and then to print the first name and last name associated with that object. Student s = new Student("Priya", "Banerjee", -1); System.out.println(s); Which of the following best explains why the code segment does not work as expected?

The code segment will compile, but the instance variables will not be initialized correctly because the variable names firstName, lastName, and age refer to the local variables inside the constructor.

Consider the method seqSearch, which implements a sequential search algorithm. public int seqSearch(int[] arr, int target) { for (int j = 0; j < arr.length; j++) { if (arr[j] == target) { return j; } } return -1; } Consider another method, seqSearch2, which modifies seqSearch to use an enhanced for loop. public int seqSearch2(int[] arr, int target) { for (int j : arr) { if (j == target) { return j; } } return -1; } Which of the following best describes the difference in the behavior of seqSearch2 relative to seqSearch as a result of the modification?

The modification in seqSearch2 will cause the value of target to be returned instead of the index of target in cases where target appears in arr.

int k = 35; while (k >= 0) { System.out.println("X"); k -= 5; } How many times will the string "X" be printed as a result of executing the code segment? A)1 B)7 C)8 D)35 E)More than 35 times, because the code segment will cause an infinite loop.

c


Related study sets

CMSC 202 - Final Exam Important Definitions and Concepts

View Set

Processes, Media, and Channels Quiz

View Set

Chapter 2 - Network Infrastructure and Documentation

View Set

Infant and Toddler Midterm Practice

View Set

"Final Exam", Professor Gawn Econ 101 Multiple Choice

View Set

Chapter 1.1 and 1.2 Religion Test

View Set