0% found this document useful (0 votes)
4 views3 pages

Java Programming Practice Questions

The document contains a series of Java programming practice questions, each accompanied by input and output examples. It covers a variety of topics including string manipulation, array operations, and character counting. The questions are designed to test fundamental programming skills and concepts in Java.

Uploaded by

shieldxzero
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Java Programming Practice Questions

The document contains a series of Java programming practice questions, each accompanied by input and output examples. It covers a variety of topics including string manipulation, array operations, and character counting. The questions are designed to test fundamental programming skills and concepts in Java.

Uploaded by

shieldxzero
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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…)

Common questions

Powered by AI

To reverse an array in Java, you can use the two-pointer technique. Start one pointer at the beginning and the other at the end, then swap the elements at these pointers and move them towards each other until they meet. For example, reversing `[1, 2, 3, 4, 5]` results in `[5, 4, 3, 2, 1]`.

To find the second highest occurring character, build a frequency table for each character. Convert these frequencies into a sorted list and find the second last element (giving second highest). For the string 'aabbccdddee', 'd' appears as the second most frequently occurring character.

To check if an array is sorted in ascending order, iterate through the array and compare each pair of consecutive elements. If you find any instance where a prior element is greater than the following one, the array is not sorted. If no such pairs are found, the array is sorted. For example, `[1, 2, 3, 4, 5]` is sorted.

To count uppercase and lowercase letters in a string, iterate through each character and use the `Character.isUpperCase()` and `Character.isLowerCase()` to determine the case of each letter. For instance, in "Hello World", there are 2 uppercase and 8 lowercase letters.

To print each character of a string along with its ASCII value, you need to iterate over the string and use the charAt() method to get each character, then cast it to an int to obtain the ASCII value. For example, for input "abc", the output will be 'a : 97 b : 98 c : 99'.

To check if two strings are anagrams, you can sort the characters of both strings and compare them. If they are both the same length with identical sorted characters, they are anagrams. For example, 'listen' and 'silent' can be sorted to 'eilnst' in both cases, confirming they are anagrams.

Yes, it is possible to convert a string to uppercase without using the `toUpperCase()` method by iterating over each character, checking if it is lowercase, and converting it manually by subtracting 32 from the ASCII value of any lowercase letter to get its uppercase equivalent. For example, 'a' can be converted to 'A' by this method.

To determine if two strings are rotations of each other, concatenate one of the strings with itself and check if the other string is a substring of this new concatenated string. For example, 'abcd' and 'cdab' are rotations since 'cdab' is a substring of 'abcdabcd'.

An efficient way to remove duplicate characters without HashSet or Collections is to iterate through the string, using an additional data structure such as a boolean array to track characters that have been encountered. If a character appears for the first time, add it to the result. For example, 'programming' becomes 'progamin'.

To count the number of words in a sentence, one efficient approach in a Java program is to split the string by spaces using the `split(" ")` method, which will return an array of words, and then return the length of this array. For instance, input "This is a test." will result in an output of 4.

You might also like