1.What is Java and what are its main features?
Answer:
Java is a high-level, object-oriented, platform-independent programming language.
Main features:
Object-Oriented
Platform independent (Write Once, Run Anywhere)
Simple and secure
Robust (handles errors well)
Multithreaded (supports multiple tasks in parallel)
2. What is the difference between JDK, JRE, and JVM?
Answer:
JDK (Java Development Kit): For developers; contains JRE + compilers + tools.
JRE (Java Runtime Environment): To run Java programs; contains JVM + libraries.
JVM (Java Virtual Machine): Executes Java bytecode; it’s the engine that runs Java programs.
3. What is the meaning of platform independence in Java?
Answer:
Java code is compiled into bytecode, which can run on any machine that has a JVM. So, the same program runs on Windows, Linux, Mac without changing the code.
4. What is bytecode?
Answer:
Bytecode is the intermediate code generated by the Java compiler (.class file) which is executed by the JVM.
5. Explain OOPS concepts.
Answer:
Main OOP concepts in Java:
Encapsulation: Hiding data using classes and access modifiers.
Inheritance: One class can use properties of another.
Polymorphism: Same method name, different behavior.
Abstraction: Showing only essential details, hiding implementation.
6. What are primitive data types in Java?
Answer:
There are 8 primitive types:
byte, short, int, long (integers)
float, double (decimals)
char (single character)
boolean (true/false)
7. What is the difference between int and Integer?
Answer:
int is a primitive data type.
Integer is a wrapper class for int (an object).
Wrapper classes are used when we need objects, e.g., in collections like ArrayList.
8. What is type casting? Types of casting?
Answer:
Type casting means converting one data type to another.
Types:
Widening (implicit): Smaller to larger type (e.g., int → long).
Narrowing (explicit): Larger to smaller type (e.g., double → int).
9. What is the default value of variables in Java?
Answer:
Local variables: No default value (must be initialized).
Instance/static variables have defaults, e.g.:
int → 0
boolean → false
Object references → null
10. What is the difference between if-else and switch?
Answer:
if-else works with conditions (boolean expressions).
switch works with specific values (like int, String, enum).
Use switch when checking one variable against many fixed values.
11. What is a loop? Types of loops in Java?
Answer:
A loop is used to repeat a block of code.
Types:
for loop
while loop
do-while loop
Enhanced for (for-each) loop
12. Difference between break and continue?
Answer:
break: Exits the loop completely.
continue: Skips the current iteration and moves to the next one.
13. What is a class? What is an object?
Answer:
Class: A blueprint or template (e.g., Car design).
Object: A real instance of a class (e.g., a specific car).
14. What is a constructor? Types of constructors?
Answer:
A constructor is a special method used to initialize an object.
Types:
Default constructor: No parameters.
Parameterized constructor: With parameters.

No comments:
Post a Comment