...


Fichier Détails

Cartes-fiches 83
Langue English
Catégorie Informatique
Niveau Autres
Crée / Actualisé 02.05.2019 / 03.05.2019
Lien de web
https://card2brain.ch/box/20190502_ocp_before_exam
Intégrer
<iframe src="https://card2brain.ch/box/20190502_ocp_before_exam/embed" width="780" height="150" scrolling="no" frameborder="0"></iframe>

What is the goal of Stream.collect()?

To create a mutable reduction for performance, eg. for StringBuilder or Lists:

Stream<String> stream = Stream.of("a", "b");

TreeSet<String> set = stream.collect(TreeSet::new, TreeSet::add, TreeSet::addAll)

AddAll can combine multiple subsets of Treeset - used when processed in parallel!

What the name of the single abstract method of DoubleConsuer, IntConsumer and LongConsumer?

accept

What is the goal of the UnaryOperator Interface?

A special case of Function, for which the Input and Output Parameter are the same. It is actually a subclass of the Function interface

What the name of the single abstract method of DoubleSupplier, IntSupplier and LongSupplier?

getAsDouble

getAsInt

getAsLong

What the name of the single abstract method of DoublePredicate IntPredicate and LongPredicate?

test

What the name of the single abstract method of DoubleFunction<R>, IntFunction<R> and LongFunction<R>?

R apply(double|int|long arg)

Which functional interface would you use to fill in the blank to make the follwing code compile?

double d = 1.0;

____ f1 = x -> 1;

f1.applyAsInt(d);

double d = 1.0;

DoubleToIntFunction f1 = x -> 1;

f1.applyAsInt(d);

Accepts a double and returns an int (implied by the name applyAsInt)

What is the goal of the Function Interface?

Turning one parameter into a value of a potentially different type and returning it

What the name of the single abstract method of DoubleUnaryOperator, IntUnaryOperator and LongUnaryOperator?

applyAsDouble

applyAsInt

applyAsLong

What must be considered when using the plus___ and minus___ Methods on  LocalDate / LocalTime / LocalDateTime / ZonedDateTime?

Check for compatibility! Years, Months, Week and Days do not work on LocalTime and Hours, Minutes, Seconds and Nonos do not work on LocalDate!

What is the purpose of the Collectors.mapping function?

It is useful for multi-level reduction.

Map<City, Set<String>> lastNamesByCity = people.stream()
     .collect(
            groupingBy(Person::getCity,
               mapping(Person::getLastName, toSet())));

How can you manually construct a ZonedDateTime instance? Are there any pitfalls?

ZoneId zone = ZoneId.of("US/Easter");
ZonedDateTime zone = ZonedDateTime.of(2019, 1, 20, 6, 15, 30, 200, zone); # Last Parameter
 

There isn't an option to pass the Month-Enum!

What is the Output of the following and what does it mean?

System.out.println(Duration.ofDays(1))

PT24H

PT stands for Period of Time (=Duration). 24 Hours are printed but minutes and seconds are omitted (empty)

What is an instant? How can it be constructed?

An Instant represents a specific moment in time in the GMT time szone.

aZonedDateTime.toInstant();
Instant.now()

What has to be considered when calculating instants with the plus methods?

You can only add Days, Hours but no weeks and years.
eg. instant.plus(1, ChronoUnit.WEEKS) throws an exception

What happens with LocalDate right before and after a dailight saving change?

The timeZone offset changes, eg:

2016-03-13T01:30-05:00[US/Eastern]

2016-03-13T03:30-04:00[US/Eastern]

How can you create a Java Class Bundle?

By extending the LsitResourceBundle

How does java determine which resource bundle to use?

  • Always look for the Java Class first, then the property file
  • Drop one thing at a time, if there are no matches, strting with the coutnry and then the language
  • Default locale and default resource bulde come last

In which package are the classes for Number formatting?

java.text

What must be considered when using date and time formatting?

Not everything is compatible, eg DateTimeFormatter.ofLocalizedTime cannot be used with a LocalDate

When is a java.text.ParseException thrown? Is it checked?

Used when converting a String to a number.

IT IS CHECKED!

Is the NotSerializableException checked?

Yes (extends IOException)

Can you declare more than one finally block?

No

What rules apply to the catch clauses?

  • It is ilegal to declare a subclass exception in a chatch block that is lower down in the list than a superclass
  • you cannot catch a checked exception type that cannot potentially be thrown by the try clause

What happens if an exception is thrown in a try-with-resource statmement?

It depends on how the try block endend:

  • Try-block ended normally: The exception from the close method is thrown and must either be handeled in a catch block or by the caller (checked exceptions must be declared, of course)
  • Try-block ended with an exception: The exception from the close method is added as a suppressed exception to the one from the try-block.

What does this code do?
public void rerhrowing() throws SQLException, DateTimeParseException {
  try {
    parseData();
  } catch (Exception e) {
    System.err.println(e);
    throw e;
  }
}

It is a short for the following code to prevent duplicates. Java can infer the specific Exception from the method signature

public void rerhrowing() throws SQLException, DateTimeParseException {
  try {
    parseData();
  } catch (SQLException | DateTimeParseException e) {
    System.err.println(e);
    throw e;
  }
}

Is IllegalFormatException a checked exception?

No

What has to be minded when using the Thread.sleep method?

It declares the checked InterruptedException which must be handeled

What is important when using the ExecutorService class?

It is important to call the shutdown() method. A thread executor creates a non-demon thread on the first task that is executed, so failing to call shutdown will result in you application never terminating.

What is the difference between Runnable and Callable

Callable can return a value and allows a throws declaration of Exception

@FunctionalInterface Callable<V> { V call() throws Exception;}

@FunctionalInterface Runnable { void run()}

What common tools does java offer to prevent race conditions?

  • Monitor using the synchronized keyword = Locking
  • Atomic clases that provide atomic operations

What is the difference between a BlockingQueue/BlockingDeque and "normal" Queue/Deque?

The Blocking versions offer additional methods that will wait for a specific amount of time to complete an operation:

eg. boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException -> Returns false on timeout

eg. E poll(long timeout, TimeUnit unit) throws InterruptedException -> Returns null on timeout

What is the main difference betwen a serial stream an a parallel stream?

Seria streams are ordered whereas parallel streams are not necessarily

What are the three steps of the fork/join framework?

  1. Create a ForkJoinTask
    • ForkJoinTask<?> task = new MyActionOrTask(...);
  2. Create the ForkJoinPool
    • ForkJoinPool pool = new ForkJoinPool();
  3. Start the ForkJoinTask
    • pool.invoke(action)
    • Object result = pool.invoke(task);

Do ExecutorService.submit and ExecutorService.execute throw a checked exception

No

How can you convert an INputStream into a Reader

By wrapping it with a InputStreamReader

What are PrintStream and PrintWriter for?

They write formatted representations of Java objects to a binary stream

Is the int parameter for the InputSTream.mark()  method required?

Yes

How can you determine EOF when using a FileReader?

When the readLine() method returns null or or read() returns -1

How can you determine EOF when using a ObjectInputStream?

Catch the EOFException!