📚

 > 

💻 

 > 

⚙️

5.12 MC Answers and Review

6 min readdecember 22, 2021


AP Computer Science A 💻

130 resources
See Units

Answers and Review for Multiple Choice Practice on Writing Classes 

https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2F-o7tHnqsXEVgn.png?alt=media&token=f4f831bc-6e3c-42f2-82ba-c3357f1fb09e

Image From Wikipedia.

 
⛔STOP!⛔ Before you look at the answers make sure you gave this practice quiz a try so you can assess your understanding of the concepts covered in unit #. Click here for the practice questions: AP Name Unit 5 Multiple Choice Questions.
Facts about the test: The AP Computer Science A exam has 40 multiple choice questions and you will be given 90 minutes to complete the section. That means it should take you around 34 minutes to complete 15 questions.

*The following questions were not written by CollegeBoard and although they cover information outlined in the AP Computer Science A Course and Exam Description the formatting on the exam may be different.


1. Which of the following is a correct header for a method of the Example class that would return the value of the private instance variable x so that it can be used in a class other than Example?

A. private int getX()
B. public int getX()
C. private void getX()
D. public void getX(int x)
Answer: public=modifier int=return type getX = name of the method
📄 Study AP CSA, Unit 5.1: Anatomy of a Class

2. Consider the processWords method. Assume that each of its two parameters is a String of length two or more. Which of the following best describes the value printed when processWords is called?

A. The value result.length() - 1 is printed.
B. The value 1 is always printed
C. The value 0 is always printed
D. A substring containing the last character of word2 is printed.
Answer: result will always be 0
📄 Study AP CSA, Unit 5.4: Accessor Methods

3. Which of the following statements assigns a random integer between 25 and 60, inclusive, to rn?

A. int rn = (int) (Math.random() * 25) + 60;
B. int rn = (int) (Math.random() * 26) + 60;
C. int rn = (int) (Math.random() * 25) + 36;
D. int rn = (int) (Math.random() * 36) + 25;
Answer: random is in the Math library.. values can be 0-1, multiplied by 36 gives values between 0 & 35.999. adding 25 gives 25-60
📄 Study AP CSA, Unit 5.6: Writing Methods

4. Which of the following best describes the value of the Boolean expression a && !(b || a)

A. The value is always true
B. The value is always false
C. The value is true when a has the value false, and is false otherwise
D. The value is true when either a or b has the value true, and is false otherwise.
Answer: && is AND ! is NOT || is OR
📄 Study AP CSA, Unit 5.6: Writing Methods

5. In the following code segment, the string str has been declared and initialized. The code segment is intended to print the number of strings in the array animals that have str as a substring. Which of the following changes should be made so the code segment works as intended?

A. The Boolean expression in the for loop header should be changed to i < animals.length
B. The Boolean expression in the for loop header should be changed to i < animals.length -1.
C. The Boolean expression in the for loop header should be changed to i < animals[i].length
D. The condition in the if statement should be changed to animals[i].equals(str).
Answer: exclued the case where i could be = to animals.length
📄 Study AP CSA, Unit 5.6: Writing Methods

6. Consider an integer array, nums, which has been declared and initialized with one or more integer values. Which of the following code segments updates nums so that each element contains the square of its original value?

A. II only
B. I and II only
C. II and III only
D. I, II, and III
Answer: definitely not III. II works (and is in every choice). Although I is close, it won't work
📄 Study AP CSA, Unit 5.6: Writing Methods

7. Which of the following represents the contents of numbers after the code segment has been executed?

A. ["one", "four", "five"]
B. ["three", "four", "two"]
C. ["three", "two", "five"]
D. ["three", "four", "five"]
Answer: "one" and "two" are replaced with "three" and "four" and then "five" added after
📄 Study AP CSA, Unit 5.6: Writing Methods

8. Consider the following method, which should return a list containing the elements of the parameter myList with all elements removed. Which of the following best explains why the code doesn't work as intended?

A. The code segment causes an IndexOutOfBoundsException for all lists because of an incorrect Boolean expression in the for loop
B. The code segment skips some elements of myList because the indexes of all subsequent elements change by one when a list element is removed.
C. The code segment causes an IndexOutOfBoundsException for lists with at least one even element because the indexes of all subsequent elements change by one when a list element is removed.
D. The code segment removes the wrong elements of myList because the condition in the if statement to test whether an element is even is incorrect
Answer: The code skips some of the elements because some of the indexes change by one when an element is removed
📄 Study AP CSA, Unit 5.6: Writing Methods

9. For the following class definitions & the code segment below, what will be printed upon execution?

A. 6
B. 68
C. 124
D. 1212
Answer: the code segment is not in the class Thing1 or Thing2
📄 Study AP CSA, Unit 5.6: Writing Methods

10. What is a class used to define?

A. a method
B. a variable
C. a static parameter
D. a return value
Answer: definition of a class
📄 Study AP CSA, Unit 5.1: Anatomy of a Class 

11. Which of the following is the best definition of a constructor?

A. a way to initialize a mutator
B. a way to initialize a list
C. a way to initialize a method
D. a way to initialize a field
Answer: definition of a constructor, also used to initialize fields in a new object
📄 Study AP CSA, Unit 5.2: Constructors 

12. In Java, what is an object?

A. An object is one element in a constructor method
B. An object names a variable that can change in a program
C. An object does the actual work in an object-oriented program
D. An object is a static variable in a program
Answer: definition of an object in an OOP
📄 Study AP CSA, Unit 5.2: Constructors

13. Which of the following is a correct call to a static method?

A. name.substring(7);
B. Math.pow (3,6);
C. circle.getDiameter();
D. student.setEmail("smith@student.dist221.org");
Answer: A static method must be called by the class & has the form: className.methodName(parameters)
📄 Study AP CSA, Unit 5.7: Static Variables and Methods

14. The following code is an example of ______?

A. general code for a mutator
B. general code for a getter
C. general code for an accessor
D. general code for a static method
Answer: a mutator is also known as a setter
📄 Study AP CSA, Unit 5.5: Mutator Methods

15. Which of the following is a decent definition for a mutator method?

A. A mutator method is accessible outside of the class
B. A mutator method is often a static method
C. A mutator method often sets the instance variable to a value in its parameter
D. A mutator method is also a getter for the method
Answer: definition of a mutator method
📄 Study AP CSA, Unit 5.5: Mutator Methods

What can we help you do now?

🔍 Study for Unit 6
🦘 Jump to AP CSA Unit 6 Multiple Choice Questions
🤝 Connect with other students studying AP CSA with Hours
Browse Study Guides By Unit
Unit 1 – Primitive Types
📱Unit 2 – Using Objects
🖥Unit 3 – Boolean Expressions & if Statements
🕹Unit 4 – Iteration
⚙️Unit 5 – Writing Classes
⌚️Unit 6 – Array
💾Unit 7 – ArrayList
💻Unit 8 – 2D Array
🖲Unit 9 – Inheritance
🖱Unit 10 – Recursion
🙏Exam Reviews

Fiveable
Fiveable
Home
Stay Connected

© 2023 Fiveable Inc. All rights reserved.


© 2023 Fiveable Inc. All rights reserved.