Java_Aug_Sep2023
PART A:
Short Answer Questions
1. Define Polymorphism.
-Polymorphism is a concept in object-oriented programming that allows objects to be treated as
instances of their parent class rather than their actual class. It enables one interface to be used for a
general class of actions. There are two types:
- Compile-time Polymorphism (Method Overloading): Same method name with different
parameters.
- Run-time Polymorphism (Method Overriding): Same method name in the parent and child class.
2. What are the Java Tokens?
- Java tokens are the smallest elements of a Java program. They are:
- Keywords: Reserved words like `class`, `public`, `static`, etc.
- Identifiers: Names given to variables, classes, methods, etc.
- Literals: Fixed values like `10`, `'a'`, `"Hello"`.
- Operators: Symbols like `+`, `-`, `*`, `/`.
- Separators: Symbols like `;`, `,`, `()`, `{}`, `[]`.
3. How to create objects?
- Objects in Java are created using the `new` keyword. For example:
ClassName obj = new ClassName();
Example:
class Car {
String model;
Car myCar = new Car();
myCar.model = "Tesla";
4. What is Swing in Java?
- Swing is a part of Java Foundation Classes (JFC) used to create window-based applications. It is
built on AWT (Abstract Window Toolkit) and provides more sophisticated components like tables,
lists, scroll panes, buttons, etc.
5. Define Threads.
- A Thread in Java is a lightweight process. It is a separate path of execution and allows multiple
operations to run concurrently. Java provides built-in support for multithreaded programming.
6. What is a package
- A packagein Java is a namespace that organizes a set of related classes and interfaces. It helps in
avoiding name conflicts and controlling access.
package mypack;
public class MyClass {
// class body
PART B:
Four marks Questions
7. Explain features of Java.
- Simple: Easy to learn, syntax similar to C++.
- Object-Oriented: Everything is an object.
- Portable: Write once, run anywhere (WORA).
- Platform-independent: Java bytecode runs on any platform with a JVM.
- Secure: Provides a secure execution environment.
- Robust: Strong memory management, exception handling.
- Multithreaded: Supports multithreading.
- High Performance: Just-In-Time (JIT) compilers.
8. What is a constructor? Explain constructor overloading with an example.
- A constructor is a special method invoked when an object is instantiated. It initializes the object.
- Constructor Overloading: Multiple constructors with different parameter lists.
class Example {
int x;
Example() {
x = 0;
Example(int a) {
x = a;
Example obj1 = new Example();
Example obj2 = new Example(10);
9. What is inheritance? Discuss various types of inheritance.
- Inheritance is a mechanism where one class inherits the properties and behavior of another class.
- Single Inheritance: One subclass inherits from one superclass.
- Multilevel Inheritance: A class is derived from another derived class.
- Hierarchical Inheritance: Multiple subclasses inherit from one superclass.
- Multiple Inheritance (through interfaces in Java): A class implements multiple interfaces.
- Hybrid Inheritance: Combination of two or more types of inheritance.
10. Explain layout manager in Java.
- Layout managers are used to arrange components in a container. Types:
- FlowLayout: Arranges components in a left-to-right flow.
- BorderLayout: Arranges components in five regions: north, south, east, west, center.
- GridLayout: Arranges components in a grid with equal-sized cells.
- CardLayout: Manages two or more components, each as a card.
- GridBagLayout: More flexible and complex than GridLayout.
11. What is exception handling? Explain types of exceptions.
- Exception Handling in Java is a mechanism to handle runtime errors to maintain the normal flow
of the application.
- Checked Exceptions: Compile-time exceptions (e.g., IOException).
- Unchecked Exceptions: Runtime exceptions (e.g., NullPointerException).
- Errors: Serious problems that a reasonable application should not try to catch (e.g.,
OutOfMemoryError)
try {
// code that may throw an exception
} catch (ExceptionType e) {
// code to handle the exception
12. WAP to find the sum of digits of a given number using objects and class.
class SumOfDigits {
int number;
SumOfDigits(int num) {
this.number = num;
int calculateSum() {
int sum = 0, temp = number;
while (temp != 0) {
sum += temp % 10;
temp /= 10;
return sum;
public static void main(String[] args) {
SumOfDigits obj = new SumOfDigits(1234);
System.out.println("Sum of digits: " + obj.calculateSum());
}
PART C:
Eight Mark’s Questions
13. a) Discuss the access specifiers with example.
- Access Specifiers define the scope of class members (variables and methods).
- private: Accessible only within the class.
- default (no modifier): Accessible within the same package.
- protected: Accessible within the same package and subclasses.
- public: Accessible from anywhere.
class Example {
private int a;
public int b;
protected int c;
int d; // default access
b) Explain different types of looping statements.
- For Loop: Repeats a statement or block for a specified number of times.
Example code:
for (int i = 0; i < 10; i++) {
System.out.println(i);
- While Loop: Repeats a statement or block while a condition is true.
Example code:
int i = 0;
while (i < 10) {
System.out.println(i);
i++;
- Do-While Loop: Similar to while loop, but executes at least once.
Example code
int i = 0;
do {
System.out.println(i);
i++;
} while (i < 10);
14. a) Explain process of applet execution.
- Applet is a Java program that runs in a web browser.
- Steps of execution:
1. Load: The applet is loaded by the browser or applet viewer.
2. Initialize: The `init()` method is called for initialization.
3. Start: The `start()` method is called to start the execution.
4. Paint: The `paint()` method is called to draw on the applet window.
5. Stop: The `stop()` method is called to stop the execution.
6. Destroy: The `destroy()` method is called before the applet is destroyed.
Example code:
import java.applet.Applet;
import java.awt.Graphics;
public class MyApplet extends Applet {
public void init() {
// initialization code
}
public void start() {
// start code
public void paint(Graphics g) {
g.drawString("Hello, Applet!", 20, 20);
public void stop() {
// stop code
public void destroy() {
// destroy code
b) What are the features of Swing?
- Lightweight Components: Do not rely on native peer components.
- Pluggable Look and Feel: Can change the appearance of components at runtime.
- Rich Set of GUI Components: More than AWT, including tables, trees, sliders.
- MVC Architecture: Separate the model, view, and controller.
- Double Buffering: Smooth graphics rendering.
15. a) Explain the life cycle of a thread.
- New: Thread is created but not yet started.
- Runnable: Thread is ready to run but waiting for CPU time.
- Running: Thread is executing.
- Blocked/Waiting: Thread is blocked or waiting for a resource.
- Terminated: Thread has finished execution.
Example code:
public class My
Thread extends Thread {
public void run() {
System.out.println("Thread is running");
public static void main(String[] args) {
MyThread t = new MyThread();
t.start(); // Thread moves from New to Runnable state
b) What is an array? Explain one dimension array.
- An array is a collection of elements of the same type stored in contiguous memory locations.
- One-dimensional array: Linear array
int[] arr = new int[5]; // declaration and instantiation
arr[0] = 10; // initialization
System.out.println(arr[0]); // accessing element
16. What is an interface? Explain different kinds of interfaces.
- An interface in Java is a reference type, similar to a class, that can contain only constants,
method signatures, default methods, static methods, and nested types. Interfaces cannot contain
instance fields or constructors.
- Types of interfaces:
- Marker Interface: No methods, used to indicate a particular property of the class (e.g.,
Serializable).
- Functional Interface: An interface with exactly one abstract method (e.g., Runnable,
Comparator).
17. a) Explain the life cycle of an applet.
- Load: Applet class is loaded.
- Initialize: `init()` method is called for initialization.
- Start: `start()` method is called to start execution.
- Paint: `paint()` method is called to perform drawing.
- Stop: `stop()` method is called to stop execution.
- Destroy: `destroy()` method is called before applet is destroyed.
public class MyApplet extends Applet {
public void init() { /* initialization code */ }
public void start() { /* start code */ }
public void paint(Graphics g) { /* drawing code */ }
public void stop() { /* stop code */ }
public void destroy() { /* destroy code */ }
b) Differentiate method overloading and method overriding.
- Method Overloading: Multiple methods with the same name but different parameters
class Example {
void display(int a) { }
void display(String b) { }
- Method Overriding: Subclass provides a specific implementation of a method already defined in
its superclass.
class Parent {
void show() { }
class Child extends Parent {
@Override
void show() { }
18. a) Write a program to implement arithmetic operations using inheritance.
class Arithmetic {
int a = 10;
int b = 5;
class Addition extends Arithmetic {
int add() {
return a + b;
class Subtraction extends Arithmetic {
int subtract() {
return a - b;
public class Main {
public static void main(String[] args) {
Addition add = new Addition();
Subtraction sub = new Subtraction();
System.out.println("Addition: " + add.add());
System.out.println("Subtraction: " + sub.subtract());
b) What are the advantages and disadvantages of Java Beans?
- Advantages:
- Reusability: Beans can be reused in different environments.
- Customization: Beans can be customized in design-time environments.
- Ease of use: Can be manipulated visually.
- Compatibility: Follows a set of conventions for property, event, and method naming.
- Disadvantages:
- Complexity: Can be complex to set up for beginners.
- Performance: May be slower due to the overhead of the bean framework.
- Size: Larger codebase for the same functionality.