Skip to main content

Posts

Java Exception Handling MCQ Test

  Loading…

The final keyword

The `final` keyword in Java is used to define constants, prevent method overriding, and create immutable classes. Here are the main uses of the `final` keyword: 1. Final Variables: When applied to a variable, the `final` keyword indicates that the variable's value cannot be changed once initialized. 2. Final Methods: When applied to a method, the `final` keyword indicates that the method cannot be overridden by subclasses. This is commonly used in class inheritance to enforce certain behaviors that should not be modified. 3. Final Classes: When applied to a class, the `final` keyword indicates that the class cannot be subclassed. This is often used to create immutable classes, which cannot be extended or modified. Here are some examples to illustrate the usage of the `final` keyword: Final Variables: public class FinalExample {     final int MAX_VALUE = 100; // Constant variable     public static void main(String[] args) {         FinalExample o...

Getter and setter methods

Getter and setter methods, also known as accessor and mutator methods, are used to retrieve and modify the values of private variables (fields) in a class. They provide controlled access to the class attributes, allowing encapsulation and maintaining data integrity. Here's an example demonstrating getter and setter methods in Java: public class Person {     private String name;     private int age;     // Getter method for the name attribute     public String getName() {         return name;     }     // Setter method for the name attribute     public void setName(String name) {         this.name = name;     }     // Getter method for the age attribute     public int getAge() {         return age;     }     // Setter method for the age attribute     public void setAge(int age) {       ...

Java Code for Matrix Operations (Addition, Subtraction & Multiplication)

  Copy import java.util.Scanner; public class MatrixOperations { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Input matrix A System.out.println("Enter the number of rows and columns of matrix A:"); int rowsA = scanner.nextInt(); int colsA = scanner.nextInt(); int[][] matrixA = new int[rowsA][colsA]; System.out.println("Enter the elements of matrix A:"); for (int i = 0; i

Second Largest number in the Array

  Copy import java.util.Scanner; public class SecondLargestInArray { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter the size of the array: "); int size = scanner.nextInt(); int[] arr = new int[size]; System.out.println("Enter the elements of the array:"); for (int i = 0; i largest) { secondLargest = largest; largest = arr[i]; } else if (arr[i] > secondLargest && arr[i] != largest) { secondLargest = arr[i]; } } if (secondLargest == Integer.MIN_VALUE) { System.out.println("There is no second largest element."); } else { System.out.println("The second largest element in the array is: " + secondLargest); } scanner.close(); } }

Basic Java Programs

  Copy public class BasicJavaProgram { public static void main(String[] args) { // Variables int num1 = 10; int num2 = 5; // Arithmetic Operators int sum = num1 + num2; int difference = num1 - num2; int product = num1 * num2; int quotient = num1 / num2; int remainder = num1 % num2; // Comparison Operators boolean isEqual = num1 == num2; boolean isNotEqual = num1 != num2; boolean isGreater = num1 > num2; boolean isLess = num1 = num2; boolean isLessOrEqual = num1 0) && (num2 0) || (num2 num2) { System.out.println("num1 is greater than num2"); } else if (num1

Basic Java program for implementation operators, variables, control flow statements

 public class BasicJavaProgram {     public static void main(String[] args) {         // Variables         int num1 = 10;         int num2 = 5;         // Arithmetic Operators         int sum = num1 + num2;         int difference = num1 - num2;         int product = num1 * num2;         int quotient = num1 / num2;         int remainder = num1 % num2;         // Comparison Operators         boolean isEqual = num1 == num2;         boolean isNotEqual = num1 != num2;         boolean isGreater = num1 > num2;         boolean isLess = num1 < num2;         boolean isGreaterOrEqual = num1 >= num2;         boolean isLessOrEqual = num1 <= num2;   ...