String In Java Interview Questions And Answers Pdf
File Name: string in java interview questions and answers .zip
- Top 50 Java String Interview Questions and Answers
- Top 100 Java Interview Questions and Answers (Download PDF)
- Top 20 Java Interview Programs for Programming and Coding Interview
I would really, really, like to see the solution of O n time complexity for: 19 Write a program to find longest palindrome in a string.
For more java articles ,Click here to Subscribe JavaHungry. Java Hungry. Java developers tutorials and coding. String class needs no introduction. String class is one of the most used and very important class in java.
Top 50 Java String Interview Questions and Answers
In this tutorial, we have provided a big list of basic Java interview programs with actual logical code examples asked in Programming and Coding Interviews for freshers and experienced candidates. Important and basic Java programs that are generally asked in the technical round of Java and Automation Interviews.
This has now become a general practice by the interviewers to ask basic Java Programs in interviews rather than just focusing on theoretical aspects. For this, we have come up with an idea of listing down a few very important Java Programs along with the proper explanation of each program.
Moreover, we have also included the respective outputs which will give you a fair idea about how that program worked. The flow of the program and the concepts are properly explained wherever possible throughout this article. A list of the most popular Java Programming interview questions and answers are explained below and these questions will help you to clear any Automation Interview successfully.
Q 1 Write a Java Program to reverse a string without using String inbuilt function. The object of the string builder class str2 will be further used to append the value stored in the string variable str. Thereafter, we are using the inbuilt function of the string builder reverse and storing the new reversed string in str2. Finally, we are printing str2.
Q 2 Write a Java Program to reverse a string without using String inbuilt function reverse. Answer: There are several ways with which you can reverse your string if you are allowed to use the other string inbuilt functions.
In this method, we are initializing a string variable called str with the value of your given string. Then, we are converting that string into a character array with the toCharArray function. Thereafter, we are using for loop to iterate between each character in reverse order and printing each character. This is another method in which you are declaring your string variable str and then using Scanner class to declare an object with a predefined standard input object.
We have used nextLine which will read the input with the spaces between the words of a string. Thereafter, we have used a split method to split the string into its substrings no delimiter given here.
Finally, we have printed the string in reverse order using for loop. This is almost like method 2, but here we did not use the split method. We have used the scanner class and nextLine for reading the input string. Then, we have declared an integer length which has the length of the input string. Thereafter, we have printed the string in the reverse order using for loop. However, we have used the charAt index method which will return the character at any specific index.
After each iteration, the character will be concatenated to reverse the string variable. This program will accept the values of x and y through the command line when executed. A temp variable is also declared. Now, the logic of the program goes like this — we are assigning temp or third variable with the value of x, and then assigning x with the value of y and again assigning y with the value of temp.
So, after the first complete iteration, the temp will have a value of x, x will have a value of y and y will have a value of temp which is x. Q 4 Write a Java Program to swap two numbers without using the third variable.
Answer: Rest all things will be the same as the above program. Only the logic will change. Till here, x still has the sum of both x and y.
But y has the value of x. This will assign x with the value of y and vice versa. First of all, we have declared our string variable called str. Then we have used split function delimited by single space so that we can split multiple words in a string.
Thereafter, we have declared HashMap and iterated using for loop. Inside for loop, we have an if-else statement in which wherever at a particular position, the map contains a key, we set the counter at that position and add the object to the map. Note: The same program can be used to count the number of characters in a string. The size of the map can get using the size method. Thereafter, we have used a While loop for iterating through the map which contains one key-value pair for each element.
Keys and Values can be retrieved through getKey and getValue. Q 7 Write a Java Program to find whether a number is prime or not. Answer: Here, we have declared two integers temp and num and used Scanner class with nextInt as we have integer only. One boolean variable isPrime is set to true. Thereafter, we have used for loop starting from 2, less than half of the number is entered and incremented by 1 for each iteration.
Temp will have the remainder for every iteration. If the remainder is 0, then isPrime will be set to False. Q 8 Write a Java Program to find whether a string or number is palindrome or not. Answer: You can use any of the reverse string program explained above to check whether the number or string is palindrome or not.
What you need to do is to include one if-else statement. If the original string is equal to a reversed string then the number is a palindrome, otherwise not. Enter the number or String vijay reverse is:yajiv The number is not a palindrome. Q 9 Write a Java Program for the Fibonacci series. For Example 0,1,1,2,3,5,8,13,21………. In this program, we have used Scanner class again with nextInt discussed above. Initially, we are entering through command line the number of times the Fibonacci has to iterate.
We have declared integer num and initialized a,b with zero and c with one. Then, we have used for loop to iterate. The logic goes like a is set with the value of b which is 0, then b is set with the value of c which is 1. Then, c is set with the sum of both a and b. Enter the number of times 10 Fibonacci Series of the number is: 0 1 1 2 3 5 8 13 21 Then, we have used While Loop with an iterator. Whenever the iterator has next element, it will display that element until we reach the end of the list.
So it will iterate three times. Likewise, we have done for Advanced For Loop where we have created an object called obj for the ArrayList called list. Then printed the object. Thereafter, we have put the condition of For Loop where the iterator i is set to 0 index, then it is incremented by 1 until the ArrayList limit or size is reached.
Finally, we have printed each element using a get index method for each iteration of For Loop. Q 11 Write a Java Program to demonstrate an explicit wait condition check. We are not considering Fluent wait in this program. The implicit wait is those waits that are executed irrespective of any condition. In the below program, you can see that it is for Google Chrome and we have used some inbuilt methods to set the property, maximizing window, URL navigation, and web element locating.
If you see the last line of the code, we have passed window. If you want to scroll up then pass some value in arg1 if you want to scroll down then pass some value in arg2. Q 13 Write a Java Program to open all links of gmail. Once you have opened a website such as Gmail using get or navigate.
Starting ChromeDriver 2. Q 14 Write a Selenium code to switch to the previous tab. We see this as an important third party because we can achieve the different navigation within a browser and its tabs if you know the shortcut keys. The same thing can be achieved through the code as well. Observe the following code just after we see the instantiation of Robot class.
Q 15 Write a Java Program to find the duplicate characters in a string. Then, we have created a character array to convert our string variable to the character.
With the help of for loop, we are performing a comparison between different characters at different indexes. If two characters of consecutive index match, then it will print that character and the counter will be incremented by 1 after each iteration.
Q 16 Write a Java Program to find the second-highest number in an array. Here, we have two integers- the largest and second-largest. Both set to the first index of the element. Then, we have printed all the elements using for loop. Now, the logic is when the element at the 0th index is greater than the largest, then assign arr to largest and secondLargest to largest.
Again, if the element at the 0th index is greater than the secondLargest, then assign secondLargest to arr. This will be repeated for each iteration and ultimately after comparing or completing iterations up to array length will give you the secondLargest element. The given array is: 14 46 47 94 94 52 86 36 94 89 Second largest number is Largest Number is:
Top 100 Java Interview Questions and Answers (Download PDF)
String is not a keyword in java. String is a final class in java. There are two ways to create string objects in java. One is using new operator and another one is using string literals. The objects created using new operator are stored in the heap memory and objects created using string literals are stored in string constant pool.
Feel free to comment, ask questions if you have any doubt. Pages Home core java spring online courses thread java 8 coding sql books oop interview certification free resources best. There is not a single Java program out there which is not using String objects and that's why it's very important from the interview point of view as well. In this article, I am going to share 35 String-based questions from different Java interviews. It includes questions on java.
mideastjustice.org class are created. s1 and s3 are references to same object. Q Why Strings in Java are called as Immutable? Ans: In.
Top 20 Java Interview Programs for Programming and Coding Interview
In this tutorial, we have provided a big list of basic Java interview programs with actual logical code examples asked in Programming and Coding Interviews for freshers and experienced candidates. Important and basic Java programs that are generally asked in the technical round of Java and Automation Interviews. This has now become a general practice by the interviewers to ask basic Java Programs in interviews rather than just focusing on theoretical aspects. For this, we have come up with an idea of listing down a few very important Java Programs along with the proper explanation of each program.