/** * Functions hooked into colormag_action_head action. * * @hooked colormag_head - 10 */ do_action( 'colormag_action_head' ); wp_head(); ?>

Java interview programming questions when you are attending the Android Interview

Here is the list of java programs that will help you to crack the java interview as well as android interview. 


1.Write a program to count the frequency of number 2 from the below given  array 

Public class FrequencyProgram
{
public static void main(String[ ] args)
{
Int count=0,x=2;
Int arr[ ]={1,2,2,2,3,4,5,6};

for(int i=0;i<arr.length;i++)
{
if(arr[i]==x)
{
count++;
}
}
System.out.print(count);
}
}

Output:

3

2.Write a program  to find the missing number from the given array

public class MissingNumberProgram
{
public static void main(String[ ] args)
{
int arr[ ]=new int[ ]{1,2,3,5,6,7};

int nSum=n(n+1)/2; //total sum of n numbers
int sum=0; //sum of numbers which is given in the array
for(int i=0;i<arr.length;i++)
{
sum=sum+arr[i];
}

int missingNumber=nSum-sum;
System.out.println(“Missing number=”+missingNumber);
}
}

Output:

Missing number=4

3.Write a program to find the frequency of each number in a given array

import java.util.*;
public class FrequencyOfNumber
{
public static void main(String[ ] args)
{
HashMap<Integer,Integer> hm=new HashMap<Integer,Integer>( );
int arr[ ]=new int[ ] {1,2,2,3,3,4,5,5};

for(int i=0;i<arr.length;i++)
{
if(hm.containsKey(arr[i])
{
hm.put(arr[i],hm.get(arr[i])+1);
}
else
{
hm.put(arr[i],1);
}
}
System.out.println(hm);
}
}

Output:

1=1

2=2

3=2

4=1

5=2

4.Write a program to print  This is a “book”

There is a simple way to print This is a “book”.
We will use \’’

System.out.println(“This is a book \”book\” “);

Output:
This is a “book”

5.Write a program to separate the numeric,alphabet and special symbols from the given input

Here we will use the two methods  Character.isDigit(a ) and Character.isAlphabets( a).

Where “a” is a character

public class SeparateNumericAlphabetsSysmbol
{
public static void main(String[ ] args)
{
String s=”suman12345@#$%”;

String number=””,character=””,symbol=””;

for(int i=0;i<s.length( );i++)
{
if(Character.isDigit(s.charAt(i))
{
number=number+s.charAt(i);
}
else if(Character.isAlphabet(s.charAt(i))
{
character=character+s.charAt(i);

}
else
{
symbol=symbol+s.charAt(i);

}
}
System.out.println(“Number= ”+number);
System.out.println(“Character= ”+character);
System.out.println(“Symbol=” +symbol);
}
}

Output:

Number=12345

Character=suman

Symbol=@#$%


sumankumar80

Hello Friends,I have written and developed this website because you can learn technology easily ,prepare for the placement and also for the any type of examination . By :Suman Kumar

Leave a Reply

Your email address will not be published. Required fields are marked *