Wednesday, October 26, 2011

Java Interview Questions part-2 (Strings,String Builder,String Buffer)



Q1.       Given:
22.     StringBuilder sb1 = new StringBuilder("123");
23.     String s1 = "123";
24.     // insert code here
25.     System.out.println(sb1 + " " + s1);

Which code fragment, inserted at line 24, outputs "123abc 123abc"?
A.   sb1.append("abc"); s1.append("abc");                       B.  sb1.append("abc"); s1.concat("abc");
C.   sb1.concat("abc"); s1.append("abc");             D.  sb1.concat("abc"); s1.concat("abc");
E.   sb1.append("abc"); s1 = s1.concat("abc");                    F.  sb1.concat("abc"); s1 = s1.concat("abc");
G.   sb1.append("abc"); s1 = s1 + s1.concat("abc");          H.  sb1.concat("abc"); s1 = s1+s1.concat("abc");

Q2.       Given:
11.  String test = "Test A. Test B. Test C.";
12.  // insert code here
13.  String[] result = test.split(regex);

Which regular expression, inserted at line 12, correctly splits test into "Test A", "Test B", and "Test C"?

A. String regex = "";                               B. String regex = " ";
C. String regex = ".*";                            D. String regex = "\\s";
E. String regex = "\\.\\s*";              F. String regex = "\\w[ \.] +";

Q3.       Given:
public static void test(String str) {
int check = 4;
if (check = str.length())(assain a value to a length to that is inavalid {
System.out.print(str.charAt(check -= 1) +", ");
} else {
System.out.print(str.charAt(0) + ", ");
}
}
and the invocation:
test("four");
test("tee");
test("to");

What is the result?

A. r, t, t,               B. r, e, o,           C. Compilation fails.                       D. An exception is thrown at runtime.

Q4.       Given:
            public static void main(String[] args) {
                        String str = "null";
            if (str == null) {
            System.out.println("null");
} else (str.length() == 0) {(if missing
System.out.println("zero");
            } else {
            System.out.println("some");
}
}

What is the result?

A.  null                          B.  zero                         C.  some                       D.  Compilation fails.
E.  An exception is thrown at runtime.

Q5.       Given:
            public class TestString1 {
                        public static void main(String[] args) {
String str = "420";
            str += 42;
System.out.print(str);
            }
}

What is the output?

A.   42                                       B.   420                                     C.   462
D.   42042                                E.   Compilation fails.                  F.   An exception is thrown at runtime.

Q6.       Given:
public class KungFu {
public static void main(String[] args) {
Integer x = 400;
Integer y = x;
x++;
StringBuilder sb1 = new StringBuilder("123");
StringBuilder sb2 = sb1;
sb1.append("5");
System.out.println((x==y) + " " + (sb1==sb2));
}
}

What is the result?

A.   true true                              B.   false true                          C.   true false
D.   false false                           E.   Compilation fails.                  F.   An exception is thrown at runtime.

Q7.       Given:
String test = "a1b2c3";
String[] tokens = test.split("\\d");
for(String s: tokens) System.out.print(s + " ");

What is the result?

A.  a b c                       B.  1 2 3                        C.  a1b2c3              D.  a1 b2 c3
E.  Compilation fails.       F.  The code runs with no output.                 G.  An exception is thrown at runtime.

Q8.       Which two scenarios are NOT safe to replace a StringBuffer object with a StringBuilder object? (Choose two.)

A.   When using versions of Java technology earlier than 5.0.
B.   When sharing a StringBuffer among multiple threads.
C.   When using the java.io class StringBufferInputStream.
D.   When you plan to reuse the StringBuffer to build more than one string.

Q9.       Given:
1.   public class TestString3 {
2.         public static void main(String[] args) {
3.                     // insert code here
5.                     System.out.println(s);
6.         }
7.   }

Which two code fragments, inserted independently at line 3, generate the output 4247? (Choose two.)

A.   String s = "123456789";
s = (s-"123").replace(1,3,"24") - "89";
B.   StringBuffer s = new StringBuffer("123456789");
s.delete(0,3).replace(1,3,"24").delete(4,6);
C.   StringBuffer s = new StringBuffer("123456789");
s.substring(3,6).delete(1,3).insert(1, "24");
D.   StringBuilder s = new StringBuilder("123456789");
s.substring(3,6).delete(1,2).insert(1, "24");
E.   StringBuilder s = new StringBuilder("123456789");
s.delete(0,3).delete(1,3).delete(2,5).insert(1, "24");

Q10.     Consider following code:

            public class String1
{
                        public static void main(String[] args)  {
                                    String s = null;
                                    String t = "null";
                                    if (s==t)
                                                System.out.println("s equal to t");
                                    else
                                                System.out.println("s not equal to t");
                        }
            }

            what will result if you try to compile and run above code?

            A.  It compiles successfully, but throws NullPointerException at if (s==t)
            B.  It will not compile.
            C.  It compiles successfully and output "s equal to t"
            D.  It compiles successfully and output "s not equal to t"

Q11.     class String2 {
                        static void m1(String s1, String s2) {
                                    s1.insert(1, "D"); (there is no method insert in string
                                    s1 = s1 + s2;
                        }

                        public static void main(String[] s) {
                                    String s1 = "A", s2 = "B";
                                    m1(s1,s2);
                                    System.out.print(s1 + s2);
                        }
            }

            What is the result of attempting to compile and run the program?

            A.  Prints: AB                             B.  Prints: ABB                           C.  Prints: ADB
            D.  Prints: ADBB                        E.  Compile-time error                        F.  Run-time error
            G.  None of the above

Q12.     Please consider following code:

            String s1=" 5 + 5 = 10 ";
            s1.trim();
            s1.replace('+', '-');
     
            How many String objects will be created after executing above lines?(for every operation it creates a object
  
            A. 1                              B. 2                              C. 3                              D. 4

Q13.     String s = "Hi";
            StringBuffer sb = new StringBuffer(s);
            String s1 = new String("There");
            StringBuffer sb1=new StringBuffer(s1);

            if(s==sb) {
                        System.out.println("s==sb");
            }if(s.equals(sb)) {
                        System.out.println("s.equals(sb)");
            }if(s1.equals(sb1)) {
                        System.out.println("s1.equals(sb1)");
            }           Please, select which of the following will be part of output?

            A.  It will not compile at if(s==sb) because operands on both side are not compatible
            B.  It will print s1.equals(sb1)
            C.  It will print s.equals(sb)
            D.  It will compile successfully, but it will not output anything

Q14.     class A {String s1 = "A";}
            class B extends A {String s1 = "B";}
            class C extends B {String s1 = "C";}

            class String4 {
                        static void m1(A x) {System.out.print(x.s1);}
                        static void m1(B x) {System.out.print(x.s1);}
                        static void m1(C x) {System.out.print(x.s1);}

                        public static void main(String[] args) {
                                    A a; B b; C c; a = b = c = new C();
                                    m1(a); m1(b); m1(c);
                        }
            }

            What is the result of attempting to compile and run the program?

            A.  Prints: AAA               B.  Prints: ABC                        C.  Prints: CBA               D.  Prints: CCC                          
            E.  Compile-time error    F.  Run-time error          G.  None of the above

Q15.     What is the output for the following code ?

            class String5 {
                        public String get(int i) {
                                    return null;
                        }

                        public static void main(String ka[]) {
                                    System.out.println((new String()).get(1));
                        }
            };

            A.   0                B.   NullPointerException at run-time        C.   Compile error          D.   null

Q16.     11.        class Converter {          
            12.                    public static void main(String[] args) {
            13.                                Integer i = args[0]; (incompatible type
            14.                                int j = 12;
            15.                                System.out.println("It is " + (j==i) + " that j==i.");
            16.                    }
            17.        }
                       
            What is the result when the programmer attempts to compile the code and run it with the command java      Converter 12?

            A. It is true that j==i.                                         B. It is false that j==i.
            C. An exception is thrown at runtime.                   D. Compilation fails because of an error in line 13.

Q17.     13.        public class Pass {
            14.                    public static void main(String [] args) {
            15.                                int x = 5;
            16.                                Pass p = new Pass();
            17.                                p.doStuff(x);
            18.                                System.out.print(" main x = " + x);
            19.                    }
            20.                    void doStuff(int x) {
            21.                                System.out.print(" doStuff x = " + x++);
            22.                    }
            23.        }
                       
            What is the result?

            A. Compilation fails.                                B. An exception is thrown at runtime.
            C. doStuff x = 6 main x = 6                     D. doStuff x = 5 main x = 5
            E. doStuff x = 5 main x = 6                     F. doStuff x = 6 main x = 5

Q18.     1.         interface A { public void aMethod(); }
            2.         interface B { public void bMethod(); }
            3.         interface C extends A,B { public void cMethod(); }
            4.         class D implements B {
            5.                     public void bMethod(){}
            6.         }
            7.         class E extends D implements C {
            8.                     public void aMethod(){}
            9.                     public void bMethod(){}
            10.                    public void cMethod(){}
            11.        }
                       
            What is the result?

            A. Compilation fails because of an error in line 3.
            B. Compilation fails because of an error in line 7.
            C. Compilation fails because of an error in line 9.
            D. If you define D e = new E(), then e.bMethod() invokes the version of bMethod() defined in Line 5.
            E. If you define D e = (D)(new E()), then e.bMethod() invokes the version of bMethod() defined in Line 5.    F. If you define D e = (D)(new E()), then e.bMethod() invokes the version of bMethod() defined in Line 9.

Q19.     A company that makes Computer Assisted Design (CAD) software has, within its application, some utility      classes that are used to perform 3D rendering tasks. The company's chief scientist has just improved the           performance of one of the utility classes' key rendering algorithms, and has assigned a programmer to           replace the old algorithm with the new algorithm. When the programmer begins researching the utility      classes, she is happy to discover that the algorithm to be replaced exists in only one class. The       programmer reviews that class's API, and replaces the old algorithm with the new algorithm, being careful that her changes adhere strictly to the class's API. Once testing has begun, the programmer discovers that       other classes that use the class she changed are no longer working properly. What design flaw is most         likely the cause of these new bugs?

            A. Inheritance                            B. Tight coupling                                 C. Low cohesion
            D. High cohesion                        E. Loose coupling                                   F. Object immutability

Q20.     1.         class Super {
            2.                     private int a;
            3.                     protected Super(int a) { this.a = a; }
            4.         }
                        ...
            11.        class Sub extends Super {
            12.                    public Sub(int a) { super(a); }
            13.                    public Sub() { this.a = 5; }
            14.        }

            Which two, independently, will allow Sub to compile? (Choose two.)
           
            A. Change line 2 to: public int a;              B. Change line 2 to: protected int a;
            C. Change line 13 to: public Sub() { this(5); }          D. Change line 13 to: public Sub() { super(5); }
            E. Change line 13 to: public Sub() { super(a); }

Q21.     1.         package geometry;
            2.         public class Hypotenuse {
            3.                     public InnerTriangle it = new InnerTriangle();
            4.                     class InnerTriangle {
            5.                                 public int base;
            6.                                 public int height;
            7.                     }
            8.         }

            Which statement is true about the class of an object that can reference the variable base?
           
            A. It can be any class.                                                    B. No class has access to base.
            C. The class must belong to the geometry package.             D. The class must be a subclass of the class Hypotenuse.

Q22.     1.         public class Breaker2 {
            2.                     static String o = "";
            3.                     public static void main(String[] args) {
            4.                                 z:
            5.                                 for(int x = 2; x < 7; x++) {
            6.                                             if(x==3) continue;
            7.                                             if(x==5) break z;
            8.                                             o = o + x;
            9.                                 }
            10.                                System.out.println(o);
            11.                    }
            12.        }
                       
            What is the result?
           
            A. 2                  B. 24                C. 234               D. 246               E. 2346             F. Compilation fails.

Q23.     11.        class Mammal { }
            12.
            13.        class Raccoon extends Mammal {
            14.                    Mammal m = new Mammal();
            15.        }
            16.       
            17.        class BabyRaccoon extends Mammal { }

            Which four statements are true? (Choose four.)

            A. Raccoon is-a Mammal.                   B. Raccoon has-a Mammal.    C. BabyRaccoon is-a Mammal.
            D. BabyRaccoon is-a Raccoon.                E. BabyRaccoon has-a Mammal.
            F. BabyRaccoon is-a BabyRaccoon.

Q24.     10:       public class Hello {
            11:                   String title;
            12:                   int value;
            13:                   public Hello() {
            14:                               title += " World";
            15:                   }
            16:                   public Hello(int value) {
            17:                               this.value = value;
            18:                               title = "Hello";
            19:                               Hello();
            20:                   }
            21:       }
            and:
            30:       Hello c = new Hello(5);
            31:       System.out.println(c.title);

            What is the result?

            A. Hello             B. Hello World               C. Compilation fails.               D. Hello World 5
            E. The code runs with no output.             F. An exception is thrown at runtime.

Q25.     1.         public class Plant {
            2.                     private String name;
            3.                     public Plant(String name) { this.name = name; }
            4.                     public String getName() { return name; }
            5.         }
           
            1.         public class Tree extends Plant {
            2.                     public void growFruit() { }
            3.                     public void dropLeaves() { }
            4.         }
                       
            Which statement is true?
           
            A. The code will compile without changes.
            B. The code will compile if public Tree() { Plant(); } is added to the Tree class.
            C. The code will compile if public Plant() { Tree(); } is added to the Plant class.
            D. The code will compile if public Plant() { this("fern"); } is added to the Plant class.
            E. The code will compile if public Plant() { Plant("fern"); } is added to the Plant class.

Q26.     11.        public enum Title {
            12.                    MR("Mr."), MRS("Mrs."), MS("Ms.");
            13.                    private final String title;
            14.                    private Title(String t) { title = t; }
            15.                    public String format(String last, String first) {
            16.                                return title + " " + first + " " + last;
            17.                    }
            18.        }
            19.        public static void main(String[] args) {
            20.                    System.out.println(Title.MR.format("Doe", "John"));
            21.        }

            What is the result?

            A. Mr. John Doe                                                          B. An exception is thrown at runtime.
            C. Compilation fails because of an error in line 12.            D. Compilation fails because of an error in line 15.
            E. Compilation fails because of an error in line 20.

Q27.     11.        public interface A111 {
            12.                    String s = "yo";
            13.                    public void method1();
            14.        }
            17.        interface B { }
            20.        interface C extends A111, B {
            21.                    public void method1();
            22.                    public void method1(int x);
            23.        }

            What is the result?
           
            A. Compilation succeeds.                                         B. Compilation fails due to multiple errors.
            C. Compilation fails due to an error only on line 20.       D. Compilation fails due to an error only on line 21.
            E. Compilation fails due to an error only on line 22.       F. Compilation fails due to an error only on line 12.

Q28.     1.         interface TestA { String toString(); }
            2.         public class Test {
            3.                     public static void main(String[] args) {
            4.                     System.out.println(new TestA() {
            5.                                             public String toString() { return "test"; }
            6.                                 });
            7.                     }
            8.         }
                       
            What is the result?
           
            A. test                                     B. null                           C. An exception is thrown at runtime.      
            D. Compilation fails because of an error in line 1. E. Compilation fails because of an error in line 4.
            F. Compilation fails because of an error in line 5.

Q29.     11.        static class A {
            12.                    void process() throws Exception { throw new Exception(); }
            13.        }
            14.        static class B extends A {
            15.                    void process() { System.out.println("B"); }
            16.        }
            17.        public static void main(String[] args) {
            18.                    new B().process();
            19.        }

            What is the result?
           
            A. B                  B. The code runs with no output.             C. Compilation fails because of an error in line 12.            D. Compilation fails because of an error in line 15.    E. Compilation fails because of an error in line 18.

Q30.     A company has a business application that provides its users with many different reports:
                        receivables reports, payables reports, revenue projects, and so on. The company has just purchased some new, state-of-the-art, wireless printers, and a programmer has been assigned the task of enhancing all of the reports to use not only the company's old printers, but the new wireless printers as well. When the programmer starts looking into the application, the programmer discovers that because of the design of the application, it is necessary to make changes to each report to support the new printers. Which two design concepts most likely explain this situation? (Choose two.)

            A. Inheritance                B. Low cohesion                      C. Tight coupling        
            D. High cohesion            E.



0 comments:

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Vamshi krishnam raju | Bloggerized by Vamshi krishnam raju - Vamshi krishnam raju | Vamshi krishnam raju