HandsOn Questions
HandsOn Questions
Sample Input 2:
ABCDEF
feCBAd
Sample Output 2:
ABCDEF
Assume that the maximum number of elements in the array is 20.
Sample Input 1:
4
2
5
1
4
Sample Output 1:
6
Sample Input 2:
3
1
1
1
Sample Output 2:
no even numbers
3. String Occurences
Obtain two strings from user as input. Your program should count the number of
occurences of second word of second sentence in the first sentence.
Return the count as output. Note - Consider case.
Include a class UserMainCode with a static method countNoOfWords that accepts two
string variables. The return type is the modified string.
Create a Class Main that would be used to accept two input strings and call the static
method present in UserMainCode.
Input and Output Format:
Input consists of two strings with a maximum size of 100 characters.
Output consists of a single string.
Refer the sample output for formatting specifications.
Sample Input 1:
abc bcd abc bcd abc abc
av abc
Sample Output 1:
4
Sample Input 2:
ABC xyz AAA
w abc
Sample Output 2:
0
4. ArrayList Manipulation
Write a program that performs the following actions:
1. Read 2n integers as input.
2. Create two array lists to store n elements in each array list.
3. Write a function generateOddEvenList that accepts these two array lists as input.
4. The function fetches the odd index elements from first array list and even index
elements from second array list and adds them to a new array list according to their
index.
5. Return the arraylist.
Include a class UserMainCode with the static method generateOddEvenList that accepts
two arraylists and returns an arraylist.
Create a Class Main that would be used to read 2n integers and call the static method
present in UserMainCode.
Note:
- The index of first element is 0.
- Consider 0 an even number.
- Maintain order in the output array list
Input and Output Format:
Input consists of 2n+1 integers. The first integer denotes the size of the arraylist, the next n
integers are values of the first arraylist, and the last n integers are values of the second
arraylist.
Output consists of a modified arraylist as per step 4.
Refer sample output for formatting specifications.
Sample Input 1:
5
12
13
14
15
16
2
3
4
5
6
Sample Output 1:
2
13
4
15
6
5. Mastering Hashmap
You have recently learnt about HashMaps, and in order to master them, you try and use
them in all of your programs.
Your trainer/teacher has given you the following exercise:
1. Read 2n numbers as input, where the first number represents a key and second
number represents a value. Both the numbers are of integer type.
2. Write a function getAverageOfOdd to find out average of all values whose keys are
represented by odd numbers. Assume the average is an integer and never a decimal
number. Return the average as output. Include this function in class UserMainCode.
Create a Class Main that would be used to read 2n numbers and build the hashmap. Call the
static method present in UserMainCode.
Input and Output Format:
Input consists of 2n+ 1 integers. The first integer specifies the value of n (essentially the
hashmap size). The next pair of n numbers denote the key and value.
Output consists of an integer representing the average.
Refer sample output for formatting specifications.
Sample Input 1:
4
2
34
1
4
5
12
4
22
Sample Output 1:
8
6. Anagram
Write a program to check whether the two given strings are anagrams.
Note: Rearranging the letters of a word or phrase to produce a new word or phrase using all
the original letters exactly once is called “Anagram."
Include a class UserMainCode with a static method “getAnagram” that accepts 2 strings as
arguments and returns an integer. The method returns 1 if the 2 strings are anagrams. Else,
it returns -1.
Create a Class Main that would get 2 Strings as input and call the static
method getAnagram present in the UserMainCode.
Input and Output Format:
Input consists of 2 strings. Assume that all characters in the string are lower case letters.
Output consists of a string that is either “Anagram” or “Not Anagram.”
Sample Input 1:
eleven plus two
twelve plus one
Sample Output 1:
Anagrams
Sample Input 2:
orchestra
carthorse
Sample Output 2:
Anagrams
Sample Input 3:
hello
world
Sample Output 3:
Not Anagrams
7. Retirement
Consider an input, HashMap, that contains key as the ID and DOB as value of employees.
Write a program to find out employees eligible for retirement. A person is eligible for
retirement if his or her age is greater than or equal to 60.
Assume that the current date is 01/01/2014.
Include a class UserMainCode with a static method “retirementEmployeeList” that accepts a
HashMap<String,String> as input and returns an ArrayList<String>. In this method, add the
Employee IDs of all the persons eligible for retirement to list and return the sorted list.
(Assume date is in dd/MM/yyyy format).
Create a Class Main that would get the HashMap as input and call the static
method retirementEmployeeList present in the UserMainCode.
Input and Output Format:
The first line of the input consists of an integer n that corresponds to the number of
employees.
The next 2 lines of the input consist of strings that correspond to the id and DOB of
employee 1.
The next 2 lines of the input consist of strings that correspond to the id and DOB of
employee 2.
and so on...
Output consists of the list of employee ids eligible for retirement in sorted order.
Sample Input :
4
C1010
02/11/1987
C2020
15/02/1980
C3030
14/12/1952
T4040
20/02/1950
Sample Output:
[C3030, T4040]
8. Kaprekar Number
Write a program to check whether the given input number is a Kaprekar number or not.
Note : A positive whole number “n” that has “d” number of digits is squared and split into
two pieces: a right-hand piece that has “d” digits and a left-hand piece that has remaining
“d” or “d-1” digits. If the sum of the two pieces is equal to the number, then “n” is a
Kaprekar number.
The Kaprekar number assign to output variable 1, else -1.
Example 1:
Input 1:9
9^2 = 81, right-hand piece of 81 = 1 and left hand piece of 81 = 8
Sum = 1 + 8 = 9, i.e., equal to the number. Hence, 9 is a Kaprekar number.
Example 2:
Input 1:45
Hint:
45^2 = 2025, right-hand piece of 2025 = 25 and left hand piece of 2025 = 20
Sum = 25 + 20 = 45, i.e. equal to the number. Hence, 45 is a Kaprekar number."
Include a class UserMainCode with a static method “getKaprekarNumber” that accepts an
integer argument and returns an integer. The method returns 1 if the input integer is a
Kaprekar number, else the method returns -1.
Create a Class Main that would get the an integer as input and call the static
method getKaprekarNumber present in the UserMainCode.
Input and Output Format:
Input consists of an integer.
Output consists of a single string that is either “Kaprekar Number” or “Not A Kaprekar
Number.”
Sample Input 1:
9
Sample Output 1:
Kaprekar Number
Sample Input 2:
45
Sample Output 2:
Kaprekar Number
Sample Input 3:
4
Sample Output 3:
Not A Kaprekar Number
9. Vowels
Based on a given string input, write a program to find the word that has the maximum
number of vowels. If two or more words have the maximum number of vowels, print the
first word.
Include a class UserMainCode with a static method “storeMaxVowelWord” that accepts a
string argument and returns the word containing the maximum number of vowels.
Create a Class Main that would get the String as input and call the static
method storeMaxVowelWord present in the UserMainCode.
Input and Output Format:
Input consists of a string. The string may contain both lower case and upper case letters.
Output consists of a string.
Sample Input :
What is your name?
Sample Output :
Your
Input and Output Format:
Input consists of 2n+2 integers. The first integer denotes the size of the arraylist, the next
“n” integers are values to the first arraylist, the next “n” integers are values to the second
arraylist, and the last input corresponds to that set operation type.
Output consists of a modified arraylist as per step 4.
Refer sample output for formatting specifications.
Sample Input 1:
3
1
2
3
3
5
7
+
Sample Output 1:
1
2
3
5
7
Sample Input 2:
4
10
9
8
7
2
4
6
8
*
Sample Output 2:
8
Sample Input 3:
4
5
10
15
20
0
10
12
20
-
Sample Output 3:
5
15
Input and Output Format:
Input consists of 1 integer and n strings. The first integer denotes the size of the arraylist,
and the next n strings are score patterns described above.
Output consists of a string with the name of the top scorer.
Refer sample output for formatting specifications.
Sample Input 1:
3
sunil-56-88-23
bindul-88-70-10
john-70-49-65
Sample Output 1:
john
Sample Output 1:
Appreciation
13. Adjacent Swaps
Write a program that accepts a string as a parameter and returns the string with each pair
of adjacent letters reversed. If the string has an odd number of letters, the last letter is
unchanged.
Include a class UserMainCode with a static method swapPairs that accepts a string. The
return type is a string with each pair of adjacent letters reversed.
Create a Class Main that would be used to accept two Input strings and call the static
method present in UserMainCode.
Input and Output Format:
Input consists of a string with maximum size of 100 characters.
Output consists of a single string.
Refer sample output for formatting specifications.
Sample Input 1:
forget
Sample Output 1:
ofgrte
Sample Input 2:
New York
Sample Output 2:
eN woYkr
14. Password
Based on a given string , write a program to find whether it is a valid password or not.
Validation Rule:
Atleast 8 characters
Atleast 1 number(1,2,3...)
Atleast 1 special character(@,#,%...)
Atleast 1 alphabet(a,B...)
Include a class UserMainCode with a static method “validatePassword” that accepts a
string argument and returns a boolean value. The method returns true if the password is
acceptable; otherwise, the method returns false.
Create a Class Main which would get a string as input and call the
staticmethod validatePassword present in the UserMainCode.
Input and Output Format:
Input consists of a String.
Output consists of a String that is either “Valid” or “Invalid”
Sample Input 1:
hello@1010
Sample Output 1:
Valid
Sample Input 2:
punitha3
Sample Output 2:
Invalid
A Company wants to give bonuses to its employees. You have been assigned as the
programmer to automate this process. You would like to showcase your skills by creating a
quick prototype. The prototype consists of the following steps:
1. Read Employee details from the User. The details would include id, DOB (date of
birth), and salary in the given order. The datatype for id, DOB, and salary is integer,
string, and integer respectively.
2. You decide to build two HashMaps. The first HashMap contains employee id as key
and DOB as value, and the second hashmap contains the same employee id as key
and salary as value.
3. An employee in the age range of 25 to 30 years (inclusive) should get bonus of 20%
of his salary and an employee in the range of 31 to 60 years (inclusive) should get a
bonus of 30% of his salary. Store the result in TreeMap, with Employee ID as key and
revised salary as value. Assume the age is calculated based on the date 01-09-2014.
(Typecast the bonus to integer).
4. Other Rules:
a. If Salary is less than 5000, store -100.
b. If the age is less than 25 or greater than 60, store -200.
c. “a” takes priority over “b,” i.e, if both a and b are true, then store -100.
5. You decide to write a function calculateRevisedSalary that takes the above
HashMaps as input and returns the TreeMap as output. Include this function in class
UserMainCode.
Create a Class Main that would be used to read employee details in step 1 and build the two
HashMaps. Call the static method present in UserMainCode.
Input and Output Format:
Input consists of employee details. The first number indicates the size of the group of
employees. The next three values indicate the employee id, employee DOB, and employee
salary. The Employee DOB format is “dd-mm-yyyy”
Output consists of a single string.
Refer sample output for formatting specifications.
Sample Input 1:
2
1010
20-12-1987
10000
2020
01-01-1985
14400
Sample Output 1:
1010
12000
2020
17280
Write a program to read two String variables in DD-MM-YYYY. Compare the two dates and
return the older date in 'MM/DD/YYYY' format.
Include a class UserMainCode with a static method findOldDate that accepts the string
values. The return type is the string.
Create a Class Main that would be used to accept the two string values and call the static
method present in UserMainCode.
Sample Input 1:
05-12-1987
8-11-2010
Sample Output 1:
12/05/1987
Create a Class Main that would be used to accept an integer array and call the static method
present in UserMainCode.
Input consists of n+1 integers, where n corresponds the size of the array, followed by n
integers.
Sample Input :
6
4
8
6
1
9
4
Sample Output :
4
[In the sequence 4 8 6 1 9 4, the maximum distance is 8 (between 1 and 9). The function
should return the index of the greatest of two. In this case, it is 9 (which is at index 4) and
output = 4.]
Create a Class Main that would be used to accept a string and call the static method present
in UserMainCode.
Sample Input 1:
ALD3245E
Sample Output 1:
Valid
Sample Input 2:
OLE124F
Sample Output 2:
Invalid
Input and Output Format:
Include a class UserMainCode with a static method getDay that accepts the string. The
return type (string) should be the day of the week.
Create a Class Main that would be used to accept Input string and call the static method
present in UserMainCode.
Sample Input 1:
07-13-2012
Sample Output 1:
Friday
Write a program to read a string containing date in DD-MM-YYYY format. Find the number
of days in the given month.
Create a Class Main that would be used to accept the string and call the static method
present in UserMainCode.
Sample Input 1:
12-06-2012
Sample Output 1:
30
Sample Input 2:
10-02-2012
Sample Output 2:
29
If his age is greater than 18, then print “eligible,” else print “not-eligible.”
Include a class UserMainCode with a static method getAge that accepts the string value. The
return type is the string.
Create a Class Main that would be used to accept the two string values and call the static
method present in UserMainCode.
Sample Input 1:
16/11/1991
Sample Output 1:
eligible
27. Book
Create a class named Author with the following private member variables.
name of type String
email of type String
gender of type String
The class Author implements Comparable interface.
Include appropriate getters and setters.
Include appropriate constructors.
Create a class named Book with the following private member variables
private String name;
private List<Author> authorList;
private double price;
private int qtyInStock = 0; // Default value --- 0
Include appropriate getters.
Include default constructor, 4-argument constructor, and a 3-argument constructor
(qtyInStock is set to 0).
Override the toString() method defined in the Object class to display the book details in the
format as shown in the sample output.
Input and Output Format:
Refer the sample input and output for formatting specifications.
All text in bold corresponds to input and the rest corresponds to output.
Sample Input and Output 1:
Enter the book name
Fundamentals of Algms
Enter the number of authors
3
Enter the author name
Horowitz
Enter the author email id
[email protected]
Enter the author's gender
Male
Enter the author name
Sahni
Enter the author email id
[email protected]
Enter the author's gender
Male
Enter the author name
Rajasekaran
Enter the author email id
[email protected]
Enter the author's gender
Male
Enter the book price
560
Is the book currently available? Type “Yes/No” (Not case sensitive)
No
Fundamentals of Algms authored by Horowitz Sahni Rajasekaran costs Rs.560.0: Not
Available
Sample Input and Output 2:
Enter the book name
Fundamentals of Algms
Enter the number of authors
3
Enter the author name
Horowitz
Enter the author email id
[email protected]
Enter the author's gender
Male
Enter the author name
Sahni
Enter the author email id
[email protected]
Enter the author's gender
Male
Enter the author name
Rajasekaran
Enter the author email id
[email protected]
Enter the author's gender
Male
Enter the book price
560
Is the book currently available? Type Yes/No (Not case sensitive)
Yes
Enter the number of books available
5
Fundamentals of Algms authored by Horowitz Sahni Rajasekaran costs Rs.560.0 : Available
You are trained in Core Java/C# training, so you decided to design an object-oriented
system. As a first step, you want to get employee information such as
First Name
Last Name
Mobile Number
Email Address
There is no higher limit to store the number of employee in the application. Get the number
of employees first before getting all the employee details.
All the employee details are collected and stored in ArrayList. For most of the reports, you
want to order the employees based on their first name. So, once you collect all the
employee details, print all of them sorted by their first name.
Note:
In case of Java, Use Java Format Specifier:
System.out.format("%-15s %-15s %-15s %-30s %-15s\
n","Firstname","Lastname","Mobile","Email","Address");
Sample Output:
Create Card class with attributes symbol and number. From the main method, collect details
of each card (symbol and number) from the user.
Collect all these cards in a set, since set is used to store unique values or objects.
Cards need to be compared with each other to identify whether both the cards are of same
symbol. For this, we need to implement equals method.
Once we collect all four different symbols, display the first occurrence of card details in
alphabetical order.
Problem Constraints :
Given the values of x, y, and z, write a program to compute Sam's profit percentage.
Hint:
For x=20, y=375 and z=33
Cost Price of 1 toy = 375/12 = Rs. 31.25
Selling Price of 1 toy = Rs.33
So gain = 33-31.25 = Rs.1.75
Profit % = 1.75/31.25*100 = 5.6%
Input Format:
Input consists of 3 integers: x, y, and z. x is number of dozens purchased. y is cost per dozen.
z is selling price per item.
Output Format:
Refer the Sample Input and Output for formatting details. The profit percentage needs to be
printed correct to 2 decimal places.
Sample Input and Output:
[All text in bold corresponds to input and the rest corresponds to output]
Enter the number of dozens of toys purchased
20
Enter the price per dozen
375
Enter the selling price of 1 toy
33
Sam's profit percentage is 5.60 percent
Once all the details are captured, print all the distinct symbols in alphabetical order from the
Map. For each symbol, print all the card details, number of cards, and their sum
respectively.