Java Flashcards

1
Q

What is more restrictive - protected or default?

A

Default (package-private). It allows same package only.
Protected allows other packages if a class in that package extends the class where variable is declared.

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

What are default values of these instance variables?
~~~
public class A {
String a;
int i;
}
~~~

A

null, 0

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

Can we call both methods and variables of the parent class using super?

A

Yes. Both methods and variables.
Pay attention not to call non-static members from static.

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

What would be the output of this class? _______

class B {
    public B(var s) {}
}
A

Does not compile because var cannot be used as a method or a constructor parameter.

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

Can a constructor be static or/and final?

A

No. However it can be private or protected or package-private (default).

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

How to properly call a constructor from another constructor in the same class?

A

Using this().
The new ClassName() will compile, but will create another object.
The ClassName() will not compile.

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

If neither this() nor super() are called from our constructor, what is called by default?

A

super()

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

What is Class Initialization order?

A

First all parent class static variables in the order they appear, then all parent class initializers in the order they appear.

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

How many times Class initialization happens?

A

One time at most. It happens on either start of the program or when a static variable is referenced or shortly before an instance of the class is created. Or never if the class never used.

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

What is initialization order of an object of a class?

A

Parent class static variables, then static initializers. Child class static variables, then static initializers. Parent class non-static variables, then non-static initializers, then parent constructor. Child class non-static variables, then non-static initializers, then child constructor.

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

Can we extend the class that defines private constructors only?

A

No.

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