SPPU Core Java Important Questions and Answers
1. Explain JVM, JRE and JDK.
JVM (Java Virtual Machine)
JVM is a virtual machine that converts Java bytecode into machine code and executes it.
Functions of JVM:
- Executes Java programs
- Provides platform independence
- Manages memory
- Performs garbage collection
JRE (Java Runtime Environment)
JRE provides the environment required to run Java applications.
JRE Contains:
- JVM
- Java libraries
- Supporting files
Use:
Used only for running Java programs.
JDK (Java Development Kit)
JDK is a complete package used for developing Java applications.
JDK Contains:
- JRE
- Compiler (javac)
- Debugging tools
- Development utilities
Use:
Used for both developing and running Java programs.
2. Explain Access Specifiers in Java.
Access specifiers control the visibility of variables, methods, and classes.
private – Accessible only within same class
default – Accessible within same package
protected – Accessible within package and subclass
public – Accessible everywhere
3. What is Garbage Collection?
Garbage collection is the process of automatically deleting unused objects from memory.
Advantages:
- Frees unused memory
- Prevents memory leakage
- Improves performance
Method Used:
[Link]();
4. Explain this and super keyword.
this Keyword
this refers to the current class object.
Uses:
- Access current class variables
- Call current class methods
- Invoke current class constructor
Example:
[Link] = name;
super Keyword
super refers to the parent class object.
Uses:
- Access parent class variables
- Call parent class methods
- Invoke parent class constructor
Example:
[Link]();
5. What is Abstract Class?
An abstract class is a class that cannot be instantiated and may contain abstract and non-abstract methods.
Features:
- Declared using abstract keyword
- Used for abstraction
- Can contain constructors and static methods
Example:
abstract class Animal{
abstract void sound();
}
6. Difference between AWT and Swing.
AWT:
- Heavyweight components
- Platform dependent
- Less features
- Uses native OS components
- Package: [Link]
Swing:
- Lightweight components
- Platform independent
- More features
- Pure Java components
- Package: [Link]
7. Explain File Handling in Java.
File handling is used to create, read, write, and delete files.
Important Classes:
- File
- FileReader
- FileWriter
- BufferedReader
Example:
File f = new File("[Link]");
Operations:
1. Create file
2. Read file
3. Write file
4. Delete file
8. What is ArrayList?
ArrayList is a dynamic array class in Java that stores elements dynamically.
Features:
- Resizable
- Maintains insertion order
- Allows duplicate elements
Example:
ArrayList list = new ArrayList();
Methods:
- add()
- remove()
- get()
- size()
9. Explain Wrapper Classes.
Wrapper classes convert primitive data types into objects.
Primitive Type -> Wrapper Class
int -> Integer
char -> Character
float -> Float
double -> Double
Advantages:
- Used in collections
- Supports object manipulation
Example:
Integer a = 10;
10. Explain Layout Managers.
Layout managers are used to arrange GUI components automatically in a container.
Types of Layout Managers:
1. FlowLayout
2. BorderLayout
3. GridLayout
4. CardLayout
5. GridBagLayout
FlowLayout:
setLayout(new FlowLayout());
BorderLayout:
setLayout(new BorderLayout());
GridLayout:
setLayout(new GridLayout(2,2));