chapter 4 ( fill-in the blanks / True or False / Explanations )

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Identify the false statement. a. A method header always contains a return type, an identifier, and a parameter list within parentheses. b. Arguments are used in method calls; they are passed to parameters in method headers. c. A class can contain any number of methods, and each method can be called any number of times.

A method header always contains a return type, an identifier, and a parameter list within parentheses.

The expressions that are passed to a method in an invocation are called a. actual parameters b. formal parameters c. formal arguments d. formals e. Any of these

ANS: A The formals (formal parameters, formal arguments) are those given in a method's header, where it is declared. The actual parameters (actuals, actual arguments) are the expressions that actually are transmitted to a method in an invocation.

Consider a Rational class designed to represent rational numbers as a pair of ints, along with methods reduce (to reduce the rational to simplest form), gcd (to find the greatest common divisor of two ints), as well as methods for addition, subtraction, multiplication, and division. Why should the reduce and gcd methods be declared to be private? a. because they will never be used b. because they will only be called from methods inside Rational c. because they will only be called from the constructor of Rational d. because they do not use any of the Rational instance data e. This is incorrect; they should be declared as public

ANS: B All items of a class that are declared to be private are only accessible to entities within that class, whether they are instance data or methods. In this case, since these two methods are only called from other methods (including the constructor) of Rational, they are declared private to promote information hiding to a greater degree. Note that answer C is not a correct answer because the reduce method calls the gcd method, so one of the methods is called from a method other than the constructor.

If a method does not have a return statement, then a. it will produce a syntax error when compiled b. it must be a void method c. it cannot be called from outside the class that defined the method d. it must be defined to be a public method e. it must be an int, double, float, or String method

ANS: B All methods are implied to return something and therefore there must be a return statement. However, if the programmer wishes to write a method that does not return anything, and therefore does not need a return statement, then it must be a void method (a method whose header has void as its return type).

Given the method defined here, which of the following method calls is legal? public void foo(int a, int b) a. foo(0, 0.1); b. foo(0/1, 2*3); c. foo(0); d. foo(); e. foo(1+2, 3*0.1);

ANS: B The only legal method call is one that passes twoint parameters. In the case of answer B, 0 / 1 is an int division (equal to 0) and 2 * 3 is an int multiplication. So this is legal. The answers for A and E contain two parameters, but the second of each is a double. The answers for C and D have the wrong number of parameters.

Visibility modifiers include a. public, private b. public, private, protected c. public, private, protected, final d. public, protected, final, static e. public, private, protected, static

ANS: B public, private, protected control the visibility of variables and methods. final controls whether a variable, method, or class can be further changed or overridden; it does not control visibility. static controls whether a variable or method is associated with instances of a class or the class itself.

Which of the following is not a kind of object that is used to create a graphical user interface in JavaFX? a. control b. event c. image d. event handler e. All of these are objects used to create a GUI in JavaFX

ANS: C An image is not a kind of object used to create a GUI even though it is an object. It can only be displayed with an ImageView JavaFX node.

To define a class that will represent a car, which of the following definition is most appropriate? a. private class car b. public class car c. public class Car d. public class CAR. e. private class Car

ANS: C Classes should be defined to be public so that they can be accessed by other classes. And following Java naming convention, class names should start with a capital letter and be lower case except for the beginning of each new word, so Car is more appropriate than car or CAR.

The behavior of an object is defined by the object's a. instance data b. constructor c. visibility modifiers d. methods e. All of these

ANS: D The methods dictate how the object reacts when it is passed messages. Each message is implemented as a method, and the method is the code that executes when the message is passed. The constructor is one of these methods but all of the methods combined dictate the behavior. The visibility modifiers do impact the object's performance indirectly.

Identify the false statement. a. A variable declaration is a statement that reserves a named memory location and includes a data type, an identifier, an optional assignment operator and assigned value, and an ending semicolon. b. An item's data type determines what legal identifiers can be used to describe variables and whether the variables can occupy memory. c. A variable is a named memory location that you can use to store a value; it can hold only one value at a time, but the value it holds can change.

An item's data type determines what legal identifiers can be used to describe variables and whether the variables can occupy memory.

Identify the false statement. a. In the method header public static void main(String[] args), the word static means that a method is accessible and usable, even though no objects of the class exist. b. In the method header public static void main(String[] args), the word void means that the main() method is an empty method. c. In the method header public static void main(String[] args), the word public is an access specifier.

In the method header public static void main(String[] args), the word void means that the main() method is an empty method.

Java does not execute instructions on a computer directly, but rather on the ____.

Java Virtual Machine

To create interactive programs that accept input from a user, you can use ____.

System.in

A program or class that instantiates objects of another prewritten class is a(n) ____.

class client

Variables that are shared by every instantiation of a class are ____.

class variables

Non-executing program statements that provide documentation are called program ____.

comments

After you write and save a Java application file, you ____ and then ____ it.

compile, interpret

A ____ translates high-level language statements into machine code.

compiler

If you attempt to add a float, an int, and a byte, the result will be a(n) ____.

float

When you run your program you don't see evidence of the changes you just made to it. You realize that you forgot to ____ the program.

recompile

A(n) ____ statement causes a method to end and the program's logic to return to the calling method.

return

A class's constructor usually defines a. how an object is initialized b. how an object is interfaced c. the number of instance data in the class d. the number of methods in the class e. if the instance data are accessible outside of the object directly

ANS: A The constructor should be used to "construct" the object, that is, to set up the initial values of the instance data. This is not essential, but is typically done. The interface of an object is dictated by the visibility modifiers used on the instance data and methods.

The relationship between a class and an object is best described as a. classes are instances of objects b. objects are instances of classes c. objects and classes are the same thing d. classes are programs while objects are variables e. objects are the instance data of classes

ANS: B Classes are definitions of program entities that represent classes of things/entities in the world. Class definitions include instance data and methods. To use a class, it is instantiated. These instances are known as objects. So, objects are instances of classes. Program code directly interacts with objects, not classes.

A variable whose scope is restricted to the method where it was declared is known as a(n) a. parameter b. global variable c. local variable d. public instance data e. private instance data

ANS: C Local variables are those that are "local" to the method in which they have been declared; that is, they are accessible only inside that method. Global variables are those that are accessible from anywhere, while parameters are the variables passed into a method. Instance data can be thought of as global variables for an entire object.

An example of passing a message to a String where the message has a String parameter would occur in which of the following messages? a. length b. substring c. equals d. toUpperCase e. None of these; it is not possible to pass a String as a parameter to a String

ANS: C The length and toUpperCase messages do not have parameters and substring has two int parameters. For equals, a String must be passed as a parameter so that the String receiving the message can be compared to the String passed as a parameter.

In order to preserve encapsulation of an object, we would do all of the following except for which one? a. make the instance data private b. define the methods in the class to access and manipulate the instance data c. make the methods of the class public d. make the class final e. all of these preserve encapsulation

ANS: D Encapsulation means that the class contains both the data and the methods needed to manipulate the data. In order to preserve encapsulation properly, the instance data should not be directly accessible from outside of the classes, so the instance data are made private and methods are defined to access and manipulate the instance data. Further, the methods to access and manipulate the instance data are made public so that other classes can use the object. The reserved word final is used to control inheritance and has nothing to do with encapsulation.

Which of the following reserved words in Java is used to create an instance of a class? a. class b. public c. public or private (can use either) d. import e. new

ANS: E

The software failure at the Denver International Airport's baggage handling system is a good example of a. how a large system can be obsolete by the time it is developed b. how designers sometimes have too much faith in the technology they are using c. how failures are often a result of multiple variables caused by a system as a whole in actual operation d. how the use of a centralized computer is unrealistic in today's distributed processing e. All of these

ANS: E All of these are good examples of why software fails.

Given the method defined here, which of the following method calls is legal? public void doublefoo(double x) a. doublefoo(0); b. doublefoo(0.555); c. doublefoo(0.1 + 0.2); d. foo(0.1, 0.2); e. all except D are legal

ANS: E In the case of A, the value 0 (an int) is widened to a double. In the case of C, the addition is performed yielding 0.3 and then doublefoo is called. The parameter list in D is illegal since it contains two double parameters instead of 1.

Instance data for a Java class a. are limited to primitive types b. are limited to Strings c. are limited to objects (e.g., Strings, classes defined by other programmers) d. may be primitive types or objects but objects must be defined to be private e. may be primitive types or objects

ANS: E The instance data are the entities that make up the class and may be any type available whether primitive or object, and may be public or private. By using objects as instance data, it permits the class to be built upon other classes. This relationship where a class has instance data that are other classes is known as a has-a relationship.

In a UML diagram for a class a. classes are represented as rectangles b. there may be a section containing the name of the class c. there may be a section containing the attributes of the class d. there may be a section containing the methods of the class e. All of these

ANS: E Those four attributes correctly describe a UML representation of a class.

Having multiple class methods of the same name where each method has a different number of or type of parameters is known as a. encapsulation b. information hiding c. tokenizing d. importing e. method overloading

ANS: E When methods share the same name, they are said to be overloaded. The number and type of parameters passed in the message provides the information by which the proper method is called.

Java methods can return more than one item if they are modified with the reserved word continue, as in public continue int foo() {...}

ANS: F All Java methods return a single item, whether it is a primitive data type an object, or void. The reserved word continue is used to exit the remainder of a loop and test the condition again.

All Java classes must contain a main method which is the first method executed when the Java class is called on.

ANS: F Only the driver program requires a main method. The driver program is the one that is first executed in any Java program (except for Applets), but it may call upon other classes as needed, and these other classes do not need main methods.

Formal parameters are those that appear in the method call and actual parameters are those that appear in the method header.

ANS: F The question has the two definitions reversed. Formal parameters are those that appear in the method header, actual parameters are the parameters in the method call (those being passed to the method).

Java methods can only return primitive types.

ANS: F Java methods can also return objects such as String.

A GUI control sets up an event but it is the programmer who writes the code for the event handler which executes when an event occurs.

ANS: T

Because an Image cannot directly be added to a container, it must be displayed using an ImageView object.

ANS: T

While multiple objects of the same class can exist, in a given program there can only be one version of each class.

ANS: T A class is an abstraction; that is, it exists as a definition, but not as a physical instance. Physical instances are created when an object is instantiated using new. Therefore, there can be many objects of type String, but only one String class.

Regarding the software failure described at the Denver International Airport in the text, it is critical to factor in errors and inefficiencies that alway occur in a complex system.

ANS: T A good system is designed using a well-built systems analysis and design methodology that helps reduce the maintenance portion of the system's lifecycle.

A constructor may contain a return statement so long as no value (or expression) is returned.

ANS: T Constructors may contain non-value-returning return statements. Doing so is discouraged, but it is legal.

An object should be encapsulated in order to guard its data and methods from inappropriate access.

ANS: T Encapsulation is the concept that objects should be protected from accidental (or purposeful) misuse

Defining formal parameters requires including each parameter's type.

ANS: T In order for the compiler to check to see if a method call is correct, the compiler needs to know the types for the parameters being passed. Therefore, all formal parameters (those defined in the method header) must include their type. This is one element that makes Java a strongly typed language.

The interface of a class is based on those data instances and methods that are declared public.

ANS: T The interface is how an outside agent interacts with the object. Interaction is only available through those items declared to be public in the class's definition.

Identify the false statement. a. A nested class resides within another class. b. Exposition describes the relationship between classes when an object of one class is a data field within another class. c. When you use an object as a data member of another object, you must remember to supply values for the contained object if it has no default constructor.

Exposition describes the relationship between classes when an object of one class is a data field within another class.

Identify the false statement. a. Java packages are available only if you explicitly name them within your program. b. The creators of Java have produced hundreds of classes for you to use in your programs. c. The implicitly imported java.lang package contains fundamental Java classes.

Java packages are available only if you explicitly name them within your program.

Identify the false statement. a. Javadoc comments begin with a forward slash and two asterisks ( /** ) and end with an asterisk and a forward slash ( */ ); they are used to generate documentation with a program named javadoc. b. Line comments start with two forward slashes ( // ) and end with two backslashes ( \\ ); they can extend across as many lines as needed. c. Block comments start with a forward slash and an asterisk ( /* ) and end with an asterisk and a forward slash ( */ ); they can extend across as many lines as needed.

Line comments start with two forward slashes ( // ) and end with two backslashes ( \\ ); they can extend across as many lines as needed.

Identify the false statement. a. Methods declared as static receive a this reference that contains a reference to the object associated with them. b. A final static field's value is shared by every object of a class. c. Methods declared as static are called class methods.

Methods declared as static receive a this reference that contains a reference to the object associated with them.

Identify the false statement. a. When it is part of the same program as void myMethod (int age, String name), the following method would be ambiguous: String myMethod (int zipCode, String address) b. When it is part of the same program as void myMethod (int age, String name), the following method would be ambiguous: void myMethod (int x, String y) c. When it is part of the same program as void myMethod (int age, String name), the following method would be ambiguous: void myMethod (String name, int age)

When it is part of the same program as void myMethod (int age, String name), the following method would be ambiguous: void myMethod (String name, int age)

Identify the false statement. a. A class's instance variables override locally declared variables with the same names that are declared within the class's methods. b. You cannot declare the same variable name more than once within a block, even if a block contains other blocks. c. A variable ceases to exist, or goes out of scope, at the end of the block in which it is declared.

A class's instance variables override locally declared variables with the same names that are declared within the class's methods.

Identify the false statement. a. When a method is declared with public access, methods in other classes can call it. b. Not all methods return a value, but every method requires a return type. c. A method header is also called an implementation.

A method header is also called an implementation.

What happens if you declare a class constructor to have a void return type? a. You will most likely receive a syntax error. b. The program will compile with a warning but you'll get a run-time error. c. There is nothing wrong with declaring a constructor with a void return type. d. The class's default constructor will be used instead of the one you're declaring. e. None of these

ANS: A It is a syntax violation to declare a constructor with any type, even void, so you'll receive a syntax error.

Consider a sequence of method invocations as follows: main calls m1, m1 calls m2, m2 calls m3, and then m2 calls m4, m3 calls m5. If m4 has just terminated, what method will resume execution? a. m1 b. m2 c. m3 d. m5 e. main

ANS: B Once a method terminates, control resumes with the method that called that method. In this case, m2 calls m4 so that when m4 terminates, m2 is resumed.

Every class definition must include a constructor.

ANS: F Java allows classes to be defined without constructors. However, there is a default constructor that is used in such a case.

Accessors and mutators provide mechanisms for controlled access to a well-encapsulated class.

ANS: T Accessors provide read access to variables that otherwise would be inaccessible. Mutators provide write access to otherwise inaccessible variables.

Identify the false statement. a. Assume that myName is a String defined as "molly". The value of myName.indexOf('M') is -1. b. Assume that myName is a String defined as "molly". The value of myName.toUpperCase() is "Molly". c. Assume that myName is a String defined as "molly". The value of myName.length() is "5".

Assume that myName is a String defined as "molly". The value of myName.toUpperCase() is "Molly".

____ describes the relationship between classes when an object of one class is a data field within another class.

Composition

The method parseInt() converts a(n) _____.

String to an integer

If a class is named Student, the class constructor name is____.

Student()

Which of the following is not an option that can be displayed with a confirm dialog box? a. Yes b. Try Again c. Cancel d. No

Try Again

Which of the following could be the last legally coded line of a method declared as public static int getVal(double sum)? a. return 2.3; b. Any of these could be the last coded line of the method. c. return; d. return 77;

return 77;

The compiler determines which version of a method to call by the method's ____.

signature

The method that extracts a string from within another string is _____.

substring()

A boolean variable can hold ____.

the value true or false

Named computer memory locations are called ____.

variables

If a method is written to receive a double parameter, and you pass an integer to the method, then the method will ____.

work correctly; the integer will be promoted to a double

The "equal to" relational operator is ____.

==

The following method header definition will result in a syntax error: public void aMethod();

ANS: T The reason for the syntax error is because it ends with a ; symbol. It instead needs to be followed by {} with 0 or more instructions inside of the brackets. An abstract method will end with a ; but this header does not define an abstract method.

A method defined in a class can access the class's instance data without needing to pass them as parameters or declare them as local variables.

ANS: T The instance data are globally available to all of the class's methods and therefore the methods do not need to receive them as parameters or declare them locally. If variables of the same name as instance data were declared locally inside a method then the instance data would be "hidden" in that method because the references would be to the local variables.

Identify the false statement. a. When you compare Strings with the == operator, you are comparing their memory addresses, not their values. b. To create a String object, you must use the keyword new and explicitly call the class constructor. c. When you compare Strings with the equals() method, you are comparing their values, not their memory addresses.

To create a String object, you must use the keyword new and explicitly call the class constructor.

Identify the false statement. a. When you declare a primitive variable or instantiate an object from a class, you provide both a type and an identifier. b. Unlike a primitive variable, an instantiated object cannot be passed into or returned from a method. c. The address of an instantiated object can be assigned to a declared reference of the same type.

Unlike a primitive variable, an instantiated object cannot be passed into or returned from a method.

Identify the false statement. a. When you create a String, you have the option of omitting the keyword new, but when you initialize a StringBuilder object, you must use the keyword new, the constructor name, and an initializing value between the constructor's parentheses. b. When you create a StringBuilder object with an initial value of "Juan", its capacity is 16. c. If a StringBuilder named myAddress contains "817", then myAddress.append(" Maple Lane"); alters myAddress to contain "817 Maple Lane".

When you create a StringBuilder object with an initial value of "Juan", its capacity is 16.

Identify the false statement. a. An object name is a reference; it holds a memory address. b. When you declare an object, you give it a name and set aside enough memory for the object to be stored. c. When you don't write a constructor for a class, Java creates one for you; the name of the constructor is always the same as the name of its class.

When you declare an object, you give it a name and set aside enough memory for the object to be stored.

Identify the false statement. a. A variable of type int can hold any whole number value from approximately negative two billion to positive two billion. b. You can use the data types byte or short to hold larger values than can be accommodated by an int. c. When you assign a value to an int variable, you do not type any commas; you type only digits and an optional plus or minus sign to indicate a positive or negative integer.

You can use the data types byte or short to hold larger values than can be accommodated by an int.

The method public static boolean testValue(int response) returns ____.

a boolean value

A(n) ____ data type is a type whose implementation is hidden and accessed through its public methods.

abstract

Using a method name to contain or encapsulate a series of statements is an example of ____.

abstraction

The toString() method converts a(n) _____ to a String. a. char b. float c. int d. all of the above

all of the above

Which of the following elements is not required in a variable declaration?

an assigned value

The code between a pair of curly braces in a method is a ____.

block

Which of the following data types can store a value in the least amount of memory?

byte

A constructor ____ overloaded. a. must be b. cannot be c. can be d. is always automaticall

can be

A constructor ____ parameters. a. must receive b. cannot receive c. can receive a maximum of 10 d. can receive

can receive

The first position in a String _____.

is position zero

A sequence of characters enclosed within double quotation marks is a _____.

literal string

The most basic circuitry-level computer language, which consists of on and off switches, is ____.

machine language

The principle used in creating private access is sometimes called ____ and is an important component of object-oriented programs.

information hiding

The method with the declaration public static int aMethod() has a method type of ____.

int

The difference between int and Integer is _____.

int is a primitive type; Integer is a class

The remainder operator ____.

is also called the modulus operator

When a block exists within another block, the blocks are ____.

nested

Java is architecturally ____.

neutral

Unlike when you create a String, when you create a StringBuilder, you must use the keyword _____.

new

If you use the automatically-supplied default constructor when you create an object, ____.

numeric fields are set to 0 (zero)

If you create a class that contains one method and then instantiate two objects, you usually store ____ for use with the objects.

one copy of the method

Java classes are stored in a folder or ____.

package

All method declarations contain ____.

parentheses

Arguments to methods always appear within ____.

parentheses

Most class data fields are ____.

private

The individual operations used in a computer program are often grouped into logical units called ____.

procedures


संबंधित स्टडी सेट्स

Chapter 19: Documenting and Reporting

View Set

The Crucible - Act I Study Guide

View Set

Tale of Two Cities Quotes Speaker

View Set

The Science of Energy Crude Oil-Petroleum Pt. 1

View Set

Chapter 6&7 Hunter Education Test Review

View Set

Fundamentals final (quizzes 9-15)

View Set

Module 2: The Federal Gift Tax Quiz 1

View Set