Q1) java is a sta cally-typed language, you must declare the type of a variable before you can use
it.
Java data types are divided into two main categories: Primi ve and Non-Primi ve.
1. Primi ve Data Types
Primi ve types are the most basic data types built into the language. They serve as the building
blocks for data manipula on. There are 8 primi ve types:
Numeric Types (Integers)
byte: 8-bit signed integer. Useful for saving memory in large arrays. (Range: -128 to 127)
short: 16-bit signed integer. (Range: -32,768 to 32,767)
int: 32-bit signed integer. This is the default type for whole numbers.
long: 64-bit signed integer. Used when int isn't large enough. (Note: Append an 'L' to the
value, e.g., 100L).
Floa ng-Point Types (Decimals)
float: 32-bit single-precision. (Note: Append an 'f', e.g., 5.99f).
double: 64-bit double-precision. This is the default for decimal numbers.
Others
boolean: Represents one bit of informa on, but its size isn't precisely defined. It only has
two possible values: true or false.
char: 16-bit Unicode character. (e.g., 'A' or '\u0041').
2. Non-Primi ve (Reference) Data Types
Non-primi ve types refer to objects and are created by the programmer (except for String, which is
predefined but s ll a reference type).
Strings: A sequence of characters (e.g., "Hello World").
Arrays: Collec ons of variables of the same type.
Classes & Interfaces: User-defined types that encapsulate data and methods.
Key Differences
Feature Primi ve Non-Primi ve
Always has a value (defaults to 0 or
Value Can be null.
false).
All reference types are the same size (memory
Size Depends on the specific type.
address).
Feature Primi ve Non-Primi ve
Can be used to call methods to perform
Methods Cannot be used to call methods.
opera ons.
Memory Stored on the Stack. Stored on the Heap.
Summary Code Example
Java
public class Main {
public sta c void main(String[] args) {
int myNum = 5; // Integer (primi ve)
float myFloatNum = 5.99f; // Floa ng point (primi ve)
char myLe er = 'D'; // Character (primi ve)
boolean myBool = true; // Boolean (primi ve)
String myText = "Hello"; // String (non-primi ve)
[Link](myNum);
Q2)
ava is a sta cally-typed language, you must declare the type of a variable before you can use it.
Java data types are divided into two main categories: Primi ve and Non-Primi ve.
1. Primi ve Data Types
Primi ve types are the most basic data types built into the language. They serve as the building
blocks for data manipula on. There are 8 primi ve types:
Numeric Types (Integers)
byte: 8-bit signed integer. Useful for saving memory in large arrays. (Range: -128 to 127)
short: 16-bit signed integer. (Range: -32,768 to 32,767)
int: 32-bit signed integer. This is the default type for whole numbers.
long: 64-bit signed integer. Used when int isn't large enough. (Note: Append an 'L' to the
value, e.g., 100L).
Floa ng-Point Types (Decimals)
float: 32-bit single-precision. (Note: Append an 'f', e.g., 5.99f).
double: 64-bit double-precision. This is the default for decimal numbers.
Others
boolean: Represents one bit of informa on, but its size isn't precisely defined. It only has
two possible values: true or false.
char: 16-bit Unicode character. (e.g., 'A' or '\u0041').
2. Non-Primi ve (Reference) Data Types
Non-primi ve types refer to objects and are created by the programmer (except for String, which is
predefined but s ll a reference type).
Strings: A sequence of characters (e.g., "Hello World").
Arrays: Collec ons of variables of the same type.
Classes & Interfaces: User-defined types that encapsulate data and methods.
Key Differences
Feature Primi ve Non-Primi ve
Always has a value (defaults to 0 or
Value Can be null.
false).
All reference types are the same size (memory
Size Depends on the specific type.
address).
Can be used to call methods to perform
Methods Cannot be used to call methods.
opera ons.
Memory Stored on the Stack. Stored on the Heap.
Summary Code Example
Java
public class Main {
public sta c void main(String[] args) {
int myNum = 5; // Integer (primi ve)
float myFloatNum = 5.99f; // Floa ng point (primi ve)
char myLe er = 'D'; // Character (primi ve)
boolean myBool = true; // Boolean (primi ve)
String myText = "Hello"; // String (non-primi ve)
[Link](myNum);
Q3)
import java.u [Link];
// The Blueprint (Class)
class Calculator {
// Methods to perform opera ons
public double add(double x, double y) { return x + y; }
public double subtract(double x, double y) { return x - y; }
public double mul ply(double x, double y) { return x * y; }
public double divide(double x, double y) {
if (y == 0) {
[Link]("Error! Division by zero.");
return 0;
return x / y;
// The Execu on Point
public class Main {
public sta c void main(String[] args) {
Scanner reader = new Scanner([Link]);
// 1. Crea ng an Object of the Calculator class
Calculator myCalc = new Calculator();
[Link]("Enter two numbers: ");
double num1 = [Link]();
double num2 = [Link]();
[Link]("Enter an operator (+, -, *, /): ");
char operator = [Link]().charAt(0);
double result;
// 2. Using the Object to perform calcula ons
switch (operator) {
case '+': result = [Link](num1, num2); break;
case '-': result = [Link](num1, num2); break;
case '*': result = [Link] ply(num1, num2); break;
case '/': result = [Link](num1, num2); break;
default:
[Link]("Invalid operator!");
return;
[Link]("The result is: " + result);
}
Q4)
import java.u [Link];
class BankAccount {
private String accountHolder;
private double balance;
// Constructor to ini alize the account
public BankAccount(String name, double ini alDeposit) {
[Link] = name;
[Link] = ini alDeposit;
// Method to check balance
public void checkBalance() {
[Link]("\n--- Account Summary ---");
[Link]("Holder: " + accountHolder);
[Link]("Current Balance: $" + balance);
// Method to deposit money
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
[Link]("Successfully deposited: $" + amount);
} else {
[Link]("Invalid deposit amount.");
// Method to withdraw money
public void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
[Link]("Successfully withdrawn: $" + amount);
} else if (amount > balance) {
[Link]("Insufficient funds! Transac on failed.");
} else {
[Link]("Invalid withdrawal amount.");
public class BankSystem {
public sta c void main(String[] args) {
Scanner input = new Scanner([Link]);
// Crea ng an object (Crea ng a new bank account)
BankAccount myAccount = new BankAccount("Alex", 500.00);
// Simple Menu-Driven Interface
[Link]("Welcome to the Bank, " + "Alex!");
[Link](200.0); // Deposi ng money
[Link](150.0); // Withdrawing money
[Link](); // Checking final balance
[Link]();