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

Java Interview Question And Answers Pdf ( Quiz -6 )

Find the Largest Element: Write a function that finds and returns the largest element in an array of integers.

public class FindLargestElement {
    public static int findLargestElement(int[] array) {
        if (array == null || array.length == 0) {
            throw new IllegalArgumentException("Array must not be empty or null");
        }

        int maxElement = array[0];
        for (int element : array) {
            if (element > maxElement) {
                maxElement = element;
            }
        }
        return maxElement;
    }

    public static void main(String[] args) {
        int[] array = {12, 5, 27, 8, 18};
        System.out.println("Largest Element: " + findLargestElement(array));
    }
}

Check 50+ JAVA Projects with Source Code

Check for Anagrams: Create a program or function that checks if two given strings are anagrams of each other (contain the same characters with the same frequencies).

import java.util.Arrays;

public class AnagramChecker {
    public static boolean areAnagrams(String str1, String str2) {
        char[] charArray1 = str1.replaceAll("[\\s]", "").toLowerCase().toCharArray();
        char[] charArray2 = str2.replaceAll("[\\s]", "").toLowerCase().toCharArray();
        Arrays.sort(charArray1);
        Arrays.sort(charArray2);

        return Arrays.equals(charArray1, charArray2);
    }

    public static void main(String[] args) {
        String word1 = "listen";
        String word2 = "silent";
        System.out.println("Are Anagrams: " + areAnagrams(word1, word2));
    }
}

Check 100+ JAVA Spring Boot Projects with Source Code

Calculate Fibonacci Sequence: Write a function to generate the Fibonacci sequence up to a specified limit. The Fibonacci sequence starts with 0 and 1, and each subsequent number is the sum of the two preceding ones.

image-25 Free Java Interview Question And Answers Pdf  [Quiz -6 ]
Java Interview Question And Answers Pdf
public class FibonacciSequence {
    public static void generateFibonacci(int limit) {
        int a = 0, b = 1;
        System.out.print(a + " " + b + " ");

        for (int i = 2; i < limit; i++) {
            int next = a + b;
            System.out.print(next + " ");
            a = b;
            b = next;
        }
    }

    public static void main(String[] args) {
        int sequenceLimit = 10; // Example limit
        generateFibonacci(sequenceLimit);
    }
}

Binary to Decimal Converter: Implement a program or function that converts a binary number (in string or integer form) to its decimal equivalent.

public class BinaryToDecimalConverter {
    public static int binaryToDecimal(String binary) {
        return Integer.parseInt(binary, 2);
    }

    public static void main(String[] args) {
        String binaryNumber = "1101"; // Example binary number
        System.out.println("Decimal Equivalent: " + binaryToDecimal(binaryNumber));
    }
}
image-61-e1697647455884 Free Java Interview Question And Answers Pdf  [Quiz -6 ]
Complete Java Course with Real Projects – Updategadh
Complete Java Course
cropped-Ug-logo-1-32x32 Free Java Interview Question And Answers Pdf  [Quiz -6 ]updategadh.com

Find Common Elements: Write a function that takes two arrays as input and returns a new array containing the common elements present in both arrays.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class FindCommonElements {
    public static Integer[] findCommonElements(int[] array1, int[] array2) {
        List<Integer> commonElements = new ArrayList<>();

        for (int element1 : array1) {
            for (int element2 : array2) {
                if (element1 == element2) {
                    commonElements.add(element1);
                    break;
                }
            }
        }

        return commonElements.toArray(new Integer[0]);
    }

    public static void main(String[] args) {
        int[] array1 = {1, 2, 3, 4, 5};
        int[] array2 = {3, 5, 7, 9};
        System.out.println("Common Elements: " + Arrays.toString(findCommonElements(array1, array2)));
    }
}

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
See also  Swift Interview Questions and Answers: A Comprehensive Guide

Post Comment