Previous Practical Exam Questions
FIFTH SEMESTER BCA PROGRAMME (CBCSS) PRACTICAL EXAMINATION MARCH
2021 Software Lab V
Max Marks: 80 (Q1: 25 marks, Q2: 35 marks, Record: 10 marks, Viva: 10 Marks)
SET 1
1. Write an applet program to display a traffic light.
2. Create a class called Matrix which contains a 2d integer array. Include the following member
functions
a. To read the matrix,
b. To display the matrix ,
c. check whether the given matrix is symmetric or not. (Solution @ end of the page)
SET 2
1. Write a swing program to accept a value in a textbox then find the area of a circle and
display the result in the second textbox?
2. Write multithreaded program to print lowercase letters and uppercase letters from two
different threads with suitable delay.
SET 3
1. Write an applet program to show how to pass a parameter from an applet code?
2. Write a Java Program to calculate the Result. Result should consist of name, seatno,
date, center number and marks of semester three exam. Create a User Defined
Exception class MarksOutOfBoundsException, If Entered marks of any subject is
greater than 100 or less than 0, and then program should create a user defined
Exception of type.
SET4
1. Write an applet program to draw a house
2. Create a class called Matrix which contains a 2d integer array, m & n (order of matrix)
as data members. Include the following member functions
a. To read the matrix,
b. To display the matrix ,
c. Overload a method product () to find the product of two matrices and to
multiply each element of a matrix with a constant value
SET5
1. Write a swing program to accept an integer in a textbox then reverse that number and
display the result in the second textbox?
2 Write a Java program which creates a class named 'Employee' having the following
members: Name, Age, Phone number, Address, Salary. It also has a method named
'printSalary( )' which prints the salary of the Employee. Two classes 'Officer' and
'Manager' inherits the 'Employee' class. The 'Officer' and 'Manager' classes have
data members 'specialization' and 'department' respectively. Now, assign name, age,
phone number, address and salary to an officer and a manager by making an object of
both of these classes and print the same.
SET6
SET7
SET8
SET9
SET10
SET
SET SET
SET
SET
SET
SET 1 SOLUTION
write java program to Create a class called Matrix which contains a 2d integer array. Include the following
member functions
a. To read the matrix,
b. To display the matrix ,
c. check whether the given matrix is symmetric or not.
Here is the Java program implementing the Matrix class with the required functionalities:
import java.util.Scanner;
public class Matrix {
private int[][] matrix;
private int rows;
private int cols;
// Constructor to initialize the matrix dimensions
public Matrix(int rows, int cols) {
this.rows = rows;
this.cols = cols;
this.matrix = new int[rows][cols];
// Function to read the matrix
public void readMatrix() {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the elements of the matrix:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print("Element [" + i + "][" + j + "]: ");
matrix[i][j] = scanner.nextInt();
// Function to display the matrix
public void displayMatrix() {
System.out.println("The matrix is:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print(matrix[i][j] + " ");
System.out.println();
// Function to check if the matrix is symmetric
public boolean isSymmetric() {
if (rows != cols) {
// A non-square matrix cannot be symmetric
return false;
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (matrix[i][j] != matrix[j][i]) {
return false;
return true;
// Main method to test the Matrix class
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input matrix dimensions
System.out.print("Enter the number of rows: ");
int rows = scanner.nextInt();
System.out.print("Enter the number of columns: ");
int cols = scanner.nextInt();
// Create a Matrix object
Matrix mat = new Matrix(rows, cols);
// Read, display, and check symmetry
mat.readMatrix();
mat.displayMatrix();
if (mat.isSymmetric()) {
System.out.println("The matrix is symmetric.");
} else {
System.out.println("The matrix is not symmetric.");
scanner.close();
Explanation of the Code:
1. Class Members:
o matrix: 2D integer array to store the matrix elements.
o rows and cols: Dimensions of the matrix.
2. Constructor:
o Initializes the matrix dimensions and allocates memory for the 2D array.
3. Methods:
o readMatrix(): Takes user input for the matrix elements.
o displayMatrix(): Prints the matrix in a structured format.
o isSymmetric(): Checks if the matrix is square and whether matrix[i][j] equals matrix[j][i].
4. Main Method:
o Reads matrix dimensions, creates a Matrix object, and invokes the methods to read,
display, and check symmetry.
Sample Output:
Enter the number of rows: 3
Enter the number of columns: 3
Enter the elements of the matrix:
Element [0][0]: 1
Element [0][1]: 2
Element [0][2]: 3
Element [1][0]: 2
Element [1][1]: 4
Element [1][2]: 5
Element [2][0]: 3
Element [2][1]: 5
Element [2][2]: 6
The matrix is:
123
245
356
The matrix is symmetric.
which are the similar functions used like .isSymmetric() in 2 dimensional arrays
In 2D arrays, functions similar to .isSymmetric() often involve analyzing or transforming the matrix based
on its structure, properties, or elements. Here are some examples of similar utility functions and their
purposes:
1. isSquare()
Purpose: Check if the given matrix is a square matrix (i.e., rows = columns).
public boolean isSquare() {
return rows == cols;
}
2. isDiagonal()
Purpose: Check if the matrix is a diagonal matrix (non-diagonal elements are zero).
public boolean isDiagonal() {
if (!isSquare()) return false; // Only square matrices can be diagonal.
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (i != j && matrix[i][j] != 0) {
return false;
return true;
3. isIdentity()
Purpose: Check if the matrix is an identity matrix (diagonal elements are 1, others are 0).
public boolean isIdentity() {
if (!isSquare()) return false;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (i == j && matrix[i][j] != 1) return false;
if (i != j && matrix[i][j] != 0) return false;
return true;
4. isUpperTriangular()
Purpose: Check if the matrix is upper triangular (all elements below the main diagonal are zero).
public boolean isUpperTriangular() {
if (!isSquare()) return false;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < i; j++) { // Below the main diagonal
if (matrix[i][j] != 0) {
return false;
return true;
5. isLowerTriangular()
Purpose: Check if the matrix is lower triangular (all elements above the main diagonal are zero).
public boolean isLowerTriangular() {
if (!isSquare()) return false;
for (int i = 0; i < rows; i++) {
for (int j = i + 1; j < cols; j++) { // Above the main diagonal
if (matrix[i][j] != 0) {
return false;
return true;
6. transpose()
Purpose: Create the transpose of a matrix (interchange rows and columns).
public Matrix transpose() {
Matrix transposed = new Matrix(cols, rows);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
transposed.matrix[j][i] = this.matrix[i][j];
return transposed;
7. isSkewSymmetric()
Purpose: Check if the matrix is skew-symmetric (transpose equals the negative of the matrix).
public boolean isSkewSymmetric() {
if (!isSquare()) return false;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (matrix[i][j] != -matrix[j][i]) {
return false;
return true;
8. isEqual(Matrix other)
Purpose: Check if two matrices are equal.
public boolean isEqual(Matrix other) {
if (this.rows != other.rows || this.cols != other.cols) return false;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (this.matrix[i][j] != other.matrix[i][j]) {
return false;
}
return true;
9. getTrace()
Purpose: Calculate the trace of a square matrix (sum of diagonal elements).
public int getTrace() {
if (!isSquare()) throw new IllegalArgumentException("Matrix is not square!");
int trace = 0;
for (int i = 0; i < rows; i++) {
trace += matrix[i][i];
return trace;
10. isZeroMatrix()
Purpose: Check if all elements in the matrix are zero.
public boolean isZeroMatrix() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (matrix[i][j] != 0) {
return false;
}
return true;
These utility methods can help analyze and manipulate 2D matrices in various ways, allowing for greater
flexibility in working with mathematical and logical operations in matrix processing.