Q1.a.
Discuss different datatypes supported by java along with the default values and literals (8
Marks)
Java data types specify the type of data a variable can store. Java supports two main
categories of data types:
1. Primitive Data Types
Primitive data types are basic built-in types that store simple values. Java supports 8 primitive
data types.
Data Type Size Default Value Literal Example
byte 1 byte 0 byte b = 10;
short 2 bytes 0 short s = 200;
int 4 bytes 0 int i = 1000;
long 8 bytes 0L long l = 100000L;
sfloat 4 bytes 0.0f float f = 10.5f;
double 8 bytes 0.0d double d = 20.75;
char 2 bytes '\u0000' char c = 'A';
boolean 1 bit false boolean flag = true;
2. Non-Primitive (Reference) Data Types
Non-primitive data types store references to objects. Their default value is null.
Data Type Default Value Literal / Example
String null "Hello"
Arrays null {1, 2, 3}
Class null new ClassName()
Interface null Implemented by class
Literals in Java
Literals are fixed values assigned directly to variables.
Types of literals:
Integer literals → 10, 0x1A, 077
Floating-point literals → 10.5, 3.14f
Character literals → 'A', '\n'
String literals → "Java"
Boolean literals → true, false
Null literal → null
Q1.b. Develop a java program to convert temperature in Celsius to farenheit (6 Marks)
import [Link];
class CelsiusToFahrenheit
{
public static void main(String args[])
{
float celsius, fahrenheit;
Scanner sc = new Scanner([Link]);
[Link]("Enter temperature in Celsius: ");
celsius = [Link]();
fahrenheit = (celsius * 9 / 5) + 32;
[Link]("Temperature in Fahrenheit = " + fahrenheit);
}
}
Sample Output
Enter temperature in Celsius: 25
Temperature in Fahrenheit = 77.0
Q1.c. Justify the statement compile once and run anywhere in java (6 Marks)
Java follows the principle “Compile Once and Run Anywhere (CORA)”, which means a Java
program compiled on one platform can run on any other platform without modification. This
allows the following features.
1. Java Compilation
o Java source code (.java) is compiled by the Java Compiler (javac).
o The compiler converts source code into bytecode (.class), not machine-
specific code.
2. Platform-Independent Bytecode
o Bytecode is platform independent.
o It is the same for all operating systems like Windows, Linux, and macOS.
3. Role of Java Virtual Machine (JVM)
o Every platform has its own JVM.
o JVM converts bytecode into platform-specific machine code at runtime.
4. No Need for Recompilation
o Since bytecode remains unchanged, the same .class file can be executed on
different platforms.
o Only the JVM needs to be installed on the target system.
5. Security and Portability
o JVM performs bytecode verification, ensuring safe execution.
o This enhances portability and security across platforms.
Q2.a. List the various operators supported in java. Illustrate the working of >>> and >>
with example (8 Marks)
Operators Supported in Java
Java operators are special symbols used to perform operations on variables and values. They
are classified into the following types:
1 Arithmetic Operators +,-,*,/,%
2 Relational (Comparison) Operators < , > , <= , >= , == , != Right Shift
Operator
3 Logical Operators && , || , ! (>>)
4 Assignment Operators = , += , -= , *= , /= , %= >> is
5 Unary Operators + , - , ++ , -- , ! called
Signed Right
6 Bitwise Operators &,|,^,~ Shift
Operator.
7 Shift Operators << , >> , >>>
It shifts
8 Conditional (Ternary) Operator ?:
bits to the
right
preserving the sign bit (MSB).
Used for dividing a number by powers of 2.
Example:
int a = 8; // Binary: 00001000 int b = a >> 2;
Working: 00001000 >> 2 = 00000010
Output: b = 2
For negative numbers, the leftmost bit (sign bit) is filled with 1.
Unsigned Right Shift Operator (>>>)
>>> is called Unsigned Right Shift Operator.
It shifts bits to the right filling zeros in the leftmost bits, regardless of sign.
Does not preserve sign.
Example:
int a = -8; int b = a >>> 2;
Working:
-8 in binary (32-bit): 11111111 11111111 11111111 11111000
After >>> 2 : 00111111 11111111 11111111 11111110
Output: b = 1073741822
Q2.b. Develop a java program to add two matrices using command line arguments (10
Marks)
class MatrixAddition
{
public static void main(String args[])
{
int r, c, k = 2;
// Read number of rows and columns
r = [Link](args[0]);
c = [Link](args[1]);
int a[][] = new int[r][c];
int b[][] = new int[r][c];
int sum[][] = new int[r][c];
// Read elements of first matrix
for(int i = 0; i < r; i++)
{
for(int j = 0; j < c; j++)
{
a[i][j] = [Link](args[k++]);
}
}
// Read elements of second matrix
for(int i = 0; i < r; i++)
{
for(int j = 0; j < c; j++)
{
b[i][j] = [Link](args[k++]);
}
}
// Add matrices
for(int i = 0; i < r; i++)
{
for(int j = 0; j < c; j++)
{
sum[i][j] = a[i][j] + b[i][j];
}
}
// Display result
[Link]("Sum of matrices:");
for(int i = 0; i < r; i++)
{
for(int j = 0; j < c; j++)
{
[Link](sum[i][j] + " ");
}
[Link]();
}
}
}
Output
Sum of matrices:
68
10 12
Q2.c. Explain the syntax of declaration of 2D arrays in java (2 Marks)
A 2-D array in Java is declared as an array of arrays.
General Syntax:
dataType[][] arrayName;
Example:
int[][] a;
The array can also be declared and initialized using:
int a[][] = new int[3][4]; Here, 3 represents the number of rows and 4 represents the number
of columns.
Q3.a. Examine java garbage collection mechanism by classifying the three generations
of java heap. (6 Marks)
Java uses automatic garbage collection (GC) to manage memory. The Java Heap is divided
into three generations based on object lifetime. This generational approach improves
performance by collecting short-lived objects more frequently. Garbage Collection (GC) in
Java is an automatic memory management process that reclaims memory occupied by objects
that are no longer in use, thereby preventing memory leaks and improving application
performance.
1. Role of Garbage Collector
The Garbage Collector is a part of the Java Virtual Machine (JVM).
It automatically identifies and deletes unused objects.
Programmers do not need to explicitly free memory.
2. Objects become eligible for GC when:
Reference is set to null
Object goes out of scope
Reference variable is reassigned
Anonymous objects are created
3. Advantages of Garbage Collection
Prevents memory leaks
Improves program reliability
Simplifies memory management
Enhances application stability
Generations
1. Young Generation
Stores newly created objects.
Divided into:
o Eden Space – where objects are initially allocated.
o Survivor Spaces (S0 & S1) – objects that survive a minor GC are moved here.
Minor Garbage Collection occurs frequently.
Most objects are short-lived and collected here.
2. Old Generation (Tenured Generation)
Stores long-lived objects that survive multiple minor GCs.
Objects are promoted from the Young Generation.
Major / Full Garbage Collection occurs here.
GC is less frequent but more time-consuming.
3. Permanent Generation / Metaspace
Stores class metadata, method information, and static variables.
PermGen existed up to Java 7.
From Java 8 onwards, it is replaced by Metaspace, which uses native memory.
Helps in efficient class loading and unloading.
Q3.b. Develop a java program to find area of rectangle, area of circle and area of
triangle using method overloading concept. call these methods from main method with
suitable inputs (10 Marks)
Concept Used: Method Overloading
Method overloading allows multiple methods with the same name but different parameter
lists (type or number of parameters).
Program Code
class AreaOverloading
{
// Method to find area of rectangle
static void area(double length, double breadth)
{
double result = length * breadth;
[Link]("Area of Rectangle = " + result);
}
// Method to find area of circle
static void area(double radius)
{
double result = 3.14 * radius * radius;
[Link]("Area of Circle = " + result);
}
// Method to find area of triangle
static void area(double base, double height, int x)
{
double result = 0.5 * base * height;
[Link]("Area of Triangle = " + result);
}
public static void main(String args[])
{
// Calling overloaded methods
area(10, 5); // Rectangle
area(7); // Circle
area(8, 6, 1); // Triangle
}
}
Sample Output:
Area of Rectangle = 50.0
Area of Circle = 153.86
Area of Triangle = 24.0
Q3.c. Interpret the general form of a class with example (2 Marks)
A class in Java is a blueprint that defines data members (variables) and member functions
(methods).
General Syntax:
class ClassName
{
dataType variable1;
dataType variable2;
returnType methodName()
{
// method body
}
}
Example:
class Student
{
int rollNo;
String name;
void display()
{
[Link](rollNo + " " + name);
}
}
Q4.a. Outline the following keywords with example i. this [Link] (6 Marks)
i. this Keyword
The this keyword in Java refers to the current object of the class. It is mainly used to
distinguish between instance variables and local variables when they have the same name.
Uses of this
Refers to current class instance variable
Invokes current class method
Passes current object as argument
Example:
class Student
{
int id;
Student(int id)
{
[Link] = id; // refers to instance variable
}
void display()
{
[Link]("ID = " + id);
}
}
ii. static Keyword
The static keyword is used to create class-level variables and methods. Static members are
shared among all objects of the class.
Characteristics
Memory allocated only once
Can be accessed without creating an object
Belongs to class, not object
Example:
class Counter
{
static int count = 0;
Counter()
{
count++;
[Link](count);
}
public static void main(String args[])
{
new Counter();
new Counter();
new Counter();
}
}
Output:
1
2
3
Q4.b. Develop a java program to create a class called Employee which contains Name,
Designation, empid and basic Salary as instance variables and read() and write() as
methods. Using this class, read and write five employee information from main method
(10 Marks)
Program Code
import [Link];
class Employee
{
String name;
String designation;
int empid;
double basicSalary;
// Method to read employee details
void read()
{
Scanner sc = new Scanner([Link]);
[Link]("Enter Employee ID: ");
empid = [Link]();
[Link](); // consume newline
[Link]("Enter Name: ");
name = [Link]();
[Link]("Enter Designation: ");
designation = [Link]();
[Link]("Enter Basic Salary: ");
basicSalary = [Link]();
}
// Method to display employee details
void write()
{
[Link](empid + "\t" + name + "\t" + designation + "\t" + basicSalary);
}
public static void main(String args[])
{
Employee e[] = new Employee[5];
// Create objects and read details
for(int i = 0; i < 5; i++)
{
e[i] = new Employee();
[Link]("\nEnter details of Employee " + (i + 1));
e[i].read();
}
// Display employee details
[Link]("\nEmpID\tName\tDesignation\tSalary");
for(int i = 0; i < 5; i++)
{
e[i].write();
}
}
}
Sample Output (Format)
Enter details of Employee 1
Enter Employee ID: 101
Enter Name: Rahul
Enter Designation: Manager
Enter Basic Salary: 45000
...
EmpID Name Designation Salary
101 Rahul Manager 45000
102 Anita Developer 40000
Q4.c. Interpret with example type of constructors (4 marks)
A constructor is a special method used to initialize objects. In Java, constructors have the
same name as the class and no return type.
1. Default Constructor
A constructor without parameters.
It initializes instance variables with default values.
Example:
class Sample
{
int x;
Sample() // default constructor
{
x = 10;
}
public static void main(String args[])
{
Sample s = new Sample();
[Link](s.x);
}
}
2. Parameterized Constructor
A constructor that accepts parameters.
Used to initialize objects with user-defined values.
Example:
class Student
{
int id;
String name;
Student(int i, String n) // parameterized constructor
{
id = i;
name = n;
}
public static void main(String args[])
{
Student s = new Student(101, "Ravi");
[Link]([Link] + " " + [Link]);
}
}
Q5.a. Illustrate the usage of super keyword in java. Also explain dynamic method
dispatch (10 marks)
Uses of super Keyword
1. Referring to Parent Class Instance Variables: Used when parent and child classes have
variables with the same name.
Example:
class Parent
{
int x = 10;
}
class Child extends Parent
{
int x = 20;
void display()
{
[Link](super.x); // Parent class variable
[Link](x); // Child class variable
}
public static void main(String args[])
{
Child c = new Child();
[Link]();
}
}
2. Invoking Parent Class Method: Used to call a superclass method overridden in subclass.
Example:
class Parent
{
void show()
{
[Link]("Parent class method");
}
}
class Child extends Parent
{
void show()
{
[Link](); // calls parent method
[Link]("Child class method");
}
public static void main(String args[])
{
Child c = new Child();
[Link]();
}
}
3. Calling Parent Class Constructor: super() is used to invoke the constructor of parent class.
Example:
class Parent
{
Parent()
{
[Link]("Parent constructor");
}
}
class Child extends Parent
{
Child()
{
super();
[Link]("Child constructor");
}
public static void main(String args[])
{
new Child();
}
}
Dynamic Method Dispatch
Dynamic Method Dispatch is a mechanism by which a call to an overridden method is
resolved at runtime, based on the type of object, not the reference.
It is a key concept of runtime polymorphism in Java.
Explanation
A superclass reference can refer to a subclass object
The method call depends on the actual object, not the reference
Achieved using method overriding
Example:
class Animal
{
void sound()
{
[Link]("Animal makes a sound");
}
}
class Dog extends Animal
{
void sound()
{
[Link]("Dog barks");
}
public static void main(String args[])
{
Animal a;
a = new Dog(); // superclass reference, subclass object
[Link](); // runtime decision
}
}
Output:
Dog barks
Q5.b. Build a java program to create an interface resizable with method resize(int
radius) that allow an object to be resized. Create a class circle that implements resizable
interface and implements the resize method (10 Marks)
Program Code
import [Link];
// Interface definition
interface Resizable
{
void resize(int radius);
}
// Class implementing the interface
class Circle implements Resizable
{
int radius;
// Implementing resize method
public void resize(int radius)
{
[Link] = radius;
double area = 3.14 * radius * radius;
[Link]("Resized Circle Radius = " + radius);
[Link]("Area of Circle = " + area);
}
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter new radius: ");
int r = [Link]();
Circle c = new Circle();
[Link](r);
}
}
Sample Output
Enter new radius: 7
Resized Circle Radius = 7
Area of Circle = 153.86
Q6.a. Compare and contrast method overloading and method overriding with suitable
example (8 Marks)
Method Overloading
Method overloading allows multiple methods with the same name in the same class but with
different parameter lists.
Key Points
Occurs in the same class
Parameters must differ (number / type / order)
Return type alone is not sufficient
Achieved at compile time (compile-time polymorphism)
Example:
class Calculator
{
int add(int a, int b)
{
return a + b;
}
int add(int a, int b, int c)
{
return a + b + c;
}
public static void main(String args[])
{
Calculator c = new Calculator();
[Link]([Link](5, 10));
[Link]([Link](5, 10, 15));
}
}
Method Overriding
Method overriding occurs when a subclass provides its own implementation of a method
already defined in its superclass.
Key Points
Occurs in different classes (inheritance required)
Method signature must be same
Return type should be same or covariant
Achieved at runtime (runtime polymorphism)
Uses dynamic method dispatch
Example:
class Parent
{
void show()
{
[Link]("Parent class method");
}
}
class Child extends Parent
{
void show()
{
[Link]("Child class method");
}
public static void main(String args[])
{
Parent p = new Child();
[Link]();
}
}
Comparison Table
Feature Method Overloading Method Overriding
Same method name, different Same method name and
Definition
parameters parameters
Class Requirement Same class Different classes
Inheritance Not required Required
Polymorphism
Compile-time Runtime
Type
Method Signature Must differ Must be same
Binding Time Compile time Runtime
Q6.b. Define inheritance and list the different types of inheritance in java (4 marks)
Inheritance is an object-oriented concept in Java by which one class (subclass or child class)
acquires the properties and methods of another class (superclass or parent class). It promotes
code reusability and supports polymorphism.
Types of Inheritance in Java
1. Single Inheritance
One subclass inherits from one superclass.
2. Multilevel Inheritance
A class is derived from another derived class.
3. Hierarchical Inheritance
Multiple subclasses inherit from a single superclass.
4. Multiple Inheritance (Through Interface)
A class implements more than one interface (Java does not support multiple
inheritance using classes).
Q6.c. Build a java program to create a class named shape. Create three subclasses
named cicle, triangle and square. Each has two methods draw() and erase().
Demonstrate polymorphism concepts by developing suitable methods and main
program. (8 Marks)
Program Code
// Superclass
class Shape
{
void draw()
{
[Link]("Drawing Shape");
}
void erase()
{
[Link]("Erasing Shape");
}
}
// Subclass Circle
class Circle extends Shape
{
void draw()
{
[Link]("Drawing Circle");
}
void erase()
{
[Link]("Erasing Circle");
}
}
// Subclass Triangle
class Triangle extends Shape
{
void draw()
{
[Link]("Drawing Triangle");
}
void erase()
{
[Link]("Erasing Triangle");
}
}
// Subclass Square
class Square extends Shape
{
void draw()
{
[Link]("Drawing Square");
}
void erase()
{
[Link]("Erasing Square");
}
}
// Main class
class PolymorphismDemo
{
public static void main(String args[])
{
Shape c = new Circle();
[Link]();
[Link]();
Shape t = new Triangle();
[Link]();
[Link]();
Shape s = new Square();
[Link]();
[Link]();
}
}
Sample Output
Drawing Circle
Erasing Circle
Drawing Triangle
Erasing Triangle
Drawing Square
Erasing Square
Q7.a. Examine the various levels of access protections available for packages and their
implications with suitable examples (10 Marks)
In Java, access protection controls the visibility of classes, variables, methods, and
constructors. It helps in data hiding, security, and modular programming, especially when
using packages. Java provides four levels of access protection. Java allow fine-grained
control over the visibility of variables and methods within classes, subclasses, and packages
through access protection.
Java provides four access levels:
1. Public – accessible everywhere
2. Protected – accessible within package & subclasses
3. Default – accessible within same package
4. Private – accessible only within class
Example program:
package Mypkg;
public class A {
public int pubvar = 10; // public accesss
protected int provar = 20; // protected access
int defvar = 30; // default access
private int privar = 40; // private access
public void show() {
[Link]("Inside ClassA:");
[Link]("Public: " + pubvar);
[Link]("Protected: " + provar);
[Link]("Default: " + defvar);
[Link]("Private: " + privar);
}
}
package Mypkg;
public class B {
public static void main(String[] args) {
A a1 = new A();
[Link]("Accessing members from Class B");
[Link]("Public: " + [Link]); // allowed
[Link]("Protected: " + [Link]); // allowed
[Link]("Default: " + [Link]); // allowed
// [Link]("Private: " + [Link]); // ❌Not allowed
[Link](); // Calls method inside Class A
}
}
Expected Output:
Accessing members from Class B:
Public: 10
Protected: 20
Default: 30
Inside Class A:
Public: 10
Protected: 20
Default: 30
Private: 40
Q7.b. Build a java program for a banking application to throw an exception, where a
person tries to withdraw the amount even though he/she has lesser than minimum
balance (10 marks)
class MinimumBalanceException extends Exception {
public MinimumBalanceException(String message) {
super(message);
}
}
class BankAccount {
private double balance;
private final double MIN_BALANCE = 1000;
// Constructor
BankAccount(double balance) {
[Link] = balance;
}
// Method to withdraw amount
void withdraw(double amount) throws MinimumBalanceException {
if (balance - amount < MIN_BALANCE) {
throw new MinimumBalanceException(
"Withdrawal denied! Minimum balance of Rs. " + MIN_BALANCE + " must be
maintained."
);
}
balance = balance - amount;
[Link]("Withdrawal successful!");
[Link]("Remaining Balance: Rs. " + balance);
}
}
import [Link];
public class BankingApp {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
// Initial balance
BankAccount account = new BankAccount(5000);
[Link]("Enter amount to withdraw: ");
double amount = [Link]();
try {
[Link](amount);
} catch (MinimumBalanceException e) {
[Link]("Exception Caught: " + [Link]());
}
[Link]();
}
}
Sample Output
Case 1: Valid Withdrawal
Enter amount to withdraw: 3000
Withdrawal successful!
Remaining Balance: Rs. 2000
Case 2: Invalid Withdrawal
Enter amount to withdraw: 4500
Exception Caught: Withdrawal denied! Minimum balance of Rs. 1000 must be maintained.
Q9.a. Explain Thread. With a neat example discuss the different methods by which a
thread is created in Java. (6 Marks)
A thread is a smallest unit of a process that can be scheduled for execution and enables
concurrent execution of two or more parts of a Java program.A thread in Java is a lightweight
unit of execution that represents an independent path of execution within a program. Threads
allow a Java program to perform multiple tasks simultaneously, which is known as
multithreading.
Ways to Create a Thread in Java
1. By extending the Thread class
2. By implementing the Runnable interface
[Link] Thread by Extending Thread (INHERITANCE)
class MyThread extends Thread {
public void run() {
✅ Expected Output:
[Link](“MyThread is running");
} MyThread is running
public static void main(String[] args) {
MyThread t = new MyThread();
[Link]();
}
}
2. Creating a Thread by Implementing Runnable Interface (INTERFACING)
class MyRunnable implements Runnable {
Expected Output:
public void run() {
[Link]("Thread is running"); Thread is running
}
public static void main(String[] args) {
MyRunnable r = new MyRunnable();
Thread t = new Thread(r); // create thread for executing class myRunnable
[Link](); // start thread
}
}
Q9.b. How synchronization can be achieved between threads in java. Explain with
example (6 Marks).
Thread synchronization in Java is a mechanism used to control access to shared resources so that
only one thread can execute a critical section at a time. It prevents race conditions and ensures
data consistency in multithreaded programs. Java provides synchronization mainly using the
synchronized keyword, which can be applied in two ways:
1. Synchronized Method
Entire method is locked
Only one thread can access the method at a time
Lock is acquired on the object
Example:
class Account {
int balance = 1000;
synchronized void withdraw(int amount) {
if (balance >= amount) {
balance = balance - amount;
[Link]([Link]().getName() +
" withdrew " + amount);
} else {
[Link]("Insufficient balance");
}
}
}
2. Synchronized Block
Locks only a specific block of code
Improves performance by reducing lock scope
Example:
class Account {
int balance = 1000;
void withdraw(int amount) {
synchronized (this) {
if (balance >= amount) {
balance -= amount;
[Link]([Link]().getName() +
" withdrew " + amount);
}
}
}
}
Thread Execution
class Test {
public static void main(String[] args) {
Account acc = new Account();
Thread t1 = new Thread(() -> [Link](700), "Thread-1");
Thread t2 = new Thread(() -> [Link](700), "Thread-2");
[Link]();
[Link]();
}
}
Q9.c. Develop a java program for automatic conversion of wrapper class type into
corresponding primitive type that demonstrate unboxing. (8 Marks)
Unboxing in Java is the automatic conversion of a wrapper class object into its corresponding
primitive data type. This feature was introduced in Java 5 to simplify programming and improve
readability.
Java Program to Demonstrate Unboxing
public class UnboxingDemo {
public static void main(String[] args) {
// Wrapper class objects
Integer iObj = 50;
Double dObj = 25.75;
Character cObj = 'A';
Boolean bObj = true;
// Automatic unboxing
int i = iObj;
double d = dObj;
char c = cObj;
boolean b = bObj;
// Display results
[Link]("Integer object: " + iObj + " -> int value: " + i);
[Link]("Double object: " + dObj + " -> double value: " + d);
[Link]("Character object: " + cObj + " -> char value: " + c);
[Link]("Boolean object: " + bObj + " -> boolean value: " + b);
}
}
Sample Output
Integer object: 50 -> int value: 50
Double object: 25.75 -> double value: 25.75
Character object: A -> char value: A
Boolean object: true -> boolean value: true
Q10.a. Summarize the type wrappers supported in Java (6 Marks)
Wrapper classes in Java are used to convert primitive data types into objects. They are part of
the [Link] package and enable primitives to be used in collections, synchronization, and
object-oriented frameworks.
List of Type Wrappers Supported in Java
primitive Data Type W Wrapper Class Ex Example
byte ByByte By Byte b = 10;
h short Sh Short Sh Short s = 20;
t integer Int Integer Int Integer i = 100;
Q10.b. Explain autoboxing/unboxing that occurs in expressions and operators (6 marks)
Autoboxing
Autoboxing is the automatic conversion of a primitive data type into its corresponding
wrapper class object when required by the context.
Unboxing
Unboxing is the automatic conversion of a wrapper class object into its corresponding
primitive type, especially during expressions and operator evaluation.
Autoboxing in Expressions and Operators
When a primitive value is used where an object is expected, Java automatically converts it
into a wrapper object.
Example:
Integer a = 10; b=20 // int → Integer (autoboxing)
Here, 10 and 20 are primitives, but they are automatically converted into Integer objects.
Unboxing in Expressions and Operators
When arithmetic or relational operators are applied to wrapper objects, Java unboxes them
into primitives, performs the operation, and then boxes the result if required.
Example:
Integer x = 30; y = 40;
Integer z = x + y; // unboxing → addition → autoboxing
Autoboxing and Unboxing with Relational Operators
Integer p = 50;
Integer q = 60;
if (p < q) { // unboxing occurs
[Link]("p is smaller than q");
}
Q10.c. Develop a java program to create a class [Link] the base class
constructor in this class's constructor using super and start the thread. The run method
of the class starts after this. It can be observed that both main and thread and created
child thread are executed concurrently (8 Marks)
class MyThread extends Thread {
// Constructor of MyThread
MyThread(String name) {
super(name); // Calling base class (Thread) constructor
[Link]("Child thread constructor called");
}
// run method
public void run() {
for (int i = 1; i <= 5; i++) {
[Link](getName() + " running : " + i);
try {
[Link](500);
} catch (InterruptedException e) {
[Link](e);
}
}
}
}
public class ThreadDemo {
public static void main(String[] args) {
// Create child thread object
MyThread t = new MyThread("Child-Thread");
// Start the child thread
[Link]();
// Main thread execution
for (int i = 1; i <= 5; i++) {
[Link]("Main thread running : " + i);
try {
[Link](500);
} catch (InterruptedException e) {
[Link](e);
}
}
}
}
Sample Output (Order may vary)
Child thread constructor called
Main thread running : 1
Child-Thread running : 1
Main thread running : 2
Child-Thread running : 2
Main thread running : 3
Child-Thread running : 3
Main thread running : 4
Child-Thread running : 4
Main thread running : 5
Child-Thread running : 5