0% found this document useful (0 votes)
71 views7 pages

Java Sample Interview-Questions

This document lists 44 coding interview questions that are commonly asked. The questions cover a range of topics including string manipulation, array processing, data structures like HashMap, mathematical operations, date/time validation and more. Sample problems include checking for valid IP addresses, calculating nCr combinations, finding average of HashMap values, validating passwords based on rules and removing vowels from strings.

Uploaded by

Sahana Shavi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
71 views7 pages

Java Sample Interview-Questions

This document lists 44 coding interview questions that are commonly asked. The questions cover a range of topics including string manipulation, array processing, data structures like HashMap, mathematical operations, date/time validation and more. Sample problems include checking for valid IP addresses, calculating nCr combinations, finding average of HashMap values, validating passwords based on rules and removing vowels from strings.

Uploaded by

Sahana Shavi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 7

Most Asked Interview

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.

2.Number Validation in Java


Write a program to read a string of 10 digit number, check whether the string contains a
10 digit number in the format XXX-XXX-XXXX where 'X' is a digit.

3.Sum of Squares of Even Digits


Write a program to read a number, calculate the sum of squares of even digits (values)
present in the given number.

4.Fetching Middle Characters from String


Write a program to read a string of even length and to fetch two middlemost characters
from the input string and return it as string output.

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.

6.Validating Date Format


Obtain a date string in the format dd/mm/yyyy. Write code to validate the given date
against the given format.

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

8.Check Characters in a String


Write a program to read a string and to test whether first and last character are same.
The string is said to be valid if the 1st and last character are the same. Else the string is said to
be invalid.

9.Forming New Word from a String


Write a program to read a string and a positive integer n as input and construct a string
with first n and last n characters in the given string.

10.Array List Sorting and Merging


Write a code to read two int array lists of size 5 each as input and to merge the two
ArrayList, sort the merged ArrayList in ascending order and fetch the elements at 2nd, 6th and
8th index into a new ArrayList and return the final ArrayList.

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.

13.Removing vowels from String


Given a method with string input. Write code to remove vowels from even position in the
string.

14.Sum of Powers of elements in an array


Sum of Powers of elements in an array Given a method with an int array. Write code to
find the power of each individual element according to its position index, add them up and return
as output.

15.Difference between the largest and smallest elements in an array


Difference between largest and smallest elements in an array Given a method taking an
int array having size more than or equal to 1 as input. Write code to return the difference
between the largest and smallest elements in the array. If there is only one element in the array
return the same element as output.

16.Find the element position in a reversed string array


Given a method with an array of strings and one string variable as input. Write code to
sort the given array in reverse alphabetical order and return the portions of the given string in
the array.

17.Generate the series


Given a method taking an odd positive Integer number as input. Write code to evaluate
the following series: 1+3-5+7-9…+/-n.

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.

19.Calculate Electricity Bill


Given a method calculateElectricityBill() with three inputs. Write code to calculate the
current bill

20.Sum of Digits in a String


Write code to get the sum of all the digits present in the given string.
21.Check the valid Color Code
Write a program to read a string and validate whether the given string is a valid color
code based on the following rules:
- Must start with "#" symbol

- Must contain six characters after #

- It may contain alphabets from A-F or digits from 0-9

22.Three Digits Format


Write a program to read a string and check if the given string is in the format "CTS-XXX"
where XXX is a three-digit number.

23.Removing Keys from HashMap


Given a method with a HashMap<Integer, string> as input. Write code to remove all the
entries having keys multiple of 4 and return the size of the final hashmap.

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.

26.Sum of Common Elements in given arrays


Write a program to find out sum of common elements in given two arrays. If no common
elements are found print - “No common elements”.

26.Validating Input Password


Write a code get a password as string input and validate using the rules specified
below.
Apply following validations:
1. Minimum length should be 8 characters

2. Must contain any one of these three special characters @ or _ or #

3. May contain numbers or alphabets.

4. Should not start with special character or number

5. Should not end with special character

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.

29.Find the difference between Dates in months

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.

30.Sum of cubes and squares of elements in an array

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

32.Difference between two dates in days


Get two date strings as input and write code to find the difference between two dates in
days.

33.File Extension of a given file

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.

34.Find common characters and unique characters in string

Given a method with two strings as input. Write code to count the common and unique
letters in the two strings.
Note:

- Space should not be counted as a letter.

- Consider letters to be case sensitive. ie, "a" is not equal to "A".

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.

39.Average of Elements in Hashmap

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

40.Calculate Average – Hash Map


Write a method that accepts the input data as a hash map and finds out the avg of all
values whose keys are odd numbers.

41.Count Sequential Characters


Get a string as input and write code to count the number of characters that get repeated
3 times consecutively and return that count (ignore case). If no character gets repeated 3 times
consecutively return -1.

42.Length of the Largest Chunk


Write a program to read a string and find the length of the largest chunk in the string. If
there is no chunk print “No chunks” else print the length. NOTE: chunk is the letter which is
repeating 2 or more than 2 times.

43.Unique Characters in a string


Write a program that takes a string and returns the number of unique characters in the
string. If the given string does not contain any unique characters return -1

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]'

45.Odd Digit Sum


Write a program to input a String array. The input may contain digits and alphabets
(“de5g4G7R”). Extract odd digits from each string and find the sum and print the output. For
example, if the string is "AKj375A" then take 3+7+5=15 and not as 375 as a digit.

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.

48.Repeating set of characters in a string


Get a string and a positive integer n as input. The last n characters should repeat the
number of times given as second input. Write code to repeat the set of character from the given
string.

49. Reverse String


Write a Java Program to reverse a string without using String inbuilt function.

50. Swap Number


Write a Java Program to swap two numbers with using the third variable.

***--------------------------------------------------------------------------------------------***

You might also like