University Cs3391
University Cs3391
The core idea behind Java's platform independence is the principle of "Write Once, Run
Anywhere"..
The key to this lies in Java's use of bytecode. Java code is first compiled into an intermediate
form called bytecode
.
3 What are the four types of access modifiers CO1 K1
Access Modifier Visibility
public Accessible from anywhere (any class or package).
private Access
protectedble only
Accessible within the same package and by subclasses (even in different
within the same class.
packages).
Default (Package-
Accessible only within the same package.
Private)
Page 1 of 12
within a single process. concurrently.
7 Why do we need generics in java CO2 K1
Generics in Java provide a way to write type-safe code that works with objects of various types
while still allowing compile-time checks for errors. By using generics, Java allows you to write
more flexible, reusable, and maintainable code without losing type safety .
8 What is the character stream CO3 K2
Character streams are part of the java.io package. They consist of Reader and Writer classes, each
of which has specific subclasses for handling text.
9 What do u mean event handling CO3 K3
Event handling in Java refers to the mechanism that allows an application to respond to user
interactions or other events (such as mouse clicks, keyboard input, window resizing, etc.).
10 Differentiate between HBOX and VBOX CO4 K1
H Box (Horizontal Box V Box (Vertical Box )
Best used when you want to arrange Best used when you want to arrange
components top to bottom components left to right.
PART -B
(Answer all the Questions 5 x 13 = 65 Marks)
11a
Java is one of the most widely used programming languages, known for its platform independence,
robust security features, and simplicity. Below is a list of various features of Java along with
explanations:
1. Platform Independence:
4. Multithreading.
6. High Performance:.
11)b)What is the array write a java program demonstrating the use of one –
dimensional array in java
An array in Java is a data structure that stores a fixed-size collection of elements of the same type.,
Page 2 of 12
strings, objects, etc.
One-Dimensional Array:
A one-dimensional array is a linear collection of elements, where each element is accessed by its
index.
OR
11b Why do we need Static members how to access with example
1. .
1. Static Variables (Class Variables): These are variables shared by all instances of the class.
2. Static Methods: These are methods that can be called without creating an instance of the
class.
3. Static Blocks: These are used for static initialization of a class.
4. Static Classes: These are inner classes declared as static.
Static Variables: Can be accessed using the class name or through an object reference.
Static Methods: Can be called using the class name or through an object reference, but it's
recommended to use the class name.
Example: Javaprogram
11)b)iiProperties of constructor
constructor in Java is a special type of method used to initialize objects. When an object of a class is
created, the constructor is automatically invoked to initialize the object's fields.
.
Example:
3. Called Automatically:
Example:
Example:
Example
Page 3 of 12
:
12a Illustrate how to add classes in package and how to access these class in package. CO2 K2
In Java, packages are used to group related classes, interfaces, and sub-packages together. A
package helps in organizing the project structure and avoiding class name conflicts.
Steps to Create a Package, Add Classes to It, and Access Classes in a Package
1. Creating a Package
OR
12b Explain interface with example CO2 K3
n interface in Java is a reference type, similar to a class, but it can contain only abstract methods,
default methods, static methods, and constants (final variables).
13a i)Define exception and explain its different types with example. CO3 K2
An exception in Java is an event that disrupts the normal flow of the program's execution.
Page 4 of 12
1. try: The block where exceptions might occur.
2. catch: The block that catches the exception and handles it.
3. finally: A block that is always executed, whether or not an exception occurred.
4. throw: Used to explicitly throw an exception.
5. throws: Used in method declarations to specify that the method might throw exceptions.
Example Program
1. Final Variable.
2.Final Method
A method that is declared final cannot be overridden by subclasses. This is used to prevent a method
from being modified or redefined in derived classes.
Example Program:
OR
13b Differentiate string buffer and string class CO3 K2
A palindrome is a string that reads the same forward and backward, such as "madam", "racecar", or
"level".
Example program
14a Write a java program to read data from file and to write a data toa a file CO4 K3
import java.io.*;
14b Describe how to implement runnable interface for creating and starting CO4 K3
threads
// Class that implements Runnable interface
class MyRunnable implements Runnable {
@Override
public void run() {
// Code that will be executed by the thread
System.out.println(Thread.currentThread().getName() + " is running...");
try {
// Simulating some work with a sleep
Thread.sleep(1000); // 1 second
} catch (InterruptedException e) {
System.out.println("Thread interrupted");
}
System.out.println(Thread.currentThread().getName() + " has finished.");
}
}
The lifecycle of a thread describes the various states a thread can be in during its existence. A thread
can be in one of the following states at any point during its lifetime:
15a State and explain the basic of AWT event handling in detail CO5 K3
import java.awt.*;
import java.awt.event.*;
// Create a button
Button button = new Button("Click Me");
frame.setLayout(new FlowLayout());
frame.add(button);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0); // Exit the application
}
});
}
// Create a MenuBar
MenuBar menuBar = new MenuBar();
Page 8 of 12
// Create the 'File' menu
Menu fileMenu = new Menu("File");
Declare and abstact class to represent a bank account with data memer’s name account
number address and abstract methods withdraw and deposit. Method display is needed to show
balance . Derive a subclass Savings
(i) Account and add the following details: return on inverstment and the method
calc() to show the amount in the account and slow the use of with draw nd
deposit abstract methods
OR
16b Create software for dept stores to maintain the following details like item no, item CO5 K4
description, (i)requested quantity, cost price, provide the options to update the stock. Calculate
the selling price(SP=CP *20%). Create an exception whenever the selling price of item
exceeds the given amount
// Custom exception class for when the selling price exceeds the threshold
class SellingPriceExceededException extends Exception {
public SellingPriceExceededException(String message) {
super(message);
}
}
class Item {
private int itemNo;
private String description;
private int requestedQuantity;
private double costPrice;
private double sellingPrice;
Page 10 of 12
class StoreManagement {
private static final double SELLING_PRICE_THRESHOLD = 1000.00;
while (true) {
// Menu
System.out.println("\nDepartment Store Management");
System.out.println("1. Add Item");
System.out.println("2. Update Stock");
System.out.println("3. Display Item Details");
System.out.println("4. Exit");
System.out.print("Choose an option: ");
int option = scanner.nextInt();
switch (option) {
case 1:
// Add Item
System.out.print("Enter item number: ");
int itemNo = scanner.nextInt();
scanner.nextLine(); // Consume newline
System.out.print("Enter item description: ");
String description = scanner.nextLine();
System.out.print("Enter requested quantity: ");
int requestedQuantity = scanner.nextInt();
System.out.print("Enter cost price: ");
double costPrice = scanner.nextDouble();
case 2:
// Update Stock
System.out.print("Enter item number to update: ");
int updateItemNo = scanner.nextInt();
case 3:
Page 11 of 12
// Display Item Details
System.out.print("Enter item number to display: ");
int displayItemNo = scanner.nextInt();
itemFound = false;
for (int i = 0; i < itemCount; i++) {
if (items[i].itemNo == displayItemNo) {
items[i].displayDetails();
itemFound = true;
break;
}
}
if (!itemFound) {
System.out.println("Item not found.");
}
break;
case 4:
// Exit
System.out.println("Exiting the system.");
scanner.close();
return;
default:
System.out.println("Invalid option. Please try again.");
}
}
}
}
Course Faculty
R.UMA HOD
Dr.P.SUBHARAJAM
(Name /Sign / Date) (Name /Sign / Date)
Page 12 of 12