Java Programming Practice Questions with Input & Output Examples
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"
■ 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 any 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.
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]
(…and continue up to question 52 with similar style…)