0% found this document useful (0 votes)
60 views4 pages

Important Questions CC109 - BIC1224

The document discusses generalization and specialization hierarchies in object-oriented programming, defines the five steps to build a JDBC application, shows how to catch an OutOfMemoryError exception in Java, explains the concepts of classes, objects, and methods in Java, and describes how a Java program executes when a user enters a number.

Uploaded by

Pramit Neupane
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views4 pages

Important Questions CC109 - BIC1224

The document discusses generalization and specialization hierarchies in object-oriented programming, defines the five steps to build a JDBC application, shows how to catch an OutOfMemoryError exception in Java, explains the concepts of classes, objects, and methods in Java, and describes how a Java program executes when a user enters a number.

Uploaded by

Pramit Neupane
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

CC109/BIC1224

● Define the difference between generalization and specialization hierarchy?


ANS: In software engineering and object-oriented programming, generalization
and specialization hierarchies are two important concepts used to organize and
model classes or objects.
Generalization hierarchy refers to the process of creating a more general or
abstract class from two or more classes that have common features. It involves
identifying the similarities between the classes and creating a new superclass or
parent class to represent those common features. This new class can be used as
a template for creating more specialized subclasses, each of which inherits the
attributes and behavior of the parent class and adds its own unique features.

Specialization hierarchy, on the other hand, refers to the process of creating a


more specific subclass from a general or abstract parent class. It involves adding
more attributes and behaviors to the existing parent class to create a new class
that is more specialized or specific. The new subclass inherits all the attributes
and behavior of its parent class, but also adds its own unique features.

● List the required FIVE steps for building the JDBC application and explain each step.
ANS:
The following are the five steps involved in building a JDBC application:
i. Load the JDBC driver: The first step in building a JDBC application is to load the
appropriate JDBC driver that corresponds to the database management system
(DBMS) being used. The driver can be loaded by calling the Class.forName()
method and passing the fully qualified name of the driver class as a string
parameter. This step is necessary to establish a connection between the Java
program and the database server.
ii. Establish a connection to the database: After loading the driver, the next step is to
establish a connection to the database by calling the
DriverManager.getConnection() method and passing the database URL, username,
and password as parameters. The database URL is a string that identifies the
location of the database server, including the protocol, host name, port number,
and database name.
iii. Create a statement object: Once a connection is established, a Statement object
needs to be created to execute SQL statements against the database. This can be
done by calling the Connection.createStatement() method. The Statement object
can be used to execute queries, updates, and deletes on the database.
iv. Execute the SQL statement: After creating a Statement object, the next step is to
execute the SQL statement using one of the Statement object's execute()
methods. The execute() method can be used to execute any SQL statement,
including SELECT, INSERT, UPDATE, and DELETE.
v. Process the result set: If the SQL statement is a SELECT statement, the
executeQuery() method should be used instead of execute(). The executeQuery()
method returns a ResultSet object that contains the results of the query. The
ResultSet object can be processed using methods like next(), getString(), and
getInt() to retrieve data from the result set.

● ( OutOfMemoryError) Write a program that causes the JVM to throw an


OutOfMemoryError and catches and handles this error.

ANS: public class OutOfMemoryExample {

public static void main(String[] args) {

try {

int[] arr = new int[Integer.MAX_VALUE];

} catch (OutOfMemoryError e) {

System.out.println("Caught OutOfMemoryError!");

● Introduction to Classes, Objects , Method


ANS:
i. A class is a blueprint or template for creating objects. It defines the
attributes (data) and behavior (methods) that an object of that class can
have. For example, a class representing a car might have attributes such as
color, make, model, and year, as well as methods such as accelerate,
brake, and turn.
ii. An object is an instance of a class. It's created from the class
blueprint and has its own unique set of attributes and behavior. For
example, you could create multiple car objects from the car class,
each with its own color, make, model, year, and behavior.
iii. A method is a block of code that defines the behavior of an object or
class. It can be called to perform a specific action or set of actions.
Methods can take input parameters, perform operations on those
parameters, and return a value. For example, a car object's
accelerate method might take a speed parameter, increase the car's
velocity, and return the new speed.

● Explain the usage of GUI in Java.


ANS: The usage of GUI in Java has several benefits, including:
i. Improved User Experience: GUIs make it easier for users to interact with
a program by providing a visually appealing and intuitive interface.
ii. Increased Interactivity: GUIs allow users to interact with a program in
real-time, enabling them to quickly perform tasks and get feedback.
iii. Portability: Java's GUI libraries are designed to be platform-
independent, meaning that programs built with Java can run on any
operating system that supports Java.
iv. Customizability: Java's GUI libraries provide developers with a wide
range of tools for customizing the appearance and behavior of GUI
components, enabling them to create unique and tailored user
interfaces.

● Based on the java code above describe how the program executes when a user enters a
number?
ANS:
import java.util.Scanner;
public class UserInputExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
System.out.println("You entered: " + number);
scanner.close();
}
}
● Write a program for calculating the equation of Y= (X1+X2)/D where the user can enter
the
values of X1, X2, and D after execution and the program can detect the mismatched
values
and the wrong value for variable D.
ANS:
import java.util.InputMismatchException;
import java.util.Scanner;

public class EquationCalculator {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

try {
// Read input values from user
System.out.print("Enter value for X1: ");
double x1 = scanner.nextDouble();

System.out.print("Enter value for X2: ");


double x2 = scanner.nextDouble();

System.out.print("Enter value for D: ");


double d = scanner.nextDouble();
// Calculate equation result
double y = (x1 + x2) / d;

// Display result
System.out.println("The result of the equation is: " + y);

} catch (InputMismatchException e) {
// Handle mismatched input values
System.out.println("Invalid input! Please enter numbers only.");

} catch (ArithmeticException e) {
// Handle division by zero error
System.out.println("Invalid input! The value of D cannot be zero.");

} finally {
// Close the scanner to prevent resource leak
scanner.close();
}
}
}
● Given the UML diagram shown in Figure 1, write complete JAVA program to create the
class. (7 marks)

Rectangle

- height: double
- width: double
+Rectangle (height:double, width: double)
+getHeight ( ): double
+getWidth ( ): double
+calcArea ( ): void

Figure 1 : UML Diagram

● Differences public variables, protected variables, and private


variables.

You might also like