1 Flashcards
For the equals()-method, why can’t it always either return true or false?
- equals() always returns false for null
2. equals() always returns true for the object identity
Concerning equals() and hashCode(), name the two rules of the contract!
- if equals == true: hashcode must be equal
2. if equals == false: hashcode can be equal
Is the following legal? public class Outer { public static class Inner { } public static void main(String[] args) { Inner inner = new Outer().new Inner(); } }
No it is not. This results in a compiler error!
An welchen 2 Plätzen können Generics Definitionen stattfinden?
- Direkt nach dem Klassennamen
2. Direkt vor dem return Type
What is the natural ordering concerning Numbers and Strings in Java?
Lowest first:
Firstly 0-9: 0, 3, 67, 232
Then uppercase A-Z: AAB, ABB, ZDS
Then lowercase a-z: aab, abb, zds
When adding a non-Comparable Class-Instance to a sorted data structure, which has not been provided a Comparator, does this lead to:
- A compiler error
- An Exception
- Unexpected behaviour
This leads to an Exception at Runtime.
It isn’t being checked at compile-time, since both comparable and non-comparable Instances can be added to a sorted data structure.
What does the following situation lead to?
Stream s = Arrays.asList(“Hello”, “World”).stream();
s.forEach(System.out::println);
s.forEach(System.out::println);
An Exception at runtime, since the stream is used twice.
This can’t be checked by the compiler.
Is the following code legal?
Arrays.asList(“Hello”,”World”)
.filter(!String::isEmpty)
.forEach(System.out::println);
No, method references cant be combined with operators!
What TemporalUnit-Enum type can be passed to the Instant-Classes plus() method?
Everything smaller than (including) DAYS (DAYS, HALF_DAYS, HOURS, MINUTES…)
What TemporalUnit-Enum type can be passed to the LocalDateTime-Classes plus() method?
Everything smaller than (including) HALF_DAYS (HOURS, MINUTES, SECONDS…)
What TemporalUnit-Enum type can be passed to the LocalDate-Classes plus() method?
Everything larger than (including) DAYS (WEEKS, MONTHS, YEARS)
Explain the behaviour of Path.relativize() when passing it a relative path, while the reference path is absolute.
This throws an IllegalArgumentException!
What happens when you pass an absolute Path to the Path.resolve()-method?
It returns the absolute path directly.
What needs to be done for a change made by an updateable ResultSet to actually take effect?
The ResultSets updateRow()-method has to be called.
Concerning the InputStreams marking feature, which method actually throws an Exception if marking is used even though it is not supported?
The reset()-method throws the Exception.
Is ObjectIntConsumer a functional Interface?
No ObjectIntConsumer is not a functional Interface.
Is ToLongBiFunction a functional Interface?
Yes ToLongBiFunction is a functional Interface.
Describe the process when a ResourceBundle for a given Locale is requested, but the Locale is not present while a different default Locale is set.
It first checks out if a ResourceBundle for the given Locale is present.
Then it defaults back to the Systems default Locale.
Only if the Systems default Locale has no associated ResourceBundle, does it go back to the root ResourceBundle.
Does java.util.Stream have an isEmpty()-Method?
No, since the size of the stream is not known until runtime!
Is “Collection coll = new ArrayList();” legal?
Yes, it is legal since ArrayList conforms to Collection>.
Is “Collection coll = new ArrayList();” legal?
Yes, it is legal since ArrayList conforms to Collection.
Can interfaces be marked final?
No
Is NotSerializableException a checked or an unchecked Exception?
It is a checked Exception.
Is SQLException a checked or an unchecked Exception?
It is a checked Exception.
Is ParseException a checked or an unchecked Exception?
It is a checked Exception.
What happens if you do not provide a merger to the Collectors.toMap()-Method?
If there is a duplicate key an IllegalStateException is thrown!
Name an alternative way to retrieving ChronoUnit.DAYS.between(date1, date2);
date1.until(date2, ChronoUnit.DAYS);
Can you compare int and char values with “==”?
Yes, you can.
Will “Runnable r = () -> 1;” compile?
No, it won’t since it can’t have a return value!
Will the following compile? Runnable r = () -> myMethodWithReturn(); String myMethodWithReturn() { return "Hello World"; }
Yep, even though the method returns something it is legal! This is simply because Java is a bad programming language and anybody who says otherwise does not know what they are talking about.
Name something notable about the parallel reduction of a stream.
The Identity is applied to each element of the Stream!
In which way does a TreeMap order it’s elements?
It orders them by the keys, rather than the values!
Does Files.delete throw any checked Exceptions?
No, it only throws an unchecked Exception!
Name four important method of the Writer class!
- append
- close
- flush
- write (also takes in a char-Array)
What type does the parameter of the String.equalsIgnoreCase()-Method have?
String
Are InputStream and Reader classes or Interfaces?
They are abstract classes!
How would you read the creation time of a File when you have access to a BasicFileAttributesView?
You first have to call readAttributes on the instance of BasicFileAttributesView, then you can call creationTime() on the returned BasicFileAttributes.
Does the following compile? public int myMethod() { short s = 1; return s; }
Yes, since method return types enable autoboxing.
Is the following legal?
long l = 1L;
double d = l;
Yes, since long can be converted to double without loss of precision.
Is the following legal?
double d = 1.0;
long l = d;
No, since loss of precision would occur!