Language Enhancements Flashcards

0
Q

When initialize variable from primitive type does new object is used?

A

No

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

Using String in switch statement uses equals method to compare Strings

A

Yes

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

The integral types byte short int long can be expressed using the binary number system…

A

Yes

(byte)0b00100001

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

Using binary literals 0b or 0B is the same

A

Yes

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

We can use any number of underscores _ anywhere between digits in numerical literals

A

Yes

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

We can place underscore in adjacent to the decimal point literal

A

No 3.14_5555 is invalid

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

We can place underscore character prior to F or L suffix

A

No

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

int v = _90; is valid…

A

No

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

Which classes can be used as part of try with resources block…

A

Those that implement AutoClosable interface

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

The compiler can check the exact type of exception can be thrown and catch it in general Exception but declare it in the throws clause as specific types…

A

Yes

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

Any object that implements …, which includes all objects which implement java.io.Closeable can be used as a resource.

A

java.lang.AutoCloseable

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

The class BufferedReader, in Java SE 7 and later, implements the interface …

A

java.lang.AutoCloseable

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

Using try with resources if an exception is thrown from both try and finally blocks the exception from try is thrown and the exception from finally is suppressed the opposite of prior to java 7

A

Yes

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

Note that the close methods of resources are called in …

A

the opposite order of their creation.

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

Note: A try-with-resources statement can have catch and finally blocks just like an ordinary try statement. In a try-with-resources statement, any catch or finally block is run after the resources declared have been closed.

A

Yes

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

The following are the methods and constructors in Throwable that support chained exceptions….

A

Throwable getCause()
Throwable initCause(Throwable)
Throwable(String, Throwable)
Throwable(Throwable)

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

Note that it is a common mistake to forget the diamond operator
< > in the initialization expression, as in
Pair worldCup = new Pair (2010, “South Africa”); shows the compiler…

A

warning when you forget to use the diamond syntax.

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

Compiler warning when you forget to use diamond syntax…

A

Yes

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

subtyping does not work for generic parameters…

A

Yes

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

type parameters for generics have a limitation: generic type parameters should match exactly for assignments. to overcome this subtyping problem, you can use wildcard types…

A

Yes

20
Q

You cannot instantiate a generic type using new operator. For example, assuming mem is a field, the following statement will result in a compiler error: T mem = new T(); // wrong usage - compiler error

A

Yes

21
Q
You can declare non-static fields of type T, but not of static fields of type T. For example, class X {   
T instanceMem;          // okay   
static T statMem;       // wrong usage - compiler error
A

Yes

22
Q
It is not possible to have generic exception classes; as a result, the following will not compile: 
class GenericException extends Throwable { } // wrong usage - compiler error
A

Yes

23
Q

To use collections like HashSet, you must override…

A

the hashCode() and equals() methods correctly.

24
Q

A class implementing this interface can be used for iterating with a for each statement…

A

Iterable

25
Q

In a Deque you can insert or remove elements from both the ends

A

Yes

26
Q

Note that Map hierarchy does not extend the Collection interface…

A

Yes

27
Q

You can traverse in both forward and reverse directions if a class implements the ….

A

ListIterator interface

28
Q

You can traverse over the container in the forward direction if a class implements the …

A

Iterator interface

29
Q

ArrayList Fast to search, but slow to insert or delete. Allows duplicates

A

Yes

30
Q

Methods in the Iterator Interface…

A
boolean hasNext() 
E next() 
void remove()
31
Q

Before we call remove on iterator we must call next

A

Yes

32
Q

Methods in the ListIterator Interface (in Addition to Iterator Methods…

A
boolean hasPrevious()
Element previous() 
int nextIndex() 
int previousIndex() 
void set(Element) 
void add(Element
33
Q

So, insertion and deletion is very fast in…

A

LinkedList

34
Q

The container classes store references to objects, so you cannot use primitive types with any of the collection classes. Since you have wrapper classes and auto-boxing, it is not a big problem—that is why you use Character instead of char here

A

Yes

35
Q

the methods hashCode() and equals() need to be consistent for a class. For practical purposes, ensure that you follow this one rule: the hashCode() method should return the same hash value for two objects if the equals() method returns true for them

A

Yes

36
Q

A Queue follows FIFO mechanism…

A

Yes

37
Q

The Deque interface was introduced in Java 6 in java.util.collection package

A

Yes

38
Q

Commonly Used Methods in the Deque Interface…

A
void addFirst(Element) 
void addLast(Element) 
Element removeFirst() 
Element removeLast() 
Element getFirst() 
Element getLast()
39
Q

Using deque, they do not raise exception on failure…

A
boolean offerFirst(Element)
 boolean offerLast(Element) 
Element pollFirst() 
Element pollLast() 
Element peekFirst() 
Element peekLast
40
Q

There are three concrete implementations of the Deque interface…

A

LinkedList, ArrayDeque, and LinkedBlockingDeque

41
Q

remember that you cannot add elements to the List returned by the Arrays.asList() method. But, you can make changes to the elements in the returned List, and the changes made to that List are reflected back in the array

A

Yes

42
Q

You use the extends keyword for both class type as well as an interface when specifying bounded types in generics. For specifying multiple base types, you use the & symbol. For example, in List, ? will match types, extending both

A

Yes

43
Q

An interface can be declared in a class or interface as inner…

A

Yes

44
Q

A rule of thumb is to use has-a and is-a phrases for composition and inheritance, respectively to assess when to use composition or inheritance…

A

Yes

45
Q

Class inheritance implies an is-a relationship, interface inheritance implies an is-like-a relationship, and composition implies a has-a relationship…

A

Yes

46
Q

Use inheritance when a subclass specifies a base class, so that you can exploit dynamic polymorphism. in other cases, use composition to get code that is easy to change and loosely coupled. in summary, favor composition over inheritance…

A

Yes

47
Q

In composition, the lifetime of the contained object and the container object is the same, whereas that is not the case with aggregation. For example, a computer object and a CPU object share a composition relationship, while a library object and a book object share an aggregation relationship…

A

Yes