java Flashcards

1
Q

What are the main methods of iterator?

A

next, hasNext and remove

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

is Iterator an interface?

A

yes

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

What is a Stream?

A

Sequence of elements supporting sequential and

parallel aggregate operations.

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

How to implement equals correctly?

A
  1. create a protected boolean eq(object o).
    1. 1 check if o is instance of class
    2. 2 check condition
  2. check if both objects agree on eq.
example:
class Point {
protected boolean eq(Object o) {
if (!(o instanceof Point))
return false;
return ((Point)o).x == x && ((Point)o).y == y;
}
public boolean equals(Object o) {
return (this.eq(o) && ((Point)o).eq(this));
}
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

If you override equals what other method should you override?

A

hashCode

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

What does compareTo?

A

compares to objects and returns an int indicating which one is bigger.
if return > 0 then this is bigger
otherwise parameter is bigger.

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

What’s the use of Comparator?

A

comparing objects that doesn’t implement Comparable interface (don’t have compareTo method).

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

What’s a tagging interface?

A

an interface without methods, used for specifying a purpose.

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

Why not make all classes Cloneable?

A

because then we can’t have singletons.

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

What are the pitfalls of clone?

A
Don't use constructors inside clone. 
because the dynamic type might be different from static type. 
for example field A a, actually holds an object of type B.
therefore if we copy a using new A(); we will get wrong behaviour.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the 4 access modifiers in Java?

A
  1. public - everyone
  2. protected - only inheriting classes and same package
  3. default - same package
  4. private - same (toplevel) class. (if nested classes are involved).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are static nested classes?

A

defined as member classes, can only access static members.

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

What are inner classes?

A

non-static nested classes. there are 3 types:

  1. non static member classes, regular
  2. local class
  3. anonymous class
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are local classes?

A

classes defined in a method.

can access parameters as well as local (final!) vars declared in the same method before the class

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

What are anonymous classes?

A

created inside a method.

definition is also invocation.

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

anonymous class syntax

A

new () {

}

examle:
interface A {
     void g();
}
class B {
    void f() {
    A a = new A() {
    @Override public void g() {}
     };
      a.g();
  }
}
17
Q

Is it possible to inherit from an inner class?

A

yes. as long as the inheriting class can be associated with a subtype of the outer class.

18
Q

What are the advantages of java enums over pre c++11 enums?

A
  1. Enum declaration defines a class (type safety)
  2. Body can include methods and fields
  3. Values may be objects
  4. Support for iteration, naming
19
Q

Can an enum type inherit?

A

No. all enum types implicitly inherit from Class Enum.

Can implement interfaces.

20
Q

Can you extend enum classes?

A

No. enums are implicitly final.

21
Q

What’s the best way to implement a singleton in Java?

A

Enums.
public enum MySingleton {
INSTANCE;

advantage:
no need for lazy instantiation.

22
Q

Do annotations affect the code?

A

mo. but can effect things that use the code: tools, code generation etc..

23
Q

What is the annotation @FunctionalInterface?

A

asserts that the tagged interface has one and only one
method that is without implementation. So we can be
sure that it can be replaced with lambda.

24
Q

What are the 3 types of annotations?

A

Marker annotations
– Have no attributes
– @Override, @Deprecated

Single value annotations
– Provide a single piece of data.
– Attribute type can be
 primitive, string, Enum, or array of the previous.
• @Author(“Pazit”)
• @SuppressWarnings({“unchecked”,
“deprecation”})

Multi valued annotations
– @MethodKind(composite=true, mutator=false)

25
Q

What are meta-annotaitions?

A
  • Annotations that annotate annotations

* Specify how the annotation should be used

26
Q

What are the 4 meta-annotaitions we learned about?

A

@Inherited
• Does the annotation get applied to subclasses
@Target
• Where the annotation can be used (source elements)
• Default is all
@Retention
• Where is the annotation retained (where it “lives”)
• Possible values: runtime, class, source
@Repeatable
– Specifies that the tagged annotation may appear over an
element more then once.