Java Practice Questions with Input and Output
1. Print each character of a string along with its ASCII value.
Input: "abc"
Output:
a : 97
b : 98
c : 99
2. Convert a string to uppercase without using toUpperCase().
Input: "hello"
Output: "HELLO"
3. Count the number of words in a sentence.
Input: "This is a test."
Output: 4
4. Check if two strings are anagrams of each other.
Input: "listen", "silent"
Output: Yes
5. Print characters that appear only once in a string.
Input: "programming"
Output: p o a i n
6. Count the number of digits, alphabets, and special characters in a string.
Input: "abc123$%"
Output:
Digits: 3
Alphabets: 3
Special Characters: 2
7. Replace all spaces in a string with hyphens.
Input: "Hello World"
Output: "Hello-World"
8. Print the second highest occurring character in a string.
Input: "aabbccdddee"
Output: d
9. Swap the first and last characters of a string.
Input: "hello"
Output: "oellh"
10. Print all substrings of a string.
Input: "abc"
Output:
a
ab
abc
b
bc
c
11. Remove duplicate characters from a string without using HashSet or Collections.
Input: "programming"
Output: "progamin"
12. Reverse each word in a sentence.
Input: "Hello World"
Output: "olleH dlroW"
13. Check if a string contains only digits.
Input: "12345"
Output: Yes
14. Count the number of uppercase and lowercase letters in a string.
Input: "Hello World"
Output:
Uppercase: 2
Lowercase: 8
15. Print the first repeating character in a string.
Input: "swiss"
Output: s
16. Find the longest word in a sentence.
Input: "The quick brown fox jumps over the lazy dog"
Output: "jumps"
17. Check if two strings are rotations of each other.
Input: "abcd", "cdab"
Output: Yes
18. Count the frequency of each vowel in a string.
Input: "communication"
Output:
a: 1
e: 0
i: 2
o: 2
u: 1
19. Remove vowels from a string.
Input: "education"
Output: "dctn"
20. Print the difference between the count of even and odd digits in a string.
Input: "123456"
Output: 0
21. Find the largest element in an array.
Input: [3, 7, 2, 9, 5]
Output: 9
22. Find the smallest element in an array.
Input: [3, 7, 2, 9, 5]
Output: 2
23. Reverse an array.
Input: [1, 2, 3, 4, 5]
Output: [5, 4, 3, 2, 1]
24. Check if an array is sorted in ascending order.
Input: [1, 2, 3, 4, 5]
Output: Yes
25. Find the second largest element in an array.
Input: [3, 7, 2, 9, 5]
Output: 7
26. Remove duplicate elements from an array.
Input: [1, 2, 2, 3, 3, 4]
Output: [1, 2, 3, 4]
27. Count the frequency of each element in an array.
Input: [1, 2, 2, 3, 3, 3]
Output:
1: 1
2: 2
3: 3
28. Print only the even elements of an array.
Input: [1, 2, 3, 4, 5, 6]
Output: 2 4 6
29. Print only the odd elements of an array.
Input: [1, 2, 3, 4, 5, 6]
Output: 1 3 5
30. Swap the first and last elements of an array.
Input: [1, 2, 3, 4, 5]
Output: [5, 2, 3, 4, 1]
31. Print the sum of all even numbers in an array.
Input: [1, 2, 3, 4, 5, 6]
Output: 12
32. Print the sum of all odd numbers in an array.
Input: [1, 2, 3, 4, 5, 6]
Output: 9
33. Find the maximum difference between two elements in an array.
Input: [2, 3, 10, 6, 4, 8, 1]
Output: 8
34. Left rotate an array by one position.
Input: [1, 2, 3, 4, 5]
Output: [2, 3, 4, 5, 1]
35. Right rotate an array by one position.
Input: [1, 2, 3, 4, 5]
Output: [5, 1, 2, 3, 4]
36. Check if two arrays are equal.
Input: [1, 2, 3] and [1, 2, 3]
Output: Yes
37. Merge two sorted arrays into one sorted array.
Input: [1, 3, 5] and [2, 4, 6]
Output: [1, 2, 3, 4, 5, 6]
38. Find the missing number in an array containing 1 to n.
Input: [1, 2, 4, 5, 6]
Output: 3
39. Find the duplicate element in an array.
Input: [1, 2, 3, 2, 4]
Output: 2
40. Check if an array contains a subarray with sum zero.
Input: [4, 2, -3, 1, 6]
Output: Yes
41. Reverse a number.
Input: 1234
Output: 4321
42. Check if a number is a palindrome.
Input: 121
Output: Yes
43. Find the factorial of a number.
Input: 5
Output: 120
44. Check if a number is prime.
Input: 7
Output: Yes
45. Find the nth Fibonacci number.
Input: 6
Output: 8
46. Calculate the sum of digits of a number.
Input: 1234
Output: 10
47. Print all factors of a number.
Input: 12
Output: 1 2 3 4 6 12
48. Find the GCD (HCF) of two numbers.
Input: 12, 18
Output: 6
49. Find the LCM of two numbers.
Input: 4, 5
Output: 20
50. Check if a number is an Armstrong number.
Input: 153
Output: Yes
51. Print the sum of first n natural numbers.
Input: 5
Output: 15
52. Check if a number is a perfect number.
Input: 28
Output: Yes