Last Flashcards

1
Q

What primitive types can be written as binary?

A

byte
short
integer
long

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

Where can’t the underscore in digits be placed?

A
  • At the beginning or end of a number
  • Next to a decimal point
  • Prior to an F or L suffix
  • In positions where an string of digits is expected (for example splitting 0X for hexadecimal in 0_X);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Given the following classes:
interface Account{
public default String getId(){ return “0000”; }
}
interface PremiumAccount extends Account{
public String getId();
}

And a class implementing PremiumAccoun, should it implement getId()?

A

Yes.

getId is redeclared as abstract and should be implemented by the implementing class.

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

Given a try-with-resource construction with two resources. The try throws an Exception and one of the two resources throws an IOException when closed. What will be Exception that is propagated to the method calling?

A

It will propagate the Exception.

IOException will be added as a suppressed exception to the Exception.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
Which of the following are reduction methods on the Stream interface?
filter
reduce
sum
max
add
A

reduce, max

Reduction means going from a Stream of objects to a single object. Filter could still result in multiple results, sum is not available on the Stream interface and add is not a method on the Stream interface.

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

What is the result?

Path p1 = Paths.get(“c:\personal\.\photos\..\readme.txt”); Path p2 = p1.normalize();

A

c:\personal\readme.txt

. will always be redundant
.. will eliminate the preceding path (photos in this case)

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

Describe the Supplier functional interface

A
Supplier
R get();

Supplier sup = () -> “test”;

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

Describe the Function functional interface

A
Function
R apply(T t);

Function fu = (x) -> x.toString();
String y = fu.apply(1);

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

Describe the Predicate functional interface

A
Predicate
boolean test(T t);

Predicate pre = (x) -> x.equals(“hello”);
Boolean b = pre.test(“hello”);

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

Describe the Consumer functional interface

A
Consumer
void accept(T t);

Consumer con = (x) -> System.out.println(x);
con.accept(“hello”);

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

Describe the UnaryOperator functional interface

A
UnaryOperator (extends Function)
T apply(T t);

UnaryOperator op = (x) -> “hello “ + x;
op.apply(“world”);

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

What is the result of the p2, p3 and p4:
Path p1 = Paths.get(“c:\temp\test.txt”);
Path p2 = p1.resolve(Paths.get(“report.pdf”));
Path p3 = p1.resolve(Paths.get(“c:\report.pdf”));
Path p4 = p1.resolve(Paths.get(“”));

A

p1. c:\temp\test.txt\report.pdf
p2. c:\report.pdf
p3. c:\temp\test.txt

  1. If the argument is a relative path (i.e. if it doesn’t start with a root), the argument is simply appended to the path to produce the result.
  2. If the argument starts wiith a root it will simply return the argument.
  3. If the argument is an empty path it will return this path (p1).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are the rules for the path.relativize(Path path) method?

A
  • If this path and the given path do not have a root component
  • If this path and the given path have a root component then the it’s implementation dependent if a path can be constructed
  • If this path and the given path are equal, an empty path is returned.

The returned path can be appended to the this path and result in the given path.

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

Given that daylight Savings Time ends on Nov 1 at 2 AM in US/Eastern time zone. (As a result, 2 AM becomes 1 AM.), what will be the result of x?
LocalDateTime ld1 = LocalDateTime.of(2015, Month.NOVEMBER, 1, 2, 0);
ZonedDateTime zd1 = ZonedDateTime.of(ld1, ZoneId.of(“US/Eastern”));
LocalDateTime ld2 = LocalDateTime.of(2015, Month.NOVEMBER, 1, 1, 0);
ZonedDateTime zd2 = ZonedDateTime.of(ld2, ZoneId.of(“US/Eastern”));
long x = ChronoUnit.HOURS.between(zd1, zd2);

A

-2

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

Given a map with keys of type String and value of type String. When a value exists with key a and value “hello”. How can I merge a value into key a?

A

map.merge(“a”, “world”, (x, y) -> String::concat);

map.merge takes a BiFunction as the third argument.
When using a class method reference the first argument will be used as the caller for the method, the other arguments are used as parameters for the method reference.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

True or False

Given a Future object when get is called it will directly return regardless if the Future is completed.

A

False, get on the Future object will wait untill the future is completed.

17
Q

How can you use Files.find to find all files ending with .java starting in /home, with a directory depth of 5?

A

Stream s = Files.find(Paths.get(“/home”), 5, (p, a) -> p.endsWith(“.java”));

Note: the stream is lazy.

18
Q

How can you use the Files.lines method to read all lines from a file text.txt?

A

Stream l = Files.lines(Paths.get(“text.txt”));

Stream l = Files.lines(Paths.get(“text.txt”), Charset.defaultCharset());

19
Q
What is the result of the following?
interface Doer{ 
String doIt(int x, String y); 
}

Doer d = (int a, String b)->b.substring(0, a);

A

Compiles fine.

It’s not required but it you can state the types in a lambda.

20
Q

What will the following print?
Path p1 = Paths.get(“c:\temp\test1.txt”);
Path p2= Paths.get(“c:\temp\test2.txt”);
Files.copy(p1, p2, StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING);
if(Files.isSameFile(p1, p2)){
System.out.println(“file copied”);
}else{
System.out.println(“file not copied”);
}

A

file not copied

Files.isSameFile is meant to check it two paths are the same. It will not check the actual contents of the files.

21
Q

What is the Collectors.summarizing used for?

A

Takes a ToIntFunction to convert the incoming object to a primitive int. It then returns a IntSummaryStatistics. With for example getSum() methods.

22
Q

What is the result of the following?

Path p1 = Paths.get(“c:\..\temp\test.txt”); System.out.println(p1.normalize().toUri());

A

file:///c:/temp/test.txt

.. is ignored because the preceding part is the root.

23
Q

How can you truncate a Instant?

A

Instant now = Instant.now();
now.truncatedTo(ChronoUnit.DAYS);

All fields smaller than DAYS will be set to 0 (hours, minutes, seconds etc.).