Ap Comp Sci -A - Unit 5 Review

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

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.

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 Answered

- Any user defined class

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.

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 Answered

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

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

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 Answered

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

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

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.

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

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

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.

What is the difference between gette 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.

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

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

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 Answered

- is usually void

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

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

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.

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

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

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

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

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 Answered

- to return 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

Glasses

-1.5 all

Class members that should be public are I. Constructors II. Instance Variables III. Accessors/Mutators IV. Methods the class uses for itself, but the user does not need V. Methods the user needs to manipulate the objects of the class

I, III, V

john

john


Ensembles d'études connexes

Multiple Sclerosis In Class Assignment

View Set

Meeeeerkat (Topic 4 Troubleshooting)

View Set

Exam 3 Nursing psych/mental health

View Set

Intro chem lab exam 2 freezing point, hydrates, and nomenclature

View Set

ECONOMICS FINAL STUDY GUIDE PART 1

View Set

United States History Semester 1 Exam

View Set

consumer behavior final exam quizzes

View Set

NCMA CPCM 1.2 Contract Principles

View Set