[go: up one dir, main page]

0% found this document useful (0 votes)
7 views38 pages

JAVA IMP

The document outlines important Java topics essential for exams, including basic syntax, OOP concepts, methods, arrays, exception handling, collections, multithreading, file handling, Java 8 features, JDBC, memory management, access modifiers, applets, unit testing, GUI, and design patterns. It provides sample questions for each topic to aid in understanding and preparation. Additionally, it discusses the lifecycle of Java applets and their transition to modern technologies.

Uploaded by

pathaksapna34
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views38 pages

JAVA IMP

The document outlines important Java topics essential for exams, including basic syntax, OOP concepts, methods, arrays, exception handling, collections, multithreading, file handling, Java 8 features, JDBC, memory management, access modifiers, applets, unit testing, GUI, and design patterns. It provides sample questions for each topic to aid in understanding and preparation. Additionally, it discusses the lifecycle of Java applets and their transition to modern technologies.

Uploaded by

pathaksapna34
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 38

IMPORTANT TOPICS

### 1. **Basic Syntax and Structure**

- Java keywords and identifiers

- Data types (primitive and non-primitive)

- Variables, constants, and operators

- Control statements (if-else, switch-case)

- Loops (for, while, do-while)

### 2. **Object-Oriented Programming (OOP) Concepts**

- **Classes and Objects**: Understanding how to define and instantiate classes and objects.

- **Encapsulation**: Using access modifiers (private, public, etc.), getters, and setters.

- **Inheritance**: Extending classes, method overriding, and "super" keyword.

- **Polymorphism**: Method overloading and method overriding.

- **Abstraction**: Abstract classes and interfaces.

- **Association, Aggregation, and Composition**.

### 3. **Methods**

- Method declaration, definition, and calling.

- Method overloading and method overriding.

- Variable-length argument lists (varargs).

- Recursion.

### 4. **Arrays**

- One-dimensional and multidimensional arrays.


- Array initialization and accessing array elements.

- Array methods (sort, binary search, etc.).

### 5. **String Handling**

- String class and methods.

- StringBuffer and StringBuilder classes (mutability).

- String manipulation (concatenation, substring, replace, etc.).

### 6. **Exception Handling**

- Types of exceptions (checked and unchecked).

- try, catch, throw, throws, and finally keywords.

- Custom exception classes.

- Multiple catch blocks and exception chaining.

### 7. **Collections Framework**

- List, Set, Map interfaces.

- ArrayList, LinkedList, HashSet, TreeSet, HashMap, TreeMap.

- Iterators and ListIterator.

- Sorting and searching in collections.

- Collection methods (add, remove, contains, etc.).

### 8. **Multithreading**

- Creating threads (extending `Thread` class, implementing `Runnable` interface).

- Thread lifecycle (new, runnable, waiting, terminated).

- Thread synchronization (synchronized methods and blocks).


- Deadlock and thread communication (wait, notify, notifyAll).

### 9. **File Handling**

- Reading from and writing to files (FileReader, BufferedReader, FileWriter, PrintWriter).

- Serialization and deserialization.

- File handling using NIO (Path, Files, etc.).

### 10. **Java 8 Features (Lambda, Streams, etc.)**

- Lambda expressions.

- Functional interfaces.

- Stream API for processing collections.

- Method references and default methods in interfaces.

### 11. **Java I/O (Input/Output)**

- Understanding InputStream, OutputStream, Reader, and Writer classes.

- Working with file I/O.

- Serialization (ObjectInputStream and ObjectOutputStream).

### 12. **Java Database Connectivity (JDBC)**

- Basics of JDBC (connecting to databases).

- `Statement`, `PreparedStatement`, and `CallableStatement`.

- Executing queries and updates.

- Handling ResultSet.

### 13. **Java Memory Management**


- Stack and heap memory.

- Garbage Collection.

- Finalize method.

- Memory leaks and avoiding them.

### 14. **Access Modifiers**

- Public, private, protected, and default access levels.

- Static and non-static members.

### 15. **Java Applets (optional depending on syllabus)**

- Basics of applets.

- Applet lifecycle (init(), start(), stop(), destroy()).

- Applet vs. applications.

### 16. **Unit Testing in Java**

- JUnit framework for testing.

- Writing and executing test cases.

- Assertions.

### 17. **Java GUI (Swing or AWT)**

- Basic concepts of Swing or AWT.

- Creating frames, panels, and other GUI components.

- Event handling in GUI.

### 18. **Design Patterns (optional depending on syllabus)**


- Singleton Pattern.

- 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:

### 1. **Object-Oriented Programming (OOP)**

**Topic**: Classes, Objects, Inheritance, Polymorphism, Abstraction, and Encapsulation

- **Q1**: What is the difference between a class and an object in Java? Provide an example.

- **Q2**: Explain method overriding and method overloading with examples.

- **Q3**: What is encapsulation in Java? How do you implement it?

- **Q4**: What is the difference between an abstract class and an interface? When would you use
each?

### 2. **Exception Handling**

**Topic**: Try-catch, Throw, Throws, and Finally

- **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.

- **Q7**: What does the `finally` block do in Java? Can it be skipped?

- **Q8**: How do you create a custom exception class in Java?


### 3. **Collections Framework**

**Topic**: List, Set, Map, Iterators, and Generics

- **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**

**Topic**: Thread Creation, Synchronization, and Thread Lifecycle

- **Q13**: How do you create a thread in Java? Explain the difference between implementing
`Runnable` and extending the `Thread` class.

- **Q14**: What is thread synchronization in Java? Why is it important? Provide an example.

- **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.

### 5. **String Handling**

**Topic**: String, StringBuilder, and StringBuffer

- **Q17**: What is the difference between `String`, `StringBuilder`, and `StringBuffer` in Java? Which
one is thread-safe?

- **Q18**: Write a Java program to reverse a string using `StringBuilder`.

- **Q19**: How does string immutability work in Java? Why is it important?

- **Q20**: Write a program to count the number of occurrences of a character in a string.

### 6. **Java 8 Features**


**Topic**: Lambda Expressions, Streams API, Default Methods

- **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.

### 7. **File Handling**

**Topic**: File I/O and Serialization

- **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?

### 8. **JDBC (Java Database Connectivity)**

**Topic**: Database Connection, Statement, PreparedStatement

- **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`.

- **Q30**: What is the difference between `Statement` and `PreparedStatement` in JDBC?

### 9. **Java Memory Management**

**Topic**: Stack, Heap, Garbage Collection

- **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**

**Topic**: Arrays, ArrayList, and Operations

- **Q34**: Write a Java program to find the largest element in an array.

- **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.

### 11. **Access Modifiers**

**Topic**: Public, Private, Protected, Default

- **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.

### Key Characteristics of Applets:


1. **Java Applet Lifecycle**: An applet typically follows a defined lifecycle with the following methods:

- `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 code="AppletName.class" width="300" height="300">

</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.

### Example of a Simple Java Applet:

```java

import java.applet.Applet;

import java.awt.Graphics;
public class HelloApplet extends Applet {

public void paint(Graphics g) {

g.drawString("Hello, Applet!", 20, 20);

```

This simple applet displays the text "Hello, Applet!" when run.

### Transition to Modern Technologies:

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.

### Important Applet Lifecycle Methods:

1. **init()** – Initializes the applet.

2. **start()** – Starts or resumes the applet’s execution.

3. **stop()** – Pauses the applet's execution.

4. **destroy()** – Cleans up resources when the applet is destroyed.

5. **paint(Graphics g)** – Redraws the applet.

---

### Sample Exam Questions on Applets:

1. **Q1**: What is an applet in Java? Describe its lifecycle methods.


2. **Q2**: How would you embed a Java applet in an HTML page?

3. **Q3**: What is the purpose of the `paint(Graphics g)` method in an applet?

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

### 1. What is the default value of an instance variable


in Java?
- a) 0
- b) null
- c) Depends on the type
- d) Undefined

**Answer:** c) Depends on the type

### 2. Which of the following is not a valid data type in


Java?
- a) int
- b) string
- c) char
- d) boolean

**Answer:** b) string

### 3. Which of the following methods is used to start a


thread in Java?
- a) run()
- b) start()
- c) begin()
- d) execute()

**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

### 5. Which of the following is true about the `final`


keyword in Java?
- a) It can be applied to methods, classes, and
variables.
- b) It can be applied only to variables.
- c) It can be applied only to methods.
- d) It can only be applied to classes.

**Answer:** a) It can be applied to methods, classes,


and variables.

### 6. Which of the following is not an access modifier


in Java?
- a) public
- b) private
- c) protected
- d) global

**Answer:** d) global

### 7. Which of the following collections is ordered in


Java?
- a) HashSet
- b) TreeSet
- c) ArrayList
- d) HashMap

**Answer:** c) ArrayList

### 8. What is the output of the following code?


```java
String str = "Java";
str = str + " Programming";
System.out.println(str);
```
- a) JavaProgramming
- b) Java Programming
- c) Compile-time error
- d) Runtime error
**Answer:** b) Java Programming

### 9. What is the purpose of the `super` keyword in


Java?
- a) To call a constructor in the parent class
- b) To create a superclass
- c) To refer to the current class object
- d) To create an instance of the current class

**Answer:** a) To call a constructor in the parent class

### 10. Which of the following methods is used to read


user input in Java?
- a) readLine()
- b) getInput()
- c) read()
- d) nextLine()

**Answer:** d) nextLine()

### 11. What is the purpose of the `this` keyword in


Java?
- a) Refers to the current object
- b) Refers to the parent class
- c) Refers to a static method
- d) Refers to an instance variable

**Answer:** a) Refers to the current object

### 12. Which of the following is not a valid exception in


Java?
- a) ArithmeticException
- b) NullPointerException
- c) FileNotFoundException
- d) IndexOutOfBoundsError

**Answer:** d) IndexOutOfBoundsError

### 13. What does the `StringBuilder` class do in Java?


- a) Creates immutable strings
- b) Builds strings dynamically
- c) Builds arrays of characters
- d) Creates characters from strings

**Answer:** b) Builds strings dynamically

### 14. Which interface does the `HashMap` class


implement?
- a) List
- b) Set
- c) Map
- d) Collection

**Answer:** c) Map

### 15. Which method is used to compare two strings in


Java?
- a) compare()
- b) equals()
- c) match()
- d) compareTo()

**Answer:** b) equals()

### 16. Which of the following is the default access level


for members in a Java class?
- a) public
- b) private
- c) protected
- d) default

**Answer:** d) default

### 17. What does `System.exit(0)` do in Java?


- a) Terminates the current thread
- b) Terminates the program
- c) Pauses the program
- d) Throws an exception

**Answer:** b) Terminates the program

### 18. Which of the following classes is used to handle


file input and output in Java?
- a) FileReader
- b) FileOutputStream
- c) BufferedReader
- d) All of the above

**Answer:** d) All of the above

### 19. What is the purpose of the `finalize()` method in


Java?
- a) To finalize an object’s construction
- b) To clean up resources before an object is
destroyed
- c) To close an open file
- d) To prevent method overriding

**Answer:** b) To clean up resources before an object


is destroyed

### 20. What will be the output of the following code?


```java
int[] arr = {1, 2, 3, 4, 5};
System.out.println(arr[5]);
```
- a) 5
- b) ArrayIndexOutOfBoundsException
- c) null
- d) 0

**Answer:** b) ArrayIndexOutOfBoundsException

### 21. Which of the following is true about


constructors in Java?
- a) A constructor is a method that initializes an object
- b) A constructor can return a value
- c) A constructor is not mandatory for a class
- d) All of the above
**Answer:** a) A constructor is a method that
initializes an object

### 22. What does the `clone()` method do in Java?


- a) Copies an object
- b) Duplicates a method
- c) Copies the class
- d) Copies an array

**Answer:** a) Copies an object

### 23. What does the `instanceof` operator check in


Java?
- a) Checks if an object is of a specified class type
- b) Checks if an object is null
- c) Checks if an object is equal to another object
- d) Checks if a variable is an instance of a specific class

**Answer:** a) Checks if an object is of a specified


class type

### 24. What is the return type of the `hashCode()`


method in Java?
- a) String
- b) Object
- c) int
- d) void

**Answer:** c) int

### 25. What is the default value of a boolean variable


in Java?
- a) true
- b) false
- c) null
- d) Undefined

**Answer:** b) false

### 26. What is the correct way to declare an array in


Java?
- a) int[] arr;
- b) int arr[];
- c) Both a and b
- d) None of the above

**Answer:** c) Both a and b

### 27. Which of the following is used to import all the


classes from a package in Java?
- a) import package.*;
- b) import *;
- c) import all classes;
- d) import package.all;

**Answer:** a) import package.*;

### 28. Which method is used to get the length of a


string in Java?
- a) length()
- b) getLength()
- c) size()
- d) getSize()

**Answer:** a) length()

### 29. What does the `charAt()` method in Java return?


- a) The ASCII value of a character
- b) The character at the specified index
- c) The substring starting at the specified index
- d) None of the above

**Answer:** b) The character at the specified index

### 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

### 31. Which of the following is not a valid loop in


Java?
- a) for
- b) while
- c) until
- d) do-while

**Answer:** c) until

### 32. What is the purpose of the `transient` keyword


in Java?
- a) It prevents an instance variable from being
serialized
- b) It makes a variable static
- c) It makes a variable final
- d) It makes an object immutable

**Answer:** a) It prevents an instance variable from


being serialized
### 33. Which of the following classes is part of the Java
collections framework?
- a) LinkedList
- b) String
- c) Integer
- d) None of the above

**Answer:** a) LinkedList

### 34. What does `Math.pow(x, y)` do in Java?


- a) Raises x to the power of y
- b) Raises y to the power of x
- c) Multiplies x and y
- d) Returns the maximum of x and y

**Answer:** a) Raises x to the power of y


### 35. Which of the following classes can be used for
reading text from a file in Java?
- a) FileReader
- b) BufferedReader
- c) FileInputStream
- d) All of the above

**Answer:** d) All of the above

### 36. What will happen if you try to divide a number


by zero in Java?
- a) Runtime exception
- b) Compile-time error
- c) Output is infinity
- d) Output is NaN

**Answer:** a) Runtime exception


### 37. What is the default value of a reference variable
in Java?
- a) null
- b) 0
- c) Undefined
- d) false

**Answer:** a) null

### 38. What will the following code output?


```java
System.out.println(10 + 20 + "Java");
```
- a) 30Java
- b) Java30
- c) 10Java20
- d) Error

**Answer:** a) 30Java

### 39. What is the purpose of the `abstract` keyword in


Java?
- a) To create abstract classes
- b) To prevent a class from being instantiated
- c) To declare abstract methods
- d) All of the above

**Answer:** d) All of the above

### 40. Which of the following statements is used to


exit a loop in Java?
- a) exit
- b) stop
- c) break
- d) continue

**Answer:** c) break

### 41. What is the scope of a local variable in Java?


- a) Whole class
- b) Inside the method where it is defined
- c) Inside the constructor
- d) Inside the block where it is defined

**Answer:** b) Inside the method where it is defined

### 42. What will the following code print?


```java
int x = 5;
System.out.println(x++);
```
- a) 5
- b) 6
- c) Error
- d) None of the above

**Answer:** a) 5

### 43. What does the `finally` block do in exception


handling?
- a) Executes only if an exception occurs
- b) Executes only if no exception occurs
- c) Always executes after the try-catch block
- d) Executes only if the catch block executes

**Answer:** c) Always executes after the try-catch


block
### 44. What does the `static` keyword do in Java?
- a) Defines a variable for a particular class
- b) Defines a variable for a particular object
- c) Makes a variable or method accessible without
creating an object
- d) Prevents a variable from being changed

**Answer:** c) Makes a variable or method accessible


without creating an object

### 45. What is the default value of a static variable in


Java?
- a) null
- b) 0
- c) false
- d) Undefined
**Answer:** b) 0

### 46. Which of the following is not a valid keyword in


Java?
- a) interface
- b) extends
- c) super
- d) class

**Answer:** None (All are valid keywords)

### 47. What is the correct syntax to declare a two-


dimensional array in Java?
- a) int arr[][];
- b) int[] arr[];
- c) int arr[2][2];
- d) All of the above
**Answer:** d) All of the above

### 48. How do you create a constant in Java?


- a) Using the `final` keyword
- b) Using the `constant` keyword
- c) Using the `static` keyword
- d) Using the `immutable` keyword

**Answer:** a) Using the `final` keyword

### 49. What is the result of the expression `5 + 2 * 3` in


Java?
- a) 15
- b) 11
- c) 17
- d) 13
**Answer:** b) 11

### 50. What is the default value of a byte variable in


Java?
- a) 0
- b) null
- c) false
- d) Undefined

**Answer:** a) 0

You might also like