JAVA Chapter 6

25 July 2022
4.7 (114 reviews)
43 test answers

Unlock all answers in this set

Unlock answers (39)
question
Q1: Information is passed to a method in: a. a. the method name. b. b. that method's return. c. c. the method body. d. d. the arguments to the method.
answer
ANS: d. the arguments to the method.
question
2 Q2: Programs designed for maintainability are constructed from small simple pieces or modules. Modules in Java are called: a. a. methods. b. b. classes. c. c. arguments. d. d. both methods and classes.
answer
ANS: d. both methods and classes
question
2 Q3: A well-designed method a. a. performs multiple unrelated tasks. b. b. repeats code found in other methods. c. c. contains thousands of lines of code. d. d. performs a single, well-defined task.
answer
ANS: d. performs a single, well-defined task.
question
3 Q1: To declare a method as static, place the keyword static before ________ in the method's declaration. a. a. the method modifier. b. b. the return type. c. c. the method name. d. d. the argument list.
answer
ANS: b. the return type.
question
6.3 Q2: Which is a correct static method call of Math class method sqrt? a. a. sqrt( 900 );. b. b. math.sqrt( 900 );. c. c. Math.sqrt( 900 );. d. d. Math math = new Math(); math.sqrt( 900 );.
answer
ANS: c. Math.sqrt( 900 );.
question
Q3: Which of the following methods is not in the Math class? a. a. ceil. b. b. abs. c. c. parseInt. d. d. log.
answer
ANS: c. parseInt
question
3 Q4: Which of the following can be an argument to a method? a. a. Constants. b. b. Variables. c. c. Expressions. d. d. All of the above.
answer
ANS: d. All of the above.
question
3 Q5: Method log takes the logarithm of its argument with respect to what base? a. a. 10 b. b. e c. c. 2 d. d. pi
answer
ANS: b. e
question
Q6: Any field declared with keyword ________ is constant. a. a. static. b. b. const. c. c. constant. d. d. final.
answer
ANS: d. final.
question
Q7: Declaring main as ________ allows the JVM to invoke main without creating an instance of the class. a. a. public. b. b. void. c. c. static. d. d. final.
answer
ANS: c. static.
question
Q1: Variables should be declared as fields only if a. a. they are local variables. b. b. they are used only within a method. c. c. they are required for use in more than one method or their values must be saved between calls to the class's methods. d. d. they are arguments.
answer
ANS: c. they are required for use in more than one method or their values must be saved between calls to the class's methods.
question
Q2: Consider the following Java statements: int x = 9; double y = 5.3; result = calculateValue( x, y ); Which of the following statements is false? a. a. A method is called with its name and parentheses. b. b. x and y are parameters. c. c. Copies of x and y are passed to the method calculateValue. d. d. x and y are arguments.
answer
ANS: b. x and y are paramters.
question
Q3: The parameter list in the method header and the arguments in the method call must agree in: a. a. number b. b. type c. c. order d. d. all of the above
answer
ANS: d. all of the above
question
Q4: Which operator can be used in string concatenation? a. a. *. b. b. +=. c. c. ++. d. d. =+.
answer
ANS: b. +=.
question
Q5: When an object is concatenated with a String: a. a. a compilation error occurs. b. b. a runtime error occurs. c. c. the object's toString method is implicitly called to obtain the String representation of the object. d. d. the object's class name is used.
answer
ANS: c. the object's toString method is implicitly called to obtain the String representation of the object.
question
Q1: A static method can ________. a. a. call only other static methods of the same class directly. b. b. manipulate only static fields in the same class directly. c. c. be called using the class name and a dot (.). d. d. All of the above.
answer
ANS: d. All of the above.
question
Q2: Which statement is false? a. a. If a method does not return a value, the return-value-type in the method declaration can be omitted. b. b. Placing a semicolon after the right parenthesis enclosing the parameter list of a method declaration is a syntax error. c. c. Redeclaring a method parameter as a local variable in the method's body is a compilation error. d. d. Forgetting to return a value from a method that should return a value is a compilation error.
answer
ANS: a. If a method does not return a value, the return-value-type in the method declaration can be omitted.
question
Q1: Stacks are known as ________ data structures. a. a. FIFO. b. b. FILO. c. c. LIFO. d. d. LILO.
answer
ANS: c. LIFO.
question
Q2: If more method calls occur than can have their activation records stored on the program execution stack, an error known as a ________ occurs. a. a. stack overflow. b. b. stack rewind. c. c. stack full. d. d. stack empty.
answer
ANS: a. stack overflow.
question
Q1: Which of the following promotions of primitive types is not allowed to occur? a. a. char to int. b. b. int to double. c. c. short to long. d. d. double to float.
answer
ANS: d. double to float.
question
Q2: Which of the following primitive types is never promoted to another type? a. a. double. b. b. byte. c. c. boolean. d. d. Both a and c.
answer
ANS: d. Both a and c.
question
Q1: Which statement is not true. a. a. The Java API consists of packages. b. b. The Java API helps programmers avoid "reinventing the wheel." c. c. The Java API consists of import declarations. d. d. The class javax.swing.JApplet is part of the Java API.
answer
ANS: c. The Java API consists of import declarations. (The Java API is built from packages.)
question
Q2: Which of the following is not a package in the Java API? a. a. java.component. b. b. java.awt. c. c. javax.swing.event. d. d. java.lang.
answer
ANS: a. java.component.
question
Q3: The java.text package contains classes for manipulating all of the following items except a. a. classes b. b. numbers c. c. strings d. d. characters
answer
ANS: a. classes
question
Q1: Math static method random generates a random double value in the range from 0.0 a. a. up to but not including 1.0 b. b. up to and including 1.0 c. c. up to and including 100.0 d. d. up to but not including 100.0
answer
ANS: a. up to but not including 1.0
question
Q2: Which statement below could be used to simulate the outputs of tossing a quarter to get heads or tails? Suppose randomNumbers is a Random object. a. a. randomNumbers.nextInt( 7 ); b. b. randomNumbers.nextInt( 2 ); c. c. randomNumbers.nextInt( 1 ); d. d. randomNumbers.nextInt( 25 );
answer
ANS: b. randomNumbers.nextInt( 2 );
question
Q3: Which statement below could be used to simulate the outputs of rolling a six-sided die? Suppose randomNumbers is a Random object. a. a. 1 + randomNumbers.nextInt( 6 ); b. b. 1 + randomNumbers.nextInt( 2 ); c. c. 6 + randomNumbers.nextInt( 1 ); d. d. 3 + randomNumbers.nextInt( 3 );
answer
ANS: a. 1 + randomNumbers.nextInt( 6 );
question
Q1: Which statement creates a random value from the sequence 2, 5, 8, 11 and 14. Suppose randomNumbers is a Random object. a. a. 2 + 5 * randomNumbers.nextInt( 3 ); b. b. 3 + 2 * randomNumbers.nextInt( 5 ); c. c. 5 + 3 * randomNumbers.nextInt( 2 ); d. d. 2 + 3 * randomNumbers.nextInt( 5 );
answer
ANS: d. 2 + 3 * randomNumbers.nextInt( 5 );
question
Q1: You can set a Random object's seed at any time during program execution by calling the object's ________ methods. a. a. changeSeed. b. b. setSeed. c. c. resetSeed. d. d. updateSeed.
answer
ANS: b. setSeed.
question
Q1: An enumeration is a special class that's introduced by the keyword ________ and a type name. a. a. class. b. b. enum. c. c. enumeration. d. d. classEnum.
answer
ANS: b. enum.
question
Q2: The identifiers in an enumeration ________. a. a. must be unique. b. b. may be duplicated. c. c. must be lowercase letters and cannot contain numbers. d. d. must be uppercase letters and cannot contain numbers.
answer
ANS: a. must be unique.
question
Q1: Identifiers in Java have ________ and ________ scopes? a. a. method, class. b. b. class, block. c. c. block, statement. d. d. statement, file.
answer
ANS: b. class, block.
question
Q2: Which of the following statements describes block scope? a. a. It begins at the opening { of the class declaration and terminates at the closing } b. b. It limits label scope to only the method in which it is declared. c. c. It begins at the identifier's declaration and ends at the terminating right brace (}). d. d. It is valid for one statement only.
answer
ANS: c. It begins at the identifier's declaration and ends at the terminating right brace (}).
question
Q3: Which of these statements best defines scope? a. a. Scope refers to the classes that have access to a variable. b. b. Scope determines whether a variable's value can be altered. c. c. Scoping allows the programmer to use a class without using its fully qualified name. d. d. Scope is the portion of a program that can refer to an entity by its simple name.
answer
ANS: d. Scope is the portion of a program that can refer to an entity by its simple name.
question
Q1: Overloaded methods always have the same _________. a. a. method name. b. b. return type. c. c. number of parameters. d. d. order of the parameters.
answer
ANS: a. method name.
question
Q2: An overloaded method is one that a. a. has a different name than another method, but the same parameters. b. b. has the same name as another method, but different parameters (by number, types or order of the types). c. c. has the same name and parameters as a method defined in another class. d. d. has the same name and parameters, but a different return type as another method.
answer
ANS: b. has the same name as another method, but different parameters (by number, types or order of the types).
question
Q3: Which of the following methods are overloaded with respect to one another? public int max ( int a, int b ) { ... } public double max ( double a, double b ) { ... } public int max ( int a, int b, int c ) { ... } public double max ( double a, double b, double c ) { ... } a. a. A and B are overloaded; C and D are overloaded. b. b. A and C are overloaded; B and D are overloaded. c. c. A, B and C are overloaded. d. d. All these four methods are overloaded.
answer
ANS: d. All these four methods are overloaded.
question
Q4: Suppose method1 is declared as void method1 ( int a, float b ) Which of the following methods correctly overloads method1? a. a. void method2 ( int a, float b ). b. b. void method2 ( float a, int b ). c. c. void method1 ( float a, int b ). d. d. void method1 ( int b, float a ).
answer
ANS: c. void method1 ( float a, int b ).
question
Q5: A Java class can have which of the following methods? A. A. void foo( int a ) B. B. void foo( int a, int b ) C. C. void foo( double a ) D. D. void foo( double a, double b ) E. E. void foo( int b ) a. a. All of the above. b. b. A, B, D, E. c. c. A, B, C, D. d. d. A, C, D, E.
answer
ANS: c. A, B, C, D.
question
Q6: Method calls cannot be distinguished by ________. a. a. method name. b. b. return type. c. c. parameter lists. d. d. method signature.
answer
ANS: b. return type.
question
Q7: In a class containing methods with the same name, the methods are distinguished by: a. a. Number of arguments. b. b. Types of arguments. c. c. Return type. d. d. (a) and (b). e. e. (b) and (c).
answer
ANS: d. (a) and (b).
question
Q1: Java uses class ________ to represent colors using their RGB values. a. a. Color. b. b. Colors. c. c. RGBColor. d. d. RGBColors.
answer
ANS: a. Color.
question
Q2: Filled rectangles and filled circles are drawn using Graphics method ________ and ________. a. a. fillRect, fillCircle. b. b. filledRect, filledCircle. c. c. fillRect, fillOval, d. d. filledRect, filledOval.
answer
ANS: c. fillRect, fillOval.