Java Solved Question Bank
Java Solved Question Bank
9. Compare the packages and interfaces giving out any four similarities.
Both packages and interfaces are used for organization and structure, can
contain multiple elements, support access modifiers, and enhance code
reusability.
22. Determine the set of methods which define the lifecycle of a Servlet.
The servlet lifecycle methods are init(), service(), and destroy().
23. Recognize the uses of = = and = Operators.
The == operator checks for equality, while the = operator is used for
assignment.
29. Employ the technique of using layout managers, & menu in a program.
Layout managers organize UI components, while menus provide user options.
30. Interpret the methodology of applying Working with graphics, Working with
color & fonts in a program.
Working with graphics in Java involves drawing shapes and displaying text
using graphics classes.
33. Express the reasons for using Event handling & Adapter Class; Swing Classes.
Event handling responds to user interactions, while Swing provides GUI
components.
38. Recall the role of a Servlet Container providing its other name.
The servlet container manages servlet lifecycle and is also known as Web
Container.
SECTION-B
1. Elaborate on the data types with illustrations.
Java is a statically-typed language, meaning that the data type of a
variable is known at compile time. Java has two main categories of data
types:
a. Primitive Data Types: These are the basic data types in Java.
● byte (1 byte)
● short (2 bytes)
● int (4 bytes)
● long (8 bytes)
● float (4 bytes)
● double (8 bytes)
● char (2 bytes)
● boolean (1 byte)
Example:
int x = 10;
double y = 10.5;
char z = 'A';
b. Reference Data Types: These are data types that refer to
objects.
● Classes (e.g., String, ArrayList)
● Interfaces
● Arrays
Example:
String name = "John";
int[] scores = {90. 80, 70, 60};
Person(String name) {
this.name = name; // accessing class variable
}
}
b. Calling other constructors: this keyword can be used to call other
constructors in the same class.
class Person {
String name;
Person() {
this("John"); // calling another constructor
}
Person(String name) {
this.name = name;
}
}
c. Passing the current object as an argument: this keyword can be
used to pass the current object as an argument to another method.
class Person {
void printPerson() {
System.out.println(this); // passing the current object
}
}
18. Examine the various types of packages bringing out their pros and
cons.
Packages in Java are used to organize related classes and
interfaces.
Types:
1. Java API Packages: Pre-defined packages that provide various
functionalities, such as java.lang and java.util.
2. User-Defined Packages: Custom packages created by
developers to organize their code.
3. Third-Party Packages: Packages developed by third-party
vendors or open-source communities.
Pros and Cons:
● Java API Packages: Provide a wide range of functionalities, but
may not cover all specific needs.
● User-Defined Packages: Allow for custom organization and
structure, but require more effort to maintain.
● Third-Party Packages: Provide specialized functionalities, but
may have compatibility issues or require additional dependencies.
22. Bring out any three similarities and differences between adapter
classes and swing classes.
Adapter classes and Swing classes are both used in Java
programming, but they serve different purposes.
Similarities:
1. Both are used in Java programming: Both adapter classes and
Swing classes are used to build Java applications.
2. Both provide a way to handle events: Adapter classes are used
to handle events, and Swing classes provide a way to handle events through
their listeners.
3. Both are part of the Java API: Both adapter classes and Swing
classes are part of the Java API, making it easy to use them in Java applications.
Differences:
1. Purpose: Adapter classes are used to provide a default
implementation of an interface, while Swing classes are used to build graphical
user interfaces.
2. Functionality: Adapter classes provide a way to handle events,
while Swing classes provide a wide range of GUI components and functionality.
3. Usage: Adapter classes are typically used in event handling,
while Swing classes are used to build GUI applications
26. Interpret the various types of states in the life cycle of servlets with
examples.
The life cycle of a servlet refers to the stages it goes through
from its creation to its destruction.
States:
1. Loading: The servlet container loads the servlet class.
2. Instantiation: The servlet container creates an instance of the
servlet.
3. Initialization: The servlet container initializes the servlet by
calling the init() method.
4. Service: The servlet handles HTTP requests and responses.
5. Destruction: The servlet container destroys the servlet by calling
the destroy() method.
27. Examine the Class and Objects bringing out their similarities,
differences, pros and cons.
Classes and objects are fundamental concepts in object-
oriented programming.
Similarities:
1. Both are used in OOP: Both classes and objects are used in
object-oriented programming.
2. Both represent real-world entities: Both classes and objects can
represent real-world entities, such as people, places, and things.
3. Both have properties and behavior: Both classes and objects can
have properties and behavior.
Differences:
1. Class: A class is a blueprint or template that defines the
properties and behavior of an object.
2. Object: An object is an instance of a class, and has its own set of
attributes and methods.
3. Instantiation: A class can be instantiated to create multiple
objects.
Pros and Cons:
1. Classes: Classes provide a way to define a blueprint for objects,
making it easy to create multiple objects with similar properties and behavior.
2. Objects: Objects provide a way to represent real-world entities
and their behavior, making it easy to model complex systems.
SECTION-C