Programmer II Chapter 1: Java Fundamentals Flashcards
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 if it is marked final. C. The final modifier can be applied to classes and interfaces. D. A final class cannot be extended. E. An object that is marked final cannot be modified. F. Local variables cannot be declared with type var and the final modifier.
A, D. 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. The final modifier can be applied to classes, but not interfaces, making option C incorrect. Remember, interfaces are implicitly abstract, which is incompatible with final.Option D is correct, as the definition of a final class is that it cannot be extended. Option E is incorrect, as final refers only to the reference to an object, not its contents. Finally, option F is incorrect, as var and final can be used together.
What is the result of the following program?
public class FlavorsEnum { enum Flavors { VANILLA, CHOCOLATE, STRAWBERRY static final Flavors DEFAULT = STRAWBERRY; } public static void main(String[] args) { for(final var e : Flavors.values()) System.out.print(e.ordinal()+" "); } }
A. 0 1 2
B. 1 2 3
C. Exactly one line of code does not compile.
D. More than one line of code does not compile.
E. The code compiles but produces an exception at runtime.
F. None of the above
C.
When an enum contains only a list of values, the semicolon (;) after the list is optional. When an enum contains any other members, such as a constructor or variable, then it is required. Since the code is missing the semicolon, it does not compile, making option C the correct answer. There are no other compilation errors in this code. If the missing semicolon was added, then the program would print 0 12 at runtime.
What is the result of the following code? (Choose all that apply.)
1: public class Movie { 2: private int butter = 5; 3: private Movie() {} 4: protected class Popcorn { 5: private Popcorn() {} 6: public static int butter = 10; 7: public void startMovie() { 8: System.out.println(butter); 9: } 10: } 11: public static void main(String[] args) { 12: var movie = new Movie(); 13: Movie.Popcorn in = new Movie().new Popcorn(); 14: in.startMovie(); 15: } }
A. The output is 5. B. The output is 10. C. Line 6 generates a compiler error. D. Line 12 generates a compiler error. E. Line 13 generates a compiler error. F. The code compiles but produces an exception at runtime.
C.
Popcorn is an inner class. Inner classes are only allowed to contain static variables that are marked final. Since there are no other compilation errors, option C is the only correct answer. If the final modifier was added on line 6, then the code would print 10 at runtime. Note that private constructors can be used by any methods within the same class.
Which statements about default and private interface methods are correct? (Choose all that apply.)
A. A default interface method can be declared private.
B. A default interface method can be declared public.
C. A default interface method can be declared static.
D. A private interface method can be declared abstract.
E. A private interface method can be declared protected.
F. A private interface method can be declared static.
B, F.
A default interface method is always public, whether you include the identifier or not, making option B correct and option A incorrect. Interfaces cannot contain default static methods, making option C incorrect. Option D is incorrect, as private interface methods are not inherited and cannot be marked abstract. Option E is incorrect, as a method can’t be marked both protected and private. Finally, interfaces can include both private and private static methods, making option F correct.
Which of the following are valid lambda expressions? (Choose all that apply.)
A. (Wolf w, var c) -> 39 B. (final Camel c) -> {} C. (a,b,c) -> {int b = 3; return 2;} D. (x,y) -> new RuntimeException() E. (var y) -> return 0; F. () -> {float r} G. (Cat a, b) -> {}
B, D.
Option B is a valid functional interface, one that could be assigned to a Consumerreference. Notice that the final modifier is permitted on variables in the parameter list. Option D is correct, as the exception is being returned as an object and not thrown. This would be compatible with a BiFunction that included RuntimeException as its return type.
Option A is incorrect because it mixes var and non-var parameters. If one argument uses var, then they all must use var. Option C is invalid because the variable b is used twice. Option E is incorrect,as a return statement is permitted only inside braces ({}). Option F is incorrect because the variable declaration requires a semicolon (;) after it. Finally, option G is incorrect. If the type is specified for one argument, then it must be specified for each and every argument.
What are some advantages of using private interface methods? (Choose all that apply.)
A. Improve polymorphism B. Improve performance at runtime C. Reduce code duplication D. Backward compatibility E. Encapsulate interface implementation F. Portability
C, E.
You can reduce code duplication by moving shared code from default or static methods into a private or private static method. For this reason, option C is correct. Option E is also correct,as making interface methods private means users of the interface do not have access to them. The rest of the options are not related to private methods, although backward compatibility does apply to default methods.
What is the result of the following program?
public class IceCream { enum Flavors { CHOCOLATE, STRAWBERRY, VANILLA } public static void main(String[] args) { Flavors STRAWBERRY = null; switch (STRAWBERRY) { case Flavors.VANILLA: System.out.print("v"); case Flavors.CHOCOLATE: System.out.print("c"); case Flavors.STRAWBERRY: System.out.print("s"); break; default: System.out.println("missing flavor"); } } }
A. v
B. vc
C. s
D. missing flavor
E. Exactly one line of code does not compile.
F. More than one line of code does not compile.
G. The code compiles but produces an exception at runtime.
F.
When using an enum in a switch statement, the case statement must be made up of the enum values only. If the enum name is used in the case statement value, then the code does not compile. For example, VANILLA is acceptable but Flavors.VANILLA is not. For this reason, the three case statements do not compile, making option F the correct answer. If these three lines were corrected,then the code would compile and produce a NullPointerException at runtime.
Which statements about functional interfaces are true? (Choose all that apply.)
A. A functional interface can contain default and private methods. B. A functional interface can be defined by a class or interface. C. Abstract methods with signatures that are contained in public methods of java.lang.Object do not count toward the abstract method count for a functional interface. D. A functional interface cannot contain static or private static methods. E. A functional interface contains at least one abstract method. F. A functional interface must be marked with the @FunctionalInterface annotation.
A, C.
A functional interface can contain any number of non abstract methods including default,private, static, and private static. For this reason, option A is correct, and option D is incorrect.Option B is incorrect, as classes are never considered functional interfaces. A functional interface contains exactly one abstract method, although methods that have matching signatures as public methods in java.lang.Object do not count toward the single method test. For these reasons, option C is correct, and option E is incorrect. Finally, option F is incorrect. While a functional interface can be marked with the @FunctionalInterface annotation, it is not required.
Which lines, when entered independently into the blank, allow the code to print Not scared at runtime? (Choose all that apply.)
public class Ghost { public static void boo() { System.out.println("Not scared"); }
protected final class Spirit { public void boo() { System.out.println("Booo!!!"); } }
public static void main(String... haunt) { var g = new Ghost().new Spirit() {}; \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_; } }
A. g.boo() B. g.super.boo() C. new Ghost().boo() D. g.Ghost.boo() E. new Spirit().boo() F. Ghost.boo() G. None of the above
G. Trick question—the code does not compile! The Spirit class is marked final, so it cannot be extended. The main() method uses an anonymous inner class that inherits from Spirit, which is not allowed. If Spirit was not marked final, then options C and F would be correct. Option A would print Booo!!!, while options B, D, and E would not compile for various reasons.
The following code appears in a file named Ostrich.java. What is the result of compiling the sourcefile?
1: public class Ostrich { 2: private int count; 3: private interface Wild {} 4: static class OstrichWrangler implements Wild { 5: public int stampede() { 6: return count; 7: } } }
A. The code compiles successfully, and one bytecode file is generated: Ostrich.class.
B. The code compiles successfully, and two bytecode files are generated: Ostrich.class and OstrichWrangler.class.
C. The code compiles successfully, and two bytecode files are generated: Ostrich.class and Ostrich$OstrichWrangler.class.
D. A compiler error occurs on line 4.
E. A compiler error occurs on line 6.
E. The code OstrichWrangler class is a static nested class; therefore, it cannot access the instance member count. For this reason, line 6 does not compile, and option E is correct. If the static modifier on line 4 was removed, then the class would compile and produce two files: Ostrich.class and Ostrich$OstrichWrangler.class. You don't need to know that $ is the syntax, but you do need to know the number of classes and that OstrichWrangler is not a top-level class.
What is the result of the following code?
1: public interface CanWalk { 2: default void walk() { System.out.print("Walking"); } 3: private void testWalk() {} 4: } 5: public interface CanRun { 6: abstract public void run(); 7: private void testWalk() {} 8: default void walk() { System.out.print("Running"); } 9: } 10: public interface CanSprint extends CanWalk, CanRun { 11: void sprint(); 12: default void walk(int speed) { 13: System.out.print("Sprinting"); 14: } 15: private void testWalk() {} 16: }
A. The code compiles without issue.
B. The code will not compile because of line 6.
C. The code will not compile because of line 8.
D. The code will not compile because of line 10.
E. The code will not compile because of line 12.
F. None of the above
D.
In this example, CanWalk and CanRun both implement a default walk() method. The definition of CanSprint extends these two interfaces and therefore won’t compile unless the interface overrides both inherited methods. The version of walk() on line 12 is an overload, not an override, since it takes an int value. Since the interface doesn’t override the methods, the compiler can’t decide which default method to use, leading to a compiler error and making option D the correct answer.
What is the result of executing the following program?
interface Sing { boolean isTooLoud(int volume, int limit); } public class OperaSinger { public static void main(String[] args) { check((h, l) -> h.toString(), 5); // m1 } private static void check(Sing sing, int volume) { if (sing.isTooLoud(volume, 10)) // m2 System.out.println("not so great"); else System.out.println("great"); } }
A. great B. not so great C. Compiler error on line m1 D. Compiler error on line m2 E. Compiler error on a different line F. A runtime exception is thrown.
C.
The functional interface takes two int parameters. The code on line m1 attempts to use them as if one is an Object, resulting in a compiler error and making option C the correct answer. It also tries to return String even though the return data type for the functional interface method is boolean. It is tricky to use types in a lambda when they are implicitly specified. Remember to check the interface for the real type.
Which lines of the following interface declaration do not compile? (Choose all that apply.)
1: public interface Herbivore { 2: int amount = 10; 3: static boolean gather = true; 4: static void eatGrass() {} 5: int findMore() { return 2; } 6: default float rest() { return 2; } 7: protected int chew() { return 13; } 8: private static void eatLeaves() {} 9: }
A. All of the lines compile without issue. B. Line 2 C. Line 3 D. Line 4 E. Line 5 F. Line 6 G. Line 7 H. Line 8
E, G.
For this question, it helps to remember which implicit modifiers the compiler will insert and which it will not. Lines 2 and 3 compile with interface variables assumed to be public, static, and final. Line 4 also compiles, as static methods are assumed to be public if not otherwise marked.Line 5 does not compile. Non-static methods within an interface must be explicitly marked private or default. Line 6 compiles, with the public modifier being added by the compiler. Line 7 does not compile, as interfaces do not have protected members. Finally, line 8 compiles, with no modifiers being added by the compiler.
What is printed by the following program?
public class Deer { enum Food {APPLES, BERRIES, GRASS} protected class Diet { private Food getFavorite() { return Food.BERRIES; } } public static void main(String[] seasons) { switch(new Diet().getFavorite()) { case APPLES: System.out.print("a"); case BERRIES: System.out.print("b"); default: System.out.print("c"); } } }
A. b
B. bc
C. abc
D. The code declaration of the Diet class does not compile.
E. The main() method does not compile.
F. The code compiles but produces an exception at runtime.
G. None of the above
E. Diet is an inner class, which requires an instance of Deer to instantiate. Since the main() method is static, there is no such instance. Therefore, the main() method does not compile, and option E is correct. If a reference to Deer were used, such as calling new Deer().new Diet(), then the code would compile and print bc at runtime
Which of the following are printed by the Bear program? (Choose all that apply.)
public class Bear { enum FOOD { BERRIES, INSECTS { public boolean isHealthy() { return true; }}, FISH, ROOTS, COOKIES, HONEY; public abstract boolean isHealthy(); } public static void main(String[] args) { System.out.print(FOOD.INSECTS); System.out.print(FOOD.INSECTS.ordinal()); System.out.print(FOOD.INSECTS.isHealthy()); System.out.print(FOOD.COOKIES.isHealthy()); } }
A. insects B. INSECTS C. 0 D. 1 E. false F. true G. The code does not compile.
G. The isHealthy() method is marked abstract in the enum; therefore, it must be implemented in each enum value declaration. Since only INSECTS implements it, the code does not compile, making option G correct. If the code were fixed to implement the isHealthy() method in each enum value,then the first three values printed would be INSECTS, 1, and true, with the fourth being determined by the implementation of COOKIES.isHealthy().