Module 7 Flashcards
Select all that are true about classes.
A) A class is another name for an object
B) Classes encapsulate definitions of data
C) Classes are created from objects
D) Classes encapsulate behavior or methods
E) Classes contain actual values stored in instance variables
B) D)
A class encapsulates the definitions of the data, the instance variables, and the behavior, the methods. Objects are created from classes and data, stored in instance variables are in objects not classes.
Which are true about mutator and accessor methods?
A) They can be inherited by child classes.
B) A method named getName would likely be an accessor method.
C) They are usually set to be private so they can be used by the class to update or retrieve the data stored in the class.
D) A mutator method is used by another class to retrieve data from an object.
E) The accessor and mutator methods of a parent class must be included in the definition of a child class.
A) B)
Mutator and Accessor methods are public methods used by outside classes to change and retrieve data. Mutators are also called set methods because they are used to change the values stored in the private instance variables of a class. Accessors are also called get methods because they are used to retrieve data from the private instance variables. Parent class accessors and mutators are inherited by a child class and do not have to be repeated in child class definitions.
Private instances variables within an object can have their values established by which means?
A) Directly by referencing the variable from code outside of the object
B) By mutator methods
C) By accesor methods
D) By code in a constructor
E) By code from within the object
B) D) E)
Instance variables are usually modified by mutator methods however they can be modified by code in the constructor or any
other method within the object. They cannot be modified by code from outside the object.
Which of the following are true about the parent class in an inheritance relationship?
A) The keyword “extends” is used to specify which classes are parent classes.
B) The parent class does not contain child specific attributes and methods.
C) A parent class often has a defined constructor.
D) The parent class contains all attributes and methods from each child class.
E) The parent class, rather than the child class, is instantiated.
B) C)
How has object oriented programming promoted code reusability?
A) Classes are written to represent a specific service that might be needed over and over again.
B) Object oriented programming languages keep the data separate from the procedures making it easier to reuse procedures.
C) Objects are designed so they can be easily modified without affecting other code in a program which promotes code reuse.
D) Object oriented programming makes it easy to hide data and prevent accidental corruption from outside procedures.
E) Object oriented programming is designed so the methods are general purpose and can be used by many objects.
A) C) D)
Object oriented programming is designed so that data and methods are stored together in classes. Methods are written to do a specific job for a class.
Assuming that an instance variable is declared as private which of the following techniques can be used to change its value?
A) Have a method within the object change its value
B) Invoke an accessor method
C) Pass a value via the constructor which will store it in the variable
D) Invoke a mutator method
E) Reference it directly by name
A) C) D)
Private instance variables can be changed via a constructor, mutator method, or any other method within the object.
Which of the following are true about the toString method?
A) It returns a string constructed within the method
B) It returns the object in which it is defined
C) It prints information about an object
D) It is called when an object is created
E) It is automatically invoked when an object is “printed”
A) E)
When an object is printed the toString method is automatically invoked. It returns a string.
Select all of the following that are true about constructors.
A) You must always define a constructor for every class
B) Constructors are very often used to initialize instance variables
C) There can be many constructors defined within a single class
D) A constructor is a method with the same name as the class
D) If you don’t define a constructor no constructor will be executed when you create an object
B) C) D)
Constructors have the same name as the class and there can be many of them defined within a single class. Constructors executed when an object of the class is created and are often used to initialize instance variables. If you do not define a constructor the default constructor is executed.
Which of the following Java statements should be defined in most new Java classes you write?
A) Public accessor methods
B) Instance variables
C) Private mutator methods
D) a constructor method with the super keyword
E) a toString method
A) B) E)
Which of the following are true about the default constructor?
A) It sets integer fields to 0.
B) It is the only way to create an object from a class.
C) It doesn’t have any parameters.
D) It sets the class’s reference variables to a default value defined by the programmer.
E) It must be defined in the class body.
A) C)
Constructors use default values set by Java for each data type to instantiate an object from a class. A programmer can define other constructors in a class definition that can be used to instantiate (create) an object from a class. Default constructors are not defined in the class body.
Which of the following code snippets show a properly written constructor method header for a class named Book?
A) public void Book (String name, int ISBN)
B) public Book (String name)
C) public Book (String name, int ISBN)
D) public Ebook (String name, int ISBN)
E) public void book (String name, int ISBN)
B) C)
Which of the following are true about Java classes?
A) Classes define methods and attributes
B) A class is a blueprint of the description of an object
C) Classes are created from objects
D) All the attributes in a class have specific values stored in them
E) A program can only have one class
A) B)
Programmers must define all the attributes and methods that comprise an object. These definitions and programming statements appear in a programming construct called a class. A class can be viewed as a blueprint or a description of an object. A given program may consist of many classes. When the program is run it will create one or more objects from each class.
Given the following Java code, which are true statements?
public BaseballPlayer (double RBI){
super(name, number);
battingAvg = RBI;
}
A) The class called BaseballPlayer is a child class.
B) This is a mutator method definition inside the class called BaseballPlayer.
C) An object instantiated from the BaseballPlayer class would have a private instance variable called RBI.
D) This constructor method definition belongs to a class named baseballplayer.
E) The private instance variables, name and number, are inherited by the class called BaseballPlayer from a parent class.
A) E)
This code represents a constructor method definition for a class called BaseballPlayer. The call to the super constructor method indicates that BaseballPlayer is a child class in an inheritance relationship and the parent class contains two private instance variables called name and number which will be inherited. The name of the private instance variable for this class is battingAvg, not RBI. The value passed into the parameter RBI when the constructor is called will be saved in the instance variable called battingAvg. Constructors must have the same name as their class’ name, and Java names are case-sensitive.
Select all of the following that are true about what happens when an object is created.
A) A method with the same name as the class is sometimes executed within the object
B) A constructor is executed
C) The toString method is executed
D) Instance variables are initialized
E) A reference to the object is returned
A) B) D) E)
When an object is created either the default constructor or a constructor defined within the class is executed. Instance variables are initialized and a reference to the object is returned.
What does a Java class definition contain?
A) instance variables
B) get and set methods
C) constructors
D) computation methods
A) B) C) D)
Class definitions contain definitions of: instance variables, constructors, get and set methods, and computation methods.