Below, I have attached the Java programs that were asked in the automation testing interview.
Program 01: Swapping Two Numbers
- The swapNumbers method takes two integer parameters a and b.
- Inside the method, we print the values of a and b before swapping.
- The swapping logic is implemented without using a temporary variable: Add a and b and store the result in a.Subtract b from a and store the result in b.Subtract the original value of b from the new value of a and store the result in a.
- Finally, we print the values of a and b after swapping.
Program 02: Reverse a Number
- We define a ReverseNumber class with a main method to execute the program.
- Inside the main method, we initialize the number variable with the number we want to reverse.
- We then call the reverse method and pass the number as an argument. The reversed number is stored in the reversedNumber variable.
- Finally, we print both the original and reversed numbers.
- The reverse method takes an integer num as input and returns the reversed number.
- Inside the reverse method, we use a while loop to extract digits from the input number one by one.
- For each digit extracted, we multiply the current value of reversed by 10 and add the extracted digit.
- We continue this process until the input number becomes zero, at which point we return the reversed number.
Program 03: Reverse a String
- We define a ReverseString class with a main method to execute the program.
- Inside the main method, we initialize the str variable with the string we want to reverse.
- We then call the reverse method and pass the str as an argument. The reversed string is stored in the reversedStr variable.
- Finally, we print both the original and reversed strings.
- The reverse method takes a string str as input and returns the reversed string.
- Inside the reverse method, we use a for loop to iterate through the characters of the input string in reverse order.
- For each character, we append it to a StringBuilder object in reverse order.
- After iterating through all the characters, we convert the StringBuilder object to a string using the toString method and return it.
Program 04: Palindrome Number
- We define a PalindromeNumber class with a main method to execute the program.
- Inside the main method, we initialize the number variable with the number we want to check for palindrome.
- We then call the checkPalindrome method and pass the number as an argument. The method returns true if the number is a palindrome, otherwise false.
- Finally, we print whether the number is a palindrome or not.
- The checkPalindrome method takes an integer num as input and returns a boolean value indicating whether the number is a palindrome.
- Inside the checkPalindrome method, we use a while loop to reverse the number and store it in the reversedNumber variable.
- We then compare the original number with the reversed number. If they are equal, the number is a palindrome, and we return true; otherwise, we return false.
Program 05: Palindrome String
- We define a PalindromeString class with a main method to execute the program.
- Inside the main method, we initialize the str variable with the string we want to check for palindrome.
- We then call the checkPalindrome method and pass the str as an argument. The method returns true if the string is a palindrome, otherwise false.
- Finally, we print whether the string is a palindrome or not.
- The checkPalindrome method takes a string str as input and returns a boolean value indicating whether the string is a palindrome.
- Inside the checkPalindrome method, we use two pointers, left and right, initialized to the start and end of the string, respectively.
- We iterate through the string from both ends towards the center, comparing characters at corresponding positions.
- If at any point the characters are not equal, we return false, indicating that the string is not a palindrome.
- If the loop completes without finding any mismatch, we return true, indicating that the string is a palindrome.