Java Foundations 4 Flashcards
What is the purpose of the java.lang package?
It provides fundamental classes that are automatically imported, such as String, Math, Object, and System.
Is String in Java mutable or immutable?
Immutable — once created, a String object cannot be changed.
What does the public access modifier do?
Makes the class, method, or variable accessible from any other class.
What does the private access modifier mean?
Restricts access to within the same class only.
What does the protected access modifier allow?
Access within the same package and by subclasses even if they are in different packages.
What does the default (no modifier) access level mean?
Accessible only within the same package — also known as package-private.
What is the use of the static keyword in Java?
It indicates that a field or method belongs to the class, not to instances of the class.
Can a static method access instance variables directly?
No, it can only access other static fields or methods directly.
What happens when you declare a variable as final?
The variable becomes a constant and cannot be reassigned once initialized.
Is the Math class in java.lang mutable or immutable?
Immutable — all methods are static, and there is no internal state.
What is an example of a mutable class in Java?
StringBuilder — its content can be modified without creating a new object.
Why is immutability useful in multi-threaded applications?
Immutable objects are thread-safe because their state cannot change after creation.
What class does every class in Java implicitly extend?
java.lang.Object
What is the purpose of the System class in java.lang?
Provides access to system-level resources like standard input/output and environment variables.
Can static be used with inner classes?
Yes, you can create a static nested class, which does not require an instance of the outer class.