Other Useful Links:
- Manual Testing Interview Questions (01-40)
- Manual Testing Interview Questions (41-80)
- Selenium With Java Interview Questions - (01-35)
- Selenium With Java Interview Questions - (36-70)
- Java Interview Questions for QA (01-50)
- Java Interview Questions for QA (51-100)
- TestNG Interview Questions for QA (01-20)
- TestNG Interview Questions for QA (21-40)
- Maven Interview Questions for QA Automation Interview (01-15)
- Maven Interview Questions for QA Automation Interview (16-30)
- Most Frequently Asked API Testing Interview Questions (01-20)
- Most Frequently Asked API Testing Interview Questions (21-40)
16. Reverse each word’s characters in string
Input: My Name is Ram | Output: yM emaN si maR
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Original string : ");
String originalStr = scanner.nextLine();
scanner.close();
String words[] = originalStr.split("\\s");
String reversedString = "";
for (int i = 0; i < words.length; i++)
{
String word = words[i];
String reverseWord = "";
for (int j = word.length() - 1; j >= 0; j--) {
reverseWord = reverseWord + word.charAt(j);
}
reversedString = reversedString + reverseWord + " ";
}
System.out.print("Reversed string : " + reversedString);
}
}
Output:
Original string : My Name is Ram
Reversed string : yM emaN si maR
17. Reverse the words in string
Input : java is very easy | Output : easy very is java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Original string : ");
String originalStr = scanner.nextLine();
scanner.close();
String words[] = originalStr.split("\\s");
String reversedString = "";
//Reverse each word's position
for (int i = 0; i < words.length; i++) {
if (i == words.length - 1)
reversedString = words[i] + reversedString;
else
reversedString = " " + words[i] + reversedString;
}
System.out.print("Reversed string : " + reversedString);
}
}
Output:
Original string : java is very easy
Reversed string : easy very is java
18. Java program to count vowels and consonants in String
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Input string : ");
String str = scanner.nextLine();
scanner.close();
str = str.toLowerCase();
int vCount = 0, cCount = 0;
for (int i = 0; i < str.length(); i++)
{
if (str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i' || str.charAt(i) == 'o' || str.charAt(i) == 'u') {
vCount++;
} else if (str.charAt(i) >= 'a' && str.charAt(i) <= 'z')
{
cCount++;
}
}
System.out.println("Number of vowels: " + vCount);
System.out.println("Number of consonants: " + cCount);
}
}
Output:
Input string : Java Programming
Number of vowels: 5
Number of consonants: 10
19. Remove extra white spaces between words
Input: how to do in java. com
Output: how to do in java. com
public class Main {
public static void main(String[] args) {
String blogName = "how to do in java ";
String nameWithProperSpacing = blogName.replaceAll("\\s+", " ");
System.out.println( nameWithProperSpacing);
}
}
Output:
how to do in java
20. Check if two strings are anagram or not
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
String s1="listen";
String s2="silent";
char[] arr1=s1.toCharArray();
char[] arr2=s2.toCharArray();
Arrays.sort(arr1);
Arrays.sort(arr2);
if(Arrays.equals(arr1,arr2)){
System.out.println("String is Anagram");
}else{
System.out.println("String is not Anagram");
}
}
}
Output:
String is Anagram
21. Java Program to find Second Largest Number in an Array
public class SecondLargestInArray{
public static int getSecondLargest(int[] a, int total){
int temp;
for (int i = 0; i < total; i++)
{
for (int j = i + 1; j < total; j++)
{
if (a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
return a[total-2];
}
public static void main(String args[]){
int a[]={13,24,54,62,32,21};
System.out.println("Second Largest: "+getSecondLargest(a,6));
}
}
Output:
Second Largest: 54
22. First non-repeated character in a string
input:javawj | output:v
class Test {
public static void main(String[] args) {
String str = "javawj";
System.out.println("Given string is: "+str);
for(int i=0;i<str.length();i++){
boolean isUnique=true;
for(int j=0;j<str.length();j++){
if(i!=j && str.charAt(i) == str.charAt(j)){
isUnique=false;
break;
}
}
if(isUnique){
System.out.println("\nFirst Non Repeated Character: "+str.charAt(i));
break;
}
}
}
}
Output:
Given string is: javawj
First Non Repeated Character: v
23. Binary Search In Java:
class BinarySearchExample{
public static void binarySearch(int arr[], int first, int last, int key){
int mid = (first + last)/2;
while( first <= last ){
if ( arr[mid] < key ){
first = mid + 1;
}else if ( arr[mid] == key ){
System.out.println("Element is found at index: " + mid);
break;
}else{
last = mid - 1;
}
mid = (first + last)/2;
}
if ( first > last ){
System.out.println("Element is not found!");
}
}
public static void main(String args[]){
int arr[] = {10,20,30,40,50};
int key = 30;
int last=arr.length-1;
binarySearch(arr,0,last,key);
}
}
Output:
Element is found at index: 2
24. Bubble Sort in Java
public class BubbleSortExample {
static void bubbleSort(int[] arr) {
int n = arr.length;
int temp = 0;
for(int i=0; i < n; i++){
for(int j=1; j < (n-i); j++){
if(arr[j-1] > arr[j]){
//swap elements
temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}
}
}
}
public static void main(String[] args) {
int arr[] ={3,60,35,2,45,320,5};
System.out.println("Array Before Bubble Sort");
for(int i=0; i < arr.length; i++){
System.out.print(arr[i] + " ");
}
System.out.println();
bubbleSort(arr);//sorting array elements using bubble sort
System.out.println("Array After Bubble Sort");
for(int i=0; i < arr.length; i++){
System.out.print(arr[i] + " ");
}
}
}
Output:
Array Before Bubble Sort
3 60 35 2 45 320 5
Array After Bubble Sort
2 3 5 35 45 60 320
25. Write a Java program to reverse a given string with preserving the position of spaces?
Public class ReverseStringPreserveSpace {
static void reverseString(String input) {
char[] inputArray = input.toCharArray();
char[] result = new char[inputArray.length];
for (int i = 0; i < inputArray.length; i++) {
if (inputArray[i] == ' ') {
result[i] = ' ';
}
}
int j = result.length - 1;
for (int i = 0; i < inputArray.length; i++) {
if (inputArray[i] != ' ') {
if (result[j] == ' ') {
j--;
}
result[j] = inputArray[i];
j--;
}
}
System.out.println(input + " --> " + String.valueOf(result));
}
public static void main(String[] args) {
reverseString("India Is my country");
}
}
Output:
India Is my country --> yrtnu oc ym sIaidnI
26. Pattern Program-I
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5
class Test {
public static void main(String[] args) {
for(int i=1;i<=5;i++){
for(int j=1;j<=5;j++){
System.out.print(i+" ");
}
System.out.println();
}
}
}
27. Pattern Program-II
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
class Test {
public static void main(String[] args) {
for(int i=1;i<=5;i++){
for(int j=1;j<=5;j++){
System.out.print(j+" ");
}
System.out.println();
}
}
}
28. Pattern Program-III
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
class Test {
public static void main(String[] args) {
for(int i=1;i<=5;i++){
for(int j=1;j<=i;j++){
System.out.print(j+" ");
}
System.out.println();
}
}
}
29. Pattern Program-IV
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
class Test {
public static void main(String[] args) {
for(int i=1;i<=5;i++){
for(int j=1;j<=i;j++){
System.out.print(i+" ");
}
System.out.println();
}
}
}
30. Pattern Program-V
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
public class Test {
public static void main(String[] args) {
int k=1;
for(int i=1;i<=5;i++){
for(int j=1;j<=i;j++){
System.out.print(k+" ");
k++;
}
System.out.println();
}
}
}
Greetings, reader! Your input is highly important to us. Please share your thoughts in the comments section below.
Contact:
Email: piyushagrawal.automation@gmail.com
Follow on LinkedIn: Piyush Agrawal - LinkedIn
Follow on YouTube: Piyush Agrawal - Youtube
No comments:
Post a Comment