Deck 6: Methods: a Deeper Look
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/38
Play
Full screen (f)
Deck 6: Methods: a Deeper Look
1
Which is a correct static method call of Math class method sqrt?
A) sqrt(900);
B) math.sqrt(900);
C) Math.sqrt(900);
D) Math math = new Math(); math.sqrt(900);
A) sqrt(900);
B) math.sqrt(900);
C) Math.sqrt(900);
D) Math math = new Math(); math.sqrt(900);
C
2
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 method is called with its name and parentheses.
B) x and y are parameters.
C) Copies of x and y are passed to the method calculateValue.
D) x and y are arguments.
int x = 9;
Double y = 5.3;
Result = calculateValue(x, y);
Which of the following statements is false?
A) A method is called with its name and parentheses.
B) x and y are parameters.
C) Copies of x and y are passed to the method calculateValue.
D) x and y are arguments.
x and y are parameters.
3
Which of the following can be an argument to a method?
A) Constants.
B) Variables.
C) Expressions.
D) All of the above.
A) Constants.
B) Variables.
C) Expressions.
D) All of the above.
D
4
Stacks are known as ________ data structures.
A) FIFO.
B) FILO.
C) LIFO.
D) LILO.
A) FIFO.
B) FILO.
C) LIFO.
D) LILO.
Unlock Deck
Unlock for access to all 38 flashcards in this deck.
Unlock Deck
k this deck
5
When an object is concatenated with a String, ________.
A) a compilation error occurs
B) a runtime error occurs
C) the object's toString method is implicitly called to obtain the String representation of the object
D) the object's class name is concatenated with the String
A) a compilation error occurs
B) a runtime error occurs
C) the object's toString method is implicitly called to obtain the String representation of the object
D) the object's class name is concatenated with the String
Unlock Deck
Unlock for access to all 38 flashcards in this deck.
Unlock Deck
k this deck
6
Which of the following promotions of primitive types is not allowed to occur?
A) char to int.
B) double to float.
C) int to double.
D) short to long.
A) char to int.
B) double to float.
C) int to double.
D) short to long.
Unlock Deck
Unlock for access to all 38 flashcards in this deck.
Unlock Deck
k this deck
7
The parameter list in the method header and the arguments in the method call must agree in:
A) number
B) type
C) order
D) all of the above
A) number
B) type
C) order
D) all of the above
Unlock Deck
Unlock for access to all 38 flashcards in this deck.
Unlock Deck
k this deck
8
Which operator can be used in string concatenation?
A) *
B) +=
C) ++
D) =+
A) *
B) +=
C) ++
D) =+
Unlock Deck
Unlock for access to all 38 flashcards in this deck.
Unlock Deck
k this deck
9
Any field declared with keyword ________ is constant.
A) static
B) const
C) constant
D) final
A) static
B) const
C) constant
D) final
Unlock Deck
Unlock for access to all 38 flashcards in this deck.
Unlock Deck
k this deck
10
Which of the following methods is not in the Math class?
A) ceil
B) abs
C) parseInt
D) log
A) ceil
B) abs
C) parseInt
D) log
Unlock Deck
Unlock for access to all 38 flashcards in this deck.
Unlock Deck
k this deck
11
Variables should be declared as fields only if ________.
A) they are local variables
B) they are used only within a method
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) they are arguments
A) they are local variables
B) they are used only within a method
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) they are arguments
Unlock Deck
Unlock for access to all 38 flashcards in this deck.
Unlock Deck
k this deck
12
Method log takes the logarithm of its argument with respect to what base?
A) 10
B) e
C) 2
D) pi
A) 10
B) e
C) 2
D) pi
Unlock Deck
Unlock for access to all 38 flashcards in this deck.
Unlock Deck
k this deck
13
To declare a method as static, place the keyword static before ________ in the method's declaration.
A) the method modifier
B) the return type
C) the method name
D) the argument list
A) the method modifier
B) the return type
C) the method name
D) the argument list
Unlock Deck
Unlock for access to all 38 flashcards in this deck.
Unlock Deck
k this deck
14
If more method calls occur than can have their activation records stored on the program execution stack, an error known as a ________ occurs.
A) stack overflow.
B) stack rewind.
C) stack full.
D) stack empty.
A) stack overflow.
B) stack rewind.
C) stack full.
D) stack empty.
Unlock Deck
Unlock for access to all 38 flashcards in this deck.
Unlock Deck
k this deck
15
Declaring main as static allows the JVM to invoke main ________.
A) without knowing the name of the class in which main is declared.
B) by creating an object of the class in which main is declared.
C) without creating an instance of the class in which main is declared.
D) None of the above.
A) without knowing the name of the class in which main is declared.
B) by creating an object of the class in which main is declared.
C) without creating an instance of the class in which main is declared.
D) None of the above.
Unlock Deck
Unlock for access to all 38 flashcards in this deck.
Unlock Deck
k this deck
16
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.
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.
Unlock Deck
Unlock for access to all 38 flashcards in this deck.
Unlock Deck
k this deck
17
Which of the following primitive types is never promoted to another primitive type?
A) double.
B) byte.
C)
C) boolean.
D) Both a and
A) double.
B) byte.
C)
C) boolean.
D) Both a and
Unlock Deck
Unlock for access to all 38 flashcards in this deck.
Unlock Deck
k this deck
18
A well-designed method ________.
A) performs multiple unrelated tasks
B) repeats code found in other methods
C) contains thousands of lines of code
D) performs a single, well-defined task
A) performs multiple unrelated tasks
B) repeats code found in other methods
C) contains thousands of lines of code
D) performs a single, well-defined task
Unlock Deck
Unlock for access to all 38 flashcards in this deck.
Unlock Deck
k this deck
19
Which of the following statements is false?
A) If a method does not return a value, the return-value-type in the method declaration can be omitted.
B) Placing a semicolon after the right parenthesis enclosing the parameter list of a method declaration is a syntax error.
C) Redeclaring a method parameter as a local variable in the method's body is a compilation error.
D) Forgetting to return a value from a method that should return a value is a compilation error.
A) If a method does not return a value, the return-value-type in the method declaration can be omitted.
B) Placing a semicolon after the right parenthesis enclosing the parameter list of a method declaration is a syntax error.
C) Redeclaring a method parameter as a local variable in the method's body is a compilation error.
D) Forgetting to return a value from a method that should return a value is a compilation error.
Unlock Deck
Unlock for access to all 38 flashcards in this deck.
Unlock Deck
k this deck
20
Information is passed to a method in ________.
A) the method name
B) that method's return
C) the method body
D) the arguments to the method
A) the method name
B) that method's return
C) the method body
D) the arguments to the method
Unlock Deck
Unlock for access to all 38 flashcards in this deck.
Unlock Deck
k this deck
21
An overloaded method is one that ________.
A) has a different name than another method, but the same parameters
B) has the same name as another method, but different parameters (by number, types or order of the types)
C) has the same name and parameters as a method defined in another class
D) has the same name and parameters, but a different return type as another method
A) has a different name than another method, but the same parameters
B) has the same name as another method, but different parameters (by number, types or order of the types)
C) has the same name and parameters as a method defined in another class
D) has the same name and parameters, but a different return type as another method
Unlock Deck
Unlock for access to all 38 flashcards in this deck.
Unlock Deck
k this deck
22
A Java class can have which of the following methods?
A. void foo(int a)
B. void foo(int a, int b)
C. void foo(double a)
D. void foo(double a, double b)
E. void foo(int b)
A) All of the above.
B) A, B, D, E.
C) A, B, C, D.
D) A, C, D, E.
A. void foo(int a)
B. void foo(int a, int b)
C. void foo(double a)
D. void foo(double a, double b)
E. void foo(int b)
A) All of the above.
B) A, B, D, E.
C) A, B, C, D.
D) A, C, D, E.
Unlock Deck
Unlock for access to all 38 flashcards in this deck.
Unlock Deck
k this deck
23
Which statement below could be used to simulate the outputs of tossing a quarter to get heads or tails? Suppose randomNumbers is a SecureRandom object.
A) randomNumbers.nextInt(7);
B) randomNumbers.nextInt(2);
C) randomNumbers.nextInt(1);
D) randomNumbers.nextInt(25);
A) randomNumbers.nextInt(7);
B) randomNumbers.nextInt(2);
C) randomNumbers.nextInt(1);
D) randomNumbers.nextInt(25);
Unlock Deck
Unlock for access to all 38 flashcards in this deck.
Unlock Deck
k this deck
24
Overloaded methods always have the same _________.
A) method name
B) return type
C) number of parameters
D) order of the parameters
A) method name
B) return type
C) number of parameters
D) order of the parameters
Unlock Deck
Unlock for access to all 38 flashcards in this deck.
Unlock Deck
k this deck
25
Which of these statements best defines scope?
A) Scope refers to the classes that have access to a variable.
B) Scope determines whether a variable's value can be altered.
C) Scope is the portion of a program that can refer to an entity by its simple name.
D) Scope allows the programmer to use a class without using its fully qualified name.
A) Scope refers to the classes that have access to a variable.
B) Scope determines whether a variable's value can be altered.
C) Scope is the portion of a program that can refer to an entity by its simple name.
D) Scope allows the programmer to use a class without using its fully qualified name.
Unlock Deck
Unlock for access to all 38 flashcards in this deck.
Unlock Deck
k this deck
26
The jjava.text package contains classes for manipulating all of the following items except ________.
A) classes
B) numbers
C) strings
D) characters
A) classes
B) numbers
C) strings
D) characters
Unlock Deck
Unlock for access to all 38 flashcards in this deck.
Unlock Deck
k this deck
27
Identifiers in Java have ________ and ________ scopes?
A) method, class.
B) class, block.
C) block, statement.
D) statement, file.
A) method, class.
B) class, block.
C) block, statement.
D) statement, file.
Unlock Deck
Unlock for access to all 38 flashcards in this deck.
Unlock Deck
k this deck
28
Which of the following statements is false?
A) The Java API consists of packages.
B) The Java API helps programmers avoid "reinventing the wheel."
C) The Java API consists of import declarations.
D) None of the above.
A) The Java API consists of packages.
B) The Java API helps programmers avoid "reinventing the wheel."
C) The Java API consists of import declarations.
D) None of the above.
Unlock Deck
Unlock for access to all 38 flashcards in this deck.
Unlock Deck
k this deck
29
Which statement below could be used to simulate the outputs of rolling a six-sided die? Suppose randomNumbers is a SecureRandom object.
A) 1 + randomNumbers.nextInt(6);
B) 1 + randomNumbers.nextInt(2);
C) 6 + randomNumbers.nextInt(1);
D) 3 + randomNumbers.nextInt(3);
A) 1 + randomNumbers.nextInt(6);
B) 1 + randomNumbers.nextInt(2);
C) 6 + randomNumbers.nextInt(1);
D) 3 + randomNumbers.nextInt(3);
Unlock Deck
Unlock for access to all 38 flashcards in this deck.
Unlock Deck
k this deck
30
A set of named constants that start with the value 0 for the first constant and increment by 1 for each subsequent constant can be declared as a(n) ________.
A) class
B) enum
C) enumeration
D) None of the above.
A) class
B) enum
C) enumeration
D) None of the above.
Unlock Deck
Unlock for access to all 38 flashcards in this deck.
Unlock Deck
k this deck
31
Which statement creates a random value from the sequence 2, 5, 8, 11 and 14. Suppose randomNumbers is a SecureRandom object.
A) 2 + 5 * randomNumbers.nextInt(3);
B) 3 + 2 * randomNumbers.nextInt(5);
C) 5 + 3 * randomNumbers.nextInt(2);
D) 2 + 3 * randomNumbers.nextInt(5);
A) 2 + 5 * randomNumbers.nextInt(3);
B) 3 + 2 * randomNumbers.nextInt(5);
C) 5 + 3 * randomNumbers.nextInt(2);
D) 2 + 3 * randomNumbers.nextInt(5);
Unlock Deck
Unlock for access to all 38 flashcards in this deck.
Unlock Deck
k this deck
32
In a class containing methods with the same name, the methods are distinguished by ________.
A) Number of arguments
B) Types of arguments
C) Return type
D) (a) and (b)
E) (b) and (c)
A) Number of arguments
B) Types of arguments
C) Return type
D) (a) and (b)
E) (b) and (c)
Unlock Deck
Unlock for access to all 38 flashcards in this deck.
Unlock Deck
k this deck
33
Math static method random generates a random double value in the range from 0.0
A) up to but not including 1.0
B) up to and including 1.0
C) up to and including 100.0
D) up to but not including 100.0
A) up to but not including 1.0
B) up to and including 1.0
C) up to and including 100.0
D) up to but not including 100.0
Unlock Deck
Unlock for access to all 38 flashcards in this deck.
Unlock Deck
k this deck
34
Which of the following statements describes block scope?
A) It begins at the opening { of the class declaration and terminates at the closing }.
B) It limits label scope to only the method in which it is declared.
C) It begins at the identifier's declaration and ends at the terminating right brace (}).
D) It is valid for one statement only.
A) It begins at the opening { of the class declaration and terminates at the closing }.
B) It limits label scope to only the method in which it is declared.
C) It begins at the identifier's declaration and ends at the terminating right brace (}).
D) It is valid for one statement only.
Unlock Deck
Unlock for access to all 38 flashcards in this deck.
Unlock Deck
k this deck
35
Method calls cannot be distinguished by ________.
A) method name
B) return type
C) parameter lists
D) method signature
A) method name
B) return type
C) parameter lists
D) method signature
Unlock Deck
Unlock for access to all 38 flashcards in this deck.
Unlock Deck
k this deck
36
Which of the following is not a package in the Java API?
A) java.component
B) java.awt.event
C) javax.swing.event
D) java.lang
A) java.component
B) java.awt.event
C) javax.swing.event
D) java.lang
Unlock Deck
Unlock for access to all 38 flashcards in this deck.
Unlock Deck
k this deck
37
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 and B are overloaded; C and D are overloaded.
B) A and C are overloaded; B and D are overloaded.
C) A, B and C are overloaded.
D) All four methods are overloaded.
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 and B are overloaded; C and D are overloaded.
B) A and C are overloaded; B and D are overloaded.
C) A, B and C are overloaded.
D) All four methods are overloaded.
Unlock Deck
Unlock for access to all 38 flashcards in this deck.
Unlock Deck
k this deck
38
The identifiers in an enumeration ________.
A) must be unique.
B) may be duplicated.
C) must be lowercase letters and cannot contain numbers.
D) must be uppercase letters and cannot contain numbers.
A) must be unique.
B) may be duplicated.
C) must be lowercase letters and cannot contain numbers.
D) must be uppercase letters and cannot contain numbers.
Unlock Deck
Unlock for access to all 38 flashcards in this deck.
Unlock Deck
k this deck