CS UNIT 2

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

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.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")

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

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

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.

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

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

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

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

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

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, 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 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 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

WORLD HISTORY - FROM THE MIDDLE AGES UP TO THE INDUSTRIAL REVOLUTION

View Set

abeka english 12 appendix quiz S

View Set

Ch. 44, 46-48, & 50-53 Lecture Slide Questions

View Set

Texas Promulgated Contract Forms

View Set

International Marketing Exam 4 (2)

View Set

Lecture 22: Recombinant DNA, DNA-Sequencing, and Genomics

View Set