0% found this document useful (0 votes)
1 views

Java_prac[1]

The document outlines important Java programming questions and concepts across multiple units, including data types, operators, tokens, input/output streams, arrays, method overloading, exception handling, and GUI components. It provides examples of Java programs demonstrating these concepts, such as array manipulation and input/output operations. Additionally, it discusses the significance of variable scope, constructors, inheritance, and synchronization in multi-threaded applications.

Uploaded by

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

Java_prac[1]

The document outlines important Java programming questions and concepts across multiple units, including data types, operators, tokens, input/output streams, arrays, method overloading, exception handling, and GUI components. It provides examples of Java programs demonstrating these concepts, such as array manipulation and input/output operations. Additionally, it discusses the significance of variable scope, constructors, inheritance, and synchronization in multi-threaded applications.

Uploaded by

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

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) {
System.out.println("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 java.io package.
There are two types of streams:
 Input Stream: Used to take input (data goes into the program).
→ Example: System.in, FileInputStream
 Output Stream: Used to display output (data goes out of the
program).
→ Example: System.out, FileOutputStream
Java provides three default stream objects:
 System.in → takes input from keyboard
 System.out → prints output to screen
 System.err → shows error messages

✅ Simple Program using Input and Output Stream:


java
CopyEdit
import java.io.*;

public class SimpleIOExample {


public static void main(String[] args) throws IOException {
System.out.print("Enter a character: ");

// Reading a single character from keyboard


int ch = System.in.read(); // InputStream

System.out.print("You entered: ");


System.out.write(ch); // OutputStream
}
}

✅ Explanation:
 System.in.read() reads one character as a number (ASCII value).
 System.out.write(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 System.in and System.out.

✅ Conclusion:
Input and Output streams in Java are essential for interacting with users
and files. Using System.in.read() and System.out.write(), 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


System.out.println("Original Marks:");
for (int i = 0; i < marks.length; i++) {
System.out.print(marks[i] + " ");
}

// Step 3: Manipulate array (increase each mark by 5)


for (int i = 0; i < marks.length; i++) {
marks[i] = marks[i] + 5;
}

// Step 4: Display modified array


System.out.println("\nUpdated Marks:");
for (int mark : marks) {
System.out.print(mark + " ");
}
}
}

✅ Explanation:
 int[] marks declares an array of 5 integers.
 marks.length 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.
2.Describe how to handle AWT events in a Java application.
3.Analyze 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 ) :
1.Food order system using jcheckbox
2. Then find ip address
3. Then create calculator + addition and subtraction

You might also like