Test 3 Flashcards

1
Q

in a lower bounded super Number> you can only send Number or parents of Number to that list, but you can add any child or parent of Number to that list

A

When retrieving data from lowerBound you need to cast the get method, but when retrieving from lowerBound, there is no need, but only for the specific type, not childs.

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

java.nio.file.NoSuchFileException: test.txt
This exception will be thrown when the program tries to create a BufferedReader to read the file specified by the Path object.

A

java.nio.file.InvalidPathException : test.txt

This exception is thrown when the argument passed while creating Path object is invalid. For example, “c:c:test.txt”.

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

setSavepoint()

rollback()

A

c.rollback() is called. This will cause the whole transaction to rollback.

save point revierte hasta ese punto y no mas, si se elimina algo y luego se hace rollback, se reinicia todo lo que se hizo luego del savePoint

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

Solo con crear un nuevo fileWritter se crea un archivo, si no existe, si ya existe lo sobreescribe al menos que tenga en su constructor append: true

A

asd

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

Duration does not convert hours into days.

A

If call sort and the collection does not implement comparable and not send a comparator and use generics then not compile

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

Throws en los hijos solo pueden tirar checked que sean igual o hijas de las exceptiones de los padres , o tambien no pueden tirar nada

A

as

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

Files.isSameFile method doesn’t check the contents of the file. It is meant to check if the two path objects resolve to the same file or not

Files.copy method will copy the file test1.txt into test2.txt. If test2.txt doesn’t exist, it will be created.

A

ok

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

Remember that variables are not overridden.

A

All remove methods from deque or queue removes from the front

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

compareAndSet(expectedValue, newValue) is meant exactly for this purpose. It first checks if the current value is same as the expected value and if so, updates to the new value.

A

public final boolean compareAndSet(int expect, int update)
Atomically sets the value to the given updated value if the current value == the expected value.

Parameters: expect - the expected value update - the new value

Returns: true if successful. False return indicates that the actual value was not equal to the expected value.

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

Manipulating a stream doesn’t manipulate the backing source of the stream. Here, when you chain the sorted method to a stream, it returns a reference to a Stream that appears sorted. The original List which was used to create the stream will remain as it is.

A

maps worked the same way at generics that collection do

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

You can enable or disable assertions in the unnamed root (default)package (the one in the current directory) using the following commands: java -ea:… myPackage.myProgram java -da:… myPackage.myProgram

A

df

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

By default, Files.move method attempts to move the file to the target file, failing (throwing exepction) if the target file exists except if the source and target are the same file, in which case this method has no effect SAME AS FILES:COPY

A

However, when the CopyOption argument of the move method is StandardCopyOption.ATOMIC_MOVE, the operation is implementation dependent if the target file exists. The existing file could be replaced or an IOException could be thrown.

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

Map.Entry pollFirstEntry()
Removes and returns a key-value mapping associated with the least key in this map, or null if the map is empty.

Map.Entry pollLastEntry()
Removes and returns a key-value mapping associated with the greatest key in this map, or null if the map is empty.

A
NavigableMap tailMap(K fromKey, boolean inclusive) 
          Returns a view of the portion of this map whose keys are greater than (or equal to, if inclusive is true) fromKey. 

Note that the tailmap is backed by the original map so //LINE 4 affects the original map as well.

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

Files writer y FileOutputStream crean el archivo al nomas crear el objecto, sin necesidad de escribir nada

A

asd

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

Collectors.mapping vs Collectors.groupingBy

A

To collect into a Map that contains a single value by key (Map), use Collectors.toMap().
To collect into a Map that contains multiple values by key (Map>), use Collectors.groupingBy().

And mapping. converts the stream from one type to the other

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

public class ThreadLocalRandom extends Random A random number generator isolated to the current thread. Like the global Random generator used by the Math class, a ThreadLocalRandom is initialized with an internally generated seed that may not otherwise be modified. When applicable, use of ThreadLocalRandom rather than shared Random objects in concurrent programs will typically encounter much less overhead and contention. Use of ThreadLocalRandom is particularly appropriate when multiple tasks (for example, each a ForkJoinTask) use random numbers in parallel in thread pools. Usages of this class should typically be of the form: ThreadLocalRandom.current().nextX(…) (where X is Int, Long, etc). When all usages are of this form, it is never possible to accidently share a ThreadLocalRandom across multiple threads

A

asd

17
Q

Object class’s hashCode method returns unique hashcode for every object.

A

asd

18
Q
The synchronized keyword can be applied only to non-abstract methods that are defined in a class or a block of code appearing in a method or static or instance initialization blocks. It cannot be applied to methods in an interface .It cannot be applied to an abstract method of a class.
Lambdas only works with interfaces not abstract classes
A

asd

19
Q

Since the numbering of columns in a ResultSet starts with 1, getString(1) will return SID. Although the value of the SID field is INT, it can still be retrieved using getString(). Note that if a field is of type VARCHAR and if you try to retrieve the value using say getInt(), it may throw an exception at runtime if the value cannot be parsed into an Integer.

A

1

20
Q

Input params of a public method should not be validated using assertions.

Post conditions for all methods can be ensured using assertions, means put asserts in the end of the method to validate.

Input params of a private method can be validated using assertions [But NOT for public methods. public methods should throw regular exceptions when passed bad parameters.)

A

Should not be used for side effects.

For example:

public boolean doSomething() {

}

public void someMethod() { 
                 assert doSomething(); 
}
21
Q

The Collector created by Collectors.toMap throws java.lang.IllegalStateException if an attempt is made to store a key that already exists in the Map.

A

If you want to collect items in a Map and if you expect duplicate entries in the source, you should use Collectors.toMap(Function, Function, BinaryOperator) method. The third parameter is used to merge the duplicate entries to produce one entry.

For example, in this case, you can do: Collectors.toMap(b->b.getTitle(), b->b.getPrice(), (v1, v2)->v1+v2) This Collector will sum the values of the entries that have the same key. Therefore, it will print : Gone with the wind 15.0 Atlas Shrugged 15.0

22
Q

valueOf method tries to match the String argument exactly (i.e. case sensitive) with an enum constant and returns that constant if successful otherwise it throws java.lang.IllegalArgumentException.

A

Since enum maintains exactly one instance of its constants, you cannot clone it.

23
Q

PrintWriter does not have write(boolean ) method. It does have write(String), write(int ), write(char[] ) methods. It also has write(char[] buf, int off, int len) and write(String buf, int off, int len) methods that let you write a portion of the input buf.

A

Remember that none of PrintWriter’s print or write methods throw I/O exceptions (although some of its constructors may). This is unlike other streams, where you need to include exception handling (i.e. a try/catch or throws clause) when you use the stream.

24
Q

Files.find method returns a stream of Path objects.

The following is the correct signature for the find method. public static Stream find(Path start, int maxDepth, BiPredicate matcher, FileVisitOption… options) throws IOException Return a Stream that is lazily populated with Path by searching for files in a file tree rooted at a given starting file.

There is no readLines method in BufferedReader

The usage of Files.list method is correct but it returns a stream of Path objects for files contained in a directory. Unlike the Files.find method, the list method doesn’t search for files. It just returns all the files in a given directory.

A

Stream lines = Files.list(Paths.get(“c:\temp\test.txt”));, it will compile but will throw java.nio.file.NotDirectoryException because test.txt is not a directory

25
Q

resolveSibling

A

public Path resolveSibling(String other) or public Path resolveSibling(Path other)

Resolves the given path against this path’s parent path. This is useful where a file name needs to be replaced with another file name. For example, suppose that the name separator is “/” and a path represents “dir1/dir2/foo”, then invoking this method with the Path “bar” will result in the Path “dir1/dir2/bar”. If this path does not have a parent path, or other is absolute, then this method returns other. If other is an empty path then this method returns this path’s parent, or where this path doesn’t have a parent, the empty path.

26
Q

Lock.lock() returns void. Lock.tryLock() returns boolean. Had the code been: ReentrantLock rlock = new ReentrantLock(); boolean f1 = rlock.tryLock(); System.out.println(f1); boolean f2 = rlock.tryLock(); System.out.println(f2); It would have printed: true true Note that ReentrantLock implements Lock.

A

ok