Java important questions :
Unit 1:
1. Design a Java program that uses various data types and operators to
perform a calculation
2. What are the rules for naming variables in Java?
3. What are Java tokens? List the different types.
In Java, a token is the smallest unit of a program that the compiler
understands.
When we write code, the Java compiler breaks it into tokens for
processing.
These tokens form the basic building blocks of a Java program. Without
them, we cannot write any valid Java statement.
✅ Types of Java Tokens:
1. Keywords:
o These are reserved words in Java with a specific meaning.
o Examples: int, class, public, if, else
2. Identifiers:
o These are names given to variables, methods, classes, etc.
o Must start with a letter, $, or _
o Examples: age, sum, StudentName
3. Literals:
o These are constant values directly used in the program.
o Examples: 100 (integer), 3.14 (float), 'A' (char), "Hello"
(String)
4. Operators:
o Symbols used to perform operations like addition, comparison,
etc.
o Examples: +, -, *, /, =, ==
5. Separators (Special Characters):
o Used to separate parts of the code like methods, classes, etc.
o Examples: ;, (), {}, [ ], ,
🔍 Example Code:
java
CopyEdit
public class Hello {
public static void main(String[] args) {
[Link]("Hello Java");
}
}
Tokens in this example:
public, class, Hello, {, static, void, main, String, args, [], System, ., out, .,
println, ("Hello Java"), ;, }
✅ Conclusion:
Java tokens are essential elements that form the structure of a Java
program. The compiler uses these tokens to understand and compile the
code properly.
4. Demonstrate how to use input and output streams in
a Java program.
In Java, streams are used for handling input (reading data) and output
(writing data). Streams are like pipes through which data flows. Java
provides these features in the [Link] package.
There are two types of streams:
Input Stream: Used to take input (data goes into the program).
→ Example: [Link], FileInputStream
Output Stream: Used to display output (data goes out of the
program).
→ Example: [Link], FileOutputStream
Java provides three default stream objects:
[Link] → takes input from keyboard
[Link] → prints output to screen
[Link] → shows error messages
✅ Simple Program using Input and Output Stream:
java
CopyEdit
import [Link].*;
public class SimpleIOExample {
public static void main(String[] args) throws IOException {
[Link]("Enter a character: ");
// Reading a single character from keyboard
int ch = [Link](); // InputStream
[Link]("You entered: ");
[Link](ch); // OutputStream
}
}
✅ Explanation:
[Link]() reads one character as a number (ASCII value).
[Link](ch) prints the same character to the screen.
Example: If user types A, it will print You entered: A.
This is the simplest way to demonstrate both input stream and output
stream using [Link] and [Link].
✅ Conclusion:
Input and Output streams in Java are essential for interacting with users
and files. Using [Link]() and [Link](), we can perform
basic I/O operations. This helps in understanding the basic working of data
flow in a Java program
5. Create a Java program that utilizes arrays and demonstrates their
manipulation.
An array in Java is a container object that holds multiple values of the
same data type. Arrays are used when we want to store a fixed number
of elements. They are stored in contiguous memory and can be
accessed using an index starting from 0.
✅ Simple Java Program Using Arrays:
java
CopyEdit
public class ArrayExample {
public static void main(String[] args) {
// Step 1: Declare and initialize the array
int[] marks = {65, 70, 75, 80, 85};
// Step 2: Display original array elements
[Link]("Original Marks:");
for (int i = 0; i < [Link]; i++) {
[Link](marks[i] + " ");
}
// Step 3: Manipulate array (increase each mark by 5)
for (int i = 0; i < [Link]; i++) {
marks[i] = marks[i] + 5;
}
// Step 4: Display modified array
[Link]("\nUpdated Marks:");
for (int mark : marks) {
[Link](mark + " ");
}
}
}
✅ Explanation:
int[] marks declares an array of 5 integers.
[Link] gives the size of the array.
A for loop is used to read and update elements.
This program increases each student’s marks by 5 and displays the
updated list.
✅ Output:
yaml
CopyEdit
Original Marks:
65 70 75 80 85
Updated Marks:
70 75 80 85 90
✅ Conclusion:
Arrays are helpful when we need to store multiple values. This program
clearly shows how to declare, access, update, and print array values. It
demonstrates basic array manipulation in a beginner-friendly and exam-
appropriate way.
6. Describe the scope of a variable in Java and its significance.
Unit 2:
1 . Explain the concept of method overloading with an example.
2. Create a class in Java with data members and methods, and
demonstrate how to create an object of that class.
3. . Evaluate the advantages of using constructors in Java. Why is
constructor overloading useful?
4. Design a simple Java application that demonstrates inheritance and
method overriding.
.5. What are the different types of loops available in Java?.
6. Write a Java program that uses a switch statement to perform different
operations based on user input.
Unit 3:
1 . Assess the importance of exception handling in Java. What are the
consequences of not handling exceptions?
2. What are Java packages, and why are they used?
3. . Design a multi-threaded Java application that demonstrates thread
synchronization.
4. Explain the concept of thread priority and how it affects thread
execution.
.5. Create a Java application that handles exceptions and demonstrates
various error handling techniques.
6. . Justify the need for synchronization in multi-threaded applications.
What issues can arise without it?
Unit 4:
1. Explain how to create and use a popup menu in a Java application.
[Link] how to handle AWT events in a Java application.
[Link] the role of layout managers in Java GUI applications. How do
they affect component arrangement?
4. Demonstrate how to display an image in a Java GUI application
.5. What are the different types of components available in AWT?
6. . Explain the concept of event listeners and their role in event handling.
7. create a java program using switch case .
Application based (use college notes ) :
[Link] order system using jcheckbox
2. Then find ip address
3. Then create calculator + addition and subtraction