Important Java Questions - 2 Marks
1. Uses of Garbage Collection
Garbage collection in Java is used to automatically free memory by removing unused objects, preventing memory leaks, improving performance, and managing memory efficiently without manual intervention.
Garbage collection in Java is used to automatically free memory by removing unused objects, preventing memory leaks, improving performance, and managing memory efficiently without manual intervention.
2. Types of Exception in Java
Java exceptions are mainly of two types: checked exceptions (compile-time, e.g., IOException) and unchecked exceptions (runtime, e.g., NullPointerException).
Java exceptions are mainly of two types: checked exceptions (compile-time, e.g., IOException) and unchecked exceptions (runtime, e.g., NullPointerException).
3. FileInputStream Class
FileInputStream is used to read data from a file in byte form. It belongs to java.io package and is mainly used for reading binary data like images or files.
FileInputStream is used to read data from a file in byte form. It belongs to java.io package and is mainly used for reading binary data like images or files.
4. Steps to Add Components in Java
To add components in Java Swing: create a container (JFrame), create components (JButton, JTextArea), set layout manager, add components using add(), and make the frame visible.
To add components in Java Swing: create a container (JFrame), create components (JButton, JTextArea), set layout manager, add components using add(), and make the frame visible.
5. Difference between Checked & Unchecked Exception
Checked exceptions occur at compile time and must be handled (e.g., IOException), whereas unchecked exceptions occur at runtime and are not mandatory to handle (e.g., ArithmeticException).
Checked exceptions occur at compile time and must be handled (e.g., IOException), whereas unchecked exceptions occur at runtime and are not mandatory to handle (e.g., ArithmeticException).
6. Define JRE
JRE (Java Runtime Environment) is a part of Java that provides the runtime environment to execute Java programs and includes JVM along with required libraries.
JRE (Java Runtime Environment) is a part of Java that provides the runtime environment to execute Java programs and includes JVM along with required libraries.
7. How to Declare a Variable
A variable in Java is declared using data type followed by variable name, for example:
A variable in Java is declared using data type followed by variable name, for example:
int a = 10;
where int is the data type and a is the variable.
8. Define Constructor
A constructor is a special method in Java used to initialize objects, having the same name as the class and no return type.
A constructor is a special method in Java used to initialize objects, having the same name as the class and no return type.
9. Datatypes in Java
Java data types are classified into primitive types (int, float, char, boolean, etc.) and non-primitive types (String, arrays, classes, etc.) used to store different kinds of data.
Java data types are classified into primitive types (int, float, char, boolean, etc.) and non-primitive types (String, arrays, classes, etc.) used to store different kinds of data.
10. Difference between While Loop & Do-While Loop
In a while loop, the condition is checked before execution, whereas in a do-while loop, the condition is checked after execution, ensuring at least one execution.
In a while loop, the condition is checked before execution, whereas in a do-while loop, the condition is checked after execution, ensuring at least one execution.
11. Define Class and Objects
A class is a blueprint that defines variables and methods, while an object is an instance of a class that represents real-world entities and occupies memory.
A class is a blueprint that defines variables and methods, while an object is an instance of a class that represents real-world entities and occupies memory.
12. User Defined Packages
User-defined packages are packages created by programmers to organize classes and interfaces for better structure and reuse in applications.
User-defined packages are packages created by programmers to organize classes and interfaces for better structure and reuse in applications.
13. Define Java String
A String in Java is a sequence of characters used to store text and is represented by the String class from the java.lang package.
A String in Java is a sequence of characters used to store text and is represented by the String class from the java.lang package.
14. Define MVC Architecture
MVC (Model-View-Controller) is a design pattern that separates an application into Model (data), View (UI), and Controller (logic) for better organization and scalability.
MVC (Model-View-Controller) is a design pattern that separates an application into Model (data), View (UI), and Controller (logic) for better organization and scalability.
15. FileOutputStream Class
FileOutputStream is used to write data into a file in byte form. It belongs to java.io package and is commonly used for writing binary data.
FileOutputStream is used to write data into a file in byte form. It belongs to java.io package and is commonly used for writing binary data.
16. Two Methods to Create Thread
Threads in Java can be created in two ways: by extending the Thread class or by implementing the Runnable interface.
Threads in Java can be created in two ways: by extending the Thread class or by implementing the Runnable interface.
0 Comments