Computers Unit 2 AP questions
Which of the following statements assigns a random integer between 25 and 60, inclusive, to rn ? A) int rn = (int)(Math.random() * 25) + 36; B) int rn = (int)(Math.random() * 25) + 60; C) int rn = (int)(Math.random() * 26) + 60; D) int rn = (int)(Math.random() * 36) + 25; E) int rn = (int)(Math.random() * 60) + 25;
D) int rn = (int)(Math.random() * 36) + 25;
Consider the following method public static String scramble(String word, int howFar) { return word.substring(howFar + 1, word.length()) + word.substring(0, howFar); } 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 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 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
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.wfficiencyRating = 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. public static boolean containArt (String s1, String s2, String s3) { String all = s1+s2+s3; return (all.indexOf("art") !=-1); } which of the following method calls demonstrated that the method does not work as intended? A) containArt("rattrap", "similar", "today") B) containsArt ("start", "article", "Bart") C) containsArt("harm", "chortle", "crowbar") D) cntains Art("matriculate", "carat", "arbitrary") E) containsArt("darkroom", "cartoon", "articulate")
A) containArt("rattrap", "similar", "today")
A student has created an OrderedPair class to represent points on an xyxy-plane. The class contains the following. An int variable called x to represent an xx-coordinate. An int variable called y to represent a yy-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
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. 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 method getHours, which is intended to calculate the number of hours that a vehicle takes to travel between two mile markers on a highway if the vehicle travels at a constant speed of 60 miles per hour. A mile marker is a sign showing the number of miles along a road between some fixed location (for example, the beginning of a highway) and the current location. The following table shows two examples of the intended behavior of getHours, based on the int parameters marker1 and marker2. marker1marker2Return Value1002202.0100700.5 Consider the following implementation of getHours. public static double getHours(int marker1, int marker2) { /* missing statement */ return hours; } Which of the following statements can replace /* missing statement */ so getHours works as intended? A) double hours = (Math.abs(marker1) - Math.abs(marker2)) / 60.0; B) double hours = Math.abs(marker1 - marker2 / 60.0); C) double hours = Math.abs(marker1 -marker2) / 60.0; D) double hours = Math.abs((marker1 - marker2) / 60); E) double hours = (double)(Math.abs(marker1 - marker2) / 60);
C) double hours = Math.abs(marker1 -marker2) / 60.0;
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 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 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 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
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 Cat instance is vehicle
D) an attribute of the vehicle object is color
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 class declaration public class Person { private String myName; private int myYearOfBirth; public Person (String name, int yearOfBirth) { myName = name; myYearOfBrith = yearOfBirth; } public String getName() { return myName; } public void setName (String name) { myName = name; } } 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 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();
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 code segment. Assume that a is greater than zero. int a = /* value not shown */; int b = a + (int) (Math.random() * a); Which of the following best describes the value assigned to b when the code segment is executed? A) a B) 2*a C) a random integer between 0 and a - 1, inclusive D) a random integer between a and 2 * a, inclusive E) a random integer between a and 2 * a - 1, inclusive
E) a random integer between a and 2 * a - 1, inclusive