AP CS- Unit 2

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Consider the following code segment. String one = "ABC123"; String two = "C"; String three = "3"; System.out.println(one.indexOf(two)); System.out.println(one.indexOf(three)); System.out.println(two.indexOf(one)); What is printed when the code segment is executed? A. 2 5 -1 B. 2 5 2 C. 2 6 -1 D. 3 6 -1 E. -1 -1 2

A. 2 5 -1

Consider the following methods, which appear in the same class. public void slope(int x1, int y1, int x2, int y2) { int xChange = x2 - x1; int yChange = y2 - y1; printFraction(yChange, xChange); } public void printFraction(int numerator, int denominator) { System.out.print(numerator + "/" + denominator); } Assume that the method call slope(1, 2, 5, 10) appears in a method in the same class. What is printed as a result of the method call? A. 8/4 B. 5/1 C. 4/8 D. 2/1 E. 1/5

A. 8/4

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

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

A teacher has created a Student class. The class contains the following. An int variable called grade to represent the student's grade level A String variable called name to represent the student's name A double variable called average to represent the student's grade point average A method called updateAverage that updates the student's average. The object greg will be declared as type Student. Which of the following descriptions is accurate? A. greg is an instance of the Student class. B. greg is an instance of the updateAverage method. C. greg is an instance of three attributes. D. Student is an instance of the greg object. E. updateAverage is an instance of the Student class.

A. greg is an instance of the Student class.

Consider the following method. public void adjust(double max, double min, double total, double n) { total = total - max - min; n = n - 2.0; System.out.println(total / n); } Consider the call adjust(25.0, 5.0, 60.0, 5.0), which appears in a method in the same class. What is printed as a result of the method call? A. 6.0 B. 10.0 C. 12.0 D. 15.0 E. 20.0

B. 10.0

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 class. public class MagicNumber { private int num; public MagicNumber() { num = 10; } public void displayNumber() { System.out.println(num); } public void add_2() { num = num + 2; } } When located in a method in a class other than MagicNumber, which of the following code segments will compile without error? MagicNumber.add_2();MagicNumber.displayNumber(); MagicNumber n1 = new MagicNumber();n1.add_2();n1.displayNumber(); n2.add_2();n2.displayNumber(); A. I only B. II only C. III only D. II and III only E. None of the code segments will compile.

B. II only

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

Consider the following code segment. Integer num = new Integer(15); int n = num.intValue(); Which of the following statements best describes the type and contents of num and n after the code segment executes? A. num is an Integer that contains the value 15, and n is an int that contains the value 0. B. num is an Integer that contains the value 15, and n is an int that contains the value 15. C. num is an Integer that contains the value 15, and n is an Integer that contains the value 15. D. num is an int that contains the value 15, and n is an Integer that contains the value 0. E. num is an int that contains the value 15, and n is an Integer that contains the value 15.

B. num is an Integer that contains the value 15, and n is an int that contains the value 15.

Consider the following code segment. String dessert = "pie"; dessert += "straw" + dessert + "berry"; What is the value of dessert after the code segment has been executed? A. strawpieberry B. piestrawpieberry C. strawpieberrypie D. strawberry E. piestrawberry

B. piestrawpieberry

Consider the following methods, which appear in the same class. public void methodA(int arg) { int num = arg * 10; methodB(num); } public void methodB(int arg) { System.out.print(arg + 10); } Consider the call methodA(4), which appears in a method in the same class. What, if anything, is printed as a result of the call methodA(4) ? A. 14 B. 40 C. 50 D. 140 E. Nothing is printed.

C. 50

A student has created a Book class. The class contains variables to represent the following. An int variable called pages to represent the number of pages A boolean variable called isHardcover to indicate whether or not the book is hardcover The object story will be declared as type Book. Which of the following descriptions is accurate? A. An instance of the story class is Book. B. An instance of the Book object is story. C. An attribute of the story object is isHardcover. D. An attribute of the pages object is Book. E. An attribute of the Book instance is story.

C. An attribute of the story object is isHardcover.

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 code segment. String s1 = "ABCDEFGHI"; String s2 = s1.substring(6, 7); String s3 = new String("abcdefghi"); String s4 = s3.substring(4, 5); String s5 = s3.substring(2, 3); System.out.print(s2 + s4 + s5); What, if anything, is printed when the code segment is executed? A. Fdb B. FGdebc C. Gec D. GHefcd E. There is no output due to a compilation error.

C. Gec

Consider the following Vbox class. public class Vbox { private int width; private int height; private int depth; public Vbox(int w, int h, int d) { width = w; height = h; depth = d; } public Vbox(int len) { width = len; height = len; depth = len; } } Which of the following declarations, appearing in a method in a class other than Vbox, will correctly instantiate a Vbox object? I. Vbox b1 = new Vbox(4); II. Vbox b2 = new Vbox(2, 8, 4); III. Vbox b3 = new Vbox(4.0, 4.0, 4.0); A. I only B. II only C. I and II only D. I and III only E. II and III only

C. I and II only

Which of the following expressions represents x|k−j|, where x, k, and j are properly declared and initialized int variables? A. Math.abs(k - j, Math.pow(x)); B. Math.abs(Math.pow(x), k - j); C. Math.pow(x, Math.abs(k - j)); D. Math.pow(Math.abs(k - j), x); E. Math.pow.abs(x, k - j));

C. Math.pow(x, Math.abs(k - j));

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 method. public double secret(int x, double y) { return x / 2.0; } Which of the following lines of code, if located in a method in the same class as secret, will compile without error? A. int result = secret(4, 4); B. int result = secret(4, 4.0); C. double result = secret(4, 4.0); D. double result = secret(4.0, 4); E. double result = secret(4.0, 4.0);

C. double result = secret(4, 4.0);

Consider the following method. public void printSomething (int num, boolean val) { num--; System.out.print(val); System.out.print(num); } Consider the following code segment, which appears in a method in the same class as printSomething. printSomething(1, true); printSomething(2, true); What is printed as a result of executing the code segment? A. 0true1true B. 1true2true C. true0true1 D. true1true0 E. true1true2

C. true0true1

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 code segment. Integer original = new Integer(8); Integer first = new Integer(original.intValue() * 2); Integer second = new Integer(original.intValue() + 2); System.out.println(first.intValue() + " " + second.intValue()); What is printed when the code segment is executed? A. 8 8 B. 8 10 C. 10 10 D. 16 10 E. 16 18

D. 16 10

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 method. public double calculate(double x) { return x + 1.5; } The following code segment calls the method calculate in the same class. Double d1 = new Double(7.5); System.out.println(calculate(d1)); What, if anything, is printed when the code segment is executed? A. 8.0 B. 8.5 C. 9 D. 9.0 E. Nothing is printed because the code does not compile. The actual parameter d1 passed to calculate is a Double, but the formal parameter x is a double.

D. 9.0

Consider the following code segment. String str = "AP"; str += "CS " + 1 + 2; System.out.println(str); What is printed as a result of executing the code segment? A. CS AP12 B. AP CS3 C. CSAP 12 D. APCS 12 E. APCS 3

D. APCS 12

Consider the following method, which returns the lesser of its two parameters. public int min(int first, int second) { /* implementation not shown */ } Assume that each of the following expressions appears in a method in the same class as the method min. Assume also that the int variables p, q, and r have been properly declared and initialized. Which of the following expressions evaluates to the minimum value among p, q, and r? I. min(min(p, q), r) II. min(p, min(q, r)) III. min(min(p, q), p) A. I only B. II only C. III only D. I and II only E. I, II, and III

D. I and II 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? I. game.numPlayers++; II. game.addPlayer(); III. game.gameOver(); IV. 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.

The method below is intended to return the area of a square whose side length is s units. The area of a square is its side length times itself. public double squareArea(double s) { return /* missing code */ } Which of the following can be used to replace /* missing code */ so that the method works as intended? A. pow(s, 2); B. s.pow(2); C. Math.pow(s); D. Math.pow(s, 2); E. Math.pow(2, s);

D. Math.pow(s, 2);

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 class declaration. public class Thing { private String color; public Thing() { color = "Blue"; } public Thing(String setColor) { color = setColor; } } Which of the following code segments, when appearing in a class other than Thing, would create a reference of type Thing with a value of null ? A. Thing someThing = new Thing("Green"); B. Thing someThing = new Thing(""); C. Thing someThing = new Thing(); D. Thing someThing; E. Thing("Green");

D. Thing someThing;

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 method, which takes as input a temperature in degrees Fahrenheit and returns the corresponding temperature in degrees Celsius. public double fahrenheitToCelsius(double f) { double c = (f - 32) * 5 / 9; return c; } Assume that each of the following code segments appears in a method in the same class as fahrenheitToCelsius. Which of the following code segments prints the temperature in degrees Celsius that corresponds to 32 degrees Fahrenheit? A. double f = 32.0; fahrenheitToCelsius(); System.out.println(f); B. double f = 32.0; f = fahrenheitToCelsius(); System.out.println(f); C. double f = 32.0; fahrenheitToCelsius(f); System.out.println(f); D. double f = 32.0; double c = fahrenheitToCelsius(); System.out.println(c); E. double f = 32.0; double c = fahrenheitToCelsius(f); System.out.println(c);

E. double f = 32.0; double c = fahrenheitToCelsius(f); System.out.println(c);

The code segment below is intended to randomly print one of the values 2, 4, 6, or 8 with equal probability. int val = /* missing code */ ; val *= 2; System.out.print(val); Which of the following can be used to replace /* missing code */ so that the code segment works as intended? A. Math.random() * 4 + 1 B. Math.random() * 8 C. (int) (Math.random() * 4) D. (int) (Math.random() * 4 + 1) E. (int) (Math.random() * 8 + 1)

E. (int) (Math.random() * 8 + 1)

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

A school administrator has created a Student class. The class contains variables to represent the following. An int variable called studentID to represent the student's ID number A String variable called studentName to represent the student's name The school administrator has also created a Parent class. The class contains variables to represent the following. A String variable called parentName to represent the parent's name A String variable called email to represent the parent's e-mail address The object penelope will be declared as type Student. The object mrsPatel will be declared as type Parent. Which of the following descriptions is accurate? A. An attribute of the penelope object is email. B. An attribute of the penelope object is Parent. C. An attribute of the penelope object is Student. D. An attribute of the mrsPatel object is studentName. E. An attribute of the mrsPatel object is email.

E. An attribute of the mrsPatel object is email.

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? I. Student one = new Student(328564, 11); II. Student two = new Student(238783); IIl. 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

Which of the following code segments can be used to set the value of the string str to "Good morning, sunshine!" ? I. String str = "Good " + "morning," + " sunshine!"; II. String str = "Good"; str += " morning, sunshine!"; III. String str = " morning, "; str = "Good" + str + "sunshine!"; A I only B II only C III only D II and III 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);

Consider the following code segment. String word = "September"; String str1 = word.substring(0, 3); String str2 = word.substring(word.length() - 3); System.out.println(str1 + str2); What is printed when the code segment is executed? A. epbe B.epber C. Sepbe D. Seper E. Sepber

E. Sepber

Consider the following class declaration. public class VetRecord { private String name; private int age; private int weight; private boolean needsVaccine; public VetRecord(String nameP, int ageP, int weightP, boolean needsVaccineP) { name = nameP; age = ageP; weight = weightP; needsVaccine = needsVaccineP; } public VetRecord(String nameP, int ageP, int weightP) { name = nameP; age = ageP; weight = weightP; needsVaccine = true; } } A new constructor is to be added to the VetRecord class. Which of the following is NOT a possible header for the new constructor? A. VetRecord(int ageP, int weightP) B. VetRecord(int ageP, boolean needsVaccine) C. VetRecord(String nameP, int ageP) D. VetRecord(String nameP, boolean needsVaccine) E. VetRecord(String nameP, int weightP, int ageP)

E. VetRecord(String nameP, int weightP, int ageP)

Consider the following class definition. public class AnimalPrinter { public void printDog() { System.out.println("dog"); } public void printCat() { System.out.println("cat"); } /* constructors not shown */ } The method myMethod appears in a class other than AnimalPrinter. The method is intended to produce the following output. dog cat Assume that an AnimalPrinter object myPrinter has been properly declared and initialized inside myMethod. Which of the following code segments, if located in myMethod, will produce the intended output? A. printDog(); printCat(); B. printDog(AnimalPrinter); printCat(AnimalPrinter); C. AnimalPrinter.printDog(); AnimalPrinter.printCat(); D. printDog(myPrinter); printCat(myPrinter); E. myPrinter.printDog(); myPrinter.printCat();

E. myPrinter.printDog();myPrinter.printCat();

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 class. public class Purchase { private double purchase; private double tax; public Purchase(double purchaseAmt, double taxAmt) { purchase = purchaseAmt; tax = taxAmt; } public void totalAmount() { System.out.print(purchase + tax); } } Assume that a Purchase object p has been properly declared and initialized. Which of the following code segments will successfully print the total purchase amount associated with p? A. Purchase.totalAmount(); B. System.out.print(p); C. totalAmount(p); D. System.out.print(p.totalAmount()); E. p.totalAmount();

E. p.totalAmount();


Kaugnay na mga set ng pag-aaral

Business Accounting 1-5 Proc.Exam

View Set

Property and Casualty Insurance Terms & Related Concepts

View Set

RPAS Foundations and Remote Vehicles Summative

View Set

Ultrasound physics Chapter 3 review

View Set

Chapter 19 searching, sorting and Big O

View Set

Module 10: Right Triangle Trigonometry and the Unit Circle

View Set

Managing Care: Perioperative Nursing

View Set

Chapter 21: The Newborn at Risk: Congenital Disorders

View Set