1Z0-829 Flashcards
What is the command line used to compile the Java class Wolf.java in the default package, and what file does it generate?
- javac Wolf.java
-
Wolf.class
Run: javac Wolf.java (and it generates Wolf.class)
True or false?
An interface can extend multiple interfaces.
True
What security attack does using a PrepraredStatement help with?
SQL injection
True or false?
A static method is allowed to reference an instance variable.
False
(except through a reference to a class instance)
What methods on PreparedStatement can run a DELETE SQL statement?
executeUpdate() and execute()
What command is used to create a runtime image?
jlink
True or false?
The sorted() intermediate operation has overloaded versions that allow passing or omitting a Comparator.
True
Which Collections method provides a thread-safe version of an existing Map?
Collections.synchronizedMap()
True or false?
A private static interface method can access a default interface method within the same interface.
False
Given p || q, where both p and q are true expressions, which side will not be evaluated at runtime?
q
What method is used to retrieve a parallel stream from an existing stream?
parallel()
What is unreachable code?
Any lines of code that the compiler determines it is not possible to reach at runtime
True or false? Arrays.asList() returns an immutable list.
False. It returns a fixed-sized list backed by an array.
True or false? Polymorphism allows an object to be passed as a class it inherits but not an interface type.
False. Polymorphism allows an object to be passed by any reference type that the class inherits.
What is a redundant package import?
A package import that is unnecessary. Removing the import does not prevent the code from compiling.
What NIO.2 Files methods allow you to both read and write file attributes?
Files.getFileAttributeView()
What is a locale category, and how is it used?
A locale category is a way to differentiate between the display and formatting of locale-based values.
Which method should be called before calling mark() on an arbitrary InputStream?
markSupported()
True or false? Objects in Java can inherit at most one class.
False. While Java does rely on single inheritance, a class can extend another class, so a single class may inherit multiple classes.
What does executeQuery() return in JDBC?
ResultSet
True or false? If a module doesn’t contain a module-info file, one is automatically generated.
False
Under what conditions may a resource created before a try-with-resources statement be used in a try-with-resources declaration?
The resource must be marked final or effectively final.
True or false? If a method has an int return type and does not throw an exception, it must return a value.
True
Name two ways to prevent an immutable class from being extended by a mutable subclass.
Mark the class final or make all of the constructors private.
Which Lock method will wait indefinitely for a lock if it cannot be acquired?
lock()
Which of the following requires an explicit cast to be assigned to a short value? int, byte, float, bit
int and float require an explicit cast, while byte does not. Note that bit is not a primitive type in Java.
Given an interface Fly with no abstract methods, how would you create an instance of an anonymous class that implements it?
new Fly() {}
Which of these are required in a stream pipeline: source, intermediate operation, terminal operation?
Source and terminal operation
Given an outer class Rooster and its inner class Wattle, how would you create an instance of Wattle within an instance method of Rooster?
new Wattle()
What type of module is on the classpath?
Unnamed module
True or false?
All operators are evaluated from left to right.
False.
The assignment operator is evaluated from right to left.
True or false? Overloaded methods have the same method signatures.
False. The parameter lists must be different.
True or false? A static method is allowed to reference an instance variable.
False (except through a reference to a class instance)
Which Collections method provides a thread-safe version of an existing Map?
Collections.synchronizedMap()
Either of which two complementary operators can be used to test if x is greater than or equal to y?
> = or <
What does the following output?
~~~
int[] x = { 7, 8, 9};
int[] y = { 7, 7, 7};
System.out.println(Arrays.compare(x, y) + “ “ + Arrays.mismatch(x, y));
~~~
1 1
If an interface is annotated with @FunctionalInterface, what three common abstract methods can the interface declare in addition to the custom single abstract method?
toString(), hashCode(), and equals()
True or false? A vararg that takes a list of int values can be written within a method declaration using int… or int[].
False. Only … can be used to declare a vararg.
True or false? If a text block ends with gerbil”””, a new line is added.
False
True or false? An instance method that uses the synchronized modifier is equivalent to a method that does not but wraps the body of the method in synchronized(this) {} block.
True
On what class do you call a method to create a CallableStatement?
Connection
True or false?
All method references can be rewritten as a lambda.
True.
A method reference is a short form of a lambda.
var can be used as the type for which of the following: instance variable, static variable, local variable, constructor parameter, method parameter?
var can be used only as a local variable.
Which functional interface takes two parameters and returns a boolean?
BiPredicate
Without using any variables, what could you write to negate the value -5
?
-(-5)
True or false? If a try-with-resources block causes multiple exceptions to be thrown at the same time, the first exception encountered becomes the primary, and the rest become suppressed exceptions.
True
Fill in the blank: A _
class is the first non-abstract class that extends an abstract class and is required to implement all inherited abstract methods.
concrete
What does JAR stand for?
Java archive
True or false? A class must implement Closeable to be used in a try-with-resources statement.
False. The class must implement AutoCloseable.
Fill in the blank with explicit or implicit: Accessing an object by one of its supertypes can be done with an \_\_\_\_\_\_\_\_\_
cast.
implicit
What parameter type can fill in the blank for a CallableStatement for the following? cs.setInt(\_\_\_\_\_, 1);
int or String
Which functional interface is declared with two generics and has an apply() method?
Function
What will equals() return when comparing a List and Set with the same elements?
false
True or false? Unary operators have the highest order of precedence.
True
What is the name of the method that creates Path objects in the Paths class?
Paths.get()
How do you create an Optional without a value?
Optional.empty()
True or false? In a JDBC URL, the port is required.
False
Fill in the blank to make this code compile:
~~~
List<______ Number> listOfNumbers = new ArrayList<object>();
~~~</object>
? super (this is a lower bound)
How many operators in Java take three values?
Just one, the ternary operator
Name a terminal operation that can always terminate on an infinite stream.
findAny() or findFirst()
What methods can remove whitespace from the end of a String ?
trim(), strip(), stripTrailing()
What symbol is used to represent a bind variable in a PreparedStatement?
Question mark (?)
Where can a call to super() or this() occur within a constructor?
As the first statement of the constructor
What character is used to escape values in a date formatter?
A single quote (‘)
What is it called when two threads are stuck forever but appear to be active and making progress?
Livelock
Which data types are supported as the target of an if statement’s conditional clause?
Only boolean
When passing an object to a method, how can the method be written to change the object that the caller references?
It cannot. A method cannot directly change the reference the caller passes so that the caller sees the change.
What functional interface does Java provide that declares a single method that returns a boolean?
Predicate
What Map method gives you all the keys?
keySet()
Write a for-each loop that iterates over an int[] data in reverse.
Trick question. For-each loops cannot be used to iterate over a data structure in reverse order.
Fill in the blank: An instance initializer can access \_\_\_\_\_\_\_\_\_
variables declared in the class.
static and instance
True or false?
If an automatic module name is not specified, Java keeps dots in the generated name.
True
Which keyword(s) can be used to access a member declared within the class?
this
When calling a String.format() method, what is the meaning of the symbols %n and %d?
The %n symbol is used to indicate a new line, while the %d symbol is used for integer values.
Name the four rules an overridden method must follow.
An overridden method must have the exact same signature, the same or a more accessible access modifier, and a covariant return type. It also must not throw a new or broader checked exception.
What is the purpose of using a try-with-resources statement?
Ensures that resources like files and database connections are properly closed at the conclusion of the try section
What does the Path resolve() method do?
It joins two paths. If the input argument to the method is an absolute path, then that value is returned.
Fill in the blanks: When executing a jar command, \_\_\_\_\_
is used to specify the JAR should be created, while \_\_\_\_\_
is used to specify the directory used to create the JAR.
Lowercase -c is used to specify that the JAR should be created, while uppercase -C is used to specify the directory used to create the JAR.
What is the most common signature of a main() method?
public static void main(String[] args)
What does the following expression return?
~~~
new StringBuilder(“Hello”).equals(“Hello”)
~~~
false, as they are not the same object type
When converting from an IntStream to a CharStream, what mapping method do you call?
Trick question. There is no CharStream.
Does Duration or Period support hours?
Duration
Which option should you use when calling Files.copy() if you want to overwrite the target file if it already exists?
StandardCopyOption.REPLACE_EXISTING
What is the difference between the & and && operators when applied to boolean expressions?
The logical operator (&) evaluates both sides of the expression, while the short-circuit operator (&&) only evaluates the right side of the expression if the left side is true.
True or false? Queue.of() returns an immutable list.
False. This method does not exist.
True or false? Creating an instance variable with package access allows a subclass to access it under certain circumstances.
True, if the subclass is in the same package
What method should you call for OUT parameters when working with a CallableStatement?
registerOutParameter()
What functional interfaces have andThen() as a helper method?
Consumer and Function
Besides letters and numbers, what symbols are permitted to be used in identifiers?
_ and currency symbols
How many loop variables can a traditional for loop have?
There is no specified limit.
Given an interface Cat with a default method meow(), what is the syntax for accessing this default method within a class that implements Cat, assuming that the class may override the method?
Cat.super.meow()
Do lower or upper bounds allow you to add elements to a list?
Lower bounds do because they specify the class that all types must be above using super.
True or false? Garbage collection is guaranteed to run at a fixed schedule and when the JVM is low on memory.
False. The JVM controls when garbage collection is run, and when it occurs is not guaranteed.
Which keyword(s) can be used to access a member of the parent class?
super or this
True or false? If one lambda parameter uses var as the type, all other parameters in that lambda must as well.
True. The type format must be consistent within a lambda.
True or false? An anonymous class can only be declared as extending one class or implementing one interface.
True
What are acceptable data types on the right-hand side of a for-each loop?
A built-in array or class that implements java.lang.Iterable
What String method returns the position of a value in a String?
indexOf()
True or false? A class can implement two interfaces that have the same method signature.
True, provided the methods are compatible using the rules for overridden methods
What is a compile-time constant variable?
A variable that is marked final and initialized with a literal value when it is declared
What is the purpose of sealing an interface?
To restrict which interfaces can extend the interface or classes that can implement the interface
Rank these operators from highest operator precedence to lowest: + (addition) * ! == ++ ||
++
!
*
+
==
||
True or false? An instance method is allowed to reference a static variable.
True
True or false? indent(x) can take positive or negative integers as a parameter.
True. Negative integers will remove leading whitespace.
Besides itself, which numeric primitive types can be implicitly cast to double?
byte, short, int, float, long
Which sorts first: letters or numbers?
Numbers
What Collector would you use to return the type Map<String, List<String>>
?
Collectors.groupingBy()
Which operator can be used to avoid cast exceptions at runtime?
instanceof
True or false? Records are both immutable and encapsulated.
True
A sealed class most closely resembles what other top-level type?
An enum
What Java Collections Framework interface requires unique elements?
Set
A thread has started but is waiting for a lock to become available. What state is it in?
BLOCKED
True or false? A vararg that takes a list of int values can be written within a method declaration using int… or int[].
False. Only … can be used to declare a vararg.