While attending interviews you should be ready with some sample program which they might ask because they want to see how you are applying logics for a specific program.
Today I am covering very basic program which simply check count number of occurrences of a word in a string.
Program to count number of occurrences of word in a string
The scenario is- You will be getting a specific String i.e Selenium Webdriver Online Tutorial and you need to search for e character and number of occurrence as well.
Approach-
1- First accept one String from user and Store in String variable.
2- Then accept char which you want to search in String
3- Take counter variable which will increment if match found
4- Finally print the counter value.
JAVA Program to count number of occurrences of word in a string
public class CountProgram { public static void main(String[] args) { // Create object of Scanner class to accept values Scanner scan=new Scanner(System.in); System.out.println("Please Enter the String here"); // It will accept String from user and will store in variable String str1=scan.next(); System.out.println("enter the latter to count"); // It will accept char from user and will store in variable String str2=scan.next(); // Now from Second String get the value using charAt method char cha = str2.charAt(0); // take count variable int count=0; // Run a for loop that will run based on String length for(int i=0;i<str1.length()-1;i++) { // This will check if match found if(str1.charAt(i)==cha) { // It will increment the counter count++; } } // Finally print the count System.out.println(count); } }
For above program, there can be many solutions, I have posted which I use generally if you have any other solution then please let me know so that I can add in this post.
Guys be ready with such type of questions and I will try to post as much as possible.If you are looking for any specific program the comment below.
Tahir says
Thanks, Sir !!!
Please post soon.
Mukesh Otwani says
Sure
Aamer Hussain says
I am preparing for interviews for Selenium profile. Facing issues with Java questions asked in interview.
Could you please help me with core java questions necessary for Selenium & also basic(sometimes tricky) java programs that are frequently asked in Selenium interviews. Thanksss a bunch again for your help!!
Mukesh Otwani says
Hey Aamer,
I dont have Java question but I will prepare and will upload soon.
surabhi says
hi Mukesh,
Plz upload basic java programs which be asked in a selenium interview.
Mukesh Otwani says
Hi Surabhi,
Sure will upload soon.
dinesh says
Hi Mukesh,
can you post all the basic java programs which is expected in the interviews.
Mukesh Otwani says
Hi Dinesh,
yes will upload soon.
usha says
is it uploaded, please share link
Mukesh Otwani says
I’ll upload it soon