Assess Flashcards

1
Q

Chapter 1: Building Blocks

A
String puzzler = """
   One " \
   Two ""\n
   Three \"\"\"
""";
System.out.print(puzzler); //  "   One \"    Two \"\"\n\n   Three \"\"\"\n"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Chapter 2: Operators

A
void print(Collection˂Integer˃ reptiles) {
   if(reptiles instanceof \_\_\_\_\_\_\_\_\_\_\_\_\_) {
      System.out.print(snakes.toString());
   }
}

C. List snakes
F. Set˂Integer˃ snakes
G. List˂Integer˃ snakes

  • the pattern variable type must be a strict subtype of Collection˂Integer˃
    *
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Chapter 4: Core APIs

A

November 6, 2022 is the weekend that clocks fall back for daylight savings time. What is the output of the following? (Choose two.)

var date = LocalDate.of(2022, Month.NOVEMBER, 6);
var time = LocalTime.of(1, 30);
var zone = ZoneId.of("US/Eastern");
var dateTime1 = ZonedDateTime.of(date, time, zone);
var dateTime2 = dateTime1.plus(1, ChronoUnit.HOURS);

long diff = ChronoUnit.HOURS.between(dateTime1, dateTime2);
int hour = dateTime2.getHour();
boolean offset = dateTime1.getOffset() == 
   dateTime2.getOffset();
System.out.println("diff = " + diff);
System.out.println("hour = " + hour);
System.out.println("offset = " + offset);

The dateTime1 variable evaluates to 2016-11-06T01:30-04:00[US/Eastern], and dateTime2 is 2016-11-06T01:30-05:00[US/Eastern]. Option B is correct because it is an hour later. The hours are the same, making option C correct as well. However, the time zone offset changes making it change from 5:30 GMT to 6:30 GMT. Therefore, option E is not correct.

Duration uses units of hours/minutes/seconds

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Chapter 6: Class Design

A

this() call must be the first line of a constructor if present

Common strategy for making a class immutable:
1. Mark the class as final or make all of the constructors private.
2. Mark all the instance variables private and final.
3. Don’t define any setter methods.
4. Don’t allow referenced mutable objects to be modified.
5. Use a constructor to set all properties of the object, making a copy if needed.

interface variables are assumed to be public, static, and final constants.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Chapter 8: Lambdas and Functional Interfaces

A
  • Lambdas are only allowed to reference effectively final variables.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Chapter 9: Collections and Generics

A
public class Ball˂X˃ {
   public static ˂T˃ void catchBall(T t, X x) {}
   public ˂T˃ void dribbleBall(T t, X x) {}
   public ˂X˃ static void fetchBall(X t, X x) {}
   public ˂X˃ void inflateBall(X t, X x) {}
   public ˂T˃ static void spinBall(T t, X x) {}
   public static ˂X˃ void throwBall(X t, X x) {}
}

The catchBall() and spinBall() methods do not compile because X is valid only for instance methods. We can eliminate options A and E. We can also eliminate option C because fetchBall() has the ˂X˃ in the wrong place. It needs to be after the static keyword. The other methods all compile. Option G is correct because inflateBall() redefines X so the type on Ball is hidden. This means both parameters refer to the same type.

  • ArrayList, HashSet, and LinkedList are all allowed to contain null. However, a TreeSet is not,
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Chapter 10: Streams

A
  • Calling get() on an empty Optional causes an exception to be thrown
  • infinite stream is not made finite by the intermediate filter() operation. Therefore, the call to max() never terminates.
  • partitioningBy() or groupingBy()
    Java will infer the right type and pass a Predicate to the former and a Function to the latter. However, that’s not what happens in this example. By creating a Predicate variable, the type has to match, the call to groupingBy()
  • The code tries to use the same stream twice. This is not allowed, and it throws an exception that the stream has already been operated on or closed.
  • There is no such thing as an IntegerStream or OptionalInteger. The correct class names are IntStream and OptionalInt.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Chapter 11: Exceptions and Localization

A
  • Should use throws in the method declaration.
  • FileNotFoundException is a subclass of IOException, so it cannot be used together in the same multi-catch block since it is redundant.

Assume that all of the files mentioned in the answer choices exist and define the same keys. Which one will be used to find the key in line 8?

6: Locale.setDefault(new Locale("en", "US"));
7: var b = ResourceBundle.getBundle("Shark");
8: System.out.println(b.getString("name"));

Java will first look for the most specific match it can find, starting with Shark_en_US.properties. Since it is not found, it drops the country and looks for Shark_en.properties. Since no match is found, Java drops the language and goes on to Shark.properties.

  • The close() method also throws an exception on line 7, but this exception is suppressed.
  • Starting with Java 15, NullPointerException stack traces include the name of the variable that is null by default,
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Chapter 12: Modules

A
  • -module-path option is available on javac, java, and jdeps.
  • -p is equivalent for the javac and java commands. The jdeps command does not allow the short form.
  • The only command that has a show-module-resolution option is java, and the only command that has a summary option is jdeps.
  • The water and rain modules depend on each other. Since this is a cyclic dependency
  • A. javac, B. java, C. jdeps, D. jlink all take a –module-path option, expect jmod
  • Top-down migration starts with putting all JARs on the module path as automatic modules, making option D correct. Bottom-up migration starts with leaving all JARs on the classpath as unnamed modules, making options B and C correct.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Chapter 13: Concurrency

A
  • For a ReentrantLock, a thread must call unlock() the same number of times it locks the object, or the lock will not be released.
  • Since the lock is never released by the first thread, the second thread will hang indefinitely
  • The variable w refers to System.out, which is closed at the end of the try-with-resources statement but before println() is called. Since PrintStream instances do not throw exceptions when closed, nothing is printed at runtime, and option D is correct. If the stream were open, then it would print LX.
  • The scheduleAtFixedRate() method requires a TimeUnit parameter
  • The code throws a ConcurrentModificationException at runtime on line f1, since list1 is a regular collection and not a concurrent one.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Chapter 14: I/O

A
  • Files.find() method takes three arguments
  • toRealPath() declares IOException
  • return type of Files.delete()
  • The Files.deleteIfExists() method does return a boolean
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Chapter 15: JDBC

A

Since the code calls registerOutParameter() and setInt(), we see it used for both input and output.

In a ResultSet, columns are indexed starting with 1, not 0.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
List˂String˃ cats = new ArrayList˂˃();
cats.add("leo");
cats.add("Olivia");

cats.sort((c1, c2) -˃ -c2.compareTo(c1)); // line X
Collections.reverse(cats);
System.out.println(cats);
A

The code c2.compareTo(c1) sorts in descending order. However, the negative sign before it switches to ascending order. Since uppercase sorts before lowercase, that leaves us with [Olivia, leo].

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
  • A variable name may not be used as a method parameter and a local variable.
  • A method parameter also may not be used as a lambda
  • a local variable may not be used as a lambda variable in the same scope
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

1.

16: try (var sql = “{call learn (?)}”;
17: var cs = conn.prepareCall(sql,
18: ResultSet.TYPE_SCROLL_SENSITIVE);
19: cs.setInt(1, 8);
20: var rs = cs.execute()) {
21:
22: while (rs.hasNext()) {
23: System.out.println( rs.getString(3));
24: }
25: }

A

Lines 16 and 19 do not compile because they are not allowed in a try-with-resources declaration. Only declarations that implement AutoCloseable or Closeable are permitted. Line 17 does not compile because both the ResultSet type and concurrency mode are required if either is specified. Line 20 does not compile because execute() returns a boolean rather than a ResultSet. Finally, line 22 does not compile because the ResultSet method should be next(), not hasNext(). Since there are five errors, option F is correct.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly