Java Pract
Java Pract
Find the odd digits in the number, add them and find if
the sum is odd or not.if even return -1, if odd return 1
2.A integer array is given as input. find the difference between each element.
Return the index of the largest element which has the largest difference gap.
input: {2,3,4,2,3}
logic: 2-3=1,3-4=1,4-2=2,2-3=1
2 is the max diff between 4 and 2,return the index of 4(2)
output:2
3. 4.Given two integer arrays. merge the common elements into a new array. find
the sum of the elements
input1:{1,2,3,4}
input2:{3,4,5,6}
logic:{3,4}
output:7
4. Given two arrayslist. retrieve the odd position elements form first input and even
position elemetns from second list. put it into the new arraylist at the same
positions from where they are retrieved from.(consider 0th position as even
position, and two lists are of same size)
input1:{12,1,32,3}
input2:{0,12,2,23}
output:{0,1,2,3}
5. Sum of fibonaaci series upto given input.
input:3
logic:1+1+2
output:4
6. Retrieve the odd numbers till given input number. add and subtract it
consecutively and return the result.
Input:9
Output:1+3-5+7-9=-3
7. Given date in dd-MM-yyyy format.return the month in full name format(January)
input:"23-01-2012"
output:January
8. Find the day(Friday) of a date input given as MM-dd-yyyy (format)
input:12-27-2012
output:Thursday
9. Two dates are given as input in "yyyy-MM-dd" format. Find the number of months
between the two dates
input1:"2012-12-01"
input2:"2012-01-03"
output:11
10. arraylist of string type which has name#mark1#mark2#mark3 format. retrieve
the name of the student who has scored max marks(total of three)
input:{"arun#12#12#12","deepak#13#12#12"}
output:Deepak
11. Two inputs of a string array and a string is received. The array shld be sorted in
descending order. The index of second input in a array shld be retrieved.
input1:{"ga","yb","awe"}
input2:awe
output:2
12.A time input is received as stirng. Find if the hour format is in 12 hour format. the
suffix am/pm is case insensitive.
input:"09:36 am"
output:true
13. Retrieve the palindorme-true number set from given number limit and return the
sum
input1:90
input2:120
logic:99+101+111
output:311
14. find the max length-word in a given string and return the max-length word. if
two words are of same length return the first occuring word of max-length
input:"hello how are you aaaaa"
output:hello(length-5)
16. HashMap with regnum as key and mark as value. find the avg of marks whose
key is odd.
input:{12:90,35:90,33:90,56:88}
output:avg of(90+90) =90
17. A integer input is accepted. find the square of individual digits and find their
sum.
input:125
output:1*1+2*2+5*5
18. Two input strings are accepted. If the two strings are of same length concat
them and return, if they are not of same length, reduce the longer string to size of
smaller one, and concat them
input1:"hello"
input2:"hi"
output:"lohi"
input1:"aaa"
input2:"bbb"
output:"aaabbb"
22. Get two integer arrays as input. Find if there are common elements in the
arrays. find the number of common elements.
input1: {1,2,3,4}
input2: {3,4,5,6}
output: 4(3,4,3,4)
23.Find if a given pattern appears in both the input strings at same postions.
input1: "hh--ww--"
input2: "rt--er--"
output: true(false otherwise)
24. Input a int array. Square the elements in even position and cube the elements in
odd position and add them as result.
input: {1,2,3,4}
output: 1^3+2^2+3^3+4^2
25. Check whether a given string is palindrome also check whether it has atleast 2
different vowels
input: "madam"
output: false(no 2 diff vowels)
28.An arraylist of Strings is given as input. The count of the String elements that are
not of size input2 string is to be returned.
input1: {"aaa","bb","cccc","d"}
input2: "bb"
output: 3("bb"-length:2)
29. An array of integers is given.The given element is removed new array is
returned.
input1:{10,10,20,30,76}
output: {20,20,76}(10 is asked to remove)
30. Find the number of days between two input dates
31.Enter yout name and return a string "print the title first and then comma and
then first letter of initial name.
32.Write a program to accept two dates and find the difference.
33.Write a Program that accepts a string and removes the duplicate characters.
34.Write a Program which accepts a hash map of integer as key and string as value
and print the value which has the highest value for key.
35.validate a password
i) there should be atleast one digit
ii) there should be atleast one of '#','@' or '$' .
iii)the password should be of 6 to 20 characters
iv) there should be more uppercase letter than lower case.
v) should start with an upper case and end with lower case
return 1 for true and -1 for false
36. find the average of the maximum and minimum number in an integer array
37.validate the ip address in the form a.b.c.d
where a,b,c,d must be between 0and 255
if validated return 1 else return 2
38. find the three consecutive characters in a string. if the string consists any three
consecutive characters return 1 else return -1
like AAAxyzaaa will return true.
39.Input a hashmap. Count the keys which are not divisible by 4 and return.
40.String encrption. replace the odd-index character with next character(if it is 'z'
replace with 'a'..if 'a' with 'b' as such), leave the even index as such. returmn the
encrypted string.
41.Retrieve the max from array which is in a odd-index
42. GIVEN A STRING 555-666-1234
DISPLAY AS 55-56-661-234?
43.input1="Rajasthan";
input2=2;
input3=5;
output=hts;
44.input1={1,2,3}
input2={3,4,5}
input3;+(union)
output:inp1+inp2
input1:{1,2,3,4,5}
input 2:{2,3,4,5}
input3=*(intersection)
output:inp1*inp2
INPUT1:{1,2,3,4,5}
INPUT2:{3,6,7,8,9}
INPUT3:-(MINUS)
output:inp1-inp2
45. input1=committment;
output=cmmttment;
c be the first index position
remove even vowels TAKING LAST INDEX AS -1
46. Retrieve the odd-position digits from input integer. Multiply them with their
index and return their sum.
47. Check if a given string is of specific format
(i). includes validation like first three characters are capital.
(ii). includes numbers in a specific format.
48.count no.of vowels in a given string
49. Return the number of days in a month, where month and year are given as
input
50. Retrieve the non-prime numbers within the range of a given input. Add-up the
non-prime numbers and return the result.
51. Retrieve the elements in a array, which is an input, which are greater than a
specific input number. Add them and find the reverse of the sum.
52. Input1=845.69, output=3:2;
Input1=20.789; output=2:3;
Input1=20.0; output=2:1;
output is in Hashmap format.
Hint: count the no of digits.
53. HashMap<String,String>
input1={mouse:100.2,speaker:500.6,Monitor:2000.23};
String[] input2={speaker,mouse};
Float output=600.80(500.6+100.2);
58. Getting the first and last n letters from a word where wordlength > 2n.
Ex: Input: california,3.
output: calnia.
65. input1={2,4,3,5,6};
if odd find square
if even find cube
finally add it
output1=208(4+16+27+125+36)
73. Return 1 if the last & first characters of a string are equal else
return -1. Consider case.
Eg: Input = "this was great"
Output= 1
HashMap<String,String> h2={abc:fail,efg:pass}
83. input1-List1-{apple,orange,grapes}
input2-List2-{melon,apple,mango}
output={mango,orange}
operation-- In 1st list remove strings starting with 'a' or 'g'
In 2nd list remove strings ending with 'n' or 'e'
Ignore case, return in string array
84. input---input1=1;
input2=4;
input3=1;
output1=4;
operation--- print the element which is not repeated
if all the inputs r different sum all inputs
input---input1=1;
input2=2;
input3=3;
output1=6;
89. String array will be given.if a string is Prefix of an any other string in that array
means count.
92. input="hello"
output="hlo"; Alternative positions...
String[] input={"01","01010","1000","10","011"}
output=3; count the strings having prefix"10","01" but "10","01" not included
96. Input1={ABX,ac,acd};
Input2=3;
Output1=X$d
101. Find the sum of the numbers in the given input string array
Input{2AA,12,ABC,c1a)
Output:6 (2+1+2+1)
Note in the above array 12 must not considered as such
i.e,it must be considered as 1,2
101. Create a program to get the hashmap from the given input string array where
the key for the hashmap
is first three letters of array element in uppercase and the value of hashmap is the
element itself
Input:{goa,kerala,gujarat}
[string array]
Output:{{GOA,goa},{KER,kerala},{GUJ,Gujarat}}
[hashmap]
output:N+w+Y+r+
package com.cognisant;
public class Newyork {
103. input:
Searchstring s1="GeniusRajkumarDev";
String s2="Raj";
String s3="Dev";
output:
Return 1 if s2 comes before s3 in searchstring else return 2
104. intput="xyzwabcd"
intput2=2;
input3=4;
output=bawz
105.
Remove the duplicate elements and print sum of even numbers in the array..
print -1 if arr contains only odd numbers
106) input1="abc2012345"
input2="abc2112660"
input 3=4
here "abc**" refers to customer id.
12345 refers to last month eb reading and 12660 refers to this month eb reading
find the difference between two readings and multiply it by input3
ie., output=(12660-12345)*4
package com.cognisant;
public class Summ {
public static void main(String[] args)
{
String input1="abc2012345";
String input2="abc2112660";
String s1;
String s2;
String con="abc";
s1=input1.substring(5,input1.length());
s2=input2.substring(5,input1.length());
int diff=Integer.parseInt(s2)-Integer.parseInt(s1);
int out=diff*4;
System.out.println(out);
}
}
107. Given array of intergers,print the sum of elements sqaring/cubing as per the
power of their indices.
answer= sum=sum+a[i]^i;
eg:input:{2,3,5}
Output:29
108. Given array of string check whether all the elements contains only digits not
any alaphabets.
if condn satisfied print 1 else -1.
Input:{"123","23.14","522"}
output:1
Input1:{"asd","123","42.20"}
output:-1
112. count the number of times the second word in second string occurs in first
string-case sensitive
115. Get all the numbers alone from the string and return the sum.
Input"123gif"
Output:6
117. Take the word with a max length in the given sentance
in that check for vowels if so count the no.of occurances !
Input 1="Bhavane is a good girl"
Output =3
Input 1="Bhavanee is a good girl"
Output =4
118. for the given string display the middle 2 value, case satisfies only if string is of
even length
ip - java
op av
119. Given an array int a[]. Add the sum at even indexes.do the same with odd
indexes.
if both the sum is equal return 1 or return -1.
120. no of days in a month in specific year