Chapter 5 Methods Flashcards
Review Questions
1. Which statements about the final modifier are correct? (Choose all that apply.)
A. Instance and static variables can be marked final.
B. A variable is effectively final only if it is marked final.
C. An object that is marked final cannot be modified.
D. Local variables cannot be declared with type var and the final modifier.
E. A primitive that is marked final cannot be modified.
A, E
A, E.
- Instance and static variables can be marked final, making option A correct.
- Effectively final means a local variable is not marked final but whose value does not change after it is set, making option B incorrect.
- Option C is incorrect, as final refers only to the reference to an object, not its contents.
- Option D is incorrect, as var and final can be used together.
- Finally, option E is correct: once a primitive is marked final, it cannot be modified.
2. Which of the following can fill in the blank in this code to make it compile? (Choose all that apply.)
public class Ant { \_\_\_\_\_\_\_ void method() {} }
A. default
B. final
C. private
D. Public
E. String
F. zzz:
B, C
B, C.
- The keyword void is a return type.
- Only the access modifier or optional specifiers are allowed before the return type.
- Option C is correct, creating a method with private access.
- Option B is also correct, creating a method with package access and the optional specifier final. Since package access does not use a modifier, we get to jump right to final.
- Option A is incorrect because package access omits the access modifier rather than specifying default.
- Option D is incorrect because Java is case sensitive. It would have been correct if public were the choice.
- Option E is incorrect because the method already has a void return type.
- Option F is incorrect because labels are not allowed for methods.
3. Which of the following methods compile? (Choose all that apply.)
A. final static void rain() {}
B. public final int void snow() {}
C. private void int hail() {}
D. static final void sleet() {}
E. void final ice() {}
F. void public slush() {}
A, D,
A, D.
- Options A and D are correct because the optional specifiers are allowed in any order.
- Options B and C are incorrect because they each have two return types.
- Options E and F are incorrect because the return type is before the optional specifier and access modifier, respectively.
4. Which of the following can fill in the blank and allow the code to compile? (Choose all that apply.)
final \_\_\_\_\_\_\_ song = 6;
A. int
B. Integer
C. long
D. Long
E. double
F. Double
A, B, C, E
A, B, C, E.
- The value 6 can be implicitly promoted to any of the primitive types, making options A, C, and E correct.
- It can also be autoboxed to Integer, making option B correct.
- It cannot be both promoted and autoboxed, making options D and F incorrect.
5. Which of the following methods compile? (Choose all that apply.)
A. public void january() { return; }
B. public int february() { return null;}
C. public void march() {}
D. public int april() { return 9;}
E. public int may() { return 9.0;}
F. public int june() { return;}
A, C, D
A, C, D.
- Options A and C are correct because a void method is optionally allowed to have a return statement as long as it doesn’t try to return a value.
- Option B does not compile because null requires a reference object as the return type. Since int is primitive, it is not a reference object.
- Option D is correct because it returns an int value.
- Option E does not compile because it tries to return a double when the return type is int.
- Since a double cannot be assigned to an int, it cannot be returned as one either.
- Option F does not compile because no value is actually returned.
6. Which of the following methods compile? (Choose all that apply.)
A. public void violin(int… nums) {}
B. public void viola(String values, int… nums) {}
C. public void cello(int… nums, String values) {}
D. public void bass(String… values, int… nums) {}
E. public void flute(String[] values, …int nums) {}
F. public void oboe(String[] values, int[] nums) {}
A, B, F
A, B, F.
- Options A and B are correct because the single varargs parameter is the last parameter declared.
- Option F is correct because it doesn’t use any varargs parameters.
- Option C is incorrect because the varargs parameter is not last.
- Option D is incorrect because two varargs parameters are not allowed in the same method.
- Option E is incorrect because the … for a varargs must be after the type, not before it.
7. Given the following method, which of the method calls return 2? (Choose all that apply.)
public int juggle(boolean b, boolean… b2) { return b2.length; }
A. juggle();
B. juggle(true);
C. juggle(true, true);
D. juggle(true, true, true);
E. juggle(true, {true, true});
F. juggle(true, new boolean[2]);
D, F
D, F.
- Option D passes the initial parameter plus two more to turn into a varargs array of size 2.
- Option F passes the initial parameter plus an array of size 2.
- Option A does not compile because it does not pass the initial parameter.
- Option E does not compile because it does not declare an array properly. It should be new boolean[] {true, true}.
- Option B creates a varargs array of size 0, and option C creates a varargs array of size 1.
8. Which of the following statements is correct?
A. Package access is more lenient than protected access.
B. A public class that has private fields and package methods is not visible to classes outside the package.
C. You can use access modifiers so only some of the classes in a package see a particular package class.
D. You can use access modifiers to allow access to all methods and not any instance variables.
E. You can use access modifiers to restrict access to all classes that begin with the word Test.
B, D
D.
- Option D is correct. A common practice is to set all fields to be private and all methods to be public.
- Option A is incorrect because protected access allows everything that package access allows and additionally allows subclasses access.
- Option B is incorrect because the class is public. This means that other classes can see the class. However, they cannot call any of the methods or read any of the fields. It is essentially a useless class.
- Option C is incorrect because package access applies to the whole package. Option E is incorrect because Java has no such wildcard access capability.
9. Given the following class definitions, which lines in the main() method generate a compiler error? (Choose all that apply.)
// Classroom.java package my.school; public class Classroom { private int roomNumber; protected static String teacherName; static int globalKey = 54321; public static int floor = 3; Classroom(int r, String t) { roomNumber = r; teacherName = t; } } // School.java 1: package my.city; 2: import my.school.*; 3: public class School { 4: public static void main(String[] args) { 5: System.out.println(Classroom.globalKey); 6: Classroom room = new Classroom(101, "Mrs. Anderson"); 7: System.out.println(room.roomNumber); 8: System.out.println(Classroom.floor); 9: System.out.println(Classroom.teacherName); } }
A. None: the code compiles fine.
B. Line 5
C. Line 6
D. Line 7
E. Line 8
F. Line 9
B, C, D, F
B, C, D, F.
- The two classes are in different packages, which means private access and package access will not compile.
- This causes compiler errors on lines 5, 6, and 7, making options B, C, and D correct answers.
- Additionally, protected access will not compile since School does not inherit from Classroom.
- This causes the compiler error on line 9, making option F a correct answer as well.
10. What is the output of executing the Chimp program?
// Rope.java 1: package rope; 2: public class Rope { 3: public static int LENGTH = 5; 4: static { 5: LENGTH = 10; 6: } 7: public static void swing() { 8: System.out.print("swing "); 9: } } // Chimp.java 1: import rope.*; 2: import static rope.Rope.*; 3: public class Chimp { 4: public static void main(String[] args) { 5: Rope.swing(); 6: new Rope().swing(); 7: System.out.println(LENGTH); 8: } }
A. swing swing 5
B. swing swing 10
C. Compiler error on line 2 of Chimp
D. Compiler error on line 5 of Chimp
E. Compiler error on line 6 of Chimp
F. Compiler error on line 7 of Chimp
A
B.
- Rope runs line 3, setting LENGTH to 5,
- and then immediately after that runs the static initializer, which sets it to 10.
- Line 5 in the Chimp class calls the static method normally and prints swing and a space.
- Line 6 also calls the static method. Java allows calling a static method through an instance variable, although it is not recommended.
- Line 7 uses the static import on line 2 to reference LENGTH. For these reasons, option B is correct.
11. Which statements are true of the following code? (Choose all that apply.)
1: public class Rope { 2: public static void swing() { 3: System.out.print("swing"); 4: } 5: public void climb() { 6: System.out.println("climb"); 7: } 8: public static void play() { 9: swing(); 10: climb(); 11: } 12: public static void main(String[] args) { 13: Rope rope = new Rope(); 14: rope.play(); 15: Rope rope2 = null; 16: System.out.print("-"); 17: rope2.play(); 18: } }
A. The code compiles as is.
B. There is exactly one compiler error in the code.
C. There are exactly two compiler errors in the code.
D. If the line(s) with compiler errors are removed, the output is swing-climb.
E. If the line(s) with compiler errors are removed, the output is swing-swing.
F. If the line(s) with compile errors are removed, the code throws a NullPointerException.
B
B, E.
- Line 10 does not compile because static methods are not allowed to call instance methods.
- Even though we are calling play() as if it were an instance method and an instance exists, Java knows play() is really a static method and treats it as such. Since this is the only line that does not compile, option B is correct.
- If line 10 is removed, the code prints swing-swing, making option E correct.
- It does not throw a NullPointerException on line 17 because play() is a static method. Java looks at the type of the reference for rope2 and translates the call to Rope.play().
12. How many variables in the following method are effectively final?
10: public void feed() { 11: int monkey = 0; 12: if(monkey > 0) { 13: var giraffe = monkey++; 14: String name; 15: name = "geoffrey"; 16: } 17: String name = "milly"; 18: var food = 10; 19: while(monkey <= 10) { 20: food = 0; 21: } 22: name = null; 23: }
A. 1
B. 2
C. 3
D. 4
E. 5
F. None of the above. The code does not compile.
B
B.
- The test for effectively final is if the final modifier can be added to the local variable and the code still compiles.
- The monkey variable declared on line 11 is not effectively final because it is modified on line 13.
- The giraffe and name variables declared on lines 13 and 14, respectively, are effectively final and not modified after they are set.
- The name variable declared on line 17 is not effectively final since it is modified on line 22.
- Finally, the food variable on line 18 is not effectively final since it is modified on line 20.
- Since there are two effectively final variables, option B is correct.
13. What is the output of the following code?
// RopeSwing.java import rope.*; import static rope.Rope.*; public class RopeSwing { private static Rope rope1 = new Rope(); private static Rope rope2 = new Rope(); { System.out.println(rope1.length); } public static void main(String[] args) { rope1.length = 2; rope2.length = 8; System.out.println(rope1.length); } } // Rope.java package rope; public class Rope { public static int length = 0; }
A. 02
B. 08
C. 2
D. 8
E. The code does not compile.
F. An exception is thrown.
B
D.
- There are two details to notice in this code.
- First, note that RopeSwing has an instance initializer and not a static initializer.
- Since RopeSwing is never constructed, the instance initializer does not run.
- The other detail is that length is static.
- Changes from any object update this common static variable.
- The code prints 8, making option D correct.
14. How many lines in the following code have compiler errors?
1: public class RopeSwing { 2: private static final String leftRope; 3: private static final String rightRope; 4: private static final String bench; 5: private static final String name = "name"; 6: static { 7: leftRope = "left"; 8: rightRope = "right"; 9: } 10: static { 11: name = "name"; 12: rightRope = "right"; 13: } 14: public static void main(String[] args) { 15: bench = "bench"; 16: } 17: }
A. 0
B. 1
C. 2
D. 3
E. 4
F. 5
C
E.
- If a variable is static final, it must be set exactly once, and it must be in the declaration line or in a static initialization block.
- Line 4 doesn’t compile because bench is not set in either of these locations.
- Line 15 doesn’t compile because final variables are not allowed to be set after that point.
- Line 11 doesn’t compile because name is set twice: once in the declaration and again in the static block.
- Line 12 doesn’t compile because rightRope is set twice as well.
- Both are in static initialization blocks. Since four lines do not compile, option E is correct.
15. Which of the following can replace line 2 to make this code compile? (Choose all that apply.)
1: import java.util.*; 2: // INSERT CODE HERE 3: public class Imports { 4: public void method(ArrayList<String> list) { 5: sort(list); 6: } 7: }
A. import static java.util.Collections;
B. import static java.util.Collections.*;
C. import static java.util.Collections.sort(ArrayList<String>);
D. static import java.util.Collections;
E. static import java.util.Collections.*;
F. static import java.util.Collections.sort(ArrayList<String>);
B
B.
- The two valid ways to do this are
import static java.util.Collections.*;
andimport static java.util.Collections.sort;
. - Option A is incorrect because you can do a static import only on static members.
- Classes such as Collections require a regular import.
- Option C is nonsense as method parameters have no business in an import.
- Options D, E, and F try to trick you into reversing the syntax of import static.