19. Which of the following fill in the blank to print a positive integer? (Choose all that apply.)
A. compare(s1, s2)
B. mismatch(s1, s2)
C. compare(s3, s4)
D. mismatch (s3, s4)
E. compare(s4, s4)
F. mismatch (s4, s4)
19. A, B, D. The compare() method returns a positive integer when the arrays are different and the first is larger. This is the case for option A since the element at index 1 comes first alphabetically. It is not the case for option C because the s4 is longer or for option E because the arrays are the same.
The mismatch() method returns a positive integer when the arrays are different in a position index 1 or greater. This is the case for options B and D since the difference is at index 1. It is not the case for option F because there is no difference.
This is the case for option A since the element at index 1 comes first alphabetically.
This is the case for option A since the element at index 1 comes second alphabetically for the first array s1, making s1 larger.
15. What is the output of the following? (Choose all that apply.)
A. [pig, PIG, 123]
B. [PIG, pig, 123]
C. [123, PIG, pig]
D. [123, pig, PIG]
E. -3
F. -2
G. The results of binarySearch() are undefined in this example.
15. C, E. Numbers sort before letters and uppercase sorts before lowercase. This makes option C one of the answers. The binarySearch() method looks at where a value would be inserted, which is before the second element for Pippa. It then negates it and subtracts one, which is option E.
The binarySearch() method looks at where a value would be inserted, which is before the second element for Pippa.
The binarySearch() method looks at where a value would be inserted, which is after the second element for Pippa.
8. What is the output of calling printType(11)?
A. int
B. small int
C. long
D. unknown
E. Nothing is printed.
F. The code contains one line that does not compile.
G. The code contains two lines that do not compile.
H. None of the above
8. G. The first two pattern matching statements compile without issue. The variable bat is allowed to be used again, provided it is no longer in scope. Line 36 does not compile, though. Due to flow scoping, if s is not a Long, then bat is not in scope in the expression at <= 20. Line 38 also does not compile as default cannot be used as part of an if/else statement. For these two reasons, option G is correct.
The variable bat is allowed to be used again, provided it is no longer in scope.
The variable bat is allowed to be used again, provided it is still in scope.
Describing with jar
Like the java command, the jar command can describe a module. These commands are equivalent:
jar -f mods/zoo.animal.feeding.jar -d
jar --file mods/zoo.animal.feeding.jar --describe-module
The output is slightly different from when we used the java command to describe the module. With jar, it outputs the following:
zoo.animal.feeding jar:file:///absolutePath/mods/zoo.animal.feeding.jar
/!module-info.class
exports zoo.animal.feeding
requires java.base mandated
/!module-info.class
zoo.animal.feeding jar:file:///absolutePath/mods/zoo.animal.feeding.jar!/module-info.class
For the exam, you do not need to memorize the various display and formatting options for each category. You just need to know that you can set parts of the locale independently. You should also know that calling Locale.setDefault(us) after the previous code snippet will change both locale categories to en_US.
You can also sort objects that you create yourself. Java provides an interface called Comparable. If your class implements Comparable, it can be used in data structures that require comparison. There is also a class called Comparator, which is used to specify that you want to use a different order than the object itself provides.
The exam might also try to trick you. Do you see why this code doesn’t compile?
If your answer is that there is a missing keyword, you’re absolutely right. The exception is
never instantiated with the new keyword.
The exception is never instantiated without the new keyword.
There is also a class called Comparator, which is used to specify that you want to use a different order than the object itself provides.
The data structures that involve sorting do not allow null values.
Creating .class Files for Inner Classes
Compiling the Home.java class with which we have been working creates two class files. You should be expecting the Home.class file. For the inner class, the compiler creates Home$Room.class. You don’t need to know this syntax for the exam. We mention it so that you aren’t surprised to see files with $ appearing in your directories. You do need to understand that multiple class files are created from a single .java file.
Creating .class Files for Nested Classes
Why Can Local Classes Only Access final or Effectively Final Variables?
Earlier, we mentioned that the compiler generates a separate .class file for each inner class. A separate class has no way to refer to a local variable. However, if the local variable is final or effectively final, Java can handle it by passing a copy of the value or reference variable to the constructor of the local class. If it weren’t final or effectively final, these tricks wouldn’t work because the value could change after the copy was made.
Earlier, we mentioned that the compiler generates a separate .class file for each nested class.