Unit 2 APCS
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?
(int) ( Math.random () * myList.size () )
Consider the following class declaration public class SomeClass { private int num; public SomeClass(int n) { num = n; } public void increment(int more) { num = num + more; } public int getNum() { return num; } } The following code segment appears in another class. SomeClass one = new SomeClass(100); SomeClass two = new SomeClass(100); SomeClass three = one; one.increment(200); System.out.println(one.getNum() + " " + two.getNum() + " " + three.getNum()); What is printed as a result of executing the code segment?
300 100 300
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?
3z
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?
7
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?
8/4
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 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());
I only
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);
I, II, and III
The following statement assigns an integer value to x. int x = (int)(Math.random() * 5) + 10; Consider the statement that would result if the positions of 5 and 10 were swapped in the preceding statement and the resulting integer were assigned to y. int y = (int)(Math.random() * 10) + 5; Which of the following are true statements about how the possible values assigned to y differ from the possible values assigned to x ? There are more possible values of x than there are possible values of y. There are more possible values of y than there are possible values of x. The value assigned to x can sometimes be the same as the value assigned to y.
II and III
Consider the following method, which is intended to calculate and return the expression (x+y)2|a−b|−−−−−√. 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))
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?
Point2D p = new Point2D(3.0, 4.0);
Consider the processWords method. Assume that each of its two parameters is a String of length two or more. public void processWords(String word1, String word2) { String str1 = word1.substring(0, 2); String str2 = word2.substring(word2.length() - 1); String result = str2 + str1; System.out.println(result.indexOf(str2)); } Which of the following best describes the value printed when processWords is called?
The value 0 is always printed.
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?
Thing a = new Thing(); a.greet();
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 ?
WindTurbine wt = new WindTurbine(0.25);
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?
comp omp mp p
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, flase);
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?
happyBirthday is an instance of the Song class.
Which of the following statements assigns a random integer between 25 and 60, inclusive, to rn ?
int rn = (int) (Math.random() * 36) + 25;
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?
int x = function1(4, 5) + function2(1, 3);
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?
origin is an instance of the OrderedPair class.
Consider the following class definition. public class Bird { private String species; private String color; private boolean canFly; public Bird(String str, String col, boolean cf) { species = str; color = col; canFly = cf; } } Which of the following constructors, if added to the Bird class, will cause a compilation error?
public Bird(String col, String str, boolean cf) { species = str; color = col; canFly = cf; }