Common interview questions on Java program usage in Selenium
Exp-1: Write program to display fibonacci series of numbers up to 100
0,1,1,2,3,5,8,13,21....each number is equals to the sum of the preceding two numbers,
this can start with zero or one
Script:
int a, b, c;
a= 0;
b= 1;
System.out.println(a);
System.out.println(b);
c= a+b;
while( c <= 100){
System.out.println(c);
a= b;
b= c;
c= a+b;
}
Exp-2: Write program to display Prime numbers between 1 to 20
A Prime number can be divided evenly only by 1 or itself
Script:
int cnt;
for (int n=1; n <= 20; n++){
cnt=0;
for(int i=1; i<= 20; i++){
if( n%i==0){
cnt++;
}
}
if(cnt==2){
System.out.println(n);
}
}
Exp-3: Write program to find number of occurrences of "s" in "SeleniumCourses"
String myVal="SeleniumCourses";
char myChar='s';
int n=0;
for(int i=0; i <= myVal.length()-1; i++){
if(myVal.charAt(i)== myChar){
n=n+1;
//n++;
}
}
System.out.println("Occurence of S in main string is: "+n);
}
Exp-4: Write program to find index of "s" in "SeleniumCourses"
String myVal="SeleniumCourses";
System.out.println(myVal.indexOf("s")); //5
Exp-5: Write program to find number of occurence of "s" in main string and also print each "s" position
String myVal="SeleniumCourses";
char myChar='s';
int n=0;
for(int i=0; i <= myVal.length()-1; i++){
if(myVal.charAt(i)== myChar){
n=n+1;
System.out.println("S-position is: "+i);
}
}
System.out.println("Occurence of S in main string is: "+n);
Exp-6: Write program to reverse the given string
String myVal="SeleniumCourses";
String str="";
for(int i=myVal.length()-1; i >=0; i--){
str=str+myVal.charAt(i);
}
System.out.println(str);
Exp-7: Write program to check given string is a palindrome or not
(reading the same backward as forward)
ex: refer
String myVal="refer";
String str="";
for(int i=myVal.length()-1; i >=0; i--){
str=str+myVal.charAt(i);
}
if(myVal.equals(str)){
System.out.println("it is a palindrome");
}
else{
System.out.println("it is not a palindrome");
}
---------------------------------------------------------------------------------------------------------
Exp-1: Write program to display fibonacci series of numbers up to 100
0,1,1,2,3,5,8,13,21....each number is equals to the sum of the preceding two numbers,
this can start with zero or one
Script:
int a, b, c;
a= 0;
b= 1;
System.out.println(a);
System.out.println(b);
c= a+b;
while( c <= 100){
System.out.println(c);
a= b;
b= c;
c= a+b;
}
Exp-2: Write program to display Prime numbers between 1 to 20
A Prime number can be divided evenly only by 1 or itself
Script:
int cnt;
for (int n=1; n <= 20; n++){
cnt=0;
for(int i=1; i<= 20; i++){
if( n%i==0){
cnt++;
}
}
if(cnt==2){
System.out.println(n);
}
}
Exp-3: Write program to find number of occurrences of "s" in "SeleniumCourses"
String myVal="SeleniumCourses";
char myChar='s';
int n=0;
for(int i=0; i <= myVal.length()-1; i++){
if(myVal.charAt(i)== myChar){
n=n+1;
//n++;
}
}
System.out.println("Occurence of S in main string is: "+n);
}
Exp-4: Write program to find index of "s" in "SeleniumCourses"
String myVal="SeleniumCourses";
System.out.println(myVal.indexOf("s")); //5
Exp-5: Write program to find number of occurence of "s" in main string and also print each "s" position
String myVal="SeleniumCourses";
char myChar='s';
int n=0;
for(int i=0; i <= myVal.length()-1; i++){
if(myVal.charAt(i)== myChar){
n=n+1;
System.out.println("S-position is: "+i);
}
}
System.out.println("Occurence of S in main string is: "+n);
Exp-6: Write program to reverse the given string
String myVal="SeleniumCourses";
String str="";
for(int i=myVal.length()-1; i >=0; i--){
str=str+myVal.charAt(i);
}
System.out.println(str);
Exp-7: Write program to check given string is a palindrome or not
(reading the same backward as forward)
ex: refer
String myVal="refer";
String str="";
for(int i=myVal.length()-1; i >=0; i--){
str=str+myVal.charAt(i);
}
if(myVal.equals(str)){
System.out.println("it is a palindrome");
}
else{
System.out.println("it is not a palindrome");
}
---------------------------------------------------------------------------------------------------------
No comments:
Post a Comment