JAVA IMP
JAVA IMP
- **Classes and Objects**: Understanding how to define and instantiate classes and objects.
- **Encapsulation**: Using access modifiers (private, public, etc.), getters, and setters.
### 3. **Methods**
- Recursion.
### 4. **Arrays**
### 8. **Multithreading**
- Lambda expressions.
- Functional interfaces.
- Handling ResultSet.
- Garbage Collection.
- Finalize method.
- Basics of applets.
- Assertions.
- Factory Pattern.
- Observer Pattern.
- Builder Pattern.
Focus on understanding the core concepts and practicing coding exercises related to these topics for
your exams.
Here’s a list of **important Java topics** along with **sample questions** that are commonly asked in
exams:
- **Q1**: What is the difference between a class and an object in Java? Provide an example.
- **Q4**: What is the difference between an abstract class and an interface? When would you use
each?
- **Q5**: What is the difference between checked and unchecked exceptions? Provide examples.
- **Q6**: Write a Java program that demonstrates handling multiple exceptions using multiple `catch`
blocks.
- **Q9**: What is the difference between `ArrayList` and `LinkedList` in Java? Which one is more
efficient for inserting elements in the middle of the list?
- **Q10**: What is the difference between `HashSet` and `TreeSet`? Provide examples.
- **Q11**: Explain the working of a `HashMap` in Java and provide an example of how to iterate
through it.
- **Q12**: What is the purpose of the `Iterator` interface in Java? Demonstrate how to use it with a
collection.
### 4. **Multithreading**
- **Q13**: How do you create a thread in Java? Explain the difference between implementing
`Runnable` and extending the `Thread` class.
- **Q15**: What are the possible states of a thread in Java? Describe the thread lifecycle.
- **Q16**: What is the purpose of the `wait()` and `notify()` methods in Java? Provide an example
demonstrating their use.
- **Q17**: What is the difference between `String`, `StringBuilder`, and `StringBuffer` in Java? Which
one is thread-safe?
- **Q21**: Explain what a lambda expression is in Java. Provide an example of how it is used in a
collection.
- **Q22**: What is the `Stream` API in Java? How would you use it to filter and sort a collection?
- **Q23**: What is a default method in Java interfaces? Provide an example where you use it.
- **Q24**: Demonstrate how to use `map()` and `reduce()` methods in the Stream API.
- **Q25**: Write a program that reads content from a file and displays it on the console.
- **Q26**: What is the purpose of `Serializable` in Java? Provide an example of serializing and
deserializing an object.
- **Q27**: What is the difference between `FileReader` and `BufferedReader`? When would you prefer
one over the other?
- **Q28**: What are the steps involved in connecting to a database in Java using JDBC?
- **Q29**: Write a JDBC program to insert a record into a database using `PreparedStatement`.
- **Q31**: Explain the difference between stack memory and heap memory in Java.
- **Q32**: What is garbage collection in Java? How does it work? What is the role of the `finalize()`
method?
- **Q33**: How does Java manage memory for objects and primitives?
### 10. **Array and ArrayList**
- **Q35**: What is the difference between an array and an `ArrayList`? Which one would you use for
dynamically sized data?
- **Q36**: Write a program that merges two arrays into a third array.
- **Q37**: Explain the access levels of `public`, `private`, `protected`, and default in Java.
- **Q38**: Can a `private` method be accessed outside the class? Justify your answer with an example.
---
These questions should give you a solid base for studying the **most important Java topics** that
commonly appear in exams. Make sure to understand both the theory and practical implementation of
these topics!
An **Applet** is a special type of Java program that is designed to be embedded in a web page and run
in a browser. Unlike regular Java applications, applets are executed within a browser's Java Virtual
Machine (JVM) and are typically used for interactive or dynamic content such as animations, games, or
graphical user interfaces (GUIs) on web pages.
However, applets have become outdated and are no longer supported by most modern browsers due to
security concerns and the evolution of web technologies like HTML5 and JavaScript.
- `init()`: This method is called when the applet is first loaded. It is used for initializing resources.
- `start()`: This method is called after `init()`. It is used to start or resume execution, typically to begin
animation or other activities.
- `paint(Graphics g)`: This method is called whenever the applet needs to be redrawn (for example,
after being minimized and restored). It’s used for rendering graphics on the applet window.
- `stop()`: This method is called when the applet is no longer visible (for example, when the user
navigates away from the page). It can be used to release resources.
- `destroy()`: This method is called just before the applet is destroyed, and is used for cleanup
purposes.
2. **HTML Applet Embedding**: To embed an applet in a web page, the `<applet>` tag was used in
HTML, though this tag is now deprecated.
Example:
```html
</applet>
```
3. **Security Concerns**: Applets ran in a "sandbox" environment to restrict their access to system
resources (e.g., file systems, network connections), but this led to limitations in their functionality. As a
result, applets have been replaced by more secure and flexible technologies like JavaScript and HTML5.
```java
import java.applet.Applet;
import java.awt.Graphics;
public class HelloApplet extends Applet {
```
This simple applet displays the text "Hello, Applet!" when run.
Since most modern browsers have removed support for Java applets, **JavaFX** and **Swing** have
become popular alternatives for creating desktop GUI applications. For web development, JavaScript,
HTML5, CSS, and other modern web technologies have taken the lead over applets.
---
4. **Q4**: Explain the difference between `init()` and `start()` methods in an applet.
5. **Q5**: Why are applets considered outdated in modern web development? What are the
alternatives to applets for creating interactive web content?
MCQS
**Answer:** b) string
**Answer:** b) start()
### 4. What will be the output of the following code
snippet?
```java
int a = 10;
int b = 5;
System.out.println(a / b);
```
- a) 2
- b) 2.0
- c) 5
- d) Compile-time error
**Answer:** a) 2
**Answer:** d) global
**Answer:** c) ArrayList
**Answer:** d) nextLine()
**Answer:** d) IndexOutOfBoundsError
**Answer:** c) Map
**Answer:** b) equals()
**Answer:** d) default
**Answer:** b) ArrayIndexOutOfBoundsException
**Answer:** c) int
**Answer:** b) false
**Answer:** a) length()
### 30. Which of the following is the base class for all
exceptions in Java?
- a) Throwable
- b) Exception
- c) Error
- d) RuntimeException
**Answer:** a) Throwable
**Answer:** c) until
**Answer:** a) LinkedList
**Answer:** a) null
**Answer:** a) 30Java
**Answer:** c) break
**Answer:** a) 5
**Answer:** a) 0