Q1
Q1
→In Java, `this` refers to the current object. It is used to access instance variables, methods, or
constructors of the same class, avoiding naming conflicts.
- byte (1 byte), short (2 bytes), int (4 bytes), long (8 bytes): Store integers.
2. Non-Primitive:
d. what is an interface?
→ An interface in Java is a blueprint of a class. It contains abstract methods (no body) and constants.
Classes implement interfaces to achieve abstraction and multiple inheritance.
→ The Readers & Writers classes in Java are used for handling character-based input and output,
supporting Unicode characters, and simplifying file and stream operations.
• Frame: BorderLayout.
• Panel: FlowLayout.
→ ArithmeticException: Thrown when an illegal arithmetic operation, like division by zero, occurs.
NullPointerException: Thrown when trying to access or modify an object reference that is null.
→ The getContentPane() method in Java is used to retrieve the content pane of a JFrame (or other
container). It allows you to add components (like buttons, labels, etc.) to the main window, as the
content pane is where the components are placed.
3. Platform-Independent: Java code runs on any device with a Java Virtual Machine (JVM).
7. Distributed: Facilitates building networked applications with support for RMI and sockets.
2)what are the rules for method overloading and method overriding?
1. Same method name: Overloaded methods must have the same name.
2. Different parameter list: The methods must differ in the number or type of parameters.
3. Return type: Return type can be different, but it alone cannot distinguish overloaded
methods.
1. Same method signature: The method in the subclass must have the same name, return
type, and parameters.
3. Access level: The overridden method cannot have a more restrictive access modifier than
the original method (e.g., a public method cannot be overridden as private).
1. Methods:
o Interface: Can only have abstract methods (until Java 8, which added default and
static methods).
o Abstract Class: Can have both abstract methods (without implementation) and
concrete methods (with implementation).
2. Multiple Inheritance:
o Abstract Class: A class can inherit only one abstract class due to single inheritance.
3. Constructors:
4. Access Modifiers:
o Interface: All methods are implicitly public, and variables are public static final by
default.
o Abstract Class: Can have methods with various access modifiers (private, protected,
public).
5. Fields:
6. Purpose:
o Interface: Used to represent a contract for what a class can do, without specifying
how.
o Abstract Class: Used when classes share common functionality but also have
different implementations.
→Exception:
An exception is an event that disrupts the normal flow of the program's execution. It occurs
during runtime and can be caused by errors such as division by zero, file not found, or invalid
input. Exceptions are objects that represent error conditions.
Exception Handling:
Exception handling in Java allows you to manage runtime errors in a controlled way using
try, catch, throw, throws, and finally blocks. This helps maintain program flow even when
unexpected situations occur:
5. finally block: Executes code after try-catch, regardless of whether an exception occurred.
This mechanism ensures that errors are handled gracefully without crashing the application.
→ In Java, streams are used for input and output (I/O) operations. They allow you to read from and
write to various data sources like files, memory, or network connections. Streams are broadly
classified into two categories: byte streams and character streams.
1. Byte Streams:
Byte streams are used to handle I/O of raw binary data. They read and write data in the form of
bytes, which is ideal for handling all kinds of I/O, including image files, audio files, and other binary
files.
o DataInputStream: Reads primitive data types (e.g., int, float) in binary form.
• OutputStream: The superclass for all byte output streams.
2. Character Streams:
Character streams are used for I/O operations involving text data. They handle characters and are
capable of reading and writing Unicode characters, making them suitable for text files.
3. Specialized Streams:
→ import java.util.Scanner;
class Slip4A {
public static void main(String args[]){
Scanner scan=new Scanner(System.in);
try {
System.out.print("Enter String : ");
String str = scan.nextLine();
for(int i=0;i<str.length();i+=2) {
System.out.print(" " + str.charAt(i));
}
} catch (Exception e) {}
}
}
cdn ciiy
7)write a java program to calculate the area of circle,triangle and rectangle(use method
overloading).
}
Output:
9)write a java program to search a give name in an array and if it is found then display its index
otherwise display appropriate message.
→import java.util.Scanner;
if (index != -1) {
} else {
scanner.close();
if (names[i].equalsIgnoreCase(nameToSearch)) {
}
return -1; // Return -1 if name is not found
Output:
9)write a java program to display ASCII value of the characters from a file?
→import java.io.FileReader;
import java.io.IOException;
int character;
} catch (IOException e) {
Output: Hello
10)write a java program to display multiplication table of a given number into the list box by
clicking button?
→import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setLayout(new FlowLayout());
frame.add(label);
frame.add(textField);
frame.add(button);
frame.add(new JScrollPane(listBox));
button.addActionListener(new ActionListener() {
@Override
listModel.clear();
try {
});
frame.setVisible(true);
→ A Collection in Java is an object that holds a group of elements. It provides methods for storing,
accessing, manipulating, and processing data, and includes interfaces like List, Set, and Map.
The Java Collections Framework provides a set of interfaces, classes, and algorithms for managing
data. It includes interfaces like Collection, List, Set, Queue, and Map, with implementations such
as ArrayList, HashSet, and HashMap. Collections allow efficient data storage and manipulation,
supporting operations like insertion, deletion, sorting, and searching. Utility classes like Collections
offer sorting and shuffling methods. The framework simplifies data handling by offering flexible,
reusable components for various data structures and algorithms in Java.
→ AWT:
3. Provides basic GUI components like buttons, labels, and text fields.
3. Offers advanced components like tables, trees, and rich text areas.
→ import java.util.Scanner;
while(num != 0){
num /= 10;
if(isArmstrong(number)){
}else{
}
sc.close();
14)write a java program to copy only non-numeric data from one file to another file?
```java
import java.io.*;
try{
String line;
for(char c : line.toCharArray()){
if(!Character.isDigit(c)){
nonNumericData.append(c);
writer.write(nonNumericData.toString());
writer.newLine();
reader.close();
writer.close();
System.out.println("Non-numeric data copied successfully!");
}catch(IOException e){
→ The new operator in Java is used to create new objects or instances of classes. It dynamically
allocates memory for the object and returns a reference to that memory location. This operator is
essential for instantiating classes, arrays, and other data structures. When you use new, it calls the
constructor of the class, initializing the object. For example, MyClass obj = new MyClass(); creates
a new object of MyClass. The new operator is crucial for object-oriented programming in Java.
→ The finalize() method in Java is a method of the Object class that is called by the garbage
collector before an object is destroyed. It allows an object to clean up resources like closing files or
releasing memory before it is reclaimed. The finalize() method is invoked automatically by the
garbage collector, but it can be overridden to provide custom cleanup logic. However, its use is
discouraged in modern Java development, as it can introduce unpredictability and performance
issues.
super.finalize();
1. Declare the Package: Use package keyword at the top of the Java file.
3. Save the File: Save the file in a folder that matches the package name (e.g.,
mypackage/MyClass.java).
4. Compile the Code: Use javac to compile the Java file.
5. Import and Use: In other classes, use `import` to access the package's content.