JavaInterviewPrograms


Find a letter or String in a String:
String a = "santosh kumar Gaddam";
a.length();                
System.out.println(a.contains("kumar")); // find a word from given string using in-build method
for(int i=0; i
if(a.charAt(i) == 'n') {
System.out.println("found n at : " + i); //find a letter
break; //break the for loop
}
}

//Find a number in Array
int a[] = {1,2,3,4,5};
int find = 4;
for(int i=0; i
if(a[i]==find){
System.out.println("number found");
System.out.println("position of the number is : "+i);
break;
} else {
System.out.println("not found at : " + i);
}
}

//Find a Large number from an array.
int a[] = {10,1,3,8,50};
int large=a[0];
for(int i=0; i
if(a[i]>large) {
large = a[i];
}
}                
System.out.println("large number is : "+large);
//Find a Low/Minimum number from an array
int a[] = {10,1,3,8,50};
int low=a[0];
for(int i=0; i
if(a[i]
low = a[i];
}
}                
System.out.println("low number is : "+low);                

//Get the index of a 'n' number from an array
int a[] = {1,2,3,4,5,6,7,8,9,0};
int pos=7;
for(int i=0; i
if(a[i] == 7) {
System.out.println(i);
break; // use this break to exist the loop
}
}

//String Reverse
int itr = 0;
String str = "santosh";
String store[] = new String[strlen];
char c;
String temp;
for(int j=str.length()-1;j>0;j--) {                        
System.out.println(str.charAt(j));
c = str.charAt(j);
temp = Character.toString(c);
store[j] = temp;
itr++;
}

System.out.println(store.length);

for(int k=0; k
System.out.println(store[k]);
}
Or
 String str = "Saket Saurav";
  char chars[] = str.toCharArray();  // converted to character array and printed in reverse order
  for(int i= chars.length-1; i>=0; i--) {
        System.out.print(chars[i]);
  }

//Swap the values without using a third variable.
x=100;
y=200;

x=x+y; //300
y=x-y; //300-200=100
x=x-y; //300-100=200

//Count the repeated strings using HashMap
String str = "This this is is done by Saket Saket";
String[] strSplit = str.split(" ");
HashMap map = new HashMap<>();
for(int i=0; i
if(map.containsKey(strSplit[i])) {
int count = map.get(strSplit[i]);
map.put(strSplit[i], count+1);
} else {
map.put(strSplit[i], 1);
}
}
System.out.println(map);
Output: {Saket=2, by=1, this=1, This=1, is=2, done=1}

//Given Number Prime or not
int num = 83;
boolean primeRes = true;
for(int i=2; i
int res = num%i;
System.out.println(res);
if(res == 0){
primeRes = false;
break;
}                        
}
System.out.println(primeRes);

//Fibonaci series
int a,b,c;
a=0;
b=0;
c=1;
for(int i=0; i<10 i="" p="">
a = b;
b = c;
c = a+b;
System.out.println(c);
}

//Print ArrayList in 3 modes while, for and for with object
ArrayList al = new ArrayList<>();
al.add(10);
al.add(20);
al.add(30);
al.add(40);
Iterator it = al.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
for(int i=0; i
System.out.println(al.get(i));
}
for(Object i: al){
System.out.println(i);
}


// Find repeated/duplicate values from a string.
String name = "santoshkumargaddam";
char[] chr = name.toCharArray();
Set st = new HashSet<>();
for(int i=0; i
for(int j=i+1; j
if(chr[j] == chr[i]) {
st.add(Character.toString(chr[j]));
}
}
}
System.out.println(st);
Or use below program
String name = "santosh kumar gaddam";
char[] chr = name.toCharArray();
for(int i=0; i
for(int j=i+1;j
if(chr[i] == chr[j]){
System.out.println(chr[i]);
}
}
}

//Large Number from array and second large number from array.
int[] a = {1,4,1,6,3,2,1,9,2,3,8,0};
int largeNumber = 0;
int secondlargeNumber = 0;
for(int i=0; i
if(a[i] > largeNumber ) {
secondlargeNumber = largeNumber;
largeNumber = a[i];
} else if(a[i] > secondlargeNumber) {
secondlargeNumber = a[i];
}
}
System.out.println(largeNumber);
System.out.println(secondlargeNumber);                

// Remove the spaces and tabs from a given string
String name = "T hi is  Santo sh Kum ar Ga        ddam";
StringBuffer sb = new StringBuffer();
char[] chr = name.toCharArray();
for(int i=0; i < chr.length; i++){
if(chr[i] != ' ' && chr[i] != '\t'){
sb.append(chr[i]);
}                
}
System.out.println(sb);

//check the given number ArmStrong number not ?
int armstg = 153;
int temp = armstg;
int a,sum,n;
sum=0;
n=armstg;
while(n>0) {
a = n%10;
n = n/10;
sum = sum + (a*a*a);
}                
System.out.println(sum);
if(temp == sum) {
System.out.println("Arm Strong Number");
} else {
System.out.println("it's not a Arm String Number");
}

Comments

Popular posts from this blog

Handling Excel

Selenium-Revision