Helps with the upcoming CodeHS Unit 5 Test. This review has units 5.1-5.5!
Class members that should be public are
I, II, IV
I, II, III
I, III, V
I, II, III, V
I, III, V
What is the difference between a getter method and an accessor method?
A getter method allows you to get the value of a field while an accessor method sets the value of the field.
A getter method allows you to get the value of a field while an accessor method is not often used in Java.
A getter method gets the name of the class while an accessor method gets the value of the field.
There is no difference. They refer to the same idea.
.
There is no difference. They refer to the same idea.
What is the scope of private methods and private instance variables?
Any class using an object of the declaring class
The declaring class
The main method
Only the private methods of the declaring class
The declaring class
Accessors and mutators are used to
Allow private data to be accessed outside of the class and be safely modified.
Allow users to access and modify any of the class’s data in any way they wish.
Prevent users from manipulating private data.
Allow public data to be accessed and safely modified.
Allow private data to be accessed outside of the class and be safely modified.
Classes’ access specifier is generally set to
private to prevent any user from accessing and modifying any instance variables.
private so that only certain users can create objects of the class.
public so that all data and methods are accessible by any user.
public so that any user can create and use objects of the class.
public so that any user can create and use objects of the class.
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.25, -1.5)
(-1.5, -1.5)
(-1.5, -1.5)
(-1.5, -1.5)
(-1.25, -1.5)
(-2.0, -2.5)
(-1.25, -1.5)
(-1.25, -1.5)
(-1.5, -1.5)
(-1.5, -1.5)
An object’s state is defined by the object’s
methods and instance variables
instance variables and their values
access modifiers of the instance variables
methods and their return values
instance variables and their values
If you do not implement a constructor for your class,
you cannot create objects of that class type
you can create objects, but you cannot modify the instance variables’ values
you can only use the class’s static methods
Java creates one for you and gives all instance variables default values
Java creates one for you and gives all instance variables default values
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.
public Glasses(Prescription thePrescription)
{
script = thePrescription;
}
public Glasses(Prescription thePrescription)
{
script = new Prescription(thePrescription.getLeft(),thePrescription.getRight());
}
public Glasses(Prescription thePrescription)
{
script = Prescription(thePrescription.getLeft(),thePrescription.getRight());
}
public Glasses(Prescription thePrescription)
{
script = new Prescription(getLeft(), getRight());
}
public Glasses(Prescription thePrescription)
{
script = new Prescription(thePrescription.getLeft(),thePrescription.getRight());
}
The purpose of specifying a postcondition is to
specify the types of objects the method accepts.
explain the method’s end result, whether it is a return value or change in an object’s state
state the return value’s type.
set the method’s expectations before the method is executed with respect to the parameters or object’s state
explain the method’s end result, whether it is a return value or change in an object’s state
Good programmers use comments to
Tell the compiler what they want the code to do
Make their code more readable by explaining particular chunks of code
Make their code more readable by explaining what every line does
Amuse their friends by adding funny jokes in their code
Make their code more readable by explaining particular chunks of code
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);
x: 8
y: 20
x: 18
y: 20
x: 24
y: 8
This code does not compile.
x: 8
y: 20
The purpose of specifying a precondition is to
specify the types of objects the method accepts.
explain the method’s end result, whether it is a return value or change in an object’s state
state the return value’s type.
set the method’s expectations before the method is executed with respect to the parameters or object’s state
set the method’s expectations before the method is executed with respect to the parameters or object’s state.
The return type of an accessor method
is void
must be a user defined class
must be a primitive
must match the type of the instance variable being accessed
must match the type of the instance variable being accessed
Which of the following is true?
Both primitives and objects are returned by value.
Both primitives and objects are returned by reference.
Primitives are returned by value. Objects are returned by reference.
Objects are returned by value. Primitives are returned by reference.
Primitives are returned by value. Objects are returned by the reference.
The purpose of an accessor method is
to modify an instance variable
to return the value of an instance variable
to return the values of all of the instance variables
to allow the user to have direct access to an instance variable
to return the value of an instance variable
The return type of a mutator method
is usually void
must match the type of of the instance variable being mutated
must be a primitive
must be a user defined class
is usually void
Which of the following is a benefit of using a mutator method?
Only methods inside the class can change an instance variable’s value
Only methods outside of the class can change an instance variable’s value
Mutator methods can verify the new value is a valid value for the instance variable
The user can change all of the instance variables using the same mutator method
Mutator methods can verify the new value is a valid value for the instance variable
Which of the following is NOT a characteristic of a mutator?
The method changes the value of an instance variable to a user’s specified value
The method’s name (usually) starts with set
The method updates an instance variable’s value
The method returns the value of an instance variable
The method returns the value of an instance variable
The purpose of a mutator method is
to return the value of an instance variable
to safely modify an instance variable
to return the values of all of the instance variables
to allow the user to have direct access to an instance variable
to safely modify an instance variable
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?
public void setWidth(Integer newWidth)
public void copy(Rectangle other)
public void copy(Triangle other)
public void setLabel(String newLabel)
public void copy(Rectangle other)
Which of the following types can be permanently modified in a method when it is passed as a parameter to a method?
int
double
String
Any user defined class
Any user defined class
It is considered good practice to
modify objects in a method whenever you want to
only modify objects when the method postcondition has specified the modification
modify objects without telling the user
never pass objects as parameters
only modify objects when the method postcondition has specified the modification
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.
width
room
Both width and room
Neither width nor room
room
What is the difference between instance variables and static variables?
Each object has its own copy of the instance variables, but all objects share a copy of the static variables
Each object has its own copy of the static variables, but all objects share a copy of the instance variables
Instance variables can be public or private, but static variables must be public
Static variables can be public or private, but instance variables must be private
Each object has its own copy of the instance variables, but all objects share a copy of the static variables
Where must a static variable be initialized?
In the constructor
In a static method.
Outside of the class.
In the class file, but not in a method.
In the class file, but not in a method.
Static methods can access
All instance variables and class methods
Any class method, but not the instance variables
Only other static instance variables and class methods
Only private instance variables and private class methods
Only other static instance variables and class methods
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;
}
}
Only sum
sum
and count
sum
, count
, and i
sum
and result
sum
, count
, i
, and result
sum and count
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";
}
}
Hello World!
hi
hello
Nothing, this program will not run because it is not possible to have two variables with the same name.
hi
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";
}
}
Hello World!
hi
hello
Nothing, this program will not run because it is not possible to have two variables with the same name.
Hello World!
Suppose there is a local variable declared in the method of a particular class. The local variable’s scope is
the file in which it is declared
any method in which an object of the class is used
the main
method
the method in which is it declared
the method in which is it declared
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);
Formal parameters and actual arguments cannot have the same name.
hyp
has to be accessed by using findHypotenuse.hyp
hyp
is a local variable. It can only be accessed in findHypotenuse
.
That’s a lie. The code works just fine and there is no bug.
hyp
is a local variable. It can only be accessed in findHypotenuse
.
What is the this
keyword used to reference?
The this
keyword references the method that was called.
The this
keyword references the current class.
The this
keyword references the object that called the method.
The this
keyword references the instance variable that a local variable shadows.
The this
keyword references the object that called the method.
Which of the following is NOT a proper use of the keyword this
?
to access variables of the calling object
as an argument to other methods in the class
to access the private variables of other objects of the same class
to call methods of the class on the calling object
to access the private variables of other objects of the same class
Why can’t this
be used in static methods?
Static methods are not called using an object. Thus, this
would be null.
this
can only be used to reference instance variables.
Static methods must be public, and this
can only be used in private methods.
this
must be initialized in the constructor. Since static methods can be called before calling the constructor, this
was never initialized.
Static methods are not called using an object. Thus, this
would be null.
Which of the following correctly uses the this keyword?
public boolean isSame(Bird other)
{
return this.name.equals(this.other.name);
}
public boolean isSame(Bird other)
{
return this.name.equals(other.this.name);
}
public boolean isSame(Bird other)
{
return name.this.equals(other.name);
}
public boolean isSame(Bird other)
{
return this.name.equals(other.name);
}
public boolean isSame(Bird other)
{
return this.name.equals(other.name);
}
True or False: All impacts of advances in computing systems are beneficial.
True
False
False
A computing system that is biased is likely to
treat all users equally
unfairly prefer a certain kind of user over other kinds of users
crash repeatedly
be created by a large corporate organization
unfairly prefer a certain kind of user over other kinds of users
According to the ACM’s Code of Ethics, computing professionals should do all of the following EXCEPT
Be honest and trustworthy
Be fair and not discriminate
Respect privacy
Teach computing classes
Teach computing classes
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?
Be honest and trustworthy.
Be fair and take action not to discriminate.
Respect the work required to produce new ideas, inventions, creative works, and computing artifacts.
Respect privacy.
Be honest and trustworthy.