0% found this document useful (0 votes)
249 views10 pages

AP CSA Unit 2 Review

The document contains a review of various programming concepts related to Java classes, methods, and object-oriented programming. It includes problems and questions about class attributes, method calls, object instantiation, and code execution results. Additionally, it provides answers to the questions posed, covering topics such as constructors, method overloading, and string manipulation.

Uploaded by

cchelseachan1109
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
249 views10 pages

AP CSA Unit 2 Review

The document contains a review of various programming concepts related to Java classes, methods, and object-oriented programming. It includes problems and questions about class attributes, method calls, object instantiation, and code execution results. Additionally, it provides answers to the questions posed, covering topics such as constructors, method overloading, and string manipulation.

Uploaded by

cchelseachan1109
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Name: __________________________________ Date: _____________

AP CSA Unit 2 –– Review Problems


Consider the following Thing class. Each Thing object has a name attribute, which can be set
in the constructor or by using the setName method. The name of a Thing object can be
returned by the getName method.

public class Thing


{
// attributes not shown

/** Constructs a new Thing named myName


*/
public Thing(String myName)
{ /* implementation not shown */ }

/** Returns this Thing’s name


*/
public String getName()
{ /* implementation not shown */ }

/** Sets this Thing’s name to newName


*/
public void setName(String newName)
{ /* implementation not shown */ }

/** Returns a message as described in part (b)


*/
public void printMessage()
{ /* implementation not shown */ }
}

(a) Write a statement to create a new Thing object snack that has the name "potato chip".

(b) The Thing method printMessage prints a string consisting of the name of the object
followed by "_is_great". Suppose the name of the Thing object favFood is "pizza". Write a
statement that uses the printMessage method to print the string "pizza_is_great".

(c) Write a code segment to change the name of the Thing object something such that the
new name consists of the old name with one character removed at random. For example,
if something has name "ABCD", its new name could be set to "ACD".
Write the code segment below.

Important Vocabulary Terms


class
object
instance
instance variable
attribute
parameter
method
constructor
overload

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

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

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

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

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

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

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

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

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

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

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

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

13. 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) com p
c) comp com co c
d) comp omp mp p
e) comp comp comp comp

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

15. Consider the following code segment.


String one = "pizza";
String two = “pie” + one.substring(1);
System.out.println(two.indexOf(“z”));
What would be printed when the code segment is executed?
a) -1
b) 2
c) 3
d) 4
e) 5

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

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

18. Consider the following method, which is intended to calculate and


return the expression shown on the right

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?
a) Math.sqrt(x ^ 2, y ^ 2, a - b)
b) Math.sqrt((x + y) ^ 2) / Math.abs(a, b)
c) Math.sqrt((x + y) ^ 2 / Math.abs(a - b))
d) Math.sqrt(Math.pow(x + y, 2) / Math.abs(a, b))
e) Math.sqrt(Math.pow(x + y, 2) / Math.abs(a - b))

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

Answers:

1. b 11. d
2. d 12. b
3. e 13. d
4. e 14. e
5. d 15. d
6. d 16. b
7. d 17. a
8. c 18. e
9. e 19. d
10. e

You might also like