HomeAutomation

Java for Testers Interview Questions And Answers – Part 2

Java for Testers Interview Questions And Answers – Part 2

Java for Testers – Interview Questions and Answers Part-2

1)  Write a Java program to print Floyd’s triangle?

public class FloydTriangle {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number of rows");
        int rows = sc.nextInt();
        printFloydTriangle(rows);
    }
    public static void printFloydTriangle(int n){
        int number = 1;
        for(int i=0;i<n;i++){
            for(int j=0;j<=i;j++){
                System.out.print(number +" ");
                number++;
            }
            System.out.println();
        }
    }
}

2) Write a Java program to find all the sub-string of given string?

public class FindSubString {
    public static void main(String[] args) {
        String name = "Selenium And Java Interview Questions";
        System.out.println(name.contains("Java"));         // true
        System.out.println(name.contains("java"));         // false
        System.out.println(name.contains("Interview"));    // true
        System.out.println(name.contains("questions"));    // false 
    }
}

3) Write a Java program to print the given string in reverse?

public class ReverseString {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter input string");
        String s1 = sc.next();
        String s2 = reverseString(s1);
        System.out.println("Reversed String is: "+s2);
    }
    public static String reverseString(String s){
        String rev="";
        char[] arr = s.toCharArray();
        for(int i=arr.length-1;i>=0;i--)
        rev = rev + arr[i];
        return rev;
    }
}

4) Write a Java program to check whether the given number is palindrome?

public class PalindromeNumber {
    public static void main(String[] args){
        int r,sum=0,temp;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a number");
        int n = sc.nextInt();
        temp=n;
        while (n > 0) {
            r = n%10;
            sum = (sum*10) + r;
            n=n/10;
        }
        if(temp==sum)
            System.out.println("Number is palindrome");
        else
            System.out.println("Number is not palindrome");
    }
}

5) Write a Java program to add two matrix?

public class AddTwoMatrix {
    public static void main(String args[]) {
        //creating two matrices
        int a[][] = {{1, 3, 4}, {2, 4, 3}, {3, 4, 5}};
        int b[][] = {{1, 3, 4}, {2, 4, 3}, {1, 2, 4}};
        //creating another matrix to store the sum of two matrices
        int c[][] = new int[3][3];
        //adding and printing addition of 2 matrices
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                c[i][j] = a[i][j] + b[i][j];
                System.out.print(c[i][j] + " ");
            }
            System.out.println();
        }
    }
}

6) Write a Java program to multiply two matrix?

public class MultiplyTwoMatrix {
 public static void main(String args[]) {
  //creating two matrices 
  int a[][] = {{1, 1, 1}, {2, 2, 2}, {3, 3, 3}};
  int b[][] = {{1, 1, 1}, {2, 2, 2}, {3, 3, 3}};
  //creating another matrix to store the multiplication of two matrices
  int c[][] = new int[3][3]; 
  //multiplying and printing multiplication of 2 matrices 
  for(int i = 0; i < 3; i++) {
   for(int j = 0; j < 3; j++) {
    c[i][j] = 0; 
   for(int k = 0; k < 3; k++) {
    c[i][j] += a[i][k] * b[k][j];
   } 
   System.out.print(c[i][j] + " ");
  } 
  System.out.println();
 }
}

7) Write a Java program to get the transpose of matrix?

public class TransposeMatrix{
    public static void main(String args[]){
        //creating a matrix
        int original[][]={{1,3,4},{2,4,3},{3,4,5}};
        //creating another matrix to store transpose of a matrix
        int transpose[][]=new int[3][3];  //3 rows and 3 columns
        //Code to transpose a matrix
        for(int i=0;i<3;i++){
            for(int j=0;j<3;j++){
                transpose[i][j]=original[j][i];
            }
        }
        System.out.println("Printing Matrix without transpose:");
        for(int i=0;i<3;i++){
            for(int j=0;j<3;j++){
                System.out.print(original[i][j]+" ");
            }
            System.out.println();
        }
        System.out.println("Printing Matrix After Transpose:");
        for(int i=0;i<3;i++){
            for(int j=0;j<3;j++){
                System.out.print(transpose[i][j]+" ");
            }
            System.out.println();
        }
    }
}

8) Write a Java program to compare two strings?

public class CompareTwoStrings {
    public static void main(String[] args){

        Scanner sc = new Scanner(System.in);
        System.out.println("Enter first string");
        String first = sc.next();
        System.out.println("Enter second string");
        String second = sc.next();
        compare(first,second);
    }
    public static void compare(String s1, String s2){

        if(s1.compareTo(s2)==0) {
            System.out.println("Strings are equal");
        } else {
            System.out.println("Strings are not equal");
        }
    }
}

9) How to find whether a String ends with a specific character or text using Java program?

public class StringEndWith{
 public static void main(String args[]) {
  String s1 = "Java is a programming language";
 //Check if string ends with particular character 
 boolean endsWithCharacter = s1.endsWith("e");
 System.out.println("String ends with character 'e': " + endsWithCharacter);
 //Check if string ends with particular text 
 boolean endsWithText = s1.endsWith("java");
 System.out.println("String ends with String 'lang': " + endsWithText);
 }
}

10) Write a Java program to demonstrate indexOf()?

public class IndexOfExample{
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the input string:");
        String inputString = sc.nextLine();
        System.out.println("Enter the sub string:");
        String subString = sc.nextLine();
        int index = inputString.indexOf(subString);
        System.out.println("Index of sub string is: " + index);
    }
}

11) Write a Java program to demonstrate how to replace a string with another string?

public class ReplaceString{
 public static void main(String args[]) {
 String originalString = "Java for dummies";
 String newString = originalString.replace("dummies","experts");
 System.out.println("Original string is: " + originalString);
 System.out.println("New String is: " + newString);
 }
}

12) Write a Java program to split the given string?

class SplitString{
    public static void main(String []args){
        String strMain = "Java,C,Python,Perl";
        String[] arrSplit = strMain.split(",");
        for (int i=0; i < arrSplit.length; i++)
        {
            System.out.println(arrSplit[i]);
        }
    }
}

13) Write a Java program to remove the spaces before and after the given string?

class RemoveSpacesInString{
    public static void main(String []args){
        String s1 = "Interview Questions for Java";
        String newString = s1.replaceAll("\\s","");
        System.out.println("Old String: " + s1);
        System.out.println("New String: " + newString);
    }
}

14) Write a Java program to convert all the characters in given string to lower case?

class ConvertToLowerCase{
    public static void main(String []args){
        String s1 = "Interview QUESTIONS";
        String newString = s1.toLowerCase();
        System.out.println("Old String: " + s1);
        System.out.println("New String: " + newString);
    }
}

15) Write a Java program to demonstrate creating a method?

public class CreateMethodExample {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number");
        int num = sc.nextInt();
        reverseNumber(num);
    }
    public static void reverseNumber(int number){
        int reverse = 0;
        while(number!=0){
            int digit = number % 10;
            reverse = reverse * 10 + digit;
            number = number/10;
        }
       System.out.println("Reversed number " + reverse);
    }
}

16) Write a Java program to find the length of the given string?

class FindLength{
    public static void main(String []args){
        String s1 = "Interview Questions In Java";
        int length = s1.length();
        System.out.println("Length of string is: " + length);
    }
}

17) Write a Java program to concatenate the given strings?

class StringConcatenation{
    public static void main(String []args){
        String s1 = "Interview Questions In Java";
        String s2 = " And Selenium";
        String s3 = s1.concat(s2);
        System.out.println("After concatenation: "+ s3);
    }
}

18) Write a Java program to replace a string?

class ReplaceString{
    public static void main(String []args){
        String s1 = "Interview Questions In Java";
        String s2 = "Answers";
        String s3 = s1.replace("Questions","Answers");
        System.out.println("Original String: "+ s1);
        System.out.println("New String: "+ s3);
    }
}

19) Write a Java program to demonstrate a Static block?

class StaticTest { 
    static int i; 
    int j; 
    // start of static block  
    static { 
        i = 10; 
        System.out.println("static block called "); 
    } 
    // end of static block  
}
class Main { 
    public static void main(String args[]) { 
        System.out.println(Test.i);  
    } 
}

20) Explain the difference between static and instance methods in Java?

Instance method are methods which require an object of its class to be created before it can be called. Static methods are the methods in Java that can be called without creating an object of class.

21) Write a Java program to demonstrate creating multiple classes?

public class A { 
    public static void print() { 
        System.out.println("This is a method");  
    } 
}
public class B { 
    public static void main(String args[]) { 
        print();  
    } 
}

22) Write a Java program to demonstrate creating a constructor?

class ConstructorTest { 
    ConstructorTest(){ 
        System.out.println("Constructor called"); 
    }    
} 
class Main 
{ 
    public static void main (String[] args) 
    { 
        ConstructorTest test = new ConstructorTest();
    } 
}

23) Write a Java program to demonstrate constructor overloading?

class Box 
{ 
    double width, height, depth; 
    int boxNo;  
    Box(double w, double h, double d, int num) 
    { 
        width = w; 
        height = h; 
        depth = d; 
        boxNo = num; 
    } 
    Box() 
    { 
        width = height = depth = 0; 
    } 
    Box(int num) 
    { 
        this(); 
        boxNo = num; 
    } 
    public static void main(String[] args) 
    { 
        Box box1 = new Box(1); 
        System.out.println(box1.width); 
    } 
}

24) Write a Java program to demonstrate Exception Handling?

public class ExceptionExample{  
  public static void main(String args[]){  
   try{  
      //code that may raise exception  
      int data=100/0;  
   }
   catch(ArithmeticException e)
   {System.out.println(e);}   
  }  
}

25) Write a Java program to demonstrate throwing an exception?

class ThrowExceptionExample  
{ 
    public static void main(String[] args)throws InterruptedException 
    { 
        Thread.sleep(10000); 
        System.out.println("Hello World"); 
    } 
}

 

Next Steps:

> More interview questions and answers on Selenium Java, continue to the next post (Click on Next Post link below)

> Check complete Selenium Java interview questions and answers here (Click here)

Please leave your questions/comments/feedback below.

Happy Learning ?

About Me > Bijan Patel

Connect to me on Linked In (Click here)

On a mission to contribute to the Software Testing Community in all possible ways.

Comments (0)

Leave a Reply

Your email address will not be published. Required fields are marked *