Java Foundations 4 Flashcards

1
Q

What is the purpose of the java.lang package?

A

It provides fundamental classes that are automatically imported, such as String, Math, Object, and System.

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

Is String in Java mutable or immutable?

A

Immutable — once created, a String object cannot be changed.

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

What does the public access modifier do?

A

Makes the class, method, or variable accessible from any other class.

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

What does the private access modifier mean?

A

Restricts access to within the same class only.

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

What does the protected access modifier allow?

A

Access within the same package and by subclasses even if they are in different packages.

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

What does the default (no modifier) access level mean?

A

Accessible only within the same package — also known as package-private.

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

What is the use of the static keyword in Java?

A

It indicates that a field or method belongs to the class, not to instances of the class.

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

Can a static method access instance variables directly?

A

No, it can only access other static fields or methods directly.

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

What happens when you declare a variable as final?

A

The variable becomes a constant and cannot be reassigned once initialized.

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

Is the Math class in java.lang mutable or immutable?

A

Immutable — all methods are static, and there is no internal state.

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

What is an example of a mutable class in Java?

A

StringBuilder — its content can be modified without creating a new object.

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

Why is immutability useful in multi-threaded applications?

A

Immutable objects are thread-safe because their state cannot change after creation.

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

What class does every class in Java implicitly extend?

A

java.lang.Object

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

What is the purpose of the System class in java.lang?

A

Provides access to system-level resources like standard input/output and environment variables.

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

Can static be used with inner classes?

A

Yes, you can create a static nested class, which does not require an instance of the outer class.

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