Week 10 - File Manipulation, Iterators, Reflection & Custom Annotations Flashcards

1
Q

What is a regular expression?

A

a compact form to define a string

used to check the validity of strings

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

Write a regular expression for a string that starts with string “01” and is followed by 7 numerical digits from 0 to 9?

A

string.matches(“01[0-9]{7}”)

{a} for repetition a times, {a, b} from a to b times, {a,} from a to n

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

Write a regular expression for a string that matches either 00 or 111 or 0000?

A

string.matches(00|111|0000)

this expression requires exactly the same form, not ‘contains’

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

Write a regular expression for a string that matches either 00000 or 00001

A

string.matches(“0000(0|1)”)

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

Write a regular expression for a string that matches “trolo” with however many “lo” after

A

string.matches(“trolo(lo)*”)
string.matches(“trolo(lo)+”)
string.matches(“trolo(lo)?”)

(*) for repetition from 0 to n times, (+) from 1 to n, (?) from 0 to 1

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

[145] and [2-36-9] and [a-c]* mean the same as?

A

(1|4|5) and (2|3|6|7|8|9) and a string made only of characters a,b,c

We use square brackets for defining groups of characters

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

How many characterrs of internal buffer does the BufferedWriter maintain?

A

8192

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

During the write operation, the characters are written to the internal buffer or the disk?

A

internal buffer, not the disk

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

Which exception does reading and writing to the files throw and how to manage it?

A

It throws the IOException and it is managed with try-catch blokc or by adding the throws declaration in the method signature

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

In what package can we find the FileWriter and in which package can we find iterators?

A

java.io.package fro FileWriter class, java.util.package for iterators

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

What is an iterator?

A

An interface that faciliates the traversal of elements in a collection

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

What are the three main methods of Iterator interface?

A

boolean hasNext(): Returns true if there are more elements in the collection, false otherwise.
next(): returns the next element in the iteration. This method should be called only if hasNext() returns true. Otherwise, it may throw a NoSuchElementException.
void remove(): removes the last element returned by next() from the underlying collection. If the iterator does not support removal, it throws an UnsupportedOperationException.

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

Can private or final attributes be accessed and manipulated by reflection?

A

Yes.

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

Which methods do not need the object?

reflection related

A

static methods

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

What does the annotation do?

A

It simply provides information that can be used at compile time or runtime to perform further processing.

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

When do we use the @Override method?

also for funsies what package do annotations come from

A

We use the @Override annotation to mark a method that exists in a parent class, but that we want to override in a child class.

java.lang package

17
Q

Do the annotations also accept parameters as if they were methods?

A

Yes.

18
Q

Can we use annotations on any level such as classes, methods, and variables?

A

Yes.

19
Q

What do we use @Target() for?

A

Target() allows us to specify which kind of Java element this annotation is valid to be used on. If omited, this annotation is valid on any Java element.

Values that it can take: ANNOTATION_TYPE, CONSTRUCTOR, FIELD, etc.

Rest of values: METHOD, PACKAGE, PARAMETER, TYPE (class, interface, enum), TYPE_PARAMETER, TYPE_USE

20
Q

If we try to use the annotation on some other Java element (that we didn’t input in @Target, what happens?

A

It will give us the compile time error.

21
Q

What does @Retention do?

A

It indicates how long the annotated type should bekept in the program’s lifecycle.
It can take RUNTIME (while program is running), SOURCE(only before it compiles) and CLASS(during the compilation) values.

22
Q

What parameters can we pass to interface?

A

Primitive data type, string, class type, array of any of the above