Module 7 Flashcards

1
Q

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

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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.

A

B) C)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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

A) C) D)

Private instance variables can be changed via a constructor, mutator method, or any other method within the object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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

A) E)

When an object is printed the toString method is automatically invoked. It returns a string.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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

A) B) E)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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)

A

B) C)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does a Java class definition contain?

A) instance variables
B) get and set methods
C) constructors
D) computation methods

A

A) B) C) D)

Class definitions contain definitions of: instance variables, constructors, get and set methods, and computation methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

If a private variable is declared inside a class but not inside a method, where can the variable be used without an accessor method?

A) in the main method of the driver program
B) in a child class’s method
C) in a constructor defined in the same class
D) inside a method defined in the same class
E) as a method parameter in the same class

A

C) D) E)

17
Q

Which of the following are true about java objects?

A) Once attributes in an objecte are initialized all methods within that object have access to that data
B) Objects only contain methods
C) The attributes in an object contain specific values
D) Only one object can be instantiated from a given class
E) Objects are instantiated from classes

A

A) C) E)

An object is instantiated from a class. Objects contain both data (attributes) and procedures (methods). When the object is instantiated an initial values of attributes will most likely be specified and stored in the object. Thereafter, any of the methods within the object may be invoked and have access to these attributes It is possible to have many objects, all derived from the same class.

18
Q

If we have multiple constructors for a given class, which one will be executed when an object is created?

A) We will specify the unique signature of the constructor we want
B) They will have different names and we will specify the one we want
C) Multiple constructors are not allowed
D) We pass an argument to the constructor which is used to determine which constructor is executed
E) All of them are executed in the order in which they are defined in the class

A

A)

A constructor method must have the same name as the class. A class can have multiple constructors as long as their signatures, or parameter lists, vary. In this case, to execute a specific constructor it must be called with the correct argument list.

19
Q

Which of the following are true about the procedural programming paradigm?

A) Objects are used
B) Data is centralized
C) Data is separate from the code that processes it
D) Data resides in procedures
E) Programs are comprised of a series of procedures

A

B) C) E)

In procedural programming paradigm all data was centralized, programs were comprised of a series of procedures (also called subprograms or functions), and the data was passed back and forth for processing among the procedures. Data was separate from the code that processed that data.

20
Q

The private keyword when used as an access modifier for an instance variable:

A) Defines a primitive data type
B) Is used to define a constant
C) Ensures that the variable is only accessible from within the class
D) Allows the variable to be accessed from other classes
E) Ensures that the variable is only accessed from private methods

A

C)

The private keyword is an access modifier which enforces that the value of this variable is only accessible from within the class and not from outside the class.

21
Q

How are instance variables hidden within an object so that they cannot be referenced from outside the object?

A) They are named using special identifiers
B) They are not given specific values
C) They are declared as private
D) They are not declared

A

C)

Instance variables are hidden from methods outside of the object by declaring them as private.

22
Q

Which of the following are true about a child class in an inheritance relationship?

A) The child class, rather than the parent, is instantiated
B) There is a relationship between the child and the parent
C) The parent class contains all attributes and methods that are in common among all child classes
D) There is a relationship between all the children
E) The parent includes all the attributes and methods of the child

A

A) B) C)

The child is related to the parent and includes all the attributes and methods of the parent. Objects are created from the child class, not the parent. The role of the parent is to contain all the definitions of attributes and methods that are common to all the child classes.

23
Q

Objects are created from classes by using which operator?

A) New
B) Object
C) Open
D) Create

A

A)

The new operator is used to create an object from a class