Package Flashcards

(107 cards)

1
Q

What is the purpose of a package in Java?

A

A package groups related classes and interfaces together to organize code, manage namespaces, and control access.

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

What is a package in Java?

A

A package is a namespace that organizes classes and interfaces into a hierarchical structure.

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

Why are packages important?

A

They improve code organization, prevent naming conflicts, and provide access control.

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

How do packages prevent naming conflicts?

A

By allowing classes with the same name to exist in different packages.

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

Example of package naming conflict prevention?

A

java.util.Date and java.sql.Date can coexist.

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

What keyword is used to declare a package?

A

package.

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

Example package declaration?

A

package com.example.util;

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

How do packages support access control?

A

Using access modifiers like public, protected, default, and private across package boundaries.

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

What is the default package?

A

The package used when no package statement is declared.

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

Is using the default package recommended?

A

No. It is discouraged for production code.

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

Can packages contain subpackages?

A

Yes. Packages can be hierarchical, like com.company.project.module.

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

What is the naming convention for packages?

A

Lowercase names, often reversed domain names like com.company.app.

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

How do you use classes from another package?

A

By importing them using import statement.

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

Example import statement?

A

import java.util.List;

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

What is the difference between package and folder?

A

A package is a logical namespace; a folder is a physical directory.

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

Why are packages useful in large projects?

A

They improve maintainability, readability, and modular design.

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

Key rule to remember?

A

Package = namespace + organization + access control.

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

What is the java.lang package?

A

java.lang is a core package that contains fundamental classes required by the Java language and is automatically imported.

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

Why is java.lang important?

A

It provides essential classes needed for basic programming functionality.

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

Do you need to import java.lang explicitly?

A

No. It is imported automatically by the compiler.

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

What is the most important class in java.lang?

A

Object because it is the root superclass of all classes.

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

What common classes are in java.lang?

A

String, System, Object, Math, Thread, Throwable, Class.

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

What wrapper classes exist in java.lang?

A

Integer, Boolean, Character, Double, Float, Long, Short, Byte.

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

What does the System class provide?

A

Standard input/output streams and utility methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
What does the Math class provide?
Mathematical operations like sqrt(), pow(), and random().
26
What is the role of String class?
It represents immutable text strings.
27
Does java.lang contain exception classes?
Yes. It includes Exception, RuntimeException, Error, and Throwable.
28
Why is java.lang always available?
Because the compiler automatically imports it for every Java program.
29
Is java.lang part of standard library?
Yes. It is part of Java’s core standard library.
30
Can classes outside java.lang work without it?
No. Nearly all Java programs rely on classes from this package.
31
Does java.lang include multithreading support?
Yes. It includes Thread and Runnable.
32
Key rule to remember?
java.lang = automatically available core language classes.
33
Which is the most important class in Java?
The Object class is considered the most important because it is the root superclass of all classes in Java.
34
Why is Object class important?
Because every class in Java implicitly extends Object.
35
What does it mean that Object is the root class?
All Java classes inherit its methods and behavior by default.
36
Which common methods come from Object class?
toString(), equals(), hashCode(), clone(), getClass(), notify(), wait().
37
Why is equals() important?
It defines how object equality is determined.
38
Why is hashCode() important?
It supports hashing structures like HashMap and HashSet.
39
Why is toString() important?
It provides a string representation of an object.
40
Can a class avoid extending Object?
No. All classes ultimately inherit from Object.
41
Does Object support polymorphism?
Yes. Any object can be referenced as type Object.
42
Why is Object useful for generic APIs?
It allows handling any type of object.
43
Is Object class final?
No. It must be extendable because all classes inherit from it.
44
Can Object methods be overridden?
Yes. Many methods like equals() and toString() are commonly overridden.
45
What is the constructor of Object?
Object() is the default superclass constructor.
46
Why do collections rely on Object methods?
Because they use equals() and hashCode() for comparisons and storage.
47
Key rule to remember?
Object = universal base class of Java.
48
Is it mandatory to import java.lang package in Java?
No. The java.lang package is automatically imported by the compiler.
49
Why don’t we need to import java.lang manually?
Because Java implicitly makes it available to every program.
50
Who handles the automatic availability of java.lang?
The Java compiler includes it by default during compilation.
51
Can you still import java.lang explicitly?
Yes, but it is unnecessary and redundant.
52
Which commonly used classes come from java.lang?
String, Object, System, Math, Thread, Exception.
53
Would a program fail without importing java.lang?
No. Programs run normally because it is auto-imported.
54
Is java.lang the only auto-imported package?
Yes. It is the only package automatically imported in every Java file.
55
What happens if you explicitly import java.lang?
Nothing changes; it simply duplicates what Java already does.
56
Do subpackages of java.lang get auto-imported?
No. Only java.lang itself is imported automatically.
57
Example of subpackage needing import?
java.lang.reflect must be imported manually.
58
Why is java.lang auto-imported?
Because it contains core language classes needed by almost all programs.
59
Does auto-import affect performance?
No. It is handled at compile time.
60
Is JVM responsible for importing java.lang?
No. The compiler handles it, not the JVM.
61
What is the benefit of auto-importing java.lang?
It simplifies code by removing the need for repetitive imports.
62
Key rule to remember?
java.lang is always available without import.
63
Can you import the same package or class twice in a Java file?
Yes. Importing the same package or class multiple times is allowed and does not cause errors.
64
What happens if the same import is written twice?
The compiler ignores duplicates and treats them as a single import.
65
Does duplicate import affect program execution?
No. It has no impact on compilation or runtime behavior.
66
Does Java give a warning for duplicate imports?
No. The compiler typically does not report duplicate import statements.
67
Is it good practice to duplicate imports?
No. It is unnecessary and should be avoided for clean code.
68
What happens internally when duplicate imports exist?
The compiler resolves the reference once and ignores repeated declarations.
69
Can duplicate imports cause runtime overhead?
No. Imports are compile-time only and do not exist at runtime.
70
What problem can occur with importing classes of same name from different packages?
Name collision or ambiguity.
71
Example of conflicting imports?
import java.util.Date; import java.sql.Date;
72
How do you resolve class name conflicts?
Use fully qualified class name like java.util.Date.
73
Does JVM load classes multiple times if imported multiple times?
No. Classes are loaded only once per class loader.
74
Are imports required for classes in the same package?
No. Classes in the same package can access each other directly.
75
What is the purpose of import statements?
To allow using class names without fully qualified package paths.
76
Can wildcard imports cause ambiguity?
Yes. If two packages contain same class name, ambiguity occurs.
77
Key rule to remember?
Duplicate imports are allowed but unnecessary.
78
What is a static import in Java?
A static import allows you to access static members of a class without using the class name qualifier.
79
How is static import different from normal import?
Normal import imports classes; static import imports static members like methods or constants.
80
What syntax is used for static import?
import static package.ClassName.member;
81
Example of static import?
import static java.lang.Math.sqrt;
82
How do you use a static member after static import?
You can call it directly, e.g., sqrt(25) instead of Math.sqrt(25).
83
Can you import all static members of a class?
Yes, using wildcard syntax.
84
Wildcard static import example?
import static java.lang.Math.*;
85
What types of members can be statically imported?
Only static fields and static methods.
86
Does static import affect runtime performance?
No. It is resolved at compile time.
87
When should you use static import?
When static members are used frequently and readability improves.
88
When should you avoid static import?
When it reduces readability or causes ambiguity.
89
Can static imports cause naming conflicts?
Yes. If multiple static members with same name are imported.
90
Does static import change how methods work?
No. It only changes how you reference them.
91
Are static imports required for accessing static members?
No. You can always access them using ClassName.member.
92
Key rule to remember?
Static import = use static members without class name.
93
What is the difference between import static com.test.FooClass and import com.test.FooClass?
The static import imports static members of the class, while the normal import imports the class itself.
94
What does a normal import statement do?
It allows you to use a class name without writing its full package path.
95
What does a static import statement do?
It allows you to use static methods or constants without qualifying them with the class name.
96
Example of normal import usage?
import com.test.FooClass; → FooClass obj = new FooClass();
97
Example of static import usage?
import static com.test.FooClass.MAX; → int x = MAX;
98
Can normal import access static members directly?
No. You must use ClassName.member.
99
Can static import import instance members?
No. Only static members can be imported.
100
Can you static import all static members?
Yes. Using import static com.test.FooClass.*;
101
Does static import import the class itself?
No. It only imports static members.
102
Does normal import import static members automatically?
No. Static members still require class qualification unless statically imported.
103
Which is better for readability?
Normal import is usually clearer unless static members are heavily used.
104
Can both normal and static imports be used together?
Yes. They can coexist in the same file.
105
Can static imports cause ambiguity?
Yes. If multiple imported classes contain static members with the same name.
106
Are imports required if you use fully qualified names?
No. Fully qualified names remove the need for imports.
107
Key rule to remember?
Normal import → class name shortcut; Static import → static member shortcut.