java model paper 4 BCA
java model paper 4 BCA
Model paper 4
2 marks
Ans:
3. Platform Independent – Java programs can run on any computer using Java.
4. Secure – Java has built-in safety features to protect data and programs.
Ans:
Polymorphism means one thing behaving in different ways. In Java, it allows the same
method to do different tasks.
Types of Polymorphism:
3. What are instance variables? How are they different from class variables?
Ans:
Instance variables are variables that belong to an object. Each object has its own copy
of instance variables.
Class variables are variables that belong to the class. They are shared by all objects of
that class. Class variables are declared using the static keyword.
Difference:
Ans:
This keyword is used to refer to the current object of the class. It helps to access
instance variables or methods of the current object.
Super keyword is used to refer to the parent (super) class. It is used to call the parent
class’s constructor or methods.
Ans:
Run-time polymorphism happens when the method to be called is decided while the
program is running. It is also called method overriding.
Ans:
A class is a blueprint to create objects. It can have both variables and method
definitions (with code).
An interface is like a contract. It only has method declarations (no code) and constants.
Classes use implements to use an interface.
Main Difference:
An interface only shows what methods a class must have (no code inside methods).
Ans:
Serialization is the process of converting an object into a file or byte stream so it can be
saved or sent over a network.
Deserialization is the process of converting the saved data (byte stream) back into an
object.
Ans:
An Event is an action done by the user, like clicking a button, typing a key, or moving the
mouse.
Event Handling is the process of responding to those actions. In Java, it means writing
code that runs when an event happens.
Ans:
A Label in AWT is a text display used to show a short message or description. It cannot
be clicked.
A Button in AWT is a clickable component that performs an action when the user clicks
on it.
Both are part of the AWT (Abstract Window Toolkit) used for creating GUI (Graphical
User Interface) in Java.
Ans:
Thread synchronization is a way to make sure that only one thread can access shared
data at a time. It helps avoid conflicts and keeps data safe.
6 marks
Ans:
In Java, the scope of a variable means where the variable can be used or accessed in
the program. There are four main types of variable scopes:
1. Local Variables:
They are created when the method starts and destroyed when it ends.
Example:
Void show() {
System.out.println(a);
2. Instance Variables:
These variables are declared inside a class but outside any method.
Each object of the class gets its own copy of instance variables.
They are created when the object is created and destroyed when the object is
destroyed.
Example:
Class Student {
These variables are declared using the static keyword inside a class.
Example:
Class Student {
4. Parameter Variables:
Example:
System.out.println(name);
Conclusion:
Understanding variable scope helps to use variables correctly and avoid errors in Java
programs. It controls where a variable is visible, how long it lives, and who can access it.
Ans:
In Java, a labelled loop is a loop that has a name (label) before it. Labels are used to
control nested loops by allowing the program to break out of or continue specific loops
using break or continue with the label name.
This is especially useful when you have loops inside loops (nested loops), and you want
to exit or skip the outer loop based on a condition inside the inner loop.
With labelled break, you can stop the outer loop directly.
With labelled continue, you can skip to the next iteration of the outer loop.
labelName:
// code
Break labelName;
outerLoop:
if (j == 2) {
System.out.println(“i = “ + i + “, j = “ + j);
Output:
I = 1, j = 1
Explanation:
outerLoop:
if (j == 2) {
System.out.println(“i = “ + i + “, j = “ + j);
Output:
I = 1, j = 1
I = 2, j = 1
I = 3, j = 1
Explanation:
When j == 2, continue outerLoop; skips the rest of the inner loop and moves to the next i
value in the outer loop.
Conclusion:
Labelled loops in Java give more control over nested loops. They help in writing clean
and readable code when you need to break or continue from outer loops based on inner
loop conditions.
Ans: In Java, wrapper classes are used to convert primitive data types (like int, char,
float, etc.) into objects. This is useful when we want to store primitive values in
collections like ArrayList, which only work with objects.
Int Integer
Char Character
Float Float
Double Double
Boolean Boolean
This method is not recommended now because it is slow and deprecated in newer
versions of Java.
Java can also automatically convert a wrapper object back into a primitive type.
Example:
Public class WrapperExample {
// Using valueOf()
Integer a = Integer.valueOf(100);
// Autoboxing
Double b = 10.5;
// Unboxing
Conclusion:
The most common and best way to create wrapper objects is by using valueOf() or
autoboxing. These are used in collections, method calls, and more.
14. What is Event Delegation Model? How does Event Delegation Model work in Java?
Ans:
The Event Delegation Model in Java is a way to handle events (like button clicks, key
presses, mouse movements) in GUI (Graphical User Interface) programs.
In this model, event handling is separated from the GUI components. When an event
happens, it is sent (delegated) to an object that is responsible for handling it.
Main Idea:
The event is passed to the listener, not handled directly by the source.
Event Source The object where the event occurs (e.g., button, text field).
Event Object Contains information about the event (like which button was clicked).
Import java.awt.*;
Import java.awt.event.*;
f.add(b);
f.setSize(300, 300);
f.setLayout(null);
f.setVisible(true);
b.addActionListener(new ActionListener() {
});
}
}
Explanation of Example:
When the button is clicked, the message “Button was clicked!” is printed.
Conclusion:
The Event Delegation Model is the standard way of handling events in Java. It separates
the event-handling code from the user interface, making programs modular, readable,
and easier to manage.
15. Explain the steps involved in developing and executing an applet using HTML.
Ans:
An applet is a small Java program that runs inside a web browser. Applets are used to
create interactive web applications with graphics and user interface components.
Example:
Import java.applet.Applet;
Import java.awt.Graphics;
Javac MyApplet.java
Create an HTML file to load and run the applet in a browser or applet viewer.
Example:
<html>
<head><title>My Applet</title></head>
<body>
</applet>
</body>
</html>
Save it as MyApplet.html.
➢ Note: The <applet> tag is deprecated in modern browsers, so it works only with
applet viewers like the appletviewer tool.
Appletviewer MyApplet.html
This will open a window and display the output of the applet.
Conclusion:
Applets are useful for making small Java-based applications inside web pages, though
they are now outdated in modern web browsers.
Ans:
In Java, a thread is a lightweight process that allows a program to perform multiple tasks
at the same time (called multithreading).
1. User Threads
The program keeps running as long as at least one user thread is active.
Example:
t.start();
System.out.println(“Main Thread is running...”);
Output:
2. Daemon Threads
Example:
If (Thread.currentThread().isDaemon())
Else
T1.start();
T2.start();
}
Output:
Conclusion:
Both are useful for running tasks simultaneously and improving the performance of Java
applications.
8marks
(b) What is the conditional (ternary) operator (‘?:’)? Explain with a program
It is a part of JRE.
Summary:
(b) Conditional (Ternary) Operator (?:): The ternary operator is a short form of if-else
statement. It is used to decide a value based on a condition.
Example Program:
Output:
(b) Write a program to find the largest of three numbers using nested if else.
The if-else statement is used to run one block of code if a condition is true, and another
block if it is false.
Syntax:
If (condition) {
} else {
Example:
If (number > 0) {
System.out.println(“Positive number”);
} else {
(b) Program to Find the Largest of Three Numbers using Nested if-else:
Import java.util.Scanner;
Int c = input.nextInt();
Int largest;
If (a > b) {
If (a > c) {
Largest = a;
} else {
Largest = c;
} else {
If (b > c) {
Largest = b;
} else {
Largest = c;
}
19 (a) Hiw to declare, initialize and access multiple dimensional arrays? With an
example.
(b) What are access control modifiers? Explain the role of each access modifer with an
example.
(a) A multidimensional array is an array of arrays. The most common is the 2D array (like
a matrix).
Declaration:
Int[][] arr;
Initialization:
Assigning values:
Arr[0][0] = 10;
Arr[0][1] = 20;
Arr[1][2] = 30;
Accessing values:
System.out.println(arr[1][2]); // prints 30
Complete Example:
Int[][] arr = {
{1, 2, 3},
{4, 5, 6}
};
System.out.print(arr[i][j] + “ “);
} System.out.println();
}
}
(b) Access modifiers control the visibility of classes, variables, and methods in Java.
1. private
3. Protected
Accessible within the same package and in subclasses (even in other packages).
4. Public
Example:
Private int a = 1;
Int b = 2; // default
Public int d = 4;
System.out.println(a + “, “ + b + “, “ + c + “, “ + d);
These modifiers help with data hiding, security, and code organization in Java.
20 (a) What are different types of packages in java? Explain with an example.
These packages are provided by Java and are available for use. Examples include:
2. User-defined Packages
These are packages created by the user to organize their own classes and interfaces.
Package mypackage;
Import mypackage.MyClass;
Obj.display();
(b)
The java.awt (Abstract Window Toolkit) package is a part of Java’s standard library used
for creating Graphical User Interfaces (GUIs).
Key Points:
It provides components like buttons, labels, text fields, windows, and more.
Replaced in many cases by Swing (javax.swing), which is more flexible and platform-
independent, but java.awt is still used especially for base classes like Graphics.
Class Description
Public AWTExample() {
f.add(l);
f.add(t);
f.add(b);
f.setSize(300, 200);
f.setLayout(null);
f.setVisible(true);
New AWTExample();
(b) What are Mouse Events? Explain the MouseListener and MouseMotionListener
Interface Methods.
ObjectOutputStream and ObjectInputStream are used to write and read objects to and
from a file or stream.
ObjectOutputStream:
Out.writeObject(object);
ObjectInputStream:
Mouse Events occur when the user interacts with the mouse (clicks, presses, moves,
releases, etc.).
1. MouseListener
Methods:
mouseClicked(MouseEvent e)
mousePressed(MouseEvent e)
mouseReleased(MouseEvent e)
mouseEntered(MouseEvent e)
mouseExited(MouseEvent e)
2.MouseMotionListener
Methods:
mouseMoved(MouseEvent e)
mouseDragged(MouseEvent e)
Example:
addMouseListener(new MouseAdapter() {
});
addMouseMotionListener(new MouseMotionAdapter() {
System.out.println(“Mouse Moved”);
});
These interfaces are part of java.awt.event package and are useful for interactive GUI
programs.
22 (a) What is custom (user defined) exception? How do you create & use in one Java?
(b) Write the steps involved in creating a thread by implementing runnable interface.
(a) A custom exception is a user-defined class that extends Java’s Exception class. It is
used when we want to create our own error types to handle specific problems in a
program.
Example:
Super(message);
} catch (MyException e) {
Example:
System.out.println(“Thread is running...”);
t.start(); // Step 5
}
}
Using Runnable is preferred when your class needs to extend another class (since Java
doesn’t support multiple inheritance with classes).
(a) The Collection Framework in Java provides a standard way to handle groups of
objects. It includes interfaces, classes, and algorithms.
Hierarchy Overview:
Iterable
Collection
Vector TreeSet
Key Interfaces:
(b) Generic Programming in Java allows writing code that works with any data type using
a placeholder (like <T>). It increases code reusability and type safety.
Features of Generic Programming:
1. Type Safety
2. Code Reusability
Class Box<T> {
T value;
3. No Type Casting
System.out.println(item);