BP CH 7 - Classes & Objects

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Software Engineering with private Instance Variables and public set and get Methods 7.2.6 Q1: Declaring instance variables ________ is known as data hiding or information hiding. a. secure b. private c. static d. masked

ANS: b. private.

When implementing a method, use the class's set and get methods to access the class's ________ data. a. public. b. private. c. protected. d. All of the above.

ANS: b. private.

AccountTest Class to Use Class Account 7.5.2 Q1: The format specifier ________ is used to output values of type float or double. a. %f b. %d c. %fd d. %r

ANS: a. %f

When a method terminates, the values of its local variables are ________. a. saved b. copied c. restored d. lost

ANS: d. lost

Default and Explicit Initialization for Instance Variables 7.3 Q1: Which of the following statements is false? a. A floating-point number is a number with a decimal point. b. Java provides two primitive types for storing floating-point numbers in memory—float and double. c. Variables of type float represent single-precision floating-point numbers and have seven significant digits. d. Variables of type double represent double-precision floating-point numbers; these require twice as much memory as float variables and provide 14 significant digits.

ANS: Variables of type double represent double-precision floating-point numbers; these require twice as much memory as float variables and provide 14 significant digits. Actually, variables of type double provide 15 significant digits.

What happens when this is used in a constructor's body to call another constructor of the same class if that call is not the first statement in the constructor? a. A compilation error occurs. b. A runtime error occurs. c. A logic error occurs. d. Nothing happens. The program compiles and runs.

ANS: a. A compilation error occurs.

Account Class with an Instance Variable, a set Method and a get Method 7.2.1 Q1: Which of the following statements is false? a. Each class declaration that begins with the access modifier private must be stored in a file that has the same name as the class and ends with the .java filename extension. b. Every class declaration contains keyword class followed immediately by the class's name. c. Class, method and variable names are identifiers. d. An object has attributes that are implemented as instance variables and carried with it throughout its lifetime.

ANS: a. Each class declaration that begins with the access modifier private must be stored in a file that has the same name as the class and ends with the .java filename extension. private should be public

Which of the following statements is true? a. Methods and instance variables can both be either public or private. b. Information hiding is achieved by restricting access to class members via keyword public. c. The private members of a class are directly accessible to the clients of a class. d. None of the above is true.

ANS: a. Methods and instance variables can both be either public or private.

Default and No-Argument Constructors 8.6 Q1: Which statement is false? a. The compiler always creates a default constructor for a class. b. If a class's constructors all require arguments and a program attempts to call a no-argument constructor to initialize an object of the class, a compilation error occurs. c. A constructor can be called with no arguments only if the class does not have any constructors or if the class has a public no-argument constructor. d. None of the above.

ANS: a. The compiler always creates a default constructor for a class.

Compiling and Executing an App with Multiple Classes 7.2.3 Q1: Which of the following statements is false? a. The javac command can compile multiple classes at once; simply list the source-code filenames after the command with each filename separated by a comma from the next. b. If the directory containing the app includes only one app's files, you can compile all of its classes with the command javac *.java. c. The asterisk (*) in javac *.java indicates that all files in the current directory ending with the filename extension ".java" should be compiled. d. All of the above are true.

ANS: a. The javac command can compile multiple classes at once; simply list the source-code filenames after the command with each filename separated by a comma from the next. Actually, each file name should be separated by a space.

Account Class: Initializing Objects with Constructors 7.4 Q1: Java requires a ________ call for every object that's created. a. constructor b. destructor c. parameterless d. parameterized

ANS: a. constructor.

Declaring an Account Constructor for Custom Object Initialization 7.4.1 Q1: Which of the following statements is true? a. Constructors can specify parameters and return types. b. Constructors can specify parameters but not return types. c. Constructors cannot specify parameters but can specify return types. d. Constructors can specify neither parameters nor return types.

ANS: b. Constructors can specify parameters but not return types.

Which of the following statements is false? a. Scanner method next reads characters until any white-space character is encountered, then returns the characters as a String. b. To call a method of an object, follow the object name with a comma, the method name and a set of parentheses containing the method's arguments. c. A class instance creation expression begins with keyword new and creates a new object. d. A constructor is similar to a method but is called implicitly by the new operator to initialize an object's instance variables at the time the object is created.

ANS: b. To call a method of an object, follow the object name with a comma, the method name and a set of parentheses containing the method's arguments. To call a method of an object, follow the object name with a dot separator (.), the method name and a set of parentheses containing the method's arguments.

The static method ________ of class String returns a formatted String. a. printf. b. format. c. formatString. d. toFormatedString.

ANS: b. format.

Composition is sometimes referred to as a(n) ________. a. is-a relationship b. has-a relationship c. many-to-one relationship d. one-to-many relationship

ANS: b. has-a relationship.

static Import 8.12 Q1: Which syntax imports all static members of class Math? a. import java.lang.Math.*. b. import static java.lang.Math.*. c. import static java.lang.Math. d. None of the above.

ANS: b. import static java.lang.Math.*.

A programmer-defined constructor that has no arguments is called a(n) ________. a. empty constructor. b. no-argument constructor. c. default constructor. d. null constructor.

ANS: b. no-argument constructor.

Enum Types 8.9 Q1: Which statement is false? a. An enum declaration is a comma-separated list of enum constants and may optionally include other components of traditional classes, such as constructors, fields and methods. b. Any attempt to create an object of an enum type with operator new results in a compilation error. c. An enum constructor cannot be overloaded. d. enum constants are implicitly final and static.

ANS: c. An enum constructor cannot be overloaded.

final Instance Variables 8.13 Q1: Instance variables declared final do not or cannot: a. Cause syntax errors if used as a left-hand value. b. Be initialized. c. Be modified after they are initialized. d. None of the above.

ANS: c. Be modified.

Which of the following statements is false? a. Most classes you'll use in Java programs must be imported explicitly. b. There's a special relationship between classes that are compiled in the same directory. By default, such classes are considered to be in the same package—known as the default package. c. Classes in the same package are implicitly imported into main. d. An import declaration is not required when one class in a package uses another in the same package.

ANS: c. Classes in the same package are implicitly imported into main. Actually, classes in the same package are implicitly imported into the source-code files of other classes in that package.

When no access modifier is specified for a method or variable, the method or variable: a. Is public. b. Is private. c. Has package access. d. Is static.

ANS: c. Has package access.

Garbage Collection and Method finalize 8.10 Q1: Which of the following is false? a. Method finalize does not take parameters and has return type void. b. Memory leaks using Java are rare because of automatic garbage collection. c. Objects are marked for garbage collection by method finalize. d. The garbage collector reclaims unused memory.

ANS: c. Objects are marked for garbage collection by method finalize. (Objects are marked for garbage collection when there are no more references to the object).

Account UML Class Diagram with an Instance Variable and set and get Methods 7.2.4 Q1: Which of the following statements is false? a. In the UML, each class is modeled in a class diagram as a rectangle with three compartments. The top one contains the class's name centered horizontally in boldface. The middle one contains the class's attributes, which correspond to instance variables in Java. The bottom one contains the class's operations, which correspond to methods and constructors in Java. b. UML represents instance variables as an attribute name, followed by a colon and the type. c. Private attributes are preceded by the keyword private in the UML. d. The UML models operations by listing the operation name followed by a set of parentheses. A plus sign (+) in front of the operation name indicates that the operation is a public.

ANS: c. Private attributes are preceded by the keyword private in the UML. Actually, private attributes are preceded by a minus sign (-) in the UML.

Which of the following statements is true? a. The UML models a parameter of an operation by listing the parameter name, followed by a colon and the parameter value between the parentheses after the operation name. b. The UML indicates an operation's return type by placing a colon and the return value after the parentheses following the operation name. c. UML class diagrams do not specify return types for operations that do not return values. d. Declaring instance variables public is known as data hiding or information hiding.

ANS: c. UML class diagrams do not specify return types for operations that do not return values.

Controlling Access to Members 8.3 Q1: Which of the following class members should usually be private? a. Methods. b. Constructors. c. Variables (or fields). d. All of the above.

ANS: c. Variables (or fields).

Which of the following statements is true? a. Each object (instance) of the class shares the class's instance variables. b. Most instance-variable declarations are preceded with the keyword public, which is an access modifier. c. Variables or methods declared with access modifier private are accessible only to methods of the class in which they're declared. d. None of the above is true.

ANS: c. Variables or methods declared with access modifier private are accessible only to methods of the class in which they're declared.

BigDecimal gives you control over how values are rounded. By default: a. all calculations are approximate and no rounding occurs. b. all calculations are approximate and rounding occurs. c. all calculations are exact and no rounding occurs. d. all calculations are exact and rounding occurs.

ANS: c. all calculations are exact and no rounding occurs.

You can declare new classes as needed; this is one reason Java is known as a(n) ________ language. a. portable b. incremental c. extensible d. virtual

ANS: c. extensible.

Notes on Set and Get Methods 8.7 Q1: Set methods are also commonly called ________ methods and get methods are also commonly called ________ methods. a. query, mutator. b. accessor, mutator. c. mutator, accessor. d. query, accessor.

ANS: c. mutator, accessor.

Time Class Case Sudy 8.2 Q1: The _________ of a class are also called the public services or the public interface that the class provides to its clients. a. public constructors. b. public instance variables. c. public methods. d. All of the above.

ANS: c. public methods.

Time Class Case Study: Overloaded Constructors 8.5 Q1: A constructor cannot: a. be overloaded. b. initialize variables to their defaults. c. specify return types or return values. d. have the same name as the class.

ANS: c. specify return types or return values.

Class AccountTest: Initializing Account Objects When They're Created 7.4.2 Q1: If a class does not define constructors, the compiler provides a default constructor with no parameters, and the class's instance variables are initialized to ________. a. zero b. null c. their default values. d. false

ANS: c. their default values.

The format specifier %.2f specifies that two digits of precision should be output ________ in the floating-point number. a. to the left of the decimal point b. centered c. to the right of the decimal point d. None of the above.

ANS: c. to the right of the decimal point.

Which statement is false? a. The actual data representation used within the class is of no concern to the class's clients. b. Clients generally care about what the class does but not how the class does it. c. Clients are usually involved in a class's implementation. d. Hiding the implementation reduces the possibility that clients will become dependent on class-implementation details.

ANS: c: Clients are usually involved in a class's implementation.

Which of the following is false? a. A static method must be used to access private static instance variables. b. A static method has no this reference. c. A static method can be accessed even when no objects of its class have been instantiated. d. A static method can call instance methods directly.

ANS: d. A static method can call instance methods directly.

Referring to the Current Object's Member with the this Reference 8.4 Q1: When must a program explicitly use the this reference? a. Accessing a private variable. b. Accessing a public variable. c. Accessing a local variable. d. Accessing an instance variable that is shadowed by a local variable.

ANS: d. Accessing an instance variable that is shadowed by a local variable.

Additional Notes on This Example 7.2.5 Q1: A static method can ________. a. call only other static methods of the same class directly b. manipulate only static fields in the same class directly c. be called using the class name and a dot (.) d. All of the above.

ANS: d. All of the above.

Having a this reference allows: a. a method to refer explicitly to the instance variables and other methods of the object on which the method was called. b. a method to refer implicitly to the instance variables and other methods of the object on which the method was called. c. an object to reference itself. d. All of the above.

ANS: d. All of the above.

Which of the following statements is false? a. The method's return type specifies the type of data returned to a method's caller. b. Empty parentheses following a method name indicate that the method does not require any parameters to perform its task. c. When a method that specifies a return type other than void is called and completes its task, the method must return a result to its calling method d. Classes often provide public methods to allow the class's clients to set or get private instance variables; the names of these methods must begin with set or get.

ANS: d. Classes often provide public methods to allow the class's clients to set or get private instance variables; the names of these methods must begin with set or get. Actually, the names of these methods need not begin with set or get, but this naming convention is recommended.

Which of the following statements is false? a. By convention class names begin with an uppercase letter, and method and variable names begin with a lowercase letter. b. Instance variables exist before methods are called on an object, while the methods are executing and after the methods complete execution. c. A class normally contains one or more methods that manipulate the instance variables that belong to particular objects of the class. d. Instance variables can be declared anywhere inside a class.

ANS: d. Instance variables are declared inside a class declaration but outside the bodies of the class's method declarations.

Which of the following statements is false? a. Variables declared in the body of a particular method are local variables and can be used only in that method. b. A method's parameters are local variables of the method. c. Every method's body is delimited by left and right braces ({ and }). d. Keyword null indicates that a method will perform a task but will not return any information.

ANS: d. Keyword null indicates that a method will perform a task but will not return any information. Actually, keyword void indicates that a method will perform a task but will not return any information.

Which of the following statements is false? a. If a class does not define constructors, the compiler provides a default constructor with no parameters. b. If you declare a constructor for a class, the compiler will not create a default constructor for that class. c. The UML models constructors in the third compartment of a class diagram. d. To distinguish a constructor from a class's operations, the UML places the word "constructor" between double quotes before the constructor's name.

ANS: d. To distinguish a constructor from a class's operations, the UML places the word "constructor" between double quotes before the constructor's name. Actually, the UML places the word "constructor" between guillemets (« and ») before the constructor's name.

Which of the following statements is false? a. If a program uses multiple classes from the same package, these classes can access each other's package access members directly through references to objects of the appropriate classes, or in the case of static members, through the class name. b. Package access is rarely used. c. Classes in the same source file are part of the same package. d. Use the access modifier package to give a method or variable package access.

ANS: d. Use the access modifier package to give a method or variable package access.

static Class Members 8.11 Q1: Static class variables: a. are final. b. are public. c. are private. d. are shared by all objects of a class.

ANS: d. are shared by all objects of a class.

Account Class with a balance Instance Variable of Type double 7.5.1 Q1: A __________ of a class called myClass is another class whose methods call the methods of myClass. a. consumer b. servant c. caller d. client

ANS: d. client

An import declaration is not required if you always refer to a class with its ________ name, which includes its package name and class name. a. compile-time b. default package c. paired d. fully qualified name

ANS: d. fully qualified name.

Instance Variables, set Methods and get Methods Each class you create becomes a new ________ that can be used to declare variables and create objects. a. package b. instance c. library d. type.

ANS: d. type.

How many Book objects are created by the following statement? Book[] books = new Book[10]; a. 10 b. 0 c. 5 d. None of the above.

Ans: b. 0. The statement creates an array of element that can refer to Book objects. Each element is initially null.

Case Study: Card Shuffling and Dealing Simulation 7.6 Q1: Consider integer array values, which contains 5 elements. Which statements successfully swap the contents of the array at index 3 and index 4? a. values[3] = values[4]; values[4] = values[3]; b. values[4] = values[3]; values[3] = values[4]; c. int temp = values[3]; values[3] = values[4]; values[4] = temp; d. int temp = values[3]; values[3] = values[4]; values[4] = values[3];

Ans: c. int temp = values[3]; values[3] = values[4]; values[4] = temp;

Case Study: Class GradeBook Using an Array to Store Grades 7.7 Q1: What kind of application tests a class by creating an object of that class and calling the class's methods? a. Pseudo application. b. Debugger. c. Declarator. d. Test harness.

Ans: d. Test harness.

Which of the following statements is true? a. Local variables are automatically initialized. b. Every instance variable has a default initial value—a value provided by Java when you do not specify the instance variable's initial value. c. The default value for an instance variable of type String is void. d. The argument types in the method call must be identical to the types of the corresponding parameters in the method's declaration.

e. ANS: b. Every instance variable has a default initial value—a value provided by Java when you do not specify the instance variable's initial value

Using BigDecimal for Precise Monetary Calculations 8.15 Q1: Which of the following statements is false? a. An application that requires precise floating-point calculations such as those in financial applications should use class BigDecimal from package java.math. b. We use class NumberFormat for formatting numeric values as locale-specific strings. c. In the U.S, locale, the value 15467.82 would be formatted as "15,467.82", whereas in many European locales it would be formatted as "15.467,56". d. The BigDecimal method format receives a double argument and returns a BigDecimal object that represents the exact value specied.

ANS: . d. The BigDecimal method format receives a double argument and returns a BigDecimal object that represents the exact value specied. Actually, the BigDecimal method valueOf receives a double argument and returns a BigDecimal object that represents the exact value specied.

Which method returns an array of the enum's constants? a. values. b. getValues. c. constants. d. getConstants.

ANS: a. values.

Constructors: a. Initialize instance variables. b. When overloaded, can have identical argument lists. c. When overloaded, are selected by number, types and order of types of parameters. d. Both (a) and (c).

ANS: d. Both (a) and (c).

Using public set methods helps provide data integrity if: a. The instance variables are public. b. The instance variables are private. c. The methods perform validity checking. d. Both b and c.

ANS: d. Both b and c.

A final field should also be declared ________ if it is initialized in its declaration. a. private. b. public. c. protected. d. static.

ANS: d. static.

AccountTest Class That Creates and Uses an Object of Class Account 7.2.2 Q1: A class that creates an object of another class, then calls the object's methods, is called a(n) ________ class. a. object-oriented b. inherited c. caller d. driver.

ANS: driver.

Assume class Book has been declared. Which set of statements creates an array of Books? a. Book[] books; books = new Book[numberElements]; b. Book[] books]; books = new Book()[numberElements]; c. new Book() books[]; books = new Book[numberElements]; d. All of the above.

Ans: a. Book[] books; books = new Book[numberElements];

Class GradeBook Using a Two-Dimensional Array 7.8 Q1: Which set of statements totals the values in two-dimensional int array items? a. int total = 0; for (int subItems : items) for (int item : subItems) total += item; b. int total = 0; for (int item: int[] subItems : items) total += item; c. int total = 0; for (int[] subItems : items) for (int item : items) total += item; d. int total = 0; for (int[] subItems : items) for (int item : subItems) total += item;

Ans: d. int total = 0; for (int[] subItems : items) for (int item : subItems) total += item;


Kaugnay na mga set ng pag-aaral

Organizational Communication Quiz #1

View Set

Mental health- Legal/Ethical issues Townsend CH. 3 ATI 2

View Set

Chapter 27: Plants without seeds from water to land (learning curve)

View Set

Bio Honors Chapter 5 and 6 Study Guide

View Set

Chapter 22: Bipolar Disorders: Nursing Care of Persons with Mood Lability

View Set