MyAP Quiz, Test 1/2 Emily

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

What is printed as a result of executing the following statement? S.o.P (404/10*10+1) A 4 B 5 C 41 D 401 E 405

401

Directions: Select the choice that best fits each statement. The following question(s) refer to the following information. Consider the following partial class declaration. The following declaration appears in another class.SomeClass obj = new SomeClass ( );Which of the following code segments will compile without error? A int x = obj.getA ( ); B int x; obj.getA (x); C int x = obj.myA; D int x = SomeClass.getA ( ); E int x = getA(obj);

A int x = obj.getA ( );

Which of the following expressions evaluate to 7 ? 9 + 10 % 12 (9 + 10) % 12 9 - 2 % 12 A I only B II only C I and III D II and III E I, II, and III

D II and III

Consider the following code segment. int a = 5; int b = 8; int c = 3; System.out.println(a + b / c * 2); What is printed as a result of executing this code? A 2 B 6 C 8 D 9 E 14

D 9

Which of the following statements assigns a random integer between 1 and 10, inclusive, to rn ? A int rn = (int) (Math.random()) * 10; B int rn = (int) (Math.random()) * 10 + 1; C int rn = (int) (Math.random() * 10); D int rn = (int) (Math.random() * 10) + 1; E int rn = (int) (Math.random() + 1) * 10;

D int rn = (int) (Math.random() * 10) + 1;

Consider the following class declaration. 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" ? A student = new Person ("Tom", 1995); B student.myName = "Tom"; C student.getName ("Tom"); D student.setName ("Tom"); E Person.setName ("Tom");

D student.setName ("Tom");

Consider the following code segment. String str = "CompSci"; System.out.println(str.substring(0, 3)); int num = str.length(); What is the value of num when the code segment is executed? A 3 B 4 C 5 D 6 E 7

E 7

Consider the following code segment. int one = 1; int two = 2; String zee = "Z"; System.out.println(one + two + zee); What is printed as a result of executing the code segment? A 12Z B 3Z C 12zee D 3zee E onetwozee

B 3Z

Consider the following method. What value is returned as a result of the call scramble("compiler", 3)? A "compiler" B "pilercom" C "ilercom" D "ilercomp" E No value is returned because an IndexOutOfBoundsException will be thrown.

C "ilercom"

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? A int result = myMethod(2, false); B int result = myMethod(2.5, true); C double result = myMethod(0, false); D double result = myMethod(true, 10); E double result = myMethod(2.5, true);

C double result = myMethod(0, false);

Consider the following class definition. public class ExamScore { private String studentId; private double score; public ExamScore(String sid, double s) { studentId = sid; score = s; } public double getScore() { return score; } public void bonus(int b) { score += score * b/100.0; } } 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? A 4.0 B 5.0 C 80.0 D 84.0 E 85.0

D 84.0

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? A -1.5 B 1 C 9 D 15.5 E 16

E 16

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? A int onesDigit = num % 10; int tensDigit = num / 10; B int onesDigit = num / 10; int tensDigit = num % 10; C int onesDigit = 10 / num; int tensDigit = 10 % num; D int onesDigit = num % 100; int tensDigit = num / 100; E int onesDigit = num / 100; int tensDigit = num % 100;

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

Consider the following method. public int getTheResult(int n) { int product = 1; for (int number = 1; number < n; number++) { if (number % 2 == 0) product *= number; } return product; } What value is returned as a result of the call getTheResult(8) ? A 48 B 105 C 384 D 5040 E 40320

A 48

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? A 2 2 B 2.0 2 C 2.0 2.0 D 2.25 2 E 2.25 2.0

B 2.0 2

Consider the following code segment. int x = 5; int y = 6; /* missing code */ z = (x + y) / 2; Which of the following can be used to replace /* missing code */ so that the code segment will compile? int z = 0; int z; boolean z = false; A I only B II only C I and II only D II and III only E I, II, and III

C I and II only

Directions: Select the choice that best fits each statement. The following question(s) refer to the following incomplete class declaration. Which of the following can be used to replace / * missing code * / so that advance will correctly update the time? A minutes = minutes % 60; B minutes = minutes + hours % 60; C hours = hours + minutes / 60;minutes = minutes % 60; D hours = hours + minutes % 60; minutes = minutes / 60; E hours = hours + minutes / 60; Related Content & Skills

C hours = hours + minutes / 60; minutes = minutes % 60;

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? A 8 B 08 C 008 D 0008 E Nothing is printed, because numerical values cannot be added to a String object.

D 0008

Consider the following class definition. public class Thing { public void talk() { System.out.print("Hello "); } public void name() { System.out.print("my friend"); } public void greet() { talk(); name(); } /* Constructors not shown */ } 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? A Thing a = new Thing();Thing.talk();Thing.name(); B Thing a = new Thing();Thing.greet(); C Thing a = new Thing();a.talk(); D Thing a = new Thing();a.greet(); E Thing a = new Thing();a.name();a.talk();

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

Consider the following code segment. String temp = "comp"; System.out.print(temp.substring(0) + " " + temp.substring(1) + " " + temp.substring(2) + " " + temp.substring(3)); What is printed when the code segment is executed? A comp B c o m p C comp com co c D comp omp mp p E comp comp comp comp

D comp omp mp p

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? A Te code segment leaves both k and count unchanged. B The code segment increases both k and count by 2. C The code segment increases k by 4 and count by 2. D The code segment leaves k unchanged and increases count by 2. E The code segment increases k by 2 and leaves count unchanged.

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

Consider the following code segment. double d1 = 10.0; Double d2 = 20.0; Double d3 = new Double(30.0); double d4 = new Double(40.0); System.out.println(d1 + d2 + d3.doubleValue() + d4); What, if anything, is printed when the code segment is executed? A 100.0 B 10.050.040.0 C 10.020.070.0 D 10.020.030.040.0 E There is no output due to a compilation error.

A 100.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? A 8 B 8.0 C 9 D 9.0 E 10

A 8

Consider the following method. public void doSomething() { System.out.println("Something has been done"); } 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 ? doSomething(); String output = doSomething(); 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

A I only

In the code segment below, assume that the int variable n has been properly declared and initialized. The code segment is intended to print a value that is 1 more than twice the value of n. /* missing code */ System.out.print(result); Which of the following can be used to replace /* missing code */ so that the code segment works as intended? int result = 2 * n;result = result + 1; int result = n + 1;result = result * 2; int result = (n + 1) * 2; A I only B II only C III only D I and III E II and III

A I only

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 Line 1

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? 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 The code segment compiles without error.

Consider the following class. public class WindTurbine { private double efficiencyRating; public WindTurbine() { efficiencyRating = 0.0; } public WindTurbine(double e) { efficiencyRating = e; } } Which of the following code segments, when placed in a method in a class other than WindTurbine, will construct a WindTurbine object wt with an efficiencyRating of 0.25 ? A WindTurbine wt = new WindTurbine(0.25); B WindTurbine wt = 0.25; C WindTurbine wt = new WindTurbine();wt = 0.25; D WindTurbine wt = new WindTurbine();wt.efficiencyRating = 0.25; E new WindTurbine wt = 0.25;

A WindTurbine wt = new WindTurbine(0.25);

Consider the following method, which is intended to return true if at least one of the three strings s1, s2, or s3 contains the substring "art". Otherwise, the method should return false. Which of the following method calls demonstrates that the method does not work as intended? A containsArt ("rattrap", "similar", "today") B containsArt ("start", "article", "Bart") C containsArt ("harm", "chortle", "crowbar") D containsArt ("matriculate", "carat", "arbitrary") E containsArt ("darkroom", "cartoon", "articulate")

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

Consider the following incomplete method, which is intended to return the number of integers that evenly divide the integer inputVal. Assume that inputVal is greater than 0. Which of the following can be used to replace / * condition * / so that numDivisors will work as intended? A inputVal % k == 0 B k % inputVal == 0 C inputVal % k != 0 D inputVal / k == 0 E k / inputVal > 0

A inputVal % k == 0

Consider the following code segment. System.out.print("One"); // Line 1 System.out.print("Two"); // Line 2 System.out.print("Three"); // Line 3 System.out.print("Four"); // Line 4 The code segment is intended to produce the following output, but does not work as intended. OneTwo ThreeFour Which of the following changes can be made so that the code segment produces the intended output? A Changing print to println in line 1 only B Changing print to println in line 2 only C Changing print to println in line 3 only D Changing print to println in lines 2 and 3 only E Changing print to println in lines 1, 2, 3, and 4

B Changing print to println in line 2 only

Consider the following code segment. System.out.print("Hello System.out.println"); System.out.print("!!!"); What is printed as a result of executing the code segment? A Hello!!! B Hello System.out.println!!! C Hello !!! D Hello System.out.println !!! E Nothing is printed because the text "System.out.println" cannot appear inside a print statement.

B Hello System.out.println!!!

The following code segment is intended to interchange the values of the int variables x and y. Assume that x and y have been properly declared and initialized. int temp = x; /* missing code */ Which of the following can be used to replace /* missing code */ so that the code segment works as intended? A x = y; x = temp; B x = y; y = temp; C y = x; x = temp; D y = x; temp = y; E y = x; temp = x;

B x = y; y = temp;

Assume that myList is an ArrayList that has been correctly constructed and populated with objects. Which of the following expressions produces a valid random index for myList? A (int) ( Math.random () * myList.size () ) - 1 B (int) ( Math.random () * myList.size () ) C (int) ( Math.random () * myList.size () ) + 1 D (int) ( Math.random () * (myList.size () + 1) ) E Math.random (myList.size () )

B (int) ( Math.random () * myList.size () )

Consider the following method. public int timesTwo (int n) { return n * 2; } The following code segment appears in a method in the same class as the timesTwo method. Integer val = 10; int result1 = timesTwo(val); Integer result2 = result1; System.out.print(result2); What, if anything, is printed as a result of executing the code segment? A 10 B 20 C Nothing; the code segment will not compile because timesTwo cannot accept an Integer parameter. D Nothing; the code segment will not compile because the value returned by timesTwo cannot be assigned to result1. E Nothing; the code segment will not compile because the int variable result1 cannot be assigned to the Integer variable result2.

B 20

Consider the following Book and AudioBook classes. Consider the following code segment that appears in a class other than Book or AudioBook. Which of the following best explains why the code segment will not compile? A Line 2 will not compile because variables of type Book may not refer to variables of type AudioBook. B Line 4 will not compile because variables of type Book may only call methods in the Book class. C Line 5 will not compile because the AudioBook class does not have a method named toString declared or implemented. D Line 6 will not compile because the statement is ambiguous. The compiler cannot determine which length method should be called. E Line 7 will not compile because the element at index 1 in the array named books may not have been initialized.

B Line 4 will not compile because variables of type Book may only call methods in the Book class.

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? A The code segment stores the value of (a + 3) / b in the variable num. B The code segment stores the value of (a + 3) / (b - 1) in the variable num. C The code segment stores the value of (a + 3) / (b - 2) in the variable num. D The code segment stores the value of (a + 3) / (1 - b) in the variable num. E The code segment causes a runtime error in the last line of code because num is type double and d is type int.

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

Consider the following methods, which appear in the same class. public int function1(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? A int x = function2(4, 5) + function1(1, 3); B int x = function1(4, 5) + function2(1, 3); C int x = function1(4, 5) + function2(3, 1); D int x = function1(3, 1) + function2(4, 5); E int x = function2(3, 1) + function1(4, 5);

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

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? A origin is an instance of the printXY method. B origin is an instance of the OrderedPair class. C origin is an instance of two int objects. D OrderedPair is an instance of the origin object. E printXY is an instance of the OrderedPair class.

B origin is an instance of the OrderedPair class.

A code segment (not shown) is intended to determine the number of players whose average score in a game exceeds 0.5. A player's average score is stored in avgScore, and the number of players who meet the criterion is stored in the variable count. Which of the following pairs of declarations is most appropriate for the code segment described? A double avgScore; boolean count; B double avgScore; double count; C double avgScore; int count; D int avgScore; boolean count; E int avgScore; int count;

C double avgScore; int count;

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? A int pi = 3.14159; int d = 1.5; final int c; B final int pi = 3.14159; int d = 1.5; int c; C final double pi = 3.14159; double d = 1.5; double c; D double pi = 3.14159; double d = 1.5; final double c = 0.0; E final double pi = 3.14159; final double d = 1.5; final double c = 0.0;

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

Consider the following method. public double puzzle(int x) { Double y = x / 2.0; y /= 2; return y.doubleValue(); } Assume that the method call puzzle(3) appears in a method in the same class as puzzle. What value is returned as a result of the method call? A 0.0 B 0.5 C 0.75 D 1.0 E 1.5

C 0.75

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? A 2 B 4 C 9 D 9.5 E 19

C 9

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? A ABCD B BCDE C BCEF D BCDEF E ABCDEF

C BCEF

Consider the following class declaration. Which of the following declarations will compile without error? Student a = new Student(); Student b = new Student("Juan", 15); Student c = new Student("Juan", "15"); A I only B II only C I and II only D I and III only E I, II, and III

C I and II only

A student has created a Song class. The class contains the following variables. A String variable called artist to represent the artist name A String variable called title to represent the song title A String variable called album to represent the album title The object happyBirthday will be declared as type Song. Which of the following statements is true? A artist, title, and album are instances of the Song class. B happyBirthday is an instance of three String objects. C happyBirthday is an instance of the Song class. D Song is an instance of the happyBirthday object. E Song is an instance of three String objects.

C happyBirthday is an instance of the Song class.

Consider the following code segment. System.out.print("*"); System.out.println("**"); System.out.println("***"); System.out.print("****"); What is printed as a result of executing the code segment? A * ** *** **** B * ** ******* C * ***** **** D *** *** **** E *** *******

D *** *** ****

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? A APCSA B APCS A C AP CSA D AP CS A E AP CS A

D AP CS A

Consider the following code segment. System.out.print(I do not fear computers. ); // Line 1 System.out.println(I fear the lack of them.); // Line 2 System.out.println(--Isaac Asimov); // Line 3 The code segment is intended to produce the following output but may not work as intended. I do not fear computers. I fear the lack of them. --Isaac Asimov Which change, if any, can be made so that the code segment produces the intended output? A In line 1, print should be changed to println. B In lines 2 and 3, println should be changed to print. C The statement System.out.println() should be inserted between lines 2 and 3. D In lines 1, 2, and 3, the text that appears in parentheses should be enclosed in quotation marks. E No change is needed; the code segment works correctly as is.

D In lines 1, 2, and 3, the text that appears in parentheses should be enclosed in quotation marks.

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 The value of num is the square of twice its original value.

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? A -1.0 B -0.5 C 0.0 D 0.5 E 1.0

D 0.5

Consider the following method. public int mystery(int num) { int x = num; while (x > 0) { if (x / 10 % 2 == 0) return x; x = x / 10; } return x; } What value is returned as a result of the call mystery(1034) ? A 4 B 10 C 34 D 103 E 1034

D 103

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? A 9 B 10 C 14 D 15 E 25

D 15

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? A An instance of the vehicle class is Car. B An instance of the Car object is vehicle. C An attribute of the year object is int. D An attribute of the vehicle object is color. E An attribute of the Car instance is vehicle.

D An attribute of the vehicle object is color.

Consider the following class. public class SomeMethods {public void one(int first) { / * implementation not shown * / } public void one(int first, int second) { / * implementation not shown * / } public void one(int first, String second) { / * implementation not shown * / } } Which of the following methods can be added to the SomeMethods class without causing a compile-time error? public void one(int value){ / * implementation not shown * / } public void one (String first, int second) { / * implementation not shown * / } public void one (int first, int second, int third) { / * implementation not shown * / } A I only B I and II only C I and III only D II and III only E I, II, and III

D II and III only

Consider the following class declaration. public class GameClass { private int numPlayers; private boolean gameOver; public Game() { numPlayers = 1; gameOver = false; } public void addPlayer() { numPlayers++; } public void endGame() { gameOver = true; } } Assume that the GameClass object game has been properly declared and initialized in a method in a class other than GameClass. Which of the following statements are valid? game.numPlayers++; game.addPlayer(); game.gameOver(); game.endGame(); A IV only B I and III only C I and IV only D II and IV only E II, III, and IV only

D II and IV only

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? 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 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.

Consider the code segment below. int a = 1988; int b = 1990; String claim = " that the world's athletes " + "competed in Olympic Games in "; String s = "It is " + true + claim + a + " but " + false + claim + b + "."; System.out.println(s); What, if anything, is printed when the code segment is executed? A It is trueclaima but falseclaimb. B It is trueclaim1998 but falseclaim1990. C It is true that the world's athletes competed in Olympic Games in a but false that the world's athletes competed in Olympic Games in b. D It is true that the world's athletes competed in Olympic Games in 1988 but false that the world's athletes competed in Olympic Games in 1990. E Nothing is printed because the code segment does not compile.

D It is true that the world's athletes competed in Olympic Games in 1988 but false that the world's athletes competed in Olympic Games in 1990.

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? A The code segment works as intended. B The code segment does not work as intended because val and roundedVal should be declared as the same data type. C The code segment does not work as intended because the expression (val + 0.5) should be cast to a double instead of an int. D The code segment does not work as intended because val should be cast to an int before 0.5 is added to it. E The code segment does not work as intended because the expression (int) (val + 0.5) rounds to the nearest integer only when val is positive.

E The code segment does not work as intended because the expression (int) (val + 0.5) rounds to the nearest integer only when val is positive.

Which of the following statements stores the value 3 in x ? A int x = 4 / 7; B int x = 7 / 3; C int x = 7 / 4; D int x = 5 % 8; E int x = 8 % 5;

E int x = 8 % 5;

Consider the following code segment, which is intended to assign to num a random integer value between min and max, inclusive. Assume that min and max are integer variables and that the value of max is greater than the value of min. double rn = Math.random(); int num = /* missing code */; Which of the following could be used to replace /* missing code */ so that the code segment works as intended? A (int) (rn * max) + min B (int) (rn * max) + min - 1 C (int) (rn * (max - min)) + min D (int) (rn * (max - min)) + 1 E (int) (rn * (max - min + 1)) + min

E (int) (rn * (max - min + 1)) + min

A pair of number cubes is used in a game of chance. Each number cube has six sides, numbered from 1 to 6, inclusive, and there is an equal probability for each of the numbers to appear on the top side (indicating the cube's value) when the number cube is rolled. The following incomplete statement appears in a program that computes the sum of the values produced by rolling two number cubes. int sum = / * missing code * / ; Which of the following replacements for /* missing code */ would best simulate the value produced as a result of rolling two number cubes? A 2 * (int) (Math.random() * 6) B 2 * (int) (Math.random() * 7) C (int) (Math.random() * 6) + (int) (Math.random() * 6) D (int) (Math.random() * 13) E 2 + (int) (Math.random() * 6) + (int) (Math.random() * 6)

E 2 + (int) (Math.random() * 6) + (int) (Math.random() * 6)

Consider the following class definition. public class Student { private int studentID; private int gradeLevel; private boolean honorRoll; public Student(int s, int g) { studentID = s; gradeLevel = g; honorRoll = false; } public Student(int s) { studentID = s; gradeLevel = 9; honorRoll = false; } } Which of the following code segments would successfully create a new Student object? Student one = new Student(328564, 11); Student two = new Student(238783); int id = 392349;int grade = 11;Student three = new Student(id, grade); A I only B II only C III only D I and II only E I, II, and III

E I, II, and III

Consider the following methods, which appear in the same class. public void printSum(int x, double y) { System.out.println(x + y); } public void printProduct(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. int num1 = 5; double num2 = 10.0; printSum(num1, num2); printProduct(num1, num2); What, if anything, is printed as a result of executing the code segment? A 15 50 B 15 50.0 C 15.0 50 D 15.0 50.0 E Nothing is printed because the code does not compile.

E Nothing is printed because the code does not compile.

Consider the following Point2D class. public class Point2D { private double xCoord; private double yCoord; public Point2D(double x, double y) { xCoord = x; yCoord = y; } } Which of the following code segments, appearing in a class other than Point2D, will correctly create an instance of a Point2D object? A Point2D p = (3.0, 4.0); B Point2D p = Point2D(3.0, 4.0); C new p = Point2D(3.0, 4.0); D new Point2D = p(3.0, 4.0); E Point2D p = new Point2D(3.0, 4.0);

E Point2D p = new Point2D(3.0, 4.0);

The Student class has been defined to store and manipulate grades for an individual student. The following methods have been defined for the class. /* Returns the sum of all of the student's grades */ public double sumOfGrades() { /* implementation not shown */ } /* Returns the total number of grades the student has received */ public int numberOfGrades() { /* implementation not shown */ } /* Returns the lowest grade the student has received */ public double lowestGrade() { /* implementation not shown */ } Which of the following statements, if located in a method in the Student class, will determine the average of all of the student's grades except for the lowest grade and store the result in the double variable newAverage ? A newAverage = sumOfGrades() / numberOfGrades() - 1; B newAverage = sumOfGrades() / (numberOfGrades() - 1); C newAverage = sumOfGrades() - lowestGrade() / (numberOfGrades() - 1); D newAverage = (sumOfGrades() - lowestGrade()) / numberOfGrades() - 1; E newAverage = (sumOfGrades() - lowestGrade()) / (numberOfGrades() - 1);

E newAverage = (sumOfGrades() - lowestGrade()) / (numberOfGrades() - 1);

Consider the following method that is intended to determine if the double values d1 and d2 are close enough to be considered equal. For example, given a tolerance of 0.001, the values 54.32271 and 54.32294 would be considered equal. Which of the following should replace / * missing code * / so that almostEqual will work as intended? A return (d1 - d2) <= tolerance; B return ((d1 + d2) / 2) <= tolerance; C return (d1 - d2) >= tolerance; D return ( (d1 + d2) / 2) >= tolerance; E return Math.abs(d1 - d2) <= tolerance;

E return Math.abs(d1 - d2) <= tolerance;


Ensembles d'études connexes

Ch.7 Developmental Psychology 2410

View Set

MIE 201 Exam 2: Chapters 4, 5, 8 Makanui NCSU

View Set

Chapter 3-Introduction to the Fourth Amendment

View Set

VIMCO C Vocabulario IMportante para la COmunicación

View Set

Chapter 20: Master Budgets and Performance Planning

View Set