AP Comp Sci
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?
84.0
Consider the following class declaration. Which of the following declarations will compile without error? I. Student a = new Student(); II. Student b = new Student("Juan", 15); III. Student c = new Student("Juan", "15");
I and II only
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 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 class definition. public class Points { private double num1; private double num2; public Points(int n1, int n2) // Line 6 { num1 = n1; // Line 8 num2 = n2; // Line 9 } public void incrementPoints(int value) // Line 12 { n1 += value; // Line 14 n2 += value; // Line 15 } } The class does not compile. Which of the following identifies the error in the class definition? Responses
In lines 14 and 15, the variables n1 and n2 are not defined.
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
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 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, false);
Directions: Select the choice that best fits each statement. The following question(s) refer to the following information. Consider the following partial class declaration. Question The following declaration appears in another class.SomeClass obj = new SomeClass ( );Which of the following code segments will compile without error?
int x = obj.getA ( );
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; }