Core Java Practice Lab: Cognizant Technology Solutions
Core Java Practice Lab: Cognizant Technology Solutions
1. Associates are supposed to clearly go through the program statement and understand the
requirements.
2. Associates should strictly develop the components as slated in the requirement
3. For Practice Lab, create the classes as needed for each problem statement
Create classes in the format given in the attachment Sample_Java_File.txt.
Note: While taking the test in Mettl, the class and the method definitions will be generated by Mettl
itself. You just need to add the code inside the method.
The main method creation and execution will be handled by Mettl itself.
2
Core Java Practice Lab 2012
1. Always check if there are any direct API methods available to solve the question easily
2. Use Collection.sort method if you want to sort an arraylist. In case of sorting arrays convert
the array and use Collection.sort method to sort it.
3. Converting the numeric data types to string may help to solve some problems for example
if you are asked to check if the first digits of two numbers are same convert the two
numbers to String and use the charAt() method to check it or if you want to reverse the
digits of a number etc.
4. If you are asked to remove the duplicate elements in an array. Convert it to set object. If the
array needs to be sorted order go for TreeSet
5. Try to use collection,string and wrapper APIs where ever possible.
6. While using any API methods just go through the other methods in the same API which may
help you in solving other problems
7. The Hints provided are just to help the associate solve the problem in the best way. You can
use your own algorithm/logic to solve the problem
Note: Hints, Logic and algorithm WILL NOT be provided in the actual assessment. Only
the problem statement will be given.
3
Core Java Practice Lab 2012
Exercise 1: Create a class with a method which can calculate the sum of first n natural numbers which
are divisible by 3 or 5.
Exercise 2: Create a class with a method to find the difference between the sum of the squares and
the square of the sum of the first n natural numbers.
(1^2+2^2+3^2+….9^2+10^2)-
(1+2+3+4+5…+9+10)^2
4
Core Java Practice Lab 2012
Exercise 3: Create a class containing a method to create the mirror image of a String. The method
should return the two Strings separated with a pipe(|) symbol .
5
Core Java Practice Lab 2012
Example 6: A school offers medals to the students of tenth based on the following criteria
If(Marks>=90) : Gold
Write a function which accepts the marks of students as a Hashmap and return the details of the
students eligible for the medals along with type of medal.
The input hashmap contains the student registration number as key and mark as value.
The output hashmap should contain the student registration number as key and the medal type as
value.
Example 7: Create a method which accepts a String and replaces all the consonants in the String with
the next alphabet.
6
Core Java Practice Lab 2012
Example 8: Create a method which accepts an array of integer elements and return the second smallest
element in the array
Example 9: Create a method which can perform the following operations on two String objects S1 and
S2. The output of each operation should be added to an arraylist and the arraylist should be
returned.(Assume S2 is of smaller size)
7
Core Java Practice Lab 2012
5. J***J***
Output:{“ VAAVAAVAAVAA”,”
JAVAJAAV”,” JAJAVA”,”
VJAVAJAVAA“,”J***J***“}
Example 10: Create a method that accepts a number and modifies it such that the each of the digit in
the newly formed number is equal to the difference between two consecutive digits in the original
number. The digit in the units place can be left as it is.
8
Core Java Practice Lab 2012
Example 11: Create a method which accepts the date of birth of person and date format and print the
day (SUNDAY, MONDAY…) on which he was born.
Example 12: You are asked to create an application for registering the details of jobseeker. The
requirement is:
Username should always end with _job and there should be atleast minimum of 8 characters to the left
of _job. Write a function to validate the same. Return true in case the validation is passed. In case of
validation failure return false.
Method Name validateUserName
Method Description Checks if the username is valid
Argument String userName
Return Type boolean
Logic Checks if the username ends with _job and
contains at least 8 characters to the left of _job. If
valid return true. Else return false.
9
Core Java Practice Lab 2012
Example 13: Create a method that can accept an array of String objects and sort in alphabetical order.
The elements in the left half should be completely in uppercase and the elements in the right half
should be completely in lower case. Return the resulting array.
Note: If there are odd number of String objects, then (n/2)+1 elements should be in UPPPERCASE
Example 14: Create a method which can remove a List from another List
10
Core Java Practice Lab 2012
Example 15: Create a method which can remove all the elements from a list other than the list of
elements specified.
Example 16: Create a method which accepts an array of numbers and returns the numbers and their
squares in an HashMap
Example 17: Create a method which accepts the id and the age of people as a Map and decide if they
are eligible for vote. A person is eligible for vote if his age is greater than 18. Add the IDs of all the
eligible persons to list and return the list. (Assume date is in DD/MM/yyyy format)
11
Core Java Practice Lab 2012
Example 18: Create a method which accepts an integer array, reverse the numbers in the array and
returns the resulting array in sorted order
Example 19: Create a method which accepts an integer array and removes all the duplicates in the array.
Return the resulting array in descending order
12
Core Java Practice Lab 2012
Example 20: Create a method that accepts a character array and count the number of times each
character is present in the array. Add how many times each character is present to a hash map with the
character as key and the repetitions count as value
Example 21: A String contains a list of states and capitals. Write a method which can parse the string
and return the states and capitals as map with state as key and capital as value.
The state and capital is separated by a delimiter (del1). There will be multiple state-capital pairs and
each state – capital pair is separated by another delimiter (del2).
13
Core Java Practice Lab 2012
Example 22: In a certain television game show, a couple is considered as a perfect couple if both the
husband’s and wife’s name contains the same set of characters. Each couple is provided with an ID.
Write a method which can accept a Hashmap with ID as key and the husband’s and wife’s name
separated with “-” as value. The method should generate the list of perfect couples based on the above
mentioned criteria and return their IDs as List object.
Example 23: Create a method which can perform a particular String operation based on the user’s
choice. The method should accept the String object and the user’s choice and return the output of the
operation.
Options are
14
Core Java Practice Lab 2012
Example 24: Create a method that accepts a String and checks if it is a positive string. A string is
considered a positive string, if on moving from left to right each character in the String comes after the
previous characters in the Alphabetical order.
For Example
Example 25: Create a method which accepts two Arraylist containing characters. Merge both arrays lists,
sort the elements in the resulting list and return the resulting array.
Example 26: Create a method that searches for a particular String in a List. If found, the element should
be replaced with a string having only half of the characters in the actual string
15
Core Java Practice Lab 2012
Logic Accept an arraylist and search for an element in
the list and replace with a string having only first
half of the characters in the actual string.
For Example if a search was done for APPLE and if
APPLE is found in the list, replace it with APP.
Return the modified list
Hint:
Iterate through list and find the index where the
String is present.
Take the first half of the String and set it at that
index in the arraylist. (Use set method)
Example 27: Create a method to find the sum of the first n even numbers that are divisible by 3 and 8
Example 28: Create a method to find the sum of the cubes of the digits of an n digit number
16
Core Java Practice Lab 2012
Example 29: Create a method which accepts a hash map and return the values of the map in sorted
order as a List.
Example 30: A company requires each employee to maintain a secret code. The secret code needs to
pass certain validation for getting accepted.
17
Core Java Practice Lab 2012
Example 31: Write a method to find the sum of the factorials of the first n numbers in the Fibonacci
series.
Example 32: A company transmits its String data over the network as encrypted data. The encryption
logic is as shown given below.
a a+9=j
dd+9=m
If on addition of 9 results in a char greater than z (ASCII value 122) do the encryption in a cyclic manner
starting from a. So if any character is ‘z’ it should be (z+9) which is equal to 127>122. In this case the
character would be 9 character starting from ‘a’ which ‘i’ so for adz the encrypted value should be adi
18
Core Java Practice Lab 2012
because in java the arithmetic operation
on character works on its ASCII value.
The ASCII value of ‘a’ is 97 and that of ‘z’ is
122.
Example 33: A sales company keeps track of the product purchased and sold. The company needs to
make sure that the sale date is always after the purchase date. Write a method to verify this
Example 34: A company used to keep the record of the employees in two different branches separately.
There are some employees who work in both the location. The company needs to keep track of the
employee working in both the branches. Write a method to accept the two lists containing the names of
the employees working in the two branches. The method should find out the names of employees
present in both the list and return the names as a sorted array
Example 35: In a school there are some teachers who handle two subjects (Maths and English). When
the feedback was taken their feedback was present in both Maths Feedback as well as English Feedback.
Write a method to create a consolidated feedback for the teachers for English and Maths. For those
taking both the subjects the highest feedback is taken. Write a method to accept two maps and return a
Map object containing the feedbacks of all teachers in maths and English.
19
Core Java Practice Lab 2012
Example 36: Write a method which can find the sum of the first n prime numbers. Prime numbers are
numbers which have only 1 and the number itself as its factors. 2 is the only even prime number. 1 is
neither prime nor composite.
Example 37: Write a method which accepts a String and moves all the lower case ‘a’ to the beginning of
the String.
20
Core Java Practice Lab 2012
Logic Hint :
1. Convert the string to a character array
2. Create a Stringbuffer object
3. Create a variable(count) to store the
number of ‘a’ present
4. Iterate over the character array and if the
character is ‘a’ increment count for ‘a’ else
add the character to the StringBuffer
object.
5. Finally insert the count number of ‘a’ to
the beginning of the StringBuffer object
Example 38: Write a method which can find the factors of a number. Factor is an integer which evenly
divides a number without leaving a remainder. Return the factors as an arraylist object.
Example 39: Write a method which can accept an integer and return the binary, hexadecimal and octal
equivalents of the number in a String array
21
Core Java Practice Lab 2012
Example 40: Write a method which accepts a double number and finds the sum of the digits to the left
and right of the decimal point. It should return the sum as String in the following format
For example
Input :120.520
Output: 3:7
Example 41: Write a method to validate the age of a person. The person age is considered valid if it is
above 21 years. Accept the date of birth of the person as String in date-month-year(Ex: 23-05-2012)
format and return true if the age is greater than 21.
Example 42: Write a method which can return the current date in any of the following date formats
based on the user choice
22
Core Java Practice Lab 2012
Choice 5: Return the current year
Example 43:
Consider two Hashmaps .First one containing the product name and product category code as key and
value respectively. Second HashMap contains the product name and the units sold. Write a java function
which accepts the two hash maps and return the names of products in each category which is having the
highest number of units sold.
For example
Output: {“pears”,”colgate”,”samsung”}
23
Core Java Practice Lab 2012
3. Obtain an iterator object(iterator1) to
iterate over the category names and
iterate over the set
4. Get first element in the set (category).
5. Declare two variables to maxSaleCount
and maxSaleProduct with initial values 0
and null.
6. Inside the iteration create another iterator
(iterator2) to iterate over the keys of the
salesDetails map.
7. Iterate iterator2 and get the first
element(product)
8. Check if the value for the key product in
the productDetails map is equal to
category. If found equal compare the sales
of the product with the maxCount value
and if the sales>maxCount set maxCount
as sales and maxSaleProduct as product.
9. Completing the iterator2 once will give
the product which was sold maximum in a
particular category
10. Completion of iterator1 gives the names
of the products which was sold maximum
in each categories.
Example 44: Write a method which accepts a number and return it in words.
24
Core Java Practice Lab 2012
Example 45: Consider two Hashmaps .First one containing the product name and product category code
as key and value respectively. Second HashMap contains the product name and price. Write a java
function which accepts the two hash maps , price hike rate and the product category and updates the
prices of the product in the entered category by the hike rate
For example
Input 3:10
Input4: “electronics”
25
Core Java Practice Lab 2012
Example 46: Write a method which can check whether an entered number is palindrome or not.
26