Practice 70 Java Search Programming Questions
Q1. Write a Java program to search an element in an array using linear search.
Input: [5, 2, 7, 9, 3] (Search for 7)
Expected Output: Element found at index 2
Q2. Write a Java program to implement binary search in a sorted array.
Input: [1, 3, 5, 7, 9] (Search for 5)
Expected Output: Element found at index 2
Q3. Write a Java program to search for a number in a 2D array using linear search.
Input: [[1, 4, 7], [3, 8, 2], [6, 5, 9]] (Search for 8)
Expected Output: Element found at [1][1]
Q4. Write a Java program to search for a string in an array of strings using linear search.
Input: ["apple", "banana", "cherry"] (Search for "banana")
Expected Output: Element found at index 1
Q5. Write a Java program to implement binary search in a sorted string array.
Input: ["apple", "banana", "cherry", "date"] (Search for "cherry")
Expected Output: Element found at index 2
Q6. Write a Java program to search for an element in an array using a recursive binary search.
Input: [1, 3, 5, 7, 9] (Search for 5)
Expected Output: Element found at index 2
Q7. Write a Java program to implement interpolation search in a sorted array.
Input: [10, 20, 30, 40, 50] (Search for 30)
Expected Output: Element found at index 2
Q8. Write a Java program to perform a linear search for the first occurrence of an element in an unsorted array.
Input: [3, 6, 8, 6, 9] (Search for 6)
Expected Output: Element found at index 1
Q9. Write a Java program to search for the smallest element in an array using linear search.
Input: [4, 9, 2, 5, 1]
Expected Output: Smallest element is 1
Q10. Write a Java program to search for the largest element in an array using linear search.
Input: [4, 9, 2, 5, 1]
Expected Output: Largest element is 9
Q11. Write a Java program to perform linear search for a key in a sorted array.
Input: [1, 3, 5, 7, 9] (Search for 7)
Expected Output: Element found at index 3
Q12. Write a Java program to search for an element in an array and return true if found, otherwise false.
Input: [2, 4, 6, 8] (Search for 5)
Expected Output: false
Q13. Write a Java program to perform a linear search on a list of strings and return the index of the string.
Input: ["java", "python", "ruby"] (Search for "python")
Expected Output: 1
Q14. Write a Java program to search for a number in an unsorted array and count its occurrences.
Input: [2, 3, 5, 2, 7, 2] (Search for 2)
Expected Output: Occurrences of 2: 3
Q15. Write a Java program to implement binary search in a descending order sorted array.
Input: [9, 7, 5, 3, 1] (Search for 5)
Expected Output: Element found at index 2
Q16. Write a Java program to search for the first occurrence of a number in an array using linear search.
Input: [8, 7, 8, 5, 6] (Search for 8)
Expected Output: First occurrence at index 0
Q17. Write a Java program to search for a string in an array and return its position using linear search.
Input: ["cat", "dog", "mouse"] (Search for "dog")
Expected Output: Found at index 1
Q18. Write a Java program to implement binary search on a sorted array of floating-point numbers.
Input: [1.2, 3.4, 5.6, 7.8] (Search for 5.6)
Expected Output: Element found at index 2
Q19. Write a Java program to search for an element in a 3x3 matrix using linear search.
Input: [[1, 2, 3], [4, 5, 6], [7, 8, 9]] (Search for 5)
Expected Output: Element found at [1][1]
Q20. Write a Java program to search for a character in a string using linear search.
Input: "programming" (Search for 'g')
Expected Output: Character found at index 3
Q21. Write a Java program to search for a number in an array and return the index using linear search.
Input: [5, 9, 2, 8, 7] (Search for 9)
Expected Output: Element found at index 1
Q22. Write a Java program to search for an element using a recursive linear search.
Input: [10, 20, 30] (Search for 20)
Expected Output: Element found at index 1
Q23. Write a Java program to implement ternary search on a sorted array.
Input: [1, 3, 5, 7, 9] (Search for 7)
Expected Output: Element found at index 3
Q24. Write a Java program to search for the smallest value in a sorted array using binary search.
Input: [2, 5, 7, 10]
Expected Output: Smallest value is 2
Q25. Write a Java program to implement binary search for a string in a sorted array.
Input: ["apple", "banana", "cherry"] (Search for "banana")
Expected Output: Element found at index 1
Q26. Write a Java program to search for an element using binary search in a sorted array of integers.
Input: [1, 3, 5, 7, 9] (Search for 3)
Expected Output: Element found at index 1
Q27. Write a Java program to search for a substring within a string using linear search.
Input: "Hello, World!" (Search for "World")
Expected Output: Substring found at index 7
Q28. Write a Java program to perform linear search and return the index of the last occurrence of a number.
Input: [1, 2, 3, 2, 5] (Search for 2)
Expected Output: Last occurrence at index 3
Q29. Write a Java program to search for a key in an array and return true if it exists, otherwise false.
Input: [4, 8, 12, 16] (Search for 8)
Expected Output: true
Q30. Write a Java program to search for a value in a list of strings and return the corresponding index.
Input: ["apple", "banana", "cherry"] (Search for "apple")
Expected Output: Found at index 0
Q31. Write a Java program to search for the second largest element in an array.
Input: [10, 20, 30, 40, 50]
Expected Output: Second largest element is 40
Q32. Write a Java program to search for an element in an unsorted array and return its position.
Input: [4, 8, 1, 9] (Search for 1)
Expected Output: Element found at index 2
Q33. Write a Java program to search for a number in an array using the jump search algorithm.
Input: [1, 3, 5, 7, 9] (Search for 7)
Expected Output: Element found at index 3
Q34. Write a Java program to search for a character in a string using indexOf().
Input: "hello" (Search for 'e')
Expected Output: 1
Q35. Write a Java program to search for a number in a sorted array and return its index using binary search.
Input: [1, 4, 5, 7, 9] (Search for 4)
Expected Output: Element found at index 1
Q36. Write a Java program to search for a number in a 2D array and return its position.
Input: [[1, 2], [3, 4]] (Search for 3)
Expected Output: Element found at [1][0]
Q37. Write a Java program to search for a value in an array and print "Element not found" if it doesn't exist.
Input: [3, 6, 9] (Search for 5)
Expected Output: Element not found
Q38. Write a Java program to search for the index of a specific number in an array and return -1 if not found.
Input: [4, 2, 6] (Search for 2)
Expected Output: 1
Q39. Write a Java program to implement search on a list of objects based on a specific field.
Input: [Person("John"), Person("Doe"), Person("Jane")] (Search for "Doe")
Expected Output: Found at index 1
Q40. Write a Java program to perform a linear search and return the index of the last matching element.
Input: [1, 3, 7, 5, 7] (Search for 7)
Expected Output: 4
Q41. Write a Java program to search for a key in a list of strings and return the number of occurrences.
Input: ["apple", "banana", "apple", "orange"] (Search for "apple")
Expected Output: 2
Q42. Write a Java program to search for an integer in an array and return "Not Found" if it doesn't exist.
Input: [12, 5, 7, 2] (Search for 3)
Expected Output: Not Found
Q43. Write a Java program to search for an element in a list of integers and return its index using a recursive approach.
Input: [1, 4, 6, 9] (Search for 9)
Expected Output: Element found at index 3
Q44. Write a Java program to search for a number in an array and return the total number of occurrences.
Input: [3, 3, 5, 3, 7] (Search for 3)
Expected Output: 3
Q45. Write a Java program to search for an element in an unsorted array and print the result.
Input: [5, 6, 9, 2] (Search for 6)
Expected Output: Found
Q46. Write a Java program to search for a number in a list of integers using recursive linear search.
Input: [2, 4, 6, 8] (Search for 6)
Expected Output: Element found at index 2
Q47. Write a Java program to search for an element in an array and print whether it is present or not.
Input: [1, 3, 5] (Search for 4)
Expected Output: Not Found
Q48. Write a Java program to search for the first occurrence of a number using a binary search.
Input: [1, 2, 2, 3, 4] (Search for 2)
Expected Output: Element found at index 1
Q49. Write a Java program to search for a number using binary search and return true if the element exists, otherwise false.
Input: [1, 3, 5, 7, 9] (Search for 5)
Expected Output: true
Q50. Write a Java program to search for an element in an array and return its index.
Input: [8, 2, 6] (Search for 2)
Expected Output: Element found at index 1
Q51. Write a Java program to search for the maximum element in an array.
Input: [5, 8, 3, 2]
Expected Output: Maximum element is 8
Q52. Write a Java program to search for a word in a sentence and return its position.
Input: "The quick brown fox" (Search for "fox")
Expected Output: Position of "fox" is 16
Q53. Write a Java program to search for an element using binary search in a sorted list.
Input: [10, 20, 30, 40] (Search for 20)
Expected Output: Element found at index 1
Q54. Write a Java program to search for the smallest number in an array using binary search.
Input: [10, 20, 30, 40]
Expected Output: Smallest number is 10
Q55. Write a Java program to search for a number and check if it is present in a list of numbers.
Input: [3, 7, 2, 8] (Search for 7)
Expected Output: Present
Q56. Write a Java program to perform linear search in a 3D array and return the index.
Input: [[[1, 2], [3, 4]], [[5, 6], [7, 8]]] (Search for 7)
Expected Output: Element found at [1][1]
Q57. Write a Java program to search for an element in a 1D array using binary search.
Input: [1, 3, 5, 7] (Search for 5)
Expected Output: Element found at index 2
Q58. Write a Java program to search for the first character in a string using a linear search.
Input: "java" (Search for 'a')
Expected Output: Character found at index 1
Q59. Write a Java program to search for the first occurrence of an element in an array.
Input: [2, 3, 2, 4] (Search for 2)
Expected Output: First occurrence at index 0
Q60. Write a Java program to perform a linear search on an array of integers and check if a number exists.
Input: [5, 9, 3, 7] (Search for 3)
Expected Output: Number found
Q61. Write a Java program to search for a key and return true if found in a list of integers.
Input: [1, 4, 7, 9] (Search for 7)
Expected Output: true
Q62. Write a Java program to search for an element using linear search and return its index.
Input: [5, 2, 8, 7] (Search for 8)
Expected Output: 2
Q63. Write a Java program to search for the second smallest element in an array.
Input: [1, 3, 2, 7, 5]
Expected Output: Second smallest element is 2
Q64. Write a Java program to search for a value in a 2D array and print its position.
Input: [[1, 2], [3, 4]] (Search for 4)
Expected Output: Element found at [1][1]
Q65. Write a Java program to search for the largest number in an array.
Input: [2, 8, 5, 3]
Expected Output: Largest number is 8
Q66. Write a Java program to search for an element using binary search on an array.
Input: [10, 20, 30, 40, 50] (Search for 40)
Expected Output: Element found at index 3
Q67. Write a Java program to search for the second largest element in a sorted array.
Input: [10, 20, 30, 40]
Expected Output: Second largest element is 30
Q68. Write a Java program to search for a word in an array of words.
Input: ["apple", "banana", "cherry"] (Search for "banana")
Expected Output: Found at index 1
Q69. Write a Java program to search for an element and print "Not found" if not present.
Input: [1, 2, 3] (Search for 4)
Expected Output: Not found
Q70. Write a Java program to perform binary search on a sorted string array and print its index.
Input: ["apple", "banana", "cherry"] (Search for "cherry")
Expected Output: Element found at index 2
Bikki Singh
Hi, I am the instructor of TechnoVlogs. I have a strong love for programming and enjoy teaching through practical examples. I made this site to help people improve their coding skills by solving real-world problems. With years of experience, my goal is to make learning programming easy and fun for everyone. Let's learn and grow together!