SolutionJava_Aug_Sep2023
SolutionJava_Aug_Sep2023
PART A:
1. Define Polymorphism.
- Run-time Polymorphism (Method Overriding): Same method name in the parent and child class.
- Java tokens are the smallest elements of a Java program. They are:
- Objects in Java are created using the `new` keyword. For example:
Example:
class Car {
String model;
myCar.model = "Tesla";
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;
// class body
PART B:
- A constructor is a special method invoked when an object is instantiated. It initializes the object.
class Example {
int x;
Example() {
x = 0;
Example(int a) {
x = a;
- Inheritance is a mechanism where one class inherits the properties and behavior of another class.
- BorderLayout: Arranges components in five regions: north, south, east, west, center.
- Errors: Serious problems that a reasonable application should not try to catch (e.g.,
OutOfMemoryError)
try {
} catch (ExceptionType e) {
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() {
while (temp != 0) {
temp /= 10;
return sum;
PART C:
- Access Specifiers define the scope of class members (variables and methods).
class Example {
private int a;
public int b;
protected int c;
Example code:
System.out.println(i);
Example code:
int i = 0;
i++;
Example code
int i = 0;
do {
System.out.println(i);
i++;
- Steps of execution:
Example code:
import java.applet.Applet;
import java.awt.Graphics;
// initialization code
}
public void start() {
// start code
// stop code
// destroy code
- Pluggable Look and Feel: Can change the appearance of components at runtime.
- Rich Set of GUI Components: More than AWT, including tables, trees, sliders.
Example code:
public class My
System.out.println("Thread is running");
- An array is a collection of elements of the same type stored in contiguous memory locations.
- 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).
- Method Overloading: Multiple methods with the same name but different parameters
class Example {
void display(int a) { }
void display(String b) { }
class Parent {
void show() { }
@Override
void show() { }
int a = 10;
int b = 5;
int add() {
return a + b;
int subtract() {
return a - b;
- Advantages:
- Disadvantages: