Java Sample Interview-Questions
Java Sample Interview-Questions
Coding Question
1.Check the Sum of Odd Digits of given number
Write a program to read a number, calculate the sum of odd digits (values) present in the
given number.
5.Reversing a Number
Write a program to read a positive number as input and to get the reverse of the given
number and return it as output.
7.Validate Time
Obtain a time string as input in the following format 'hh:mm am' or 'hh:mm pm'.
Write code to validate. It uses the following rules:
- It should be a valid time in 12 hrs format
- It should have case insensitive AM or PM
11.String Encryption
Given an input as String and write code to encrypt the given string using the following
rules and return the encrypted string:
1. Replace the characters at odd positions by the next character in the alphabet.
2. Leave the characters at even positions unchanged.
Note:
- If an odd position character is 'z' replace it by 'a'.
- Assume the first character in the string is at position 1.
12.Password Validation
Given a method with a password in string format as input. Write code to validate the
password using following rules:
- Must contain at least one digit
- must contain at least one of the following special characters @, #, $ # Length should
be between 6 to 20 characters.
18.String Concatenation
Write code to get two strings as input and If strings are of same length simply append
them together and return the final string. If given strings are of different length, remove starting
characters from the longer string so that both strings are of the same length then append them
together and return the final string.
24.Largest Element
Write a program to read an int array of odd length, compare the first, middle and the last
elements in the array and return the largest. If there is only one element in the array return the
same element.
25.nCr
Write a program to calculate the ways in which r elements can be selected from n
population, using nCr formula nCr=n!/r! (n-r)! where first input being n and second input being r.
27.ID Validation
Write a program to get two string inputs and validate the ID as per the specified format.
28.Remove Elements
Write a program to remove all the elements of the given length and return the size of the
final array as output. If there is no element of the given length, return the size of the same array
as output.
Given a method with two date strings in the yyyy-mm-dd format as input. Write code to
find the difference between two dates in months.
Write a program to get an int array as input and identify even and odd numbers. If the
number is odd get cube of it if number even gets square of it. Finally, add all cubes and squares
together and return it as output.
31.IP Validator
Write a program to read a string and validate the IP address. Print “Valid” if the IP
address is valid, else print “Invalid”.
Write a program to read a file name as a string and find out the file extension and return
it as output. For example, the file sun.gif has the extension gif.
Given a method with two strings as input. Write code to count the common and unique
letters in the two strings.
Note:
35.Initial Format
Write a program to input a person's name in the format "FirstName LastName" and
return the person name in the following format - "LastName, InitialOfFirstName".
36.Character cleaning
Write a program to input a String and a character, and remove that character from the
given String. Print the final string.
37.Vowel Check
Write a program to read a String and check if that String contains all the vowels. Print
"yes" if the string contains all vowels else print "no".
38.Swap Characters
Write a program to input a String and swap every 2 characters in the string. If size is an
odd number then keep the last letter as it is. Print the final swapped string.
Given a method with a HashMap<int, float> as input. Write code to find out avg of all
values whose keys are even numbers. Round the average to two decimal places and return as
output.
[Hint: If the average is 5.901, the rounded average value is 5.9 . It the average is 6.333, the
rounded average value is 6.33 . ]
44.Name Shrinking
Write a program that accepts a string as input and converts the first two names into dot-
separated initials and prints the output. The input string format is 'fn mn ln'. The output string
format is 'ln [mn's 1st character].[fn's 1st character]'
46.Unique Number
Write a program that accepts an integer as input and finds whether the number is
Unique or not. Print Unique if the number is “Unique”, else print “Not Unique”. Note: A Unique
number is a positive integer (without leading zeros) with no duplicate digits. For example 7, 135,
214 are all unique numbers whereas 33, 3121, 300 are not.
47.Color Code Validation
Give a String as color code as input and write code to validate whether the given string
is a valid color code or not.
Validation Rule: String should start with the Character '#'. Length of String is 7. It should
contain 6 Characters after '#' Symbol. It should contain Characters between 'A-F' and Digits '0-
9'. If String acceptable the return true otherwise false.
***--------------------------------------------------------------------------------------------***