Interview Question

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

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

Java Interview Questions and Answers PDF

Preparing for a Java interview becomes easier when you regularly practice programming problems. This Java Quiz – 7 covers five commonly useful coding problems related to matrices, arrays, numbers, and linked lists. Each question includes a Java implementation that can help freshers understand the logic and practice similar interview problems.

YT:- DecodeIT

1. Matrix Transpose

Question: Write a Java program or function that accepts a matrix and produces its transpose. In a transposed matrix, the original rows become columns and the original columns become rows.

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

2. Remove Duplicates from an Array

Question: Create a Java program or function that eliminates repeated values from an integer array and returns an array containing only unique elements.

The following implementation uses Java Streams and the distinct() operation to remove duplicate values before converting the result back into an integer array.

import java.util.Arrays;

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

3. Check for Perfect Number

Question: Write a function that determines whether a positive integer is a perfect number. A perfect number is a number whose proper divisors, excluding the number itself, add up exactly to that number.

The program begins with 1 as a divisor and checks possible divisor pairs up to the square root of the given number. It then compares the calculated divisor sum with the original value.

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;

        System.out.println("Is Perfect Number: " + isPerfectNumber(number));
    }
}

4. Find Missing Number

Question: Write a Java program or function that identifies the missing value from an array containing numbers from 1 through n, with one number absent. Here, n represents the length of the array.

The solution calculates the expected sum of the sequence and subtracts the sum of the values actually present in the array. The difference gives the missing number.

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};

        System.out.println("Missing Number: " + findMissingNumber(array));
    }
}

5. Reverse a Linked List

Complete Advance AI Topics: Click Here
SQL Tutorial:
Click Here

Question: Implement a function that reverses a singly linked list.

The implementation uses three references: the previous node, the current node, and the next node. Each link is redirected toward the previous node until the complete list has been reversed.

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;
        }
    }
}

Keywords: java interview questions and answers pdf, java interview questions, java interview question pdf, java coding interview questions, core java interview questions, java interview questions for freshers, java programming questions, java interview question and answer pdf

Source Code Available

Interested in This Project?

Get the complete source code for this project at a very affordable price — perfect for your portfolio, college submission, or learning. Message us on WhatsApp and we'll get back to you instantly!

Full source code included Step-by-step setup guide Instant delivery on WhatsApp Instant reply on WhatsApp
Chat on WhatsApp

We usually reply within a few minutes

Leave a Reply

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

Chat with us