Q1.
Explain Java Data Types in detail
Java Data Types define the type of data that a variable can store in a Java program. Java is a
strongly typed programming language, which means every variable must be declared with a data
type before it is used. Data types help the compiler understand how much memory to allocate and
what operations can be performed on the data.
Java data types are broadly classified into two categories: 1. Primitive Data Types 2. Non-Primitive
(Reference) Data Types
1. Primitive Data Types
Primitive data types store simple values directly in memory and are predefined by Java.
• byte: Uses 1 byte of memory. Range is -128 to 127. Used to save memory in large arrays.
• short: Uses 2 bytes of memory. Range is -32,768 to 32,767.
• int: Uses 4 bytes of memory. Most commonly used integer data type.
• long: Uses 8 bytes of memory. Used for very large numbers.
• float: Uses 4 bytes of memory. Stores decimal values with single precision.
• double: Uses 8 bytes of memory. Stores decimal values with double precision.
• char: Uses 2 bytes of memory. Stores a single Unicode character.
• boolean: Stores only two values – true or false.
Example:
int age = 25;
double salary = 45000.50;
char grade = 'A';
boolean status = true;
2. Non-Primitive Data Types
Non-primitive data types store references to objects. They are created by the programmer and can
store multiple values. Examples include String, Array, Class, and Interface.
Example:
String name = "Java";
int[] marks = {80, 85, 90};
Q2. Explain the process of creating and calling a method in Java
A method in Java is a block of code that performs a specific task. Methods help in code reusability,
modularity, and easier maintenance of programs.
Steps to Create a Method:
1. Define the access modifier (public, private, protected).
2. Specify the return type.
3. Write the method name.
4. Define parameters (if any).
5. Write the method body.
Syntax:
access_modifier return_type methodName(parameters) {
statements;
}
Example:
public int add(int a, int b) {
return a + b;
}
Calling a Method:
A method is called using the object name. If the method is static, it can be called using the class
name.
Example:
Add obj = new Add();
int result = [Link](5, 10);
Q3. Explain Interface and various forms of implementing Interface
An interface in Java is a collection of abstract methods and constants. It is used to achieve
abstraction and multiple inheritance. Interfaces define what a class must do, but not how it does it.
Syntax of Interface:
interface InterfaceName {
void methodName();
}
Forms of Implementing Interface:
1. Single Interface Implementation – A class implements one interface.
2. Multiple Interface Implementation – A class implements more than one interface.
3. Interface Inheritance – One interface extends another interface.
Example:
interface Animal {
void sound();
}
class Dog implements Animal {
public void sound() {
[Link]("Bark");
}
}
Q4. What are Wrapper Classes? Explain with example
Wrapper classes in Java are used to convert primitive data types into objects. Java provides a
wrapper class for each primitive data type. Wrapper classes are useful when working with
collections, frameworks, and object-oriented features.
Primitive to Wrapper Mapping:
int → Integer
byte → Byte
short → Short
long → Long
float → Float
double → Double
char → Character
boolean → Boolean
Example:
int num = 10;
Integer obj = [Link](num);
int value = [Link]();
This process is known as boxing (primitive to object) and unboxing (object to primitive).