UpdateGadh

UPDATEGADH.COM

Free Java Interview Question And Answers Pdf [Quiz -7]

Java Interview Question And Answers Pdf ( Quiz -7 )

YouTube player
  1. Matrix Transpose:
    Write a program or function that takes a square matrix as input and returns its transpose. The transpose of a matrix is obtained by swapping the rows and columns.
public class MatrixTranspose {
    public static int[][] transposeMatrix(int[][] matrix) {
        int rows = matrix.length;
        int columns = matrix[0].length;
        int[][] transpose = new int[columns][rows];

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                transpose[j][i] = matrix[i][j];
            }
        }

        return transpose;
    }

    public static void main(String[] args) {
        int[][] matrix = {
            {1, 2, 3},
            {4, 5, 6},
            {7, 8, 9}
        };
        int[][] transposedMatrix = transposeMatrix(matrix);
        for (int[] row : transposedMatrix) {
            for (int element : row) {
                System.out.print(element + " ");
            }
            System.out.println();
        }
    }
}

Check 50+ JAVA Projects with Source Code

  1. Remove Duplicates from an Array:
    Create a program or function that removes duplicate elements from an array and returns the array with only unique elements.
import java.util.Arrays;
import java.util.LinkedHashSet;

public class RemoveDuplicatesFromArray {
    public static int[] removeDuplicates(int[] array) {
        return Arrays.stream(array).distinct().toArray();
    }

    public static void main(String[] args) {
        int[] array = {1, 2, 3, 4, 3, 2, 5};
        int[] uniqueArray = removeDuplicates(array);
        System.out.println("Unique Array: " + Arrays.toString(uniqueArray));
    }
}

Check 100+ JAVA Spring Boot Projects with Source Code

Check for Perfect Number: Write a function to check if a given positive integer is a perfect number. A perfect number is a positive integer that is equal to the sum of its proper divisors (excluding itself).

public class PerfectNumber {
    public static boolean isPerfectNumber(int number) {
        if (number <= 1) {
            return false;
        }
        int sum = 1;
        for (int i = 2; i * i <= number; i++) {
            if (number % i == 0) {
                sum += i;
                if (i * i != number) {
                    sum += number / i;
                }
            }
        }
        return sum == number;
    }

    public static void main(String[] args) {
        int number = 28; // Example number
        System.out.println("Is Perfect Number: " + isPerfectNumber(number));
    }
}
Free Java Interview Question And Answers Pdf -Quiz -5
Java Interview Question And Answers Pdf
updategadh.com

Find Missing Number:
Write a program or function that finds the missing number in a given array of integers containing numbers from 1 to n, where n is the length of the array

public class FindMissingNumber {
    public static int findMissingNumber(int[] nums) {
        int expectedSum = nums.length * (nums.length + 1) / 2;
        int actualSum = 0;
        for (int num : nums) {
            actualSum += num;
        }
        return expectedSum - actualSum;
    }

    public static void main(String[] args) {
        int[] array = {1, 2, 4, 5, 6}; // Example array with one missing number
        System.out.println("Missing Number: " + findMissingNumber(array));
    }
}
Complete Java Course with Real Projects – Updategadh
Complete Java Course
updategadh.com

Reverse a Linked List: Implement a function to reverse a singly linked list

class ListNode {
    int val;
    ListNode next;

    ListNode(int val) {
        this.val = val;
    }
}

public class ReverseLinkedList {
    public static ListNode reverseLinkedList(ListNode head) {
        ListNode prev = null;
        ListNode current = head;
        while (current != null) {
            ListNode nextNode = current.next;
            current.next = prev;
            prev = current;
            current = nextNode;
        }
        return prev;
    }

    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        head.next = new ListNode(2);
        head.next.next = new ListNode(3);
        head.next.next.next = new ListNode(4);
        head.next.next.next.next = new ListNode(5);

        ListNode reversedList = reverseLinkedList(head);

        while (reversedList != null) {
            System.out.print(reversedList.val + " ");
            reversedList = reversedList.next;
        }
    }
}
Java Interview Question And Answers Pdf
Java Interview Question And Answers Pdf

Latest Post :-

Keyword
java interview question and answers pdf
java interview question and answer pdf
java interview question
java interview question book
java interview question pdf download
java interview question pdf free download
java interview question and answer for experienced
java interview question and answer for 3 years experience

Leave a Reply

Your email address will not be published. Required fields are marked *