NBKRIST – Java Hub

Java Packages & Core Interview Q&A

Expert View

As a software engineer, I thought I would share some questions that will be useful for students who plan to face industry interviews as freshers. From my experience, interviewers commonly ask these fundamental questions. If students are confident with these topics, they will handle many entry-level technical rounds more comfortably. These questions test core understanding and practical thinking — both important in real interviews.

Example: Packages & Import (hover the code to see output)

package nbkrist.demo; public class Greeting { public void sayHello() { System.out.println("Hello NBKRIST Students!"); } } // In another file: import nbkrist.demo.Greeting; public class Test { public static void main(String[] args) { new Greeting().sayHello(); } }

Example: java.lang – String & Math

public class LangDemo { public static void main(String[] args) { String s = "JavaFX"; System.out.println(s.toLowerCase()); System.out.println("Square root of 25: " + Math.sqrt(25)); } }

Example: java.util – Random & Formatter

import java.util.*; public class UtilDemo { public static void main(String[] args) { Random rand = new Random(); System.out.println("Random number: " + rand.nextInt(10)); Formatter fmt = new Formatter(); fmt.format("Value of PI: %.2f", Math.PI); System.out.println(fmt); } }

Example: java.time – Date & Time

import java.time.*; public class TimeDemo { public static void main(String[] args) { LocalDate today = LocalDate.now(); LocalTime now = LocalTime.now(); System.out.println("Date: " + today); System.out.println("Time: " + now); } }

Interview Questions

1. What is a package in Java?
A package groups related classes and interfaces to organize code and avoid naming conflicts. Used to structure enterprise projects into logical modules.
2. How do you import a class from another package?
Use import packageName.ClassName; (or wildcard import packageName.*;). Used to reuse utilities across different modules in a project.
3. Which package is imported automatically in every Java program?
The java.lang package is imported by default. Contains core classes like String, Math, and Object used everywhere.
4. How can you use a class without importing it?
Use its fully qualified name, e.g. java.util.Random. Helps avoid name conflicts when two classes share the same simple name.
5. What does the String class represent?
A sequence of characters with many utility methods for manipulation. Used for user input, messages, and data interchange formats (JSON/XML).
6. Why are Strings immutable?
Immutability provides thread-safety, caching, and secure usage (e.g., as keys). Important in multi-threaded servers and as keys in HashMap.
7. What does the Math class provide?
Static mathematical utilities such as sqrt(), pow(), abs(), round(). Used for calculations in finance, physics, and statistics.
8. What is the Object class?
Root of the class hierarchy; defines methods like equals(), hashCode(), toString(). Used for polymorphism and general-purpose APIs.
9. What are Wrapper Classes?
Object representations of primitives (Integer, Double, Character, etc.). Required when primitives must be used in Collections (which accept objects).
10. What is Autoboxing and Unboxing?
Autoboxing converts primitives to wrappers automatically; unboxing converts back. Simplifies code when using primitives with collections and APIs.
11. What is java.util used for?
Provides collections, date/time utilities, random generation, and helper classes. Used across virtually all applications for data handling.
12. What does Date represent?
An instant in time (legacy class, millisecond precision). Used in older APIs for timestamps and logging.
13. Why prefer java.time over Date?
java.time offers immutable, thread-safe, clearer APIs (LocalDate, Instant). Used in modern scheduling, financial systems and distributed apps.
14. What is the Formatter class for?
Formatting strings, numbers, and dates using specifiers similar to printf. Useful in report generation and formatted logs.
15. What does Random do?
Generates pseudo-random numbers, optionally with a seed for reproducibility. Used in tests, games, and randomized sampling.
16. What is Scanner used for?
Reading input from console, files or streams (part of java.util). Used for simple CLI tools and student programs.
17. What is java.util.List?
An ordered collection interface allowing duplicates (e.g., ArrayList). Used for ordered datasets like UI lists and result sets.
18. What is java.util.Map?
A key-value mapping interface (e.g., HashMap, TreeMap). Used for lookups, caches, and configuration data.
19. What are TemporalAdjusters?
Helpers in java.time to compute relative dates (next Monday, first day). Used for scheduling and payroll date calculations.
20. How do imports help in large projects?
Imports allow reusing classes across packages and keep source code readable. Essential to maintain modular, team-developed codebases.

Summary