Java Programs IO Thread Collection (Trainees)
Java Programs IO Thread Collection (Trainees)
General
1. Write a program that accepts 10 student records (roll number and score) and prints them in
decreasing order of scores. In case there are multiple records pertaining to the same student, the
program should choose a single record containing the highest score. The program should be
capable of accepting a multi-line input. Each subsequent line of input will contain a student
record, that is, a roll number and a score (separated by a hyphen). The output should consist of
the combination of roll number and corresponding score in decreasing order of score.
INPUT to program
1001-40
1002-50
1003-60
1002-80
1005-35
1005-55
1007-68
1009-99
1009-10
1004-89
1009-99
1004-89
1002-80
1007-68
1003-60
1005-55
1001-40
2. Write a program which will accept a single pair of strings separated by a comma; the program
should calculate the sum of ASCII values of the characters of each string. The program should
then subtract the sum of the ASCII values of the second string from the sum of the ASCII values
of the first string.
123ABC,456DEF
Then the sum of the ascii values of the characters in '123ABC' is 348 and in '456DEF' it is 366.
The Difference between these numbers is 348 – 366 = 18
Calculate the total distance travelled by Kermit (in centimeters) for the provided number of hops.
Exactly 4 numbers of hops will be provided to the program (one number per line) as per the below
example.
4
6
3
5
Page |1
Compiled by: SASTRY KVSJV Java Programs
4. Write a program which will take the year (YYYY) and the numeric sequence of the month (0-11)
th
as its input. The program will return the day on which the 28 of that particular month and year
falls. The input can consist of two year-month combinations, one combination per line.
0 – Jan
1 – Feb
2 – March
and so on......
1999-5
Where 1999 is the year and 5 is the numeric sequence of the month (corresponding to June). The
program should display the day on which June 28, 1999 fell, and in this case the output will be
MONDAY. The output should be displayed in uppercase letters.
1999-5
1998-6
MONDAY
TUESDAY
5. Write a program which will accept three sentences (one sentence per line) and print the words
having Initial Caps within the sentences. Below is an example.
Here is an example. If the below three sentences are given to the program as input,
This is a Program
Coding test of Initial Caps
the program Will Test You
This
Program
Coding
Initial
Caps
Will
Test
You
6. Ross is an event organizer. He has received data regarding the participation of employees in two
different events. Some employees have participated in only one event and others have
participated in both events. Ross now needs to count the number of employees who have taken
part in both events. The records received by Ross consist of employee ids, which are unique.
Write a program that accepts the employee ids participating in each event (the first line relates to
Page |2
Compiled by: SASTRY KVSJV Java Programs
the first event and the second line relates to the second event). The program should print the
number of common employee ids in both the events.
Suppose the following input is given to the program, where each line represents a different event:
1001,1002,1003,1004,1005
1106,1008,1005,1003,1016,1017,1112
Now the common employee ids are 1003 and 1005, so the program should give the output as: 2
7. Write a program to display the current date and time in different formats.
8. Write a program to add n number of days and months to a given date and output the resultant
dates.
9. Write a program to convert a given string into a proper string.
Proper String Definition: Reduce all excess spaces between words to one. Convert the first
letter of each word into uppercase and remaining into lowercase.
10. Write a program to accept a sentence and a word. Print the occurrences of the word in the
sentence.
11. Modify #11 to print the occurrence of the word as a whole word
12. Suppose you give a dinner party for six guests, but your table seats only four. In how many ways
can four of the six guests arrange themselves at the table. Any of the six guests can sit in the first
chair. Any of the remaining five can sit in the second chair. Any of the remaining four can sit in the
third chair, and any of the remaining three can sit in the fourth chair. (The last two will have to
stand.) Write a program that calculates the number of possible arrangements for any number of
guests and any number of chairs. (Assume there will never be fewer guests than chairs.)
Class
1. Create a class called Car to hold the name of a car. Create another class called Person with
two private data members: Person name, and Car reference. Created object of Person and
copy its contents using Shallow and Deep copy methodologies. Write a program to test both
cases.
2. Create a single ton class and write a program to demonstrate what happens if more than one
instance is created for the class.
IO Stream
1. Write a program to get list of all files from a user selected folder.
2. Write a program to accept the file extension as a command line argument and filter the files
by file extensions and show the file names.
3. Sometimes we need to store the objects to the file system, and need to read them whenever
required. Write a program to store and read objects from a file.
4. Write a program to create and store properties into a property file dynamically?
5. Modify the above program to create and store property file as .XML file?
6. Write a program to show current file permissions and set file permissions as per user choice.
Page |3
Compiled by: SASTRY KVSJV Java Programs
7. Write a program to allow one thread continuously generates and writes numbers and the
other thread reads and calculates the average.
8. Write a program using RandomAccessFile class to read and write data to a file. Read or write
to a specific location in the file at the file pointer. Imagine the file as a large array of data that
have their own index.
9. Write a program that takes an input file and output file as command line arguments. Write the
input file content into output file and also display the content at the same time on the screen.
(E.g. tee command in UNIX)
10. Write a program to take a text file name from which it reads text line-by-line and replaces all
duplicate white spaces with single white spaces for removing all duplicate white spaces from
the text file. And finally this program writes the whole string to the file which is without any
duplicate white space. These all operations are performed by the program after checking
whether the given is existing or not. If the file does not exist then the whole operations are
denied otherwise it performs all the operations efficiently.
Multithreading
1. Write a program to display thread states
2. Write a program to demonstrate non-synchronized method using a common resource using
threads.
3. Modify the above program to demonstrate synchronized method using a common resource
using threads.
4. Write a program to demonstrate inter thread communication.
5. Write a program to demonstrate a simple dead lock while accessing the resource by multiple
threads.
6. Write a program to avoid dead lock while accessing the resource by multiple threads.
7. Write a program to display different threads initiated while executing a Java program without
user initiating any custom threads.
8. Write a simple Timer that can periodically print a timeout message.
9. Write a program message queue for queuing (i.e. storing messages) and consuming
messages (reading messages).
10. Write a simulation program for the fruit market. The farmer will be able to produce different
types of fruits (apple, orange, grape, and watermelon), and put them in the market to sell. The
market has limited capacity and farmers have to stand in a queue if the capacity is exceeded
to sell their fruits. Consumers can come to the market any time and purchase their desired
fruits; and if the fruits they want to buy runs out, they are willing to wait until the supply of that
kind is ready.
11. Write a program to simulate bank transactions against a particular account number. The
transactions may include Deposit or Withdrawal of the money. Make sure the implementation
is thread safe
Collection
Page |4
Compiled by: SASTRY KVSJV Java Programs
General - Advanced
1. How can I take a program that runs in a DOS shell and send the text that comes from the
shell program into a Java program where it can analyzed, etc.?
2. I'd like to know on which operating system java application is running on.
3. How do I find the list of all system properties?
4. Getting Information of All Available Time Zones
Time Zone: Time Zone is the specified part of the earth that holds the special standard of the
time i.e. termed as local time. Usually time zone is based on the GMT (Greenwich Mean
Time). Each and every part of the earth has categorized for holding it's own time zones that
had been fixed according to the rotation of the earth on it's own base.
5. Write a program to take user complete date of birth (year, month and day of month) and gives
the exact age which determines the years, months and days of the calculated age of the
person as on date. The program should be fully validated for any invalid year or month or day
for date of birth.
6. Write a program to make a zip file from only one file which is given by you. It takes a file to
make it into zip format and the zip file name which has to be created from the given file.
Finally, it makes the zip file. You can archive the file from the zip file by extracting, if you want.
7. Write a program to take a jar file name which has to be viewed. It shows the contents of the
given jar file without extracting. It also shows the jar file content, a default manifest file,
META-INF/MANIFEST.MF. The default manifest file is automatically placed in the archive in
the creation of the jar file.
Page |5