Subject:Java Subject
Programming Code: 220C4A
DEPARTMENT OF COMPUTER Class: II BCA Shift : I/II
APPLICATIONS
CAT-I EXAMINATION Max. Marks: 75 Time: 3 hrs
MARCH 2025
Date:18.03.2025 Evaluate Blue Print
Answer all the questions
Part – A (10 x 2 = 20)
1. Data abstraction is the process of hiding certain details and showing only
essential information to the user. (2)
2. Java is known as platform-independent because its programs are
compiled into bytecode, which can run on any device with a Java Virtual
Machine (JVM), regardless of the underlying hardware or operating system.
(2)
3. Class: A class is the Blueprint or plan or template that describes the
details of an object. It contains attributes and methods of an object.(1)
Object: Any entity that has state and behavior is known as an Object. An
Object is an instance of an class.(1)
4. we can use final Keyword for a function to make sure that it cannot be
overridden. We can also use final in Java to make sure that a class cannot be
inherited.(2)
5. If child class has the same method as declared in the parent class, It is
known as Method Overriding in Java. (2)
6. throw: Used to explicitly throw an exception in the code (e.g., throw new
IOException();).(1)
throws: Declares the exceptions a method might throw, informing the caller
to handle them (e.g., void myMethod() throws IOException {}). (1)
7. A thread is the smallest unit of execution within a program. It allows
tasks to run concurrently, enabling multithreading, where multiple threads
perform operations simultaneously to enhance performance and
responsiveness. Threads are implemented using the Thread class or the
Runnable interface. (2)
8. The purpose of JTextArea in Swing is to allow users to input or display
multi-line text. It supports editing, selection, and scrolling when combined
with a JScrollPane, making it ideal for displaying large amounts of text. (2)
9. EDM typically refers to Entity Data Model, which represents the
structure of data in terms of entities and their relationships. It's commonly
used in frameworks like Hibernate or JPA for mapping objects to database
tables. (2)
10. The Vector class in Java is a part of the [Link] package and provides a
dynamic array that can grow or shrink as needed. It is synchronized, meaning
it is thread-safe, and is used to store elements that can be accessed using an
index, similar to an ArrayList but with built-in thread safety. (2)
Part – B (5 x 5 = 25)
11)Operators:
1. Arithmetic Operators: Perform basic mathematical operations. (1)
Example: +, -, *, /, %
Example program:-
int a = 10, b = 5;
[Link](a + b);
Output: 15
2. Relational (Comparison) Operators: Compare two values. (1)
Example: ==, !=, <, >, <=, >=
Example program:-
int a = 10, b = 5;
[Link](a > b);
Output: true
3. Logical Operators: Combine multiple boolean expressions. (1)
Example: && (AND), || (OR), ! (NOT)
Example program:-
boolean a = true, b = false;
[Link](a && b);
Output: false
4. Bitwise Operators: Work with bits in binary form.
Example: &, |, ^, ~, <<, >>
Example program:-
int a = 5, b = 3; // Binary: a=0101, b=0011
[Link](a & b); // Output: 1
5. Assignment Operators: Assign values to variables. (1)
Example: =, +=, -=, *=, /=
Example program:-
int a = 10;
a += 5; // Same as a = a + 5
[Link](a);
Output: 15
6. Unary Operators: Operate on a single operand. (1)
Example: +, -, ++, --, !
Example program:-
int a = 5;
[Link](++a);
Output: 6
7. Ternary Operator: Shortened conditional operator.
Example: condition ? value1 : value2
Example program:-
int a = 5, b = 10;
int max = (a > b) ? a : b;
[Link](max);
Output: 10
12) Packages in Java:
Definition: A package is a mechanism to encapsulate a group of related
classes, interfaces, and sub-packages. (1)
Types:
1. Built-in Packages: Predefined packages such as [Link], [Link],
[Link], [Link], [Link], etc., used for various functionalities like I/O
operations, GUI development, networking, and utility tasks. (2)
Example:-
import [Link];
public class DateExample
{
public static void main(String[] args)
{
Date today = new Date();
[Link]("Today is: " + today);
}
}
2. User-defined Packages: Packages created by users to organize their own
classes and interfaces. (2)
Syntax: package packageName;
Example:-
package mypack;
public class Subtraction
{
public void sub()
{
int a = 20, b = 15;
[Link]("Result: " + (a - b));
}
}
import [Link];
public class SubResult
{
public static void main(String[] args)
{
Subtraction s = new Subtraction();
[Link]();
}
}
[Link] in Java:
Definition: Multithreading is a feature that allows the concurrent execution of
two or more parts of a program, known as threads, to maximize CPU
utilization. Threads are lightweight processes. (1)
Ways to Implement Multithreading:
By Extending the Thread Class: (1)
- Create a class that extends [Link].
- Override the run() method.
- Create an object of the class and call the start() method to
execute the thread.
Example:- (3)
import [Link].*;
import [Link];
class gee extends Thread
{
public void run()
{
for(int i=0;i<=3;i++)
{
[Link]("Hey there");
try
{
[Link](5);
}
catch(InterruptedException e)
{
[Link](e);
}
}
}
}
class anj extends Thread
{
public void run()
{
for(int i=0;i<=3;i++)
{
[Link]("I'M Multithreading");
try
{
[Link](6);
}
catch(InterruptedException e)
{
[Link](e);
}
}
}
}
class ThreadExample
{
public static void main(String args[])
{
gee g=new gee();
anj a=new anj();
[Link]();
[Link]();
}
}
[Link] Java Swing, JCheckBox is a component used to create a checkbox that
allows the user to make a selection. It can be either checked (selected) or
unchecked (unselected), representing a binary choice like
"Yes/No" or "On/Off". (1)
Syntax:-
JCheckBox checkBox = new JCheckBox(String text); (1)
Example:- (3)
import [Link].*;
import [Link].*;
public class JCheckBoxExample
{
public static void main(String[] args)
{
JFrame f= new JFrame("JCheckBox Example");
[Link](300, 200);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](null);
JCheckBox c = new JCheckBox("Accept Terms & Conditions");
[Link](50, 50, 200, 30);
[Link](new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if([Link]())
{
[Link](f, "Accepted!");
}
else
{
[Link](f, "Not Accepted!");
}
}
});
[Link](c);
[Link](true);
}
}
15)Event Handling in Java
Event handling manages user interactions or system-generated events (e.g.,
mouse clicks, key presses) to make applications interactive. It uses Event
Listeners, Event Sources, and Event Handlers. (1)
Key Components: (1)
1. Event Listeners: Detect specific events (e.g., ActionListener for button
clicks).
2. Event Sources: Components that trigger events (e.g., buttons, checkboxes).
3. Event Handlers: Methods to define responses to events
(actionPerformed()).
Syntax:- (1)
import [Link].*;
class ClassName implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
// Event handling logic
}
public static void main(String[] args)
{
new ClassName();
}
}
Example:- (2)
import [Link].*;
import [Link].*;
class eventexample implements ActionListener
{
eventexample()
{
JFrame f=new JFrame();
JButton b=new JButton("Click Me");
[Link](this);
[Link](b);
[Link](200,200);
[Link](null);
[Link](true);
}
public void actionPerformed(ActionEvent e)
{
[Link]("Button Clicked");
}
public static void main(String args[])
{
new eventexample();
}
}
Part – C (3 x 10 = 30)
[Link]
Deriving a new class from an existing [Link] implement a keyword is
“extends”.
There are 4 types of Inheritance.
Single inheritance: (1)
A class inherits from one parent class.
Syntax class B extends A {}
Multilevel inheritance: (1)
A class inherits from a parent class, which in turn inherits from
another parent class.
Syntax :-class C extends B {} and class B extends A {}
Hierarchical Inheritance: (1)
Multiple classes inherit from one parent class.
Syntax :-class B extends A {} and class C extends A {}
Hybrid Inheritance: (1)
A combination of two or more types of inheritance. (Java doesn't
support multiple inheritance with classes, but it can be achieved using
interfaces.)
Syntax :-class D extends C implements E {}
MULTILEVEL INHERITANCE: (3)
EXAMPLE:-
import [Link].*;
class mufasa
{
void mufa()
{
[Link]("Mufasa is king of Forest");
}
}
class simba extends mufasa
{
void sim()
{
[Link]("Simba is a son of Mufasa");
}
}
class scar extends simba
{
void sca()
{
[Link]("Scar is the villan of Forest");
}
}
class Multileval
{
public static void main(String args[])throws IOException
{
scar lion=new scar();
[Link]();
[Link]();
[Link]();
}
}
Hierarchical Inheritance: (3)
EXAMPLE:-
import [Link].*;
class iduna
{
void idu()
{
[Link]("Iduna is the Oueen of Arenda");
}
}
class elsa extends iduna
{
void els()
{
[Link]("Elsa is the Daughter of Iduna");
}
}
class anna extends iduna
{
void ann()
{
[Link]("Anna is the princess of Arenda");
}
}
class Hierachical
{
public static void main(String args[])
{
elsa agnar=new elsa();
anna chris=new anna();
[Link]();
[Link]();
[Link]();
[Link]();
}
}
17. 1. JLabel Class:- (3)
The JLabel class is used to display a short string or an image icon in a Swing
GUI. It is non-editable and used for displaying information or labels for
other components.
Syntax:-
JLabel label = new JLabel("Your Text Here");
Example:-
import [Link].*;
class JLabelExample
{
public static void main(String args[])
{
JFrame f = new JFrame("JLabel Example");
[Link](300, 200);
JLabel l = new JLabel("Welcome to Swing GUI!");
[Link](l);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
}
}
2. JButton Class (4)
The JButton class represents a push button that can trigger an action when
clicked. It is a key component for user interactions in the GUI.
Syntax:-
JButton button = new JButton("Button Text");
Example:-
import [Link].*;
import [Link].*;
class JButtonExample
{
public static void main(String args[])
{
JFrame f = new JFrame("JButton Example");
[Link](300, 200);
JButton b = new JButton("Click Me");
[Link](new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
[Link]("Button clicked!");
}
});
[Link](b);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
}
}
3. JComboBox Class (3)
The JComboBox class provides a dropdown list of items that a user can select
from. It is useful for compact selection options.
Syntax:-
JComboBox<String> comboBox = new JComboBox<>(new String[]
{"Item1", "Item2"});
Example:-
import [Link].*;
class JComboBoxExample
{
public static void main(String args[])
{
JFrame f = new JFrame("JComboBox Example");
[Link](300, 200);
String[] items = {"Option 1", "Option 2", "Option 3"};
JComboBox<String> c = new JComboBox<>(items);
[Link](e -> {
[Link]("Selected: " + [Link]());
});
[Link](c);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
}
}
18)List in Java (1)
List is part of the Collection Framework in the [Link] package. It represents
an ordered collection of elements, allowing duplicates and providing index-
based access. It is dynamically resizable, unlike arrays.
Example: (4)
import [Link].*;
class GFG {
public static void main(String[] args)
{
// Creating a List of Strings using ArrayList
List<String> li = new ArrayList<>();
// Adding elements in List
[Link]("Java");
[Link]("Python");
[Link]("DSA");
[Link]("C++");
[Link]("Elements of List are:");
// Iterating through the list
for (String s : li) {
[Link](s);
}
// Accessing elements
[Link]("Element at Index 1: "+ [Link](1));
// Updating elements
[Link](1, "JavaScript");
[Link]("Updated List: " + li);
// Removing elements
[Link]("C++");
[Link]("List After Removing Element: " + li);
}
}
Output
Elements of List are:
Java
Python
DSA
C++
Element at Index 1: Python
Updated List: [Java, JavaScript, DSA, C++]
List After Removing Element: [Java, JavaScript, DSA]
ArrayList (1)
Java ArrayList is a part of the collections framework and it is a class of
[Link] package. It provides us with dynamic-sized arrays in Java.
The main advantage of ArrayList is that, unlike normal arrays, we
don’t need to mention the size when creating ArrayList. It automatically
adjusts its capacity as elements are added or removed.
Example using Arraylist (4)
import [Link].*;
public class ListExample
{
public static void main(String[] args)
{
List<String> students = new ArrayList<>();
[Link]("Pavithra");
[Link]("Saira");
[Link]("Anjali");
[Link]("First Student: " + [Link](0));
[Link]("All Students:");
for (String s : students)
{
[Link](s);
}
[Link]("Saira");
[Link]("Updated List: " + students);
}
}