JAVA - HW9 Chapters 8 & 9

¡Supera tus tareas y exámenes ahora con Quizwiz!

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

Has package access.

Which of the following class members should usually be private? Answers: Methods. Constructors. Variables (or fields). All of the above.

Variables (or fields).

The default implementation of method clone of Object performs a ________. Answers: empty copy. deep copy. full copy. shallow copy.

shallow copy.

A constructor cannot: Answers: be overloaded. initialize variables to their defaults. specify return types or return values. have the same name as the class.

specify return types or return values.

A final field should also be declared ________ if it is initialized in its declaration. Answers: private. public. protected. static.

static.

Which of the following keywords allows a subclass to access a superclass method even when the subclass has overridden the superclass method? Answers: base. this. public. super.

super.

Which method returns an array of the enum's constants? Answers: values. getValues. constants. getConstants.

values.

The default equals implementation of class Object determines: Answers: whether two references refer to the same object in memory. whether two references have the same type. whether two objects have the same instance variables. whether two objects have the same instance variable values.

whether two references refer to the same object in memory.

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? Answers: A compilation error occurs. A runtime error occurs. A logic error occurs. Nothing happens. The program compiles and runs.

A compilation error occurs.

Consider the classes below, declared in the same file: class A { int a; public A() { a = 7; } } class B extends A { int b; public B() { b = 8; } } Which of the statements below is false? Answers: Both variables a and b are instance variables. After the constructor for class B executes, the variable a will have the value 7. After the constructor for class B executes, the variable b will have the value 8. A reference of type A can be treated as a reference of type B.

A reference of type A can be treated as a reference of type B.

Which of the following statements is false? Answers: A subclass is often larger than its superclass. A superclass object is a subclass object. The class following the extends keyword in a class declaration is the direct superclass of the class being declared. Java uses interfaces to provide the benefits of multiple inheritance.

A superclass object is a subclass object.

When must a program explicitly use the this reference? Answers: Accessing a private variable. Accessing a public variable. Accessing a local variable. Accessing an instance variable that is shadowed by a local variable.

Accessing an instance variable that is shadowed by a local variable.

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

All of the above.

Which statement is true when a superclass has protected instance variables? Answers: A subclass object can assign an invalid value to the superclass's instance variables, thus leaving an object in an inconsistent state. Subclass methods are more likely to be written so that they depend on the superclass's data implementation. We may need to modify all the subclasses of the superclass if the superclass implementation changes. All of the above.

All of the above.

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

Be modified after they are initialized.

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

Both (a) and (c).

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

Both b and c.

Which of the following statements is false? Answers: A class can directly inherit from class Object. It's often much more efficient to create a class by inheriting from a similar class than to create the class by writing every line of code the new class requires. If the class you're inheriting from declares instance variables as private, the inherited class can access those instance variables directly. A class's instance variables are normally declared private to enforce good software engineering.

If the class you're inheriting from declares instance variables as private, the inherited class can access those instance variables directly.

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

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

Every class in Java, except ________, extends an existing class. Answers: Integer. Object. String. Class.

Object.

An advantage of inheritance is that Answers: All methods can be inherited. All instance variables can be uniformly accessed by subclasses and superclasses. Objects of a subclass can be treated like objects of their superclass. None of the above.

Objects of a subclass can be treated like objects of their superclass.

Overriding a method differs from overloading a method because: Answers: Overloaded methods have the same signature. Overridden methods have the same signature. Both of the above. Neither of the above.

Overridden methods have the same signature.

When a subclass constructor calls its superclass constructor, what happens if the superclass's constructor does not assign a value to an instance variable? Answers: A syntax error occurs. A compile-time error occurs. A run-time error occurs. The program compiles and runs because the instance variables are initialized to their default values.

The program compiles and runs because the instance variables are initialized to their default values.

Static class variables: Answers: are final. are public. are private. are shared by all objects of a class.

are shared by all objects of a class.

Private fields of a superclass can be accessed in a subclass Answers: by calling private methods declared in the superclass. by calling public or protected methods declared in the superclass. directly. All of the above.

by calling public or protected methods declared in the superclass.

The static method ________ of class String returns a formatted String. Answers: printf. format. formatString. toFormatedString.

format.

When overriding a superclass method and calling the superclass version from the subclass method, failure to prefix the superclass method name with the keyword super and a dot (.) in the superclass method call causes ________. Answers: a compile-time error. a syntax error. infinite recursion. a runtime error.

infinite recursion.

Inheritance is also known as the Answers: knows-a relationship. has-a relationship. uses-a relationship. is-a relationship.

is-a relationship.

Which of the following is the superclass constructor call syntax? Answers: keyword super, followed by a dot (.) . keyword super, followed by a set of parentheses containing the superclass constructor arguments. keyword super, followed by a dot and the superclass constructor name. None of the above.

keyword super, followed by a set of parentheses containing the superclass constructor arguments.

Set methods are also commonly called ________ methods and get methods are also commonly called ________ methods. Answers: query, mutator. accessor, mutator. mutator, accessor. query, accessor.

mutator, accessor.

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

no-argument constructor.

Using the protected keyword also gives a member: Answers: public access. package access. private access. block scope.

package access.

Superclass methods with this level of access cannot be called from subclasses. Answers: private. public. protected. package.

private.

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

private.

Which superclass members are inherited by all subclasses of that superclass? Answers: private instance variables and methods. protected instance variables and methods. private constructors. protected constructors.

protected instance variables and methods.

The _________ of a class are also called the public services or the public interface that the class provides to its clients. Answers: public constructors. public instance variables. public methods. All of the above.

public methods.


Conjuntos de estudio relacionados

Information tecnology and computer applications in accounting

View Set

Chapter 9: Flexible Budgets & Performance Analysis

View Set

Chapter 5: Life Insurance Underwriting and Policy Issue

View Set

Entrepreneurship: Ideas In Action Chapter 6

View Set

class 7 - chapter 17: outcome identification and planning

View Set

ОИТ. 25. Проблематика ИИ.

View Set