Tricky Java Questions For Practice
Tricky Java Questions For Practice

tricky java questions for practice – Java is an compiler language and there are lots of benefits of choosing java as a first language such as Java can be hard in beginning but at the end if you buildup good logics and concepts then it’ll be piece of cake for you to learn other languages like Python, JS, C, C++ and so on…

It is one of the most secured language among available language and lots of inbuild functions are also available which makes coding faster. It can build powerful OOPs concepts which plays very crucial role while building apps and web apps as well it gave platform independence where your apps can run on different Operating System’s environments.

Post you might like -> Essential Resources for Learning About Technology

tricky java questions for practice:

Lists of available tricky java questions for practice questions:

tricky java questions for practice:

  • Write a Java program to find the largest element in an array.
  • Implement a function to reverse a string in Java.
  • Create a program to check if a given number is prime or not.
  • Write a Java program to find the factorial of a number.
  • Implement a function to check if two strings are anagrams of each other.
  • Write a program to sort an array of integers using the bubble sort algorithm.
  • Implement a function to check if a given string is a palindrome.
  • Create a program to calculate the Fibonacci series up to a given number.
  • Write a Java program to find the second largest element in an array.
  • Implement a function to count the number of words in a string.
  • Write a program to calculate the sum of digits of a number in Java.
  • Implement a function to remove duplicates from an array in Java.
  • Create a program to convert a decimal number to binary.
  • Write a Java program to implement a stack.
  • Implement a function to merge two sorted arrays into one sorted array.
  • Write a program to find the GCD (Greatest Common Divisor) of two numbers.
  • Implement a function to find all pairs in an array that sum up to a specific target.
  • Write a Java program to find all prime numbers up to a given number.
  • Create a program to reverse a linked list in Java.
  • Implement a function to check if a number is a perfect number or not.
  • Write a program to check if a given string contains only digits.
  • Implement a function to find the median of two sorted arrays.
  • Write a Java program to implement a queue using two stacks.
  • Create a program to find the factorial of a large number using BigInteger in Java.
  • Implement a function to find the intersection of two arrays.
  • Write a program to print Pascal’s triangle.
  • Create a Java program to sort a linked list using merge sort.
  • Implement a function to find the longest palindrome substring in a string.
  • Write a Java program to check if a number is an Armstrong number.
  • Implement a function to check if a given string is a valid palindrome ignoring non-alphanumeric characters.
  • Write a program to perform matrix multiplication in Java.
  • Implement a function to find the maximum product of two integers in an array.
  • Create a Java program to find the number of vowels and consonants in a string.
  • Write a program to find the intersection of two linked lists.
  • Implement a function to rotate an array by k positions to the right.
  • Write a Java program to implement a binary search algorithm.
  • Create a program to convert a binary number to decimal.
  • Implement a function to find the first non-repeating character in a string.
  • Write a program to check if a given number is a perfect square.
  • Implement a function to check if a given number is a power of two.
  • Write a Java program to implement a depth-first search (DFS) algorithm.
  • Create a program to generate all permutations of a string.
  • Implement a function to find the maximum sum subarray in an array.
  • Write a program to count the number of occurrences of a character in a string.
  • Implement a function to find the k-th smallest element in an unsorted array.
  • Write a Java program to implement a breadth-first search (BFS) algorithm.
  • Create a program to find the missing number in an array of integers.
  • Implement a function to reverse words in a sentence without using any library method.
  • Write a program to find the duplicate elements in an array.
  • Implement a function to generate the nth Fibonacci number efficiently.

Solutions:

these are the solutions of tricky java questions for practice do try to solve one yourself before watching the answer and then you can improve.

1. Write a Java program to find the largest element in an array.

public class LargestElement {
    public static int findLargest(int[] array) {
        int largest = array[0];
        for (int i = 1; i < array.length; i++) {
            if (array[i] > largest) {
                largest = array[i];
            }
        }
        return largest;
    }

    public static void main(String[] args) {
        int[] array = {1, 2, 3, 4, 5};
        System.out.println("Largest element: " + findLargest(array));
    }
}

2. Implement a function to reverse a string in Java.

public class ReverseString {
    public static String reverse(String str) {
        String reversed = "";
        for (int i = str.length() - 1; i >= 0; i--) {
            reversed += str.charAt(i);
        }
        return reversed;
    }

    public static void main(String[] args) {
        String str = "Hello";
        System.out.println("Reversed string: " + reverse(str));
    }
}

3. Check if a given number is prime

public class PrimeCheck {
    public static boolean isPrime(int number) {
        if (number <= 1) return false;
        for (int i = 2; i <= Math.sqrt(number); i++) {
            if (number % i == 0) return false;
        }
        return true;
    }

    public static void main(String[] args) {
        int number = 29;
        System.out.println(number + " is prime: " + isPrime(number));
    }
}

4. Find the factorial of a number

public class Factorial {
    public static int factorial(int n) {
        if (n == 0) return 1;
        return n * factorial(n - 1);
    }

    public static void main(String[] args) {
        int number = 5;
        System.out.println("Factorial of " + number + " is: " + factorial(number));
    }
}

5. Implement a function to check if two strings are anagrams of each other.

import java.util.Arrays;

public class AnagramCheck {
    public static boolean areAnagrams(String str1, String str2) {
        if (str1.length() != str2.length()) return false;
        char[] array1 = str1.toCharArray();
        char[] array2 = str2.toCharArray();
        Arrays.sort(array1);
        Arrays.sort(array2);
        return Arrays.equals(array1, array2);
    }

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

6. Write a program to sort an array of integers using the bubble sort algorithm.

public class BubbleSort {
    public static void bubbleSort(int[] array) {
        int n = array.length;
        for (int i = 0; i < n - 1; i++) {
            for (int j = 0; j < n - i - 1; j++) {
                if (array[j] > array[j + 1]) {
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                }
            }
        }
    }

    public static void main(String[] args) {
        int[] array = {5, 3, 8, 4, 2};
        bubbleSort(array);
        System.out.println("Sorted array: " + Arrays.toString(array));
    }
}

7. Check if a given string is a palindrome

public class PalindromeCheck {
    public static boolean isPalindrome(String str) {
        int left = 0, right = str.length() - 1;
        while (left < right) {
            if (str.charAt(left) != str.charAt(right)) return false;
            left++;
            right--;
        }
        return true;
    }

    public static void main(String[] args) {
        String str = "racecar";
        System.out.println("Is palindrome: " + isPalindrome(str));
    }
}

8. Calculate the Fibonacci series up to a given number

public class FibonacciSeries {
    public static void fibonacci(int n) {
        int a = 0, b = 1, c;
        System.out.print(a + " " + b);
        for (int i = 2; i < n; i++) {
            c = a + b;
            System.out.print(" " + c);
            a = b;
            b = c;
        }
    }

    public static void main(String[] args) {
        int n = 10;
        System.out.print("Fibonacci series up to " + n + ": ");
        fibonacci(n);
    }
}

9. Find the second largest element in an array

public class SecondLargestElement {
    public static int findSecondLargest(int[] array) {
        int largest = Integer.MIN_VALUE;
        int secondLargest = Integer.MIN_VALUE;
        for (int i = 0; i < array.length; i++) {
            if (array[i] > largest) {
                secondLargest = largest;
                largest = array[i];
            } else if (array[i] > secondLargest && array[i] != largest) {
                secondLargest = array[i];
            }
        }
        return secondLargest;
    }

    public static void main(String[] args) {
        int[] array = {1, 2, 3, 4, 5};
        System.out.println("Second largest element: " + findSecondLargest(array));
    }
}

10. Count the number of words in a string

public class WordCount {
    public static int countWords(String str) {
        if (str == null || str.isEmpty()) {
            return 0;
        }
        String[] words = str.trim().split("\\s+");
        return words.length;
    }

    public static void main(String[] args) {
        String str = "Hello world, welcome to Java programming";
        System.out.println("Number of words: " + countWords(str));
    }
}

More tricky java questions for practice questions coming soon…

we will re update the content with full of tricky java questions for practice

Facebook vlearns
Facebook vlearns

Related Post

Leave a Reply

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