APCS EXAM

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Consider the following method. public void doSomething () { } Each of the following statements appears in a method in the same class as doSomething. Which of the following statements are valid uses of the method doSomething? I. doSomething (); II. String output = doSomething (); III. System.out.println (doSomething () ); (A) I only (B) II only (C) I and II only (D) I and III only (E) I, II, and III

I only

int shortest = /* missing value */; for (String word : wordArray) { if (word.length() < shortest) { shortest = word.length(); } } System.out.println(shortest); Which of the following should be used as the initial value assigned to shortest so that the code segment works as intended?

Integer.MAX_VALUE

Question 4 Consider the following methods, which appear in the same class. public void printSum (int x, double y) { } public void print Product (double x, int y) { System.out.println (x * y); } Consider the following code segment, which appears in a method in the same class as printSum and printProduct. System.out.println(x + y); int num1 = 5; double num2 = 10.0; print Sum (numi, num2); print Product (numi, num2); What, if anything, is printed as a result of executing the code segment?

Nothing is printed because the code does not compile.

Consider the following code segment. for (int k = 0; k < 9; k = k + 2) { if ((k % 2) != 0) { System.out.print (k + " "); } } What, if anything, is printed as a result of executing the code segment?

Nothing is printed.

Consider the following Point2D class. public class Point2D { } private double xCoord; private double yCoord; public Point 2D (double x, double y) { xCoord = x; yCoord = y; Which of the following code segments, appearing in a class other than Point 2D, will correctly create an instance of a Point 2D object?

Point2D p = new Point 2D (3.0, 4.0);

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.

The following code segment is intended to round val to the nearest integer and print the result. double val = -0.7; int roundedVal = (int) (val + 0.5); System.out.println(roundedVal); Which of the following best describes the behavior of the code segment?

The code segment does not work as intended because val and roundedVal should be declared as the same data type.

Consider the following code segment, where k and count are properly declared and initialized int variables. k++; k++; count++; k--; count++; k--; Which of the following best describes the behavior of the code segment?

The code segment leaves k unchanged and increases count by 2.

In the code segment below, assume that the int variables a and b have been properly declared and initialized. int c = a; int d = b; c += 3; d--; double num = c; num /= d; Which of the following best describes the behavior of the code segment?

The code segment stores the value of (a + 3) / (b - 1) in the variable num.

public boolean isSaltWater() { if (saltWater) { return "Salt Water"; } else { return "Fresh Water"; Which of the following best explains the reason why the class will not compile?

The value returned by the isSaltWater method is not compatible with the return type of the method.

The code segment below is intended to calculate the circumference c of a circle with the diameter d of 1.5. The circumference of a circle is equal to its diameter times pi. /* missing declarations */ c = pi * d; Which of the following variable declarations are most appropriate to replace /* missing declarations */ in this code segment?

final double pi = 3.14159; double d = 1.5; double c;

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 } Which of the following would be the best specification for a Circle method isInside that determines whether a Point lies inside this Circle?

public boolean isInside(Point p)

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.2 units and then increase the weight of b1 by 1.5 units. 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?

weight += aw;

Which of the following could replace /* missing loop header */ to ensure that the code segment will work as intended? r = " "

while (r <=101)

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

{2, 3, 3, 6, 12}

Consider the following code segment. boolean[] oldVals = {true, false, true, true}; boolean[] newVals = new boolean[4]; for (int j = oldVals.length - 1; j >= 0; j--) { newVals[j] = !(oldVals[j]); } What, if anything, will be the contents of newVals as a result of executing the code segment?

{false, true, false, false}

The following code segment appears in a class other than MenuItem or Combo. MenuItem one = new MenuItem(5.0); one.makeItAMeal(); System.out.println(one.getPrice()); What, if anything, is printed as a result of executing the code segment?

6.5

Consider the following code segment. 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

Consider the following code segment. int a = 4; int b = 5; a++; b++; int c = a + b; a -= 1; System.out.println(a + c); What is printed when the code segment is executed?

15

Consider the following code segment. int x = 5; x += 6 * 2; x -= 3 / 2; What value is stored in x after the code segment executes?

16

blackboard What is printed as a result of the call start() ?

1 2 3 4 5 6 blackboard

The following method is intended to return a string containing the character at position n in the string str. For example, getChar("ABCDE", 2) should return "C". /* missing precondition */ public String getChar(String str, int n) { return str.substring(n, n + 1); } Which of the following is the most appropriate precondition for the method so that it does not throw an exception?

/* Precondition: 0 <= n <= str.length() - 1 */

Consider the following code segment. double x = (int) (5.5 - 2.5); double y = (int) 5.5 - 2.5; System.out.println(x - y); What is printed as a result of executing the code segment?

0.5

Consider the following code segment. String str = "0"; str += str + 0 + 8; System.out.println(str); What is printed as a result of executing the code segment?

0008

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

1 2 3 4 5 6

Consider the following code segment. 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?

2.0 2

Consider the following code segment. int a = 3 + 2 * 3; int b = 4 + 3 / 2; int c = 7 % 4 + 3; double d = a + b + c; What is the value of d after the code segment is executed?

20.0

What value is returned as a result of the call mystery(4) ?

21

Consider the following code segment. for (int num= 0; num < 10; num += 2) for (int val = 0; val < 5; val++) System.out.println("hop"); How many times will System.out.println("hop") be executed?

25

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?

3

Consider the following code segment. double firstDouble = 2.5; int firstInt = 30; int secondInt = 5; double secondDouble = firstInt - secondInt / firstDouble + 2.5; What value will be assigned to secondDouble when the code segment is executed?

30.5

Which of the following statements assigns a random integer between 25 and 60, inclusive, to rn?

36, 25

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

401

What value is returned as a result of the call getTheResult(8) ?

48

Consider the following code segment. String str = "a black cat sat on a table"; int counter = 0; for (int i = 0; i < str.length() - 1; i++) { if (str.substring(i, i + 1).equals("a") && !str.substring (i + 1, i + 2).equals("b")) { counter++; } } System.out.println(counter); What is printed as a result of executing this code segment?

5

Consider the following code segment. double regularPrice = 100; boolean onClearance = true; boolean hasCoupon = false; double final Price = regular Price; regularPrice; if (onClearance) } -= final Price final Price* 0.25; if (hasCoupon) finalPrice = 5.0; } System.out.println(final Price); What is printed as a result of executing the code segment?

75.0

Consider the following code segment. double x = 4.5; int y = (int) x * 2; System.out.print(y); What is printed as a result of executing the code segment?

8

Assume that the following code segment appears in a class other than ExamScore. ExamScore es = new ExamScore ("12345", 80.0); es.bonus (5); System.out.println(es.getScore()); What is printed as a result of executing the code segment?

84.0

Consider the following code segment. int a = 5; int b = 4; int c = 2; a *= 3; b += a; b /= c; System.out.print(b); What is printed when the code segment is executed?

9

Consider the following code segment. System.out.print("AP"); System.out.println(); System.out.println("CS"); System.out.print("A"); What is printed as a result of executing the code segment?

AP CS A

A student has created a Car class. The class contains variables to represent the following. A String variable called color to represent the color of the car An int variable called year to represent the year the car was made A String variable called make to represent the manufacturer of the car A String variable called model to represent the model of the car The object vehicle will be declared as type Car. Which of the following descriptions is accurate?

An attribute of the vehicle object is color.

Consider the following code segment. 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?

The code segment compiles without error.

Consider the following code segment. String oldStr = "ABCDEF"; String newStr = oldStr.substring (1, 3) + oldStr.substring (4); System.out.println (newStr); What is printed as a result of executing the code segment?

BCEF

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?

Between 1 and 5 Element objects are created, and Element.max_value is increased for at least one object created.

else if ((b < 0) || (a <0)) System.out.println("C"); else System.out.println("D"); What is printed as a result of the call conditionalTest(3, -2)?

C

The method countTarget below is intended to return the number of times the value target appears in the array arr. The method may not work as intended.

Changing j <= arr.length; to j < arr.length;

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?

The value of num is the square of twice its original value.

Consider the following statement. boolean x (5 < 8) == (5 == 8); What is the value of x after the statement has been executed?

False

Which of the following declarations will compile without error? I. Student a = new Student(); II. Student b= new Student("Juan", 15); III. Student c = new Student("Juan", "15");

I and II only

The code segment is intended to produce the following output. 0123 0123 0123 Which of the following can be used to replace /* missing loop header */ so that the code segment works as intended?

I and III

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, II, and III

Which of the code segments above will produce the following output? 1 4 7 10 13 16 19

I, II, and III

Item i = new Item(23); Item j = new Item(32); ItemCollection c = new ItemCollection(); i.addToCollection(c); j.addToCollection(c); j.addToCollection(c); i.addToCollection(c); What is printed as a result of executing the code segment?

ID 23 accepted; ID 32 accepted; ID 32 rejected; ID 23 accepted;

Consider the following code segment, which is intended to find the average of two positive integers, x and y. 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?

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.

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?

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

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?

Line 1

Consider the following method, which is intended to calculate and return the expression (x+y)² public double calculate (double x, double y, double a, double b) { return /* missing code */; } Which of the following can replace /* missing code */ so that the method works as intended?

Math.sqrt (Math.pow (x + y, 2) / Math.abs (a - b))

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?

Sample(10, 6.2)

Consider the following code segment. Assume that num3 > num2 > 0. int num1 = 0; int num2 = /* initial value not shown */; int num3 = /* initial value not shown */; while (num2 < num3) { numl += num2; num2++; } Which of the following best describes the contents of num1 as a result of executing the code segment?

The sum of all integers from num2 to num3

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 ?

The sum of all positive even values in arr

Consider the following class definition. public class ItemInventory { private int numItems; public ItemInventory(int num) { numItems = num; } public updateItems(int newNum) { numItems = newNum; } } Which of the following best identifies the reason the class does not compile?

The updateItems method is missing a return type.

The following code segment appears in a method in a class other than Worker. The code segment is intended to print the value 800.0, but instead prints a different value because of an error in the Worker class. Worker bob = new Worker(20.0, 40.0); System.out.println(bob.getEarnings()); Which of the following best explains why an incorrect value is printed?

The variable earnings in the calculateEarnings method is a local variable.

public void changeName(String newName) { name = newName; } public int addNum(int n) { num += n; return num;

The variable num is not defined in the addNum method.

Which of the following code segments, if located in a method in a class other than Thing, will cause the message "Hello my friend" to be printed?

Thing a = new Thing(); a.greet();

Assume that both code segments initialize choice to the same integer value. Which of the following best describes the conditions on the initial value of the variable choice that will cause the two code segments to produce different output?

choice > 10

Which of the following method calls demonstrates that the method does not work as intended? "art"

containsArt ("rattrap", "similar", "today")

Consider the following method. public double myMethod (int a, boolean b) { /* implementation not shown */ } Which of the following lines of code, if located in a method in the same class as myMethod, will compile without error?

double result = myMethod (0, false);

Consider the following code segment, which is intended to print the digits of the two-digit int number num in reverse order. For example, if num has the value 75, the code segment should print 57. Assume that num has been properly declared and initialized. /* missing code */ System.out.print(onesDigit); System.out.print(tensDigit); Which of the following can be used to replace /* missing code */ so that the code segment works as intended?

int onesDigit = num % 10; int tensDigit = num / 10;

Consider the following method, which is intended to return the number of local maximum values in an array. Local maximum values are array elements that are greater than both adjacent array elements. The first and last elements of an array have only a single adjacent element, so neither the first nor the last array element is counted by this method. For example, an array containing the values {3, 9, 7, 4, 10, 12, 3, 8} has two local maximum values: 9 and 12.

int p = 1; p < data.length - 1; p++

Consider the code segment below, where arr is a one-dimensional array of integers. int sum = 0; for (int n : arr) { sum = sum + 2 * n; } System.out.print(sum); Which of the following code segments will produce the same output as the code segment above?

int sum = 0; for (int k = 0; k < arr.length; k++) { sum = sum + 2 * arr[k]; } System.out.print(sum);

Consider the following methods, which appear in the same class. public int functionl (int i, int j) { } return i + j; public int function2 (int i, int j) { } return j - i; Which of the following statements, if located in a method in the same class, will initialize the variable x to 11?

int x = function1 (4, 5) + function2 (1, 3);

Which of the following code segments, appearing in the same class as the addNum method, will result in array2 having the contents {0, 0, 13, 0, 9, 0, 0} ?

int[] array1 = {5, 2, 8, 6, 4, 3, 9}; int[] array2 = addNum(array1, 2, 4, 5);

public boolean isLeapYear (int val) f if ((val % 4) == 0) { return true; } else { return (val % 400) == 0; } Which of the following method calls will return an incorrect response?

isLeapYear (1900)

Consider the following method, which is intended to return the product of 3 and the nonnegative difference between its two int parameters. public int threeTimesDiff (int num1, int num2) { return 3 * (num1 - num2); } Which, if any, precondition is required so that the method works as intended for all values of the parameters that satisfy the precondition?

num1 >= num2

System.out.println("two equals equals three"); What, if anything, is printed as a result of executing the code segment?

one dot equals two

A student has created an OrderedPair class to represent points on an xy-plane. The class contains the following. An int variable called x to represent an x-coordinate. An int variable called y to represent a y-coordinate. A method called printXY that will print the values of x and y. The object origin will be declared as type OrderedPair. Which of the following descriptions is accurate?

origin is an instance of the OrderedPair class.

Assume that the following declaration has been made. Person student = new Person ("Thomas", 1995); Which of the following statements is the most appropriate for changing the name of student from "Thomas" to "Tom" ?

student.setName ("Tom");


Ensembles d'études connexes

Public Speaking Final 2500 Clemson University

View Set

Series 7 - Bonds Practice Questions

View Set

Chapt. 3 & 4 Hospitality financial management

View Set

Social Psych Exam 2 (Chapters 6, 7, 8)

View Set

Good Manufacturing Practices Test 1

View Set

First 60 Elements of the Periodic Table

View Set