Comp Sci Test Review
Consider the following method. What value is returned as a result of the call scramble("compiler", 3)?
"ilercom"
Consider the following method. What is returned as a result of the call mystery("computer") ?
"optr"
Consider the following method. public static String abMethod(String a, String b) { int x = a.indexOf(b); while (x >= 0) { a = a.substring(0, x) + a.substring(x + b.length()); x = a.indexOf(b); } return a; } What, if anything, is returned by the method call abMethod("sing the song", "ng") ?
"si the so"
Consider the following method, which returns an int based on its parameter x. public static int puzzle(int x) { if (x > 20) { x -= 2; } else if (x % 2 == 0) // Line 7 { x += 4; } return x; } Consider a modification to the method that eliminates the else from line 77 so that line 77 becomes if (x % 2 == 0) // Modified line 7 For which of the following values of x would the return values of the original method and the modified method differ?
22
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 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");
I and II only
Consider the following class declaration. public class IntCell { private int myStoredValue; // constructor not shown public int getValue() { return myStoredValue; } public String toString () { return "" + myStoredValue; } } Assume that the following declaration appears in a client class. IntCell m = new IntCell(); Which of these statements can be used in the client class? I. System.out.println(m.getValue()); II. System.out.println(m.myStoredValue); III. System.out.println(m);
I and III
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
Consider the following instance variables and method that appear in a class representing student information. private int assignmentsCompleted; private double testAverage; public boolean isPassing() { /* implementation not shown */ } A student can pass a programming course if at least one of the following conditions is met. The student has a test average that is greater than or equal to 90. The student has a test average that is greater than or equal to 75 and has at least 4 completed assignments. Consider the following proposed implementations of the isPassing method. I. if (testAverage >= 90) return true; if (testAverage >= 75 && assignmentsCompleted >= 4) return true; return false; II. boolean pass = false; if (testAverage >= 90) pass = true; if (testAverage >= 75 && assignmentsCompleted >= 4) pass = true; return pass; III. return (testAverage >= 90) || (testAverage >= 75 && assignmentsCompleted >= 4); Which of the implementations will correctly implement method isPassing?
I, II, and III
Consider the following attempts at method overloading. I. public class Overload { public int average(int x, int y) { /* implementation not shown */ } public int average(int value1, int value2) { /* implementation not shown */ } // There may be instance variables, constructors, // and methods that are not shown. } II. public class Overload { public int average(int x, int y) { /* implementation not shown */ } public int average(int x, int y, int z) { /* implementation not shown */ } // There may be instance variables, constructors // and methods that are not shown. } III. public class Overload { public int average(int x, int y) { /* implementation not shown */ } public int average(double x, double y) { /* implementation not shown */ } // There may be instance variables, constructors, // and methods that are not shown. } Which of the attempts at method overloading will compile without error?
II and III 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?
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 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?
Nothing is printed because the code does not compile.
Consider the definition of the Person class below. The class uses the instance variable adult to indicate whether a person is an adult or not. public class Person { private String name; private int age; private boolean adult; public Person (String n, int a) { name = n; age = a; if (age >= 18) { adult = true; } else { adult = false; } } } Which of the following statements will create a Person object that represents an adult person?
Person p = new Person ("Homer", 23);
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 following class declaration. public class Sample { private int a; private double b; public Sample(int x, double y) { a = x; b = y; } // No other constructors } The following method appears in a class other than Sample. public static void test() { Sample object = new /* missing constructor call */ ; } Which of the following could be used to replace /* missing constructor call */ so that the method will compile without error?
Sample(10, 6.2)
Consider the following class declarations. public class Alpha { private int answer() { return 10; } } public class Beta { public double sample() { Alpha item = new Alpha(); double temp = item.answer(); return temp * 2.0; } } Which of the following best describes why an error occurs when the classes are compiled?
The answer method cannot be accessed from a class other than Alpha.
Consider the following class definition. The class does not compile. public class Player { private double score; public getScore() { return score; } // Constructor not shown } The accessor method getScore is intended to return the score of a Player object. Which of the following best explains why the class does not compile?
The return type of the getScore method needs to be defined as double.
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.
The class Worker is defined below. The class includes the method getEarnings, which is intended to return the total amount earned by the worker. public class Worker { private double hourlyRate; private double hoursWorked; private double earnings; public Worker(double rate, double hours) { hourlyRate = rate; hoursWorked = hours; } private void calculateEarnings() { double earnings = 0.0; earnings += hourlyRate * hoursWorked; } public double getEarnings() { calculateEarnings(); return earnings; } } The following code segment appears in a method in a class other than Worker. The code segment is intended to print the value 800.0, but instead prints a different value because of an error in the Worker class. Worker bob = new Worker(20.0, 40.0); System.out.println(bob.getEarnings()); Which of the following best explains why an incorrect value is printed?
The variable earnings in the calculateEarnings method is a local variable.
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 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?
double hours = Math.abs(marker1 - marker2) / 60.0;
Consider the following statement. boolean x = (5 < 8) == (5 == 8); What is the value of x after the statement has been executed?
false
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 method. public static int mystery(boolean a, boolean b, boolean c) { int answer = 7; if (!a) { answer += 1; } if (b) { answer += 2; } if (c) { answer += 4; } return answer; } Which of the following method calls will return the value 11 ?
mystery(true, false, true)
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; }
Consider the following class definition. public class Tester { private int num1; private int num2; /* missing constructor */ } The following statement appears in a method in a class other than Tester. It is intended to create a new Tester object t with its attributes set to 10 and 20. Tester t = new Tester(10, 20); Which of the following can be used to replace /* missing constructor */ so that the object t is correctly created?
public Tester(int first, int second) { num1 = first; num2 = second; }
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" ?
student.setName ("Tom");
Consider the following declarations. int valueOne, valueTwo; Assume that valueOne and valueTwo have been initialized. Which of the following evaluates to true if valueOne and valueTwo contain the same value?
valueOne == valueTwo