5.1-5.5 CodeHS AP Computer Science A Flashcards

studied byStudied by 27 people
0.0(0)
get a hint
hint

Class members that should be public are

  1. I, II, IV

  2. I, II, III

  3. I, III, V

  4. I, II, III, V

1 / 39

Tags and Description

Helps with the upcoming CodeHS Unit 5 Test. This review has units 5.1-5.5!

40 Terms

1

Class members that should be public are

  1. I, II, IV

  2. I, II, III

  3. I, III, V

  4. I, II, III, V

  1. I, III, V

New cards
2

What is the difference between a getter method and an accessor method?

  1. A getter method allows you to get the value of a field while an accessor method sets the value of the field.

  2. A getter method allows you to get the value of a field while an accessor method is not often used in Java.

  3. A getter method gets the name of the class while an accessor method gets the value of the field.

  4. There is no difference. They refer to the same idea.

.

  1. There is no difference. They refer to the same idea.

New cards
3

What is the scope of private methods and private instance variables?

  1. Any class using an object of the declaring class

  2. The declaring class

  3. The main method

  4. Only the private methods of the declaring class

  1. The declaring class

New cards
4

Accessors and mutators are used to

  1. Allow private data to be accessed outside of the class and be safely modified.

  2. Allow users to access and modify any of the class’s data in any way they wish.

  3. Prevent users from manipulating private data.

  4. Allow public data to be accessed and safely modified.

  1. Allow private data to be accessed outside of the class and be safely modified.

New cards
5

Classes’ access specifier is generally set to

  1. private to prevent any user from accessing and modifying any instance variables.

  2. private so that only certain users can create objects of the class.

  3. public so that all data and methods are accessible by any user.

  4. public so that any user can create and use objects of the class.

  1. public so that any user can create and use objects of the class.

New cards
6

Question: 1

The Glasses class represents a pair of eyeglasses. One of its instance variables is a Prescription object called script. The Prescription object has instance variables that stores the prescription for the left and right eye.
Suppose the Glasses constructor was implemented as follows

    public Glasses(Prescription thePrescription)
{
script = thePrescription;
}

What is the output of the following code snippet?

// Create a new Prescription object
Prescription reading = new Prescription(-1.25, -1.5);

// Create two new Glasses objects
Glasses myGlasses = new Glasses(reading);
Glasses yourGlasses = new Glasses(reading);

// Mutator for the prescription to change the prescription
reading.updatePrescription(-1.5, -1.5);

// Prints the glasses' prescription as
// (Left Eye, Right Eye) e.g. (-2.0, -2.5)
myGlasses.printPrescription();
yourGlasses.printPrescription();

  1. (-1.25, -1.5)
    (-1.5, -1.5)

  2. (-1.5, -1.5)
    (-1.5, -1.5)

  3. (-1.25, -1.5)
    (-2.0, -2.5)

  4. (-1.25, -1.5)
    (-1.25, -1.5)

  1. (-1.5, -1.5)

    (-1.5, -1.5)

New cards
7

An object’s state is defined by the object’s

  1. methods and instance variables

  2. instance variables and their values

  3. access modifiers of the instance variables

  4. methods and their return values

  1. instance variables and their values

New cards
8

If you do not implement a constructor for your class,

  1. you cannot create objects of that class type

  2. you can create objects, but you cannot modify the instance variables’ values

  3. you can only use the class’s static methods

  4. Java creates one for you and gives all instance variables default values

  1. Java creates one for you and gives all instance variables default values

New cards
9

The Glasses class represents a pair of eyeglasses. One of its instance variables is a Prescription object called script. The Prescription object has instance variables that stores the prescription for the left and right eye.

Which of the following constructors for the Glasses class correctly sets the script instance variable correctly? Assume any method calls are valid.

  1. public Glasses(Prescription thePrescription)
    {
    script = thePrescription;
    }

  2. public Glasses(Prescription thePrescription)
    {
    script = new Prescription(thePrescription.getLeft(),thePrescription.getRight());
    }

  3. public Glasses(Prescription thePrescription)
    {
    script = Prescription(thePrescription.getLeft(),thePrescription.getRight());
    }

  4. public Glasses(Prescription thePrescription)
    {
    script = new Prescription(getLeft(), getRight());
    }

  1. public Glasses(Prescription thePrescription)

    {

    script = new Prescription(thePrescription.getLeft(),thePrescription.getRight());

    }

New cards
10

The purpose of specifying a postcondition is to

  1. specify the types of objects the method accepts.

  2. explain the method’s end result, whether it is a return value or change in an object’s state

  3. state the return value’s type.

  4. set the method’s expectations before the method is executed with respect to the parameters or object’s state

  1. explain the method’s end result, whether it is a return value or change in an object’s state

New cards
11

Good programmers use comments to

  1. Tell the compiler what they want the code to do

  2. Make their code more readable by explaining particular chunks of code

  3. Make their code more readable by explaining what every line does

  4. Amuse their friends by adding funny jokes in their code

  1. Make their code more readable by explaining particular chunks of code

New cards
12

What is the output of the following code snippet?

int x = 8;
int y = 10;

// x = y + 10;

y *= 2;

/*
y = x;
x *= 3;
*/

System.out.println("x: " + x);
System.out.println("y: " + y);

  1. x: 8
    y: 20

  2. x: 18
    y: 20

  3. x: 24
    y: 8

  4. This code does not compile.

  1. x: 8
    y: 20

New cards
13

The purpose of specifying a precondition is to

  1. specify the types of objects the method accepts.

  2. explain the method’s end result, whether it is a return value or change in an object’s state

  3. state the return value’s type.

  4. set the method’s expectations before the method is executed with respect to the parameters or object’s state

  1. set the method’s expectations before the method is executed with respect to the parameters or object’s state.

New cards
14

The return type of an accessor method

  1. is void

  2. must be a user defined class

  3. must be a primitive

  4. must match the type of the instance variable being accessed

  1. must match the type of the instance variable being accessed

New cards
15

Which of the following is true?

  1. Both primitives and objects are returned by value.

  2. Both primitives and objects are returned by reference.

  3. Primitives are returned by value. Objects are returned by reference.

  4. Objects are returned by value. Primitives are returned by reference.

  1. Primitives are returned by value. Objects are returned by the reference.

New cards
16

The purpose of an accessor method is

  1. to modify an instance variable

  2. to return the value of an instance variable

  3. to return the values of all of the instance variables

  4. to allow the user to have direct access to an instance variable

  1. to return the value of an instance variable

New cards
17

The return type of a mutator method

  1. is usually void

  2. must match the type of of the instance variable being mutated

  3. must be a primitive

  4. must be a user defined class

  1. is usually void

New cards
18

Which of the following is a benefit of using a mutator method?

  1. Only methods inside the class can change an instance variable’s value

  2. Only methods outside of the class can change an instance variable’s value

  3. Mutator methods can verify the new value is a valid value for the instance variable

  4. The user can change all of the instance variables using the same mutator method

  1. Mutator methods can verify the new value is a valid value for the instance variable

New cards
19

Which of the following is NOT a characteristic of a mutator?

  1. The method changes the value of an instance variable to a user’s specified value

  2. The method’s name (usually) starts with set

  3. The method updates an instance variable’s value

  4. The method returns the value of an instance variable

  1. The method returns the value of an instance variable

New cards
20

The purpose of a mutator method is

  1. to return the value of an instance variable

  2. to safely modify an instance variable

  3. to return the values of all of the instance variables

  4. to allow the user to have direct access to an instance variable

  1. to safely modify an instance variable

New cards
21

Each of the methods below can be found in the Rectangle class. Which of the following methods would have access to the parameter object’s private data and methods?

  1. public void setWidth(Integer newWidth)

  2. public void copy(Rectangle other)

  3. public void copy(Triangle other)

  4. public void setLabel(String newLabel)

  1. public void copy(Rectangle other)

New cards
22

Which of the following types can be permanently modified in a method when it is passed as a parameter to a method?

  1. int

  2. double

  3. String

  4. Any user defined class

  1. Any user defined class

New cards
23

It is considered good practice to

  1. modify objects in a method whenever you want to

  2. only modify objects when the method postcondition has specified the modification

  3. modify objects without telling the user

  4. never pass objects as parameters

  1. only modify objects when the method postcondition has specified the modification

New cards
24

Suppose you have the following methods defined

public double increaseWidth(double num){
num += 10;
return num;
}

public double increaseWidth(Rectangle rect){
double newWidth = rect.getWidth() * 2;
rect.setWidth(newWidth);

return newWidth;
}

Consider this code snippet

double width = 10.5;
Rectangle room = new Rectangle(10, 15);

System.out.println(width);
System.out.println(room);

increaseWidth(width);
increaseWidth(room);

System.out.println(width);
System.out.println(room);

Which variable will have a different value the second time it is printed? Assume the toString method of Rectangle prints the rectangle’s dimensions.

  1. width

  2. room

  3. Both width and room

  4. Neither width nor room

  1. room

New cards
25

What is the difference between instance variables and static variables?

  1. Each object has its own copy of the instance variables, but all objects share a copy of the static variables

  2. Each object has its own copy of the static variables, but all objects share a copy of the instance variables

  3. Instance variables can be public or private, but static variables must be public

  4. Static variables can be public or private, but instance variables must be private

  1. Each object has its own copy of the instance variables, but all objects share a copy of the static variables

New cards
26

Where must a static variable be initialized?

  1. In the constructor

  2. In a static method.

  3. Outside of the class.

  4. In the class file, but not in a method.

  1. In the class file, but not in a method.

New cards
27

Static methods can access

  1. All instance variables and class methods

  2. Any class method, but not the instance variables

  3. Only other static instance variables and class methods

  4. Only private instance variables and private class methods

  1. Only other static instance variables and class methods

New cards
28

Which variables are in scope at the point labeled // POINT A? In other words, which variables exist at that point in the code?

public class ScopeQuiz
{
private int count = 0;

public void printPointSums()
{
int sum = 0;

// POINT A

for(int i = 0; i < 10; i++)
{
sum += i;
}

int result = sum + count;
}
}

  1. Only sum

  2. sum and count

  3. sum, count, and i

  4. sum and result

  5. sum, count, i, and result

  1. sum and count

New cards
29

What would be printed the first time printPhrases was called from main?

public class PrintQuestion
{
private static String phrase = "Hello World!";

public static void printPhrases()
{
String phrase = "hi";
System.out.println(phrase);
phrase = "hello";
}
}

  1. Hello World!

  2. hi

  3. hello

  4. Nothing, this program will not run because it is not possible to have two variables with the same name.

  1. hi

New cards
30

What would the value of the static variable phrase be after the first time printPhrases was called from main?

public class PrintQuestion
{
private static String phrase = "Hello World!";

public static void printPhrases()
{
String phrase = "hi";
System.out.println(phrase);
phrase = "hello";
}
}

  1. Hello World!

  2. hi

  3. hello

  4. Nothing, this program will not run because it is not possible to have two variables with the same name.

  1. Hello World!

New cards
31

Suppose there is a local variable declared in the method of a particular class. The local variable’s scope is

  1. the file in which it is declared

  2. any method in which an object of the class is used

  3. the main method

  4. the method in which is it declared

  1. the method in which is it declared

New cards
32

The following function computes the hypotenuse of a right triangle.

public double findHypotenuse(int side1, int side2){ 
double hyp = Math.sqrt(side1 * side1 + side2 * side2);
return hyp;
}

The following code found in main throws an error. What is the bug?

int side1 = 3;
int side2 = 4;

findHypotenuse(side1, side2);

System.out.println(hyp);

  1. Formal parameters and actual arguments cannot have the same name.

  2. hyp has to be accessed by using findHypotenuse.hyp

  3. hyp is a local variable. It can only be accessed in findHypotenuse.

  4. That’s a lie. The code works just fine and there is no bug.

  1. hyp is a local variable. It can only be accessed in findHypotenuse.

New cards
33

What is the this keyword used to reference?

  1. The this keyword references the method that was called.

  2. The this keyword references the current class.

  3. The this keyword references the object that called the method.

  4. The this keyword references the instance variable that a local variable shadows.

  1. The this keyword references the object that called the method.

New cards
34

Which of the following is NOT a proper use of the keyword this?

  1. to access variables of the calling object

  2. as an argument to other methods in the class

  3. to access the private variables of other objects of the same class

  4. to call methods of the class on the calling object

  1. to access the private variables of other objects of the same class

New cards
35

Why can’t this be used in static methods?

  1. Static methods are not called using an object. Thus, this would be null.

  2. this can only be used to reference instance variables.

  3. Static methods must be public, and this can only be used in private methods.

  4. this must be initialized in the constructor. Since static methods can be called before calling the constructor, this was never initialized.

  1. Static methods are not called using an object. Thus, this would be null.

New cards
36

Which of the following correctly uses the this keyword?

  1. public boolean isSame(Bird other)

    {

    return this.name.equals(this.other.name);

    }

  2. public boolean isSame(Bird other)

    {

    return this.name.equals(other.this.name);

    }

  3. public boolean isSame(Bird other)

    {

    return name.this.equals(other.name);

    }

  4. public boolean isSame(Bird other)

    {

    return this.name.equals(other.name);

    }

  1. public boolean isSame(Bird other)

    {

    return this.name.equals(other.name);

    }

New cards
37

True or False: All impacts of advances in computing systems are beneficial.

  1. True

  2. False

  1. False

New cards
38

A computing system that is biased is likely to

  1. treat all users equally

  2. unfairly prefer a certain kind of user over other kinds of users

  3. crash repeatedly

  4. be created by a large corporate organization

  1. unfairly prefer a certain kind of user over other kinds of users

New cards
39

According to the ACM’s Code of Ethics, computing professionals should do all of the following EXCEPT

  1. Be honest and trustworthy

  2. Be fair and not discriminate

  3. Respect privacy

  4. Teach computing classes

  1. Teach computing classes

New cards
40

In 2016 Volkswagen used software to give false results on their vehicle emissions tests. Basically, they falsified results to show a “pass” when it would otherwise “fail” under normal conditions. Which of the following ACM ethical principles was most compromised by this action?

  1. Be honest and trustworthy.

  2. Be fair and take action not to discriminate.

  3. Respect the work required to produce new ideas, inventions, creative works, and computing artifacts.

  4. Respect privacy.

  1. Be honest and trustworthy.

New cards

Explore top notes

note Note
studied byStudied by 14 people
Updated ... ago
4.5 Stars(2)
note Note
studied byStudied by 5 people
Updated ... ago
5.0 Stars(1)
note Note
studied byStudied by 6 people
Updated ... ago
5.0 Stars(1)
note Note
studied byStudied by 1 person
Updated ... ago
5.0 Stars(1)
note Note
studied byStudied by 80 people
Updated ... ago
4.3 Stars(4)
note Note
studied byStudied by 5 people
Updated ... ago
5.0 Stars(1)
note Note
studied byStudied by 7 people
Updated ... ago
5.0 Stars(1)
note Note
studied byStudied by 38 people
Updated ... ago
5.0 Stars(1)

Explore top flashcards

flashcards Flashcard30 terms
studied byStudied by 17 people
Updated ... ago
5.0 Stars(1)
flashcards Flashcard110 terms
studied byStudied by 26 people
Updated ... ago
5.0 Stars(1)
flashcards Flashcard140 terms
studied byStudied by 2 people
Updated ... ago
5.0 Stars(1)
flashcards Flashcard47 terms
studied byStudied by 2 people
Updated ... ago
5.0 Stars(1)
flashcards Flashcard132 terms
studied byStudied by 21 people
Updated ... ago
5.0 Stars(1)
flashcards Flashcard68 terms
studied byStudied by 34 people
Updated ... ago
5.0 Stars(1)
flashcards Flashcard59 terms
studied byStudied by 38 people
Updated ... ago
5.0 Stars(1)
flashcards Flashcard52 terms
studied byStudied by 25 people
Updated ... ago
5.0 Stars(1)