COMP 1510 Chapter 4

25 July 2022
4.7 (114 reviews)
37 test answers

Unlock all answers in this set

Unlock answers (33)
question
Java methods can only return primitive types.
answer
ANS: F Java methods can also return objects such as String.
question
Formal parameters are those that appear in the method call and actual parameters are those that appear in the method header.
answer
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).
question
All Java classes must contain a main method which is the first method executed when the Java class is called on.
answer
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.
question
Java methods can return more than one item if they are modified with the reserved word continue, as in public continue int foo() {...}
answer
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.
question
The following method header definition will result in a syntax error: public void aMethod();
answer
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.
question
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.
answer
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.
question
The interface of a class is based on those data instances and methods that are declared public.
answer
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.
question
Defining formal parameters requires including each parameter's type.
answer
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.
question
Every class definition must include a constructor.
answer
ANS: F Java allows classes to be defined without constructors. However, there is a default constructor that is used in such a case.
question
While multiple objects of the same class can exist, in a given program there can only be one version of each class.
answer
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.
question
A constructor may contain a return statement so long as no value (or expression) is returned.
answer
ANS: T Constructors may contain non-value-returning return statements. Doing so is discouraged, but it is legal.
question
An object should be encapsulated in order to guard its data and methods from inappropriate access.
answer
ANS: T Encapsulation is the concept that objects should be protected from accidental (or purposeful) misuse
question
Accessors and mutators provide mechanisms for controlled access to a well-encapsulated class.
answer
ANS: T Accessors provide read access to variables that otherwise would be inaccessible. Mutators provide write access to otherwise inaccessible variables.
question
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.
answer
ANS: T
question
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.
answer
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.
question
Because an Image cannot directly be added to a container, it must be displayed using an ImageView object.
answer
ANS: T
question
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
answer
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.
question
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
answer
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.
question
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
answer
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.
question
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
answer
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.
question
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
answer
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).
question
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
answer
ANS: E
question
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
answer
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.
question
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
answer
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.
question
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
answer
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.
question
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
answer
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.
question
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
answer
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.
question
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
answer
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.
question
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
answer
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.
question
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);
answer
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.
question
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
answer
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.
question
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
answer
ANS: E Those four attributes correctly describe a UML representation of a class.
question
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
answer
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.
question
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
answer
ANS: A It is a syntax violation to declare a constructor with any type, even void, so you'll receive a syntax error.
question
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
answer
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.
question
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
answer
ANS: E All of these are good examples of why software fails.
question
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
answer
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.