Select chapter



basics (7.papers)
constructors (1.papers)
packages (1.papers)
inheritance (7.papers)
misc (3.papers)
exceptions (5.papers)
threads (4.papers)
overloading (1.papers)
jsp (3.papers)
servlets (9.papers)
jdbc (2.papers)
util (1.papers)
xml (2.papers)
IO stream (3.papers)
Hide

Sponsored Links


  

java arrays and strings interview questions and answers

1
What is contains method in string class? How to use it?
  1. The java.lang.String.contains() method returns true if and only if this string contains the specified sequence of char values.
  2. Below code will show how to use contains method
    String str1 = "Hello";
    System.out.println(str1.contains("ell"));
  3. both are true
  4. none
Show Answer
Share Facebook gmail Twitter
2
How to create array of objects in Java?
  1. public class MainClass
    {  
        public static void main(String args[])
        {
            System.out.println("Hello, World!");
            Emp[] employees = new Emp[10];
            for(int i=0; i<employees.length; i++){
                employees[i] = new Emp(i+1);
            }
        }
    }
    
    class Emp{
        int eno;
        public Emp(int no){
            eno = no;
            System.out.println("emp constructor called..eno is.."+eno);
        }
    }
  2. class Rextester
    {  
        public static void main(String args[])
        {
            System.out.println("Hello, World!");
            Emp[] employees = new Emp[10];
        }
    }
    
    class Emp{
        int eno;
        public Emp(int no){
            eno = no;
            System.out.println("emp constructor called..eno is.."+eno);
        }
    }
  3. class Rextester
    {  
        public static void main(String args[])
        {
            System.out.println("Hello, World!");
            Emp[] employees = new Emp[10];
            //Emp e = new Emp();
            for(int i=0; i<employees.length; i++){
                employees[i] = new Emp();
            }
        }
    }
    
    class Emp{
        int eno;
        public Emp(int no){
            eno = no;
            System.out.println("emp constructor called..eno is.."+eno);
        }
    }
  4. It is not possible to create array of objects in java.
Show Answer
Share Facebook gmail Twitter
3
How to create string array in Java?
  1. String[] myStringArray = new String[]{"a","b","c"};
  2. String[] myStringArray = new String[3];
  3. String[] myStringArray = {"a","b","c"};
  4. all are correct.
Show Answer
Share Facebook gmail Twitter
4
Program for reverse words in a sentence?
  1. public class ReverseString {
    
     public static void main()
     {
      String original = "hi how are you ?";
      String reverse="";
      int length=original.length();
      for(int i=(length-1);i>=0;i--)
      {
       reverse=reverse+original.charAt(i);
      }
      System.out.println("Reversed String is="+reverse);
     }
    }
    
  2. public class MainClass
    {  
        public static void main(String args[])
        {
            String str ="hello world hey man good bye";
            StringBuilder revStr = new StringBuilder("");
            int end = str.length(); // substring takes the end index -1
            int counter = str.length()-1;
            for (int i = str.length()-1; i >= 0; i--) {     
                if (str.charAt(i) == ' ' || i == 0) {
                    if (i != 0) {
                        revStr.append(str.substring(i+1, end));
                        revStr.append(" ");
                    }
                    else {
                        revStr.append(str.substring(i,end));
                    }
                    end = counter;
                }
                counter--;
            }
            System.out.println(revStr);
        }
    }
  3. both works fine
  4. none
Show Answer
Share Facebook gmail Twitter
5
Write a Java program to revere a string using recursion?
  1. public class ReverseString {
     
        String reverse = "";
         
        public String reverseString(String str){
             
            if(str.length() == 1){
                return str;
            } else {
                reverse += str.charAt(str.length()-1)
                        +reverseString(str.substring(0,str.length()-1));
                return reverse;
            }
        }
         
        public static void main(String a[]){
            ReverseString rs = new ReverseString();
            System.out.println("Result: "+rs.reverseString("SkillGun"));
        }
    }
  2. public class ReverseString {
    
     public static void main()
     {
      String original,reverse="";
      Scanner in= new Scanner(System.in);
      System.out.println("Enter a string to reverse");
      original=in.nextLine();
      int length=original.length();
      for(int i=(length-1);i>=0;i--)
      {
       reverse=reverse+original.charAt(i);
      }
      System.out.println("Reversed String is="+reverse);
     }
    }
    
  3. public class ReverseString {
     
        String reverse = "";
         
        public String reverseString(String str){
                reverse = str.reverse();                    
            }
        }
         
        public static void main(String a[]){
            ReverseString rs = new ReverseString();
            System.out.println("Result: "+rs.reverseString("SkillGun"));
        }
    }
  4. None are correct
Show Answer
Share Facebook gmail Twitter
Read more papers :