0% found this document useful (0 votes)
8 views9 pages

5.practice On Basic Programs

Uploaded by

anshyadav69420
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views9 pages

5.practice On Basic Programs

Uploaded by

anshyadav69420
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

PRACTICE

PROGRAMS
Write a Java program to
declare an array of
integers and initialize it
with the numbers 1 to 5.
Then, print out the first
a nd la s t e l e m e n t o f t h e
array.
public class Main {
public static void main(String[] args) {
// Declare and initialize an array of integers
int[] numbers = {1, 2, 3, 4, 5};

// Print the first element of the array


System.out.println("First element: " + numbers[0]);

// Print the last element of the array


// Since array indices start at 0, the last element is at length-1
System.out.println("Last element: " + numbers[numbers.length - 1]);
}
}
Explain with a Java program why arrays in Java
have a fixed size. Try to add a new element to an
already full array and get the
ArrayIndexOutOfBoundsException
public class Main {
public static void main(String[] args) {
// Declare and initialize an array of integers with 5 elements
int[] myArray = {1, 2, 3, 4, 5};

// Attempt to add a new element at index 5


// This will cause an ArrayIndexOutOfBoundsException,
terminating the program
myArray[5] = 6;

// This line will not be executed


System.out.println("This line will not be printed.");
}
}
Write a Java program that takes two numbers
as input and calculates their sum, difference,
product, quotient, and remainder, then prints
the results.
public class ArithmeticOperations {
public static void main(String[] args) {
// Directly provide the two numbers
int number1 = 15;
int number2 = 4;

// Calculate sum, difference, product, quotient, and remainder


int sum = number1 + number2;
int difference = number1 - number2;
int product = number1 * number2;
double quotient = number1 / (double) number2; // Cast to double for floating-point
division
int remainder = number1 % number2;
// Print the results
System.out.println("Given numbers: " + number1 + " and " + number2);
System.out.println("Sum: " + sum);
System.out.println("Difference: " + difference);
System.out.println("Product: " + product);
System.out.println("Quotient: " + quotient);
System.out.println("Remainder: " + remainder);
}
}
Write a Java program to create a 2x2 matrix
array, initialize it with values, and then print
all the values in a matrix format.
public class Main {
public static void main(String[] args) {
// Create and initialize a 2x2 string array
String[][] matrix = {
{"A1", "A2"},
{"B1", "B2"}
};

// Print the values in a matrix format


for (int i = 0; i < matrix.length; i++) { // Loop through rows
for (int j = 0; j < matrix[i].length; j++) { // Loop through columns
System.out.print(matrix[i][j] + " "); // Print each element followed
by a space
}
System.out.println(); // Move to the next line after printing all
columns in a row
}
}}

You might also like