1. Java Fundamentals & Basics (25 Questions)
Q1. What is Java programming language?
Java is a high-level, object-oriented programming language developed by Sun Microsystems in 1995. It is designed to be simple, secure and platform-independent. This means you can write Java code once and run it on any operating system (Windows, Linux, macOS) without changing it. Java is widely used for building websites, mobile apps (Android), desktop software, and enterprise applications.
Q2. What is the difference between JDK, JRE, and JVM?
- JVM (Java Virtual Machine): The engine that actually runs the Bytecode.
- JRE (Java Runtime Environment): A package that includes the JVM plus the standard libraries needed to run Java programs.
- JDK (Java Development Kit): The full toolkit for developers. It contains the JRE plus development tools like the compiler (javac) and debugger.
Note: JDK > JRE > JVM.
Q3. What is JVM and how does it work?
JVM stands for Java Virtual Machine. It is an abstract computer that runs Java bytecode (.class files).
How it works: When you compile a .java file, it becomes bytecode. The JVM reads this bytecode, converts it into machine code for your specific operating system using the Just-In-Time (JIT) compiler, and executes it.
Q4. What is the role of JRE in Java?
JRE provides the runtime environment to execute Java programs. It contains the JVM, core class libraries (like java.lang, java.util), and other files needed to run bytecode. Without JRE, you cannot run any Java application on your machine. Think of JRE as the “player” that plays the Java “movie” (bytecode).
Q5. What is JDK and why is it important?
JDK is the full software development kit that includes everything: compiler, debugger, JRE, and development tools. It is important because you cannot compile or create new Java programs without it. For beginners, installing JDK is the first step — it lets you write code in Notepad, compile it, and run it using simple commands like javac and java.
Q6. What is bytecode in Java?
Bytecode is the intermediate code generated by the Java compiler (javac). It is a platform-independent set of instructions that the JVM can understand. Example: When you write HelloWorld.java, after compilation it becomes HelloWorld.class (bytecode). This .class file can run on any machine that has a JVM.
Q7. What are the key features of Java?
Java has many beginner-friendly features:
- Simple (easy syntax like C++)
- Object-oriented
- Platform-independent
- Robust (strong memory management and exception handling)
- Secure (no direct memory access)
- Multi-threaded (can do multiple tasks at once)
- High performance (thanks to JIT compiler) These features make Java reliable for both small scripts and large enterprise projects.
Q8. Why is Java considered platform-independent?
Java is a high-level, object-oriented programming language. It is called platform-independent because of its “Write Once, Run Anywhere” (WORA) capability.
When you compile Java code, it is converted into Bytecode (.class file) rather than machine code. This Bytecode can run on any device that has a Java Virtual Machine (JVM) installed.
Q9. Explain the Java program execution process step by step.
- Write code in a .java file.
- Compile it using javac MyProgram.java → creates .class file (bytecode).
- Run it using java MyProgram → JVM loads the bytecode.
- JVM verifies the bytecode, loads classes, and executes via interpreter + JIT compiler.
- Output is displayed. This two-step process (compile + run) is what makes Java both fast and portable.
Q10. What is the difference between primitive and non-primitive (reference) data types?
Primitive data types store the actual value (e.g., int age = 25; stores 25 directly). They are fast and use less memory. Non-primitive (reference) types store the address of the object in memory (e.g., String name = “Nilesh”;). They can call methods and are created using the new keyword or literals.
Example: int is primitive, but Integer (wrapper) or String is reference.
Q11. What are primitive data types in Java? List them with sizes.
Primitive data types are the basic building blocks that store simple values directly in memory.
There are 8 of them:
| Data Type | Size | Description |
| byte | 1 byte | Small whole numbers |
| short | 2 bytes | Larger whole numbers |
| int | 4 bytes | Most common whole numbers |
| long | 8 bytes | Very large whole numbers |
| float | 4 bytes | Decimal numbers |
| double | 8 bytes | More precise decimal numbers |
| char | 2 bytes | Single character |
| boolean | 1 bit | true or false |
Q12. Can you assign 0 or 1 to a boolean?
No. In Java, boolean variables only accept true or false. Assigning 1 or 0 will cause a compiler error.
Q13. Difference between float and double data types.
Both store decimal numbers, but:
- float: 4 bytes, less precision (up to 7 decimal places), faster.
- double: 8 bytes, higher precision (up to 15 decimal places), default for decimals. Example:
//Java
float price = 99.99f; // note the f
double pi = 3.14159265359;
For most beginner calculations, double is safer and recommended.
Q14. What is the char data type and how is it different from other languages?
char stores a single character and uses 2 bytes (Unicode support). It can hold letters, digits, symbols, and even emojis from any language.
Example:
char grade = ‘A’;
char unicodeChar = ‘\u0041’; // Unicode for ‘A’
Unlike C/C++ (1 byte, ASCII), Java’s char supports international characters.
Q15. Explain boolean data type with example.
boolean can only hold two values: true or false. It is used for conditions and flags. Example:
boolean isJavaFun = true;
boolean hasError = false;
if (isJavaFun) {
System.out.println(“Yes, Java is fun!”);
}
Very useful in decision-making and loops.
Q16. What are default values for primitive data types in Java?
When you declare a variable without assigning a value, Java gives these defaults:
- byte, short, int, long → 0
- float, double → 0.0
- char → ” (empty)
- boolean → false Note: Local variables inside methods do NOT get default values — you must initialize them.
Q17. What are wrapper classes in Java?
Wrapper classes convert primitive types into objects so they can be used in collections (like ArrayList) or call methods. Examples: Integer for int, Double for double, Boolean for boolean, Character for char. They belong to java.lang package.
Q18. What is autoboxing and unboxing in Java? Explain with code.
Autoboxing: Automatic conversion of primitive to wrapper object. Unboxing: Automatic conversion of wrapper object back to primitive.
Example:
public class Example {
public static void main(String[] args) {
int num = 100;
Integer obj = num; // autoboxing
int value = obj; // unboxing
System.out.println(value);
}
} //This feature was added in Java 5 to make code cleaner.
Q19. Explain the main method in Java: public static void main(String[] args).
This is the entry point of every Java program. The JVM looks for this exact method to start execution.
- public → The access modifier that allows the JVM to execute the method from anywhere.
- static → Allows the JVM to call this method without creating an instance (object) of the class.
- void → The return type, meaning this method doesn’t return any value.
- String[] args → command-line arguments If you change even one word, the program will not run.
Q20. Can we change the order of public static in the main method?
Yes, the order of modifiers doesn’t matter. static public void main is perfectly valid. However, void must always come just before the method name.
Q21. What happens if you don’t write String[] args in the main method?
The code will compile successfully, but the JVM will not recognize it as the entry point. You will get a runtime error saying “Main method not found.”
Q22. What is a Java class and how to define one?
A class is a blueprint for creating objects. It contains variables and methods.
Basic structure:
public class MyFirstClass { // class name starts with capital letter
public static void main(String[] args) {
System.out.println(“Hello from CodexRush!”);
}
}
Every Java file must have at least one class with the same name as the file.
Q23. How to declare and initialize variables in Java?
Declaration: dataType variableName; Initialization: variableName = value; Or in one line: dataType variableName = value;
Example:
int rollNo; // declaration
rollNo = 101; // initialization
String city = “Mumbai”; // both together
Variables must be declared before use.
Q24. What are the rules for naming variables in Java?
- Can contain letters, digits, underscore (_), and dollar sign ($)
- Must start with a letter, _ or $
- Cannot start with a digit
- Case-sensitive (age and Age are different)
- Cannot use reserved keywords (int, class, public, etc.)
- Follow camelCase for multiple words (myFirstVariable) Good example: studentName, totalMarks
Q25. Explain type casting in Java with examples (widening and narrowing).
Type casting converts one data type into another.
1.Widening (automatic, smaller to larger):
int num = 100;
long bigNum = num; // automatic widening
2.Narrowing (manual, larger to smaller):
double price = 99.99;
int intPrice = (int) price; // explicit casting, decimal part is lost
System.out.println(intPrice); // prints 99




