Java Assignment Help | Core Java Practice Examples | Realcode4you



In this blog we will learn lots of examples which improve your basic skills related to core java. Below the list of many questions example which you can do yourself to improve your coding skills:


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.


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 .


Exercise 4: Create a method to check if a number is an increasing number


Example 5: Create a method to check if a number is a power of two or not


Example 6: A school offers medals to the students of tenth based on the following criteria

If(Marks>=90) : Gold

If(Marks between 80 and 90) : Silver

If(Marks between 70 and 80) : Bronze

Note: Marks between 80 and 90 means  marks>=80 and marks<90

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.

Note: Consonant refers to all alphabets excluding vowels


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)


Examples for below statements are shown in the Logic part


1. Character in each alternate index of S1 should be replaced with S2

2. If S2 appears more than once in S1, replace the last occurrence of S2 in S1 with the reverse of S2, else return S1+S2

3. If S2 appears more than once in S1, delete the first occurrence of S2 in S1, else return S1

4. Divide S2 into two halves and add the first half to the beginning of the S1 and second half to the end of S1.

Note: If there are odd number of letters in S2, then add (n/2)+1 letters to the beginning and the remaining letters to the end. (n is the number of letters in S2)

5. If S1 contains characters that is in S2 change all such characters to *


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.


Note: Take the absolute value of the difference. Ex: 6-8 = 2


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.

Note: The output should be in upper case


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.


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


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)


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


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 String is in the below format.

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


Ex: Input will be tamilnadu||chennai-karanataka||bengaluru.

Here, || will be provided as del1 and - will be provided as del2.


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

A: Add the String to itself

B: Replace alternate positions with *

C: Remove duplicate characters in the String

D: Change alternate characters to upper case


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

ANT is a positive String (Since T comes after N and N comes after A)

APPLE is not positive since L comes before P in the alphabetical order.

The method should return true if the entered string is positive


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


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


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.

The validation rules are as given

1. The secret code should be six characters long

2. The first three characters should be cod (Use .startsWith method)

3. There should be at least one digit in the code (Use .isDigit)

4. The first character should always be an upper case letter(Use isUpperCase)

5. The code should contain only alphabets and digits.

6. The number of upper case letters should be greater than lower case letters.

Return true if the above validation is passed.


Example 31: Write a method to find the sum of the factorials of the first n numbers in the Fibonacci series.

Fibonacci series is given by 0 1 1 2 3 5 8……

Factorial for a number m is given by factorial= m*m-1*m-2….1


Example 32: A company transmits its String data over the network as encrypted data. The encryption logic is as shown given below.

For a String ad the logic is as given

a ----> a+9=j

d ---> d+9=m

So the encrypted word would be jm.

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


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.

Note: HashMap contains

 Key - Teacher’s name of type String

 Value – Feedback of type Integer


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.


Ex: the 1st 5 prime numbers are 2,3,5,7,11 and sum is 28


Example 37: Write a method which accepts a String and moves all the lower case ‘a’ to the beginning of the String.


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.


For Example 1, 2 and 4 are the factor of 4


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


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

Left side sum:Right side sum


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

Choice 1 : Month-date-year(Ex: 05-26-2012)

Choice 2: Date-month-year(Ex: 26-MAY-12)

Choice 3: Date/month/year(Ex: 26/05/2012)

Choice 4: Month/date/year(Ex: 05/26/2012)

Choice 5: Return the current year

Choice 6: Return the current month as (Ex: APR)

Choice7: Return the date 10 days after the sysdate

Choice 8: Return 10 days prior to sysdate


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

Input1 :{“lux”:”soap”,”colgate”:”paste”, ”pears”:”soap”,”sony”:”electronics”,”samsung”:”electronics”}

Input 2:{“lux”:1000,”colgate”:500,”pears”:2000,”sony”:100,” samsung”,600}

Output: {“pears”,”colgate”,”samsung”}


Example 44: Write a method which accepts a number and return it in words.

For Example 123 = One Two Three


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

Input1 :{“lux”:”soap”,”colgate”:”paste”, ”pears”:”soap”,”sony”:”electronics”,”samsung”:”electronics”}

Input 2:{“lux”:1000,”colgate”:500,”pears”:2000,”sony”:100,” samsung”,600}

Input 3:10

Input4: “electronics”

Output1: :{“lux”:1000,”colgate”:500,”pears”:2000,”sony”:110,” samsung”,660}


Example 46: Write a method which can check whether an entered number is palindrome or not.


If you are looking any project or programming assignment help then you can contact us at given below contact address:


contact us: contact@realcode4you.com


or


Send your requirement directly at here:


realcode4you@gmail.com

1,517 views0 comments