unit 5 & 6 mcq ap csa

Ace your homework & exams now with Quizwiz!

Consider the following incomplete method. Method findNext is intended to return the index of the first occurrence of the value val beyond the position start in array arr. // 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)

(pos < arr.length) && (arr[pos] != val)

The following incomplete method is intended to return the largest integer in the array numbers. // precondition: numbers.length > 0 public static int findMax(int[]numbers) { int posOfMax = O; for (int index = 1; index < numbers.length; index++) { if ( /*condition*/ ) { /* statement */ } } return numbers[posOfMax]; } Which of the following can be used to replace /* condition */ and /* statement */ so that findMax will work as intended? A /* condition */ /* statement */ numbers[index] > numbers[posOfMax] posOfMax = numbers[index]; B /* condition */ /* statement */ numbers[index] > numbers[posOfMax] posOfMax = index; C /* condition */ /* statement */ numbers[index] > posOfMax posOfMax = numbers[index]; D /* condition */ /* statement */ numbers[index] < posOfMax posOfMax = numbers[index]; E /* condition */ /* statement */ numbers[index] < numbers[posOfMax] posOfMax = index;

/* condition */ /* statement */ numbers[index] > numbers[posOfMax] posOfMax = index;

Consider the following method. Method allEven is intended to return true if all elements in array arr are even numbers; otherwise, it should return false. public boolean allEven(int[] arr) { boolean isEven = /* expression */ ; for (int k = 0; k < arr.length; k++) { /* loop body */ } return isEven; } Which of the following replacements for /* expression */ and /* loop body */ should be used so that method allEven will work as intended? A /* expression *//* loop body */ false ----- if ((arr[k] % 2) == 0) isEven = true; B /* expression *//* loop body */ false----- if ((arr[k] % 2) != 0) isEven = false; else isEven = true; C /* expression *//* loop body */ true----- if ((arr[k] % 2) != 0) isEven = false; D /* expression *//* loop body */ true----- if ((arr[k] % 2) != 0) isEven = false; else isEven = true; E /* expression *//* loop body */ true----- if ((arr[k] % 2) == 0) isEven = false; else isEven = true;

/* expression *//* loop body */ true----- if ((arr[k] % 2) != 0) isEven = false;

Consider the following two methods that appear within a single class. public void changeIt(int[] list, int num) { list = new int[5]; num = 0; for (int x = 0; x < list.length; x++) list[x] = 0; } public void start() { int[] nums = {1, 2, 3, 4, 5}; int value = 6; changeIt(nums, value); for (int k = 0; k < nums.length; k++) System.out.print(nums[k] + " "); System.out.print(value); } What is printed as a result of the call start()? A 0 0 0 0 0 0 B 0 0 0 0 0 6 C 1 2 3 4 5 6 D 1 2 3 4 5 0 E changeIt will throw an exception.

1 2 3 4 5 6

The question refer to the following declarations. public class Point { private double myX; private double myyY; // postcondition: this Point has coordinates (0,0) public Point () { /* implementation not shown */ } // postcondition: this Point has coordinates (x,y) public Point(double x, double y) { /* implementation not shown */ } // other methods not shown } public class Circle { private Point myCenter; private double myRadius; // postcondition: this Circle has center at (0, 0) and radius 0.0 public Circle() { /* implementation not shown */ } // postcondition: this Circle has the given center and radius public Circle(Point center, double radius) { /* implementation not shown */ } // other methods not shown } In a client program which of the following correctly declares and initializes Circle circ with center at (29.5, 33.0) and radius 10.0 ? A Circle circ = new Circle(29.5, 33.0, 10.0); B Circle circ = new Circle((29.5, 33.0), 10.0); C Circle circ = new Circle(new Point (29.5, 33.0), 10.0); D Circle circ = new Circle(); circ.myCenter = new Point(29.5, 33.0); circ.myRadius = 10.0; E Circle circ = new Circle(); circ.myCenter = new Point(); circ.myCenter.myX = 29.5; circ.myCenter.myY = 33.0; cire.myRadius = 10.0;

Circle circ = new Circle(new Point (29.5, 33.0), 10.0);

Consider the following instance variables and method that appear in a class representing student information. private int assignmentsCompleted; private double testAverage; public boolean isPassing() { /* implementation not shown */ } A student can pass a programming course if at least one of the following conditions is met. The student has a test average that is greater than or equal to 90. The student has a test average that is greater than or equal to 75 and has at least 4 completed assignments. Consider the following proposed implementations of the isPassing method. I. if (testAverage >= 90) return true; if (testAverage >= 75 && assignmentsCompleted >= 4) return true; return false; II. boolean pass = false; if (testAverage >= 90) pass = true; if (testAverage >= 75 && assignmentsCompleted >= 4) pass = true; return pass; III. return (testAverage >= 90) || (testAverage >= 75 && assignmentsCompleted >= 4); Which of the implementations will correctly implement method isPassing? A I only B II only C I and III only D II and III only E I, II, and III

I, II, and III

Consider the following declaration of the class NumSequence, which has a constructor that is intended to initialize the instance variable seq to an ArrayList of numberOfValues random floating-point values in the range [0.0, 1.0). public class NumSequence { private ArrayList<Double> seq; // precondition: numberOfValues > 0 // postcondition: seq has been initialized to an ArrayList of // length numberOfValues; each element of seq // contains a random Double in the range [0.0, 1.0) public NumSequence(int numberOfValues) { /* missing code */ } } Which of the following code segments could be used to replace /* missing code */ so that the constructor will work as intended? I. ArrayList<Double> seq = new ArrayList<Double>(); for (int k = 0; k < numberOfValues; k++) seq.add(new Double(Math.random())); II. seq = new ArrayList<Double>(); for (int k = 0; k < numberOfValues; k++) seq.add(new Double(Math.random())); III. ArrayList<Double> temp = new ArrayList<Double>(); for (int k = 0; k < numberOfValues; k++) temp.add(new Double(Math.random())); seq = temp; A II only B III only C I and II D I and III E II and III

II and III

The following question refer to the following information. Consider the following data field and method. Method maxHelper is intended to return the largest value among the first numVals values in an array; however, maxHelper does not work as intended. private int[] nums; // precondition: 0 < numVals <= nums.length private int maxHelper(int numVals) { Line 1: int max = maxHelper(numVals - 1); Line 2: if (max > nums[numVals - 1]) return max; else return nums[numVals - 1]; } Which of the following corrects the method maxHelper so that it works as intended? A Insert the following statement before Line 1. if (numVals == 0) return numVals; B Insert the following statement before Line 1. if (numVals == 1 return nums[0]; C Insert the following statement between Line 1 and Line 2. if (numVals == 0) return numVals; D Insert the following statement between Line 1 and Line 2. if (numVals == 1) return nums[0]; E Insert the following statement between Line 1 and Line 2. if (numVals < 2) return numVals;

Insert the following statement before Line 1. if (numVals == 1 return nums[0];

Consider the following method. public static void sort(String[] arr) { for (int pass = arr.length - 1; pass >= 1; pass--) { String large = arr[0]; int index = 0; for (int k = 0; k <= pass; k++) { if ((arr[k].compareTo(large)) > 0) { large = arr[k]; index = k; } } arr[index] = arr[pass]; arr[pass] = large; } } Assume arr is the following array. "Ann""Mike""Walt""Lisa""Shari""Jose""Mary""Bill" What is the intermediate value of arr after two iterations of the outer for loop in the call sort(arr)? A "Ann""Mike""Walt""Lisa""Shari""Jose""Mary""Bill" B "Ann""Mike""Lisa""Shari""Jose""Mary""Bill""Walt" C "Ann""Bill""Jose""Lisa""Mary""Mike""Shari""Walt" D "Ann""Mike""Bill""Lisa""Mary""Jose""Shari""Walt" E "Walt""Shari""Ann""Lisa""Mike""Jose""Mary""Bill"

"Ann""Mike""Bill""Lisa""Mary""Jose""Shari""Walt"

Consider the following class declaration. public class IntCell { private int myStoredValue; // constructor not shown public int getValue() { return myStoredValue; } public String toString () { return "" + myStoredValue; } } Assume that the following declaration appears in a client class. IntCell m = new IntCell(); Which of these statements can be used in the client class? I. System.out.println(m.getValue()); II. System.out.println(m.myStoredValue); III. System.out.println(m); A I only B II only C III only D I and II E I and III

I and III

Consider the following instance variable and incomplete method. The method calcTotal is intended to return the sum of all values in vals. private int[] vals; public int calcTotal() { int total = 0; /* missing code */ return total; } Which of the code segments shown below can be used to replace /* missing code */ so that calcTotal will work as intended? I. for (int pos = 0; pos < vals.length; pos++) { total += vals[pos]; } II. for (int pos = vals.length; pos > 0; pos--) { total += vals[pos]; } III. int pos = 0; while (pos < vals.length) { total += vals[pos]; pos++; } A I only B II only C III only D I and III E II and III

I and III

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 */. for (int k = 0; k < data.length; k++){ result += k;} for (int d : data){ result += d;} 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? A I only B II only C III only D I and II E II and III

II and III

The following question refer to the following information. Consider the following data field and method. Method maxHelper is intended to return the largest value among the first numVals values in an array; however, maxHelper does not work as intended. private int[] nums; // precondition: 0 < numVals <= nums.length private int maxHelper(int numVals) { Line 1: int max = maxHelper(numVals - 1); Line 2: if (max > nums[numVals - 1]) return max; else return nums[numVals - 1]; } Which of the following best describes the conditions under which maxHelper does not work as intended? A When numVals is 1 B When numVals is even C When the elements of nums are in nonincreasing order D When the elements of nums are in nondecreasing order E Method maxHelper never works as intended.

Method maxHelper never works as intended.

Consider the following instance variable and incomplete method. The method is intended to return a string from the array words that would be last alphabetically. private String[] words; public String findLastWord() { /* missing implementation */ } Assume that words has been initialized with one or more strings containing only lowercase letters. Which of the following code segments can be used to replace /* missing implementation */ 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;

String maxWord = words[0]; for (int k = 1; k < words.length; k++) { if (words[k].compareTo(maxWord) > 0) { maxWord = words[k]; } } return maxWord;

Consider the following class declarations. public class Alpha { private int answer() { return 10; } } public class Beta { public double sample() { Alpha item = new Alpha(); double temp = item.answer(); return temp * 2.0; } } Which of the following best describes why an error occurs when the classes are compiled? A The class Alpha does not have a defined constructor. B The class Alpha must be declared as a subclass of Beta. C The class Beta must be declared as a subclass of Alpha. D The answer method cannot be accessed from a class other than Alpha. E The result of the method call item.answer() cannot be assigned to a variable of type double.

The answer method cannot be accessed from a class other than Alpha.

The question refer to the following data field and method. private int[] arr; // precondition: arr.length > 0public void mystery() { int sl = 0; int s2 = 0; for (int k = 0; k < arr.length; k++) { int num = arr[k]; if ((num > 0) && (num % 2 == 0)) sl += num; else if (num < 0) s2 += num; } System.out.println(s1); System.out.println(s2); } Which of the following best describes the value of s1 output by the method mystery ? A The sum of all positive values in arr B The sum of all positive even values in arr C The sum of all positive odd values in arr D The sum of all values greater than 2 in arr E The sum of all values less than 2 in arr

The sum of all positive even values in arr

Consider the following recursive method. /** Precondition: 0 <= numVals <= nums.length */ public static int mystery(int[] nums, int v, int numVals) { if (numVals == 0) { return 0; } else if (v == nums[numVals - 1]) { return 1 + mystery(nums, v, numVals - 1); } else { return mystery(nums, v, numVals - 1); } } Which of the following best describes the value returned by the call mystery(nums, v, nums.length) ? A The value 0 is returned. B The value 1 is returned. C The number of times that v occurs in nums is returned. D The number of times that numVals occurs in nums is returned. E Nothing is returned. A runtime error occurs because of infinite recursion.

The number of times that v occurs in nums is returned.

The question refer to the following data field and method. private int[] arr; // precondition: arr.length > 0public void mystery() { int sl = 0; int s2 = 0; for (int k = 0; k < arr.length; k++) { int num = arr[k]; if ((num > 0) && (num % 2 == 0)) sl += num; else if (num < 0) s2 += num; } System.out.println(s1); System.out.println(s2); } Which of the following best describes the value of s2 output by the method mystery ? A The sum of all positive values in arr B The sum of all positive even values in arr C The sum of all negative values in arr D The sum of all negative even values in arr E The sum of all negative odd values in arr

The sum of all negative values in arr

Consider the following class declarations. public class Shoe { private String shoeBrand; private String shoeModel; public Shoe(String brand, String model) { shoeBrand = brand; shoeModel = model; } // No other constructors } public class Boot extends Shoe { private double heelHeight; public Boot(String brand, String model, double height) { /* missing implementation */ } } Which of the following should be used to replace /* missing implementation */ so that all instance variables are initialized with parameters? A shoeBrand = brand; shoeModel = model; heelHeight = height; B super(); heelHeight = height; C super(brand, model); D heelHeight = height; super(brand, model); E super(brand, model); heelHeight = height;

super(brand, model); heelHeight = height;

Consider the following class definition. public class Box { private double weight; /** Postcondition: weight is initialized to w. */ public Box(double w) { /* implementation not shown */ } public double getWeight() { return weight; } public void addWeight(double aw) { /* missing statement */ } } The following code segment, which appears in a class other than Box, is intended to create a Box object b1 with a weight of 2.22.2units and then increase the weight of b1 by 1.51.5units. Box b1 = new Box(2.2); b1.addWeight(1.5); Which of the following statements could replace /* missing statement */ so that the code segment works as intended? A aw += weight; B aw += getWeight(); C weight += aw; D weight += getWeight(); E return weight + aw;

weight += aw;

Consider the following method. public static void methodX(int[] values) { for (int j = 0; j < values.length - 1; j++) { for (int k = j + 1; k < values.length; k++) { if (values[k] < values[j]) { values[j] = values[k]; } } } } The following code segment appears in another method of the same class. int[] numbers = {45, 1, 56, 10}; methodX(numbers); Which of the following represents the contents of the array numbers after the code segment is executed? A {1, 1, 10, 10} B {1, 10, 45, 56} C {45, 1, 56, 10} D {45, 45, 56, 45} E {56, 45, 10, 1}

{1, 1, 10, 10}

Consider the following code segment. int[] numbers = new int[5]; numbers[0] = 2; numbers[1] = numbers[0] + 1; numbers[numbers[0]] = numbers[1]; for (int x = 3; x < numbers.length; x++) { numbers[x] = numbers[x - 1] * 2; } Which of the following represents the contents of the array numbers after the code segment is executed? A {2, 3, 0, 0, 0} B {2, 3, 1, 2, 4} C {2, 3, 3, 6, 9} D {2, 3, 3, 6, 12} E {2, 4, 8, 16, 32}

{2, 3, 3, 6, 12}

Consider the following method. public static int mystery(int value) { int sum = 0; int[] arr = {1, 4, 2, 5, 10, 3, 6, 4}; for (int item : arr) { if (item > value) { sum += item; } } return sum; } What value is returned as a result of the call mystery(4) ? A 6 B 15 C 21 D 29 E 35

21

Consider the following method. public static int mystery(int[] arr) { int count = 0; int curr = arr[arr.length - 1]; for (int value : arr) { if (value > curr) { count = count + 1; } else { count = count - 1; } curr = value; } return count; } The following code segment appears in another method of the same class. int[] arr = {4, 14, 15, 3, 14, 18, 19}; System.out.println(mystery(arr)); What is printed as a result of executing the code segment? A -7 B -6 C 3 D 5 E 7

3

Consider the following method. // precondition: arr contains no duplicates; // the elements in arr are in sorted order; // 0 ≤ low ≤ arr.length; low - 1 ≤ high < arr.length public static int mystery(int[] arr, int low, int high, int num) { int mid = (low + high) / 2; if (low > high) { return low; } else if (arr[mid] < num) { return mystery(arr, mid + 1, high, num); } else if (arr[mid] > num) { return mystery(arr, low, mid - 1, num); } else // arr{mid] == num { return mid; } } How many calls to mystery (including the initial call) are made as a result of the call mystery(arr, 0, arr.length - 1, 14) if arr is the following array? A 1 B 2 C 4 D 7 E 8

4

Consider the following instance variables and incomplete method that are part of a class that represents an item. The variables years and months are used to represent the age of the item, and the value for months is always between 0 and 11, inclusive. Method updateAge is used to update these variables based on the parameter extraMonths that represents the number of months to be added to the age. private int years; private int months; // 0 <= months <= 11 // precondition: extraMonths >= 0 public void updateAge(int extraMonths) { /* body of updateAge */ } Which of the following code segments could be used to replace /* body of updateAge */ so that the method will work as intended? I. int yrs = extraMonths % 12; int mos = extraMonths / 12; years = years + yrs; months = months + mos; II. int totalMonths = years * 12 + months + extraMonths; years = totalMonths / 12; months = totalMonths % 12; III. int totalMonths = months + extraMonths; years = years + totalMonths / 12; months = totalMonths % 12; A I only B II only C III only D II and III only E I, II, and III

II and III only

Consider the static method selectSort shown below. Method selectSort is intended to sort an array into increasing order; however, it does not always work as intended. // precondition: numbers.length > 0 // postcondition: numbers is sorted in increasing order public static void selectSort(int[] numbers) { int temp; Line 1: for (int j = 0; j < numbers.length - 1; j++) { Line 2: int pos = 0; Line 3: for (int k = j + 1; k < numbers.length; k++) { Line 4: if (numbers[k] < numbers[pos]) { Line 5: pos = k; } } temp = numbers[j]; numbers[j] = numbers[pos]; numbers[pos] = temp; } } Which of the following changes should be made so that selectSort will work as intended? A Line 1 should be changed to for (int j = 0; j < numbers.length - 2; j++) B Line 2 should be changed to int pos = j; C Line 3 should be changed to for (int k = 0; k < numbers.length; k++) D Line 4 should be changed to if (numbers[k] > numbers[pos]) E Line 5 should be changed to k = pos;

Line 2 should be changed to int pos = j;

Consider the following class declaration. public class Sample { private int a; private double b; public Sample(int x, double y) { a = x; b = y; } // No other constructors } The following method appears in a class other than Sample. public static void test() { Sample object = new /* missing constructor call */ ; } Which of the following could be used to replace /* missing constructor call */ so that the method will compile without error? A Sample() B Sample(int x = 10, double y = 6.2) C Sample(int x, double y) D Sample(10, 6.2) E Sample(6.2, 10)

Sample(10, 6.2)

A BoundedintArray represents an indexed list of integers. In a BoundedIntArray the user can specify a size, in which case the indices range from 0 to size - 1. The user can also specify the lowest index, low, in which case the indices can range from low to low + size - 1. public class BoundedIntArray { private int[] myItems; // storage for the list private int myLowIndex; // lowest index public BoundedIntArray(int size) { myItems = new int[size]; myLowIndex = 0; } public BoundedIntArray(int size, int low) { myItems = new int[size]; myLowIndex = low; } // other methods not shown } Consider the following statements. BoundedIntArray arrl = new BoundedIntArray(100, 5); BoundedIntArray arr2 = new BoundedIntArray(100); Which of the following best describes arrl and arr2 after these statements? A arrl and arr2 both represent lists of integers indexed from 0 to 99. B arrl and arr2 both represent lists of integers indexed from 5 to 104. C arrl represents a list of integers indexed from 0 to 104, and arr2 represents a list of integers indexed from 0 to 99. D arrl represents a list of integers indexed from 5 to 99, and arr2 represents a list of integers indexed from 0 to 99. E arrl represents a list of integers indexed from 5 to 104, and arr2 represents a list of integers indexed from 0 to 99.

arrl represents a list of integers indexed from 5 to 104, and arr2 represents a list of integers indexed from 0 to 99.

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

int k = arr[0]; arr[0] = arr[5]; arr[5] = k;

The following incomplete method is intended to sort its array parameter arr in increasing order. // postcondition: arr is sorted in increasing order public static void sortArray(int[] arr) { int j, k; for (j = arr.length - 1; j > 0; j--) { int pos = j; for ( /* missing code */ ) { if (arr[k] > arr[pos]) { pos = k; } } swap(arr, j, pos); } } Assume that swap(arr, j, pos) exchanges the values of arr[j] and arr[pos]. Which of the following could be used to replace /* missing code */ so that executing the code segment sorts the values in array arr? A k = j - 1; k > 0; k-- B k = j - 1; k >= 0; k-- C k = 1; k < arr.length; k++ D k = 1; k > arr.length; k++ E k = 0; k <= arr.length; k++

k = j - 1; k >= 0; k--


Related study sets

Accounting Chapter 6 Practice Questions

View Set

Unit 1 (Primary and Secondary Markets Quiz)

View Set

chapter 48: Nursing care of the child with an alteration in metabolism/endocrine disorder:

View Set

MedSurg PrepU Ch 34 Assessment and Management of Patients with Inflammatory Rheumatic Disorders

View Set