1.
Data Types in Java
Primitive Data Types:
Java has eight primitive data types:
1. byte: 1 byte, -128 to 127
2. short: 2 bytes, -32,768 to 32,767
3. int: 4 bytes, -2^31 to 2^31-1
4. long: 8 bytes, -2^63 to 2^63-1
5. float: 4 bytes, single precision floating point
6. double: 8 bytes, double precision floating point
7. char: 2 bytes, a single Unicode character (0 to 65535)
8. boolean: 1 bit, true or false
Example:
java
public class DataTypesExample {
public static void main(String[] args) {
int a = 5; // Integer
double b = 10.5; // Double
char c = 'A'; // Character
boolean isJavaFun = true; // Boolean
[Link]("Integer: " + a);
[Link]("Double: " + b);
[Link]("Char: " + c);
[Link]("Boolean: " + isJavaFun);
}
2. Type Conversion
Types of Type Conversion:
● Implicit (Automatic) Type Conversion: Happens when a smaller data type is
automatically converted to a larger one (widening conversion).
● Explicit (Manual) Type Conversion: Happens when a larger data type is manually
converted to a smaller one (narrowing conversion), and this requires casting.
Example of Implicit Conversion:
java
public class ImplicitConversion {
public static void main(String[] args) {
int x = 10;
double y = x; // Implicit conversion from int to double
[Link]("Double value: " + y);
Example of Explicit Conversion:
java
public class ExplicitConversion {
public static void main(String[] args) {
double x = 10.5;
int y = (int) x; // Explicit conversion (casting) from
double to int
[Link]("Integer value: " + y);
}
3. Control Statements in Java
Control statements allow you to control the flow of execution in a program based on certain
conditions.
Types of Control Statements:
1. Decision-Making Statements:
○ if: Executes a block of code if the condition is true.
○ if-else: Executes one block of code if the condition is true, and another block
if false.
○ else-if: Used for multiple conditions.
○ switch: Executes one of several blocks of code based on the value of an
expression.
Example of if-else:
java
public class DecisionMaking {
public static void main(String[] args) {
int num = 10;
if (num > 0) {
[Link]("Positive number");
} else {
[Link]("Non-positive number");
}
Example of switch:
java
public class SwitchExample {
public static void main(String[] args) {
int day = 3;
switch(day) {
case 1:
[Link]("Monday");
break;
case 2:
[Link]("Tuesday");
break;
case 3:
[Link]("Wednesday");
break;
default:
[Link]("Invalid day");
4. Loops in Java
Loops are used to repeat a block of code multiple times based on a condition.
Types of Loops:
1. for loop: Used when the number of iterations is known.
2. while loop: Used when the number of iterations is unknown, and the loop runs while
a condition is true.
3. do-while loop: Similar to while, but guarantees at least one iteration, as the
condition is checked after executing the loop.
Example of for loop:
java
public class ForLoopExample {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
[Link]("Iteration: " + i);
Example of while loop:
java
public class WhileLoopExample {
public static void main(String[] args) {
int i = 1;
while (i <= 5) {
[Link]("Iteration: " + i);
i++;
}
Example of do-while loop:
java
public class DoWhileLoopExample {
public static void main(String[] args) {
int i = 1;
do {
[Link]("Iteration: " + i);
i++;
} while (i <= 5);
5. Arrays in Java
An array is a collection of similar data types stored in contiguous memory locations. It can
store primitive data types or objects.
Declaring and Initializing Arrays:
1. One-Dimensional Array:
○ Syntax: dataType[] arrayName = new dataType[size];
Example of One-Dimensional Array:
java
public class OneDimensionalArray {
public static void main(String[] args) {
// Declaring and initializing an array
int[] arr = {1, 2, 3, 4, 5};
// Accessing elements using indices
[Link]("First element: " + arr[0]);
[Link]("Second element: " + arr[1]);
// Iterating over the array using a for loop
for (int i = 0; i < [Link]; i++) {
[Link]("Element at index " + i + ": " +
arr[i]);
2. Multi-Dimensional Array:
○ Syntax: dataType[][] arrayName = new
dataType[rows][columns];
Example of Two-Dimensional Array:
java
public class TwoDimensionalArray {
public static void main(String[] args) {
// Declaring and initializing a 2D array
int[][] arr = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// Accessing elements in a 2D array
[Link]("Element at [0][1]: " + arr[0][1]);
// Iterating over the 2D array
for (int i = 0; i < [Link]; i++) {
for (int j = 0; j < arr[i].length; j++) {
[Link](arr[i][j] + " ");
[Link]();
Interview Questions
General Questions:
1. What are primitive data types in Java? Explain with examples.
2. What is type conversion? Explain the difference between implicit and explicit
conversion.
3. Explain the difference between if-else and switch statements with
examples.
4. What is the difference between a for loop and a while loop?
5. How do you declare and initialize arrays in Java? Explain with examples.
Questions on Loops:
1. What is the difference between while and do-while loops?
2. Explain the break and continue statements in loops with examples.
3. Can you iterate over an array using a for-each loop in Java?
Questions on Arrays:
1. What is the difference between a one-dimensional and a multi-dimensional
array?
2. How does array indexing work in Java?
3. What will happen if you try to access an array element outside its bounds?
Scenario-Based Questions:
1. If you need to process a list of student marks, which loop would you use, and
why?
2. You need to create a program to manage student data. How would you store
the data using arrays?