Test_3(27-8-2014)

20 Questions

Settings
Please wait...
Data Structure Quizzes & Trivia

Oops,String & Collection


Questions and Answers
  • 1. 
    Select Correct options in given below
    • A. 

      Collection list = new ArrayList();

    • B. 

      ArrayList list = new ArrayList();

    • C. 

      ArrayList list = new ArrayList();

    • D. 

      A,B & C

  • 2. 
    3. public class Ouch {4. static int ouch = 7;5. public static void main(String[] args) {6. new Ouch().go(ouch);7. System.out.print(" " + ouch);8. }9. void go(int ouch) {10. ouch++;11. for(int ouch = 3; ouch < 6; ouch++)12. ;13. System.out.print(" " + ouch);14. }15. } 
    • A. 

      57

    • B. 

      58

    • C. 

      87

    • D. 

      88

    • E. 

      Compilation fails

    • F. 

      An exception is thrown at runtime

  • 3. 
    Class Hexy {         public static void main(String[] args) {         Integer i = 42;         String s = (i<40)?"life":(i>50)?"universe":"everything";         System.out.println(s);     }} 
    • A. 

      Null

    • B. 

      Life

    • C. 

      Universe

    • D. 

      Everything

    • E. 

      Compilation fails

    • F. 

      . An exception is thrown at runtime

  • 4. 
    Given:class Feline {           public static void main(String[] args) {           Long x = 42L;           Long y = 44L;           System.out.print(" " + 7 + 2 + " ");           System.out.print(foo() + x + 5 + " ");           System.out.println(x + y + foo());    }    static String foo() { return "foo"; }} 
    • A. 

      9 foo47 86foo

    • B. 

      9 foo47 4244foo

    • C. 

      9 foo425 86foo

    • D. 

      9 foo425 4244foo

    • E. 

      72 foo425 86foo

    • F. 

      Compilation fails

  • 5. 
    3. interface Vessel { }4. interface Toy { }5. class Boat implements Vessel { }6. class Speedboat extends Boat implements Toy { }7. public class Tree {8. public static void main(String[] args) {9. String s = "0";10. Boat b = new Boat();11. Boat b2 = new Speedboat();12. Speedboat s2 = new Speedboat();13. if((b instanceof Vessel) && (b2 instanceof Toy)) s += "1";14. if((s2 instanceof Vessel) && (s2 instanceof Toy)) s += "2";15. System.out.println(s);16. }17. } 
    • A. 

      0

    • B. 

      01

    • C. 

      012

    • D. 

      Compilation fails

  • 6. 
    3. public class Theory {4. public static void main(String[] args) {5. String s1 = "abc";6. String s2 = s1;7. s1 += "d";8. System.out.println(s1 + " " + s2 + " " + (s1==s2));9.10. StringBuffer sb1 = new StringBuffer("abc");11. StringBuffer sb2 = sb1;12. sb1.append("d");13. System.out.println(sb1 + " " + sb2 + " " + (sb1==sb2));14. }15. } 
    • A. 

      Compilation fails

    • B. 

      The first line of output is abc abc true

    • C. 

      The first line of output is abcd abc false

    • D. 

      The second line of output is abcd abcd true

  • 7. 
    Which statements are true about comparing two instances of the same class, given that theequals() and hashCode() methods have been properly overridden? (Choose all that apply.) 
    • A. 

      If the equals() method returns true, the hashCode() comparison == might return false

    • B. 

      .If the equals() method returns false, the hashCode() comparison == might return true

    • C. 

      If the hashCode() comparison== returns true, the equals() method must return true

    • D. 

      .If the hashCode() comparison == returns true, the equals() method might return true

    • E. 

      If the hashCode() comparison != returns true, the equals() method might return true

  • 8. 
    Given:public static void before() {Set set = new TreeSet();set.add("2");set.add(3);set.add("1");Iterator it = set.iterator();while (it.hasNext())System.out.print(it.next() + " ");} 
    • A. 

      The before() method will print 12

    • B. 

      The before() method will print 123

    • C. 

      The before() method will throw an exception at runtime

    • D. 

      The before() method will not compile

  • 9. 
    Which collection class(es) allows you to grow or shrink its size and provides indexed access toits elements, but whose methods are not synchronized? (Choose all that apply.) 
    • A. 

      .java.util.HashSet

    • B. 

      Java.util.LinkedHashSet

    • C. 

      .java.util.List

    • D. 

      Java.util.ArrayList

    • E. 

      Java.util.Vector

  • 10. 
    • A. 

      Compilation error, attempting to perform binary comparison on logical data type.

    • B. 

      Compilation and output of "We are equal 10".

    • C. 

      Compilation and output of "Not equal! 20"

    • D. 

      Compilation and output of "Not equal! 10".

  • 11. 
    Public void foo( boolean a, boolean b) { if( a ) { System.out.println("A"); /* Line 5 */ } else if(a && b) /* Line 7 */ { System.out.println( "A && B"); } else /* Line 11 */ { if ( !b ) { System.out.println( "notB") ; } else { System.out.println( "ELSE" ) ; } } }
    • A. 

      If a is true and b is true then the output is "A && B"

    • B. 

      If a is true and b is false then the output is "notB"

    • C. 

      If a is false and b is true then the output is "ELSE"

    • D. 

      If a is false and b is false then the output is "ELSE"

  • 12. 
    • A. 

      Compilation fails.

    • B. 

      "odd" will always be output.

    • C. 

      "even" will always be output.

    • D. 

      "odd" will be output for odd values of x, and "even" for even values.

  • 13. 
    In the following pieces of code, A and D will compile without any error. True or false?A: StringBuffer sb1 = "abcd";B: Boolean b = new Boolean("abcd");C: byte b = 255;D: int x = 0x1234;E: float fl = 1.2;
    • A. 

      True

    • B. 

      False

  • 14. 
    • A. 

      0122

    • B. 

      0123

    • C. 

      Compilation error

    • D. 

      None of these

  • 15. 
    What is displayed when the following code is compiled and executed?String s1 = new String("Test"); String s2 = new String("Test"); if (s1==s2) System.out.println("Same"); if (s1.equals(s2)) System.out.println("Equals");
    • A. 

      Same Equals

    • B. 

      Equals

    • C. 

      Same

    • D. 

      The code compiles, but nothing is displayed upon execution.

  • 16. 
    Class PassS { public static void main(String [] args) { PassS p = new PassS(); p.start(); } void start() { String s1 = "slip"; String s2 = fix(s1); System.out.println(s1 + " " + s2); } String fix(String s1) { s1 = s1 + "stream"; System.out.print(s1 + " "); return "stream"; } }
    • A. 

      Slip stream

    • B. 

      Slipstream stream

    • C. 

      Stream slip stream

    • D. 

      Slipstream slip stream

  • 17. 
    Class Test { public static void main(String [] args) { int x=20; String sup = (x < 15) ? "small" : (x < 22)? "tiny" : "huge"; System.out.println(sup); } }
    • A. 

      Small

    • B. 

      Tiny

    • C. 

      Huge

    • D. 

      Compilation fails

  • 18. 
    Class Test { public static void main(String [] args) { int x= 0; int y= 0; for (int z = 0; z < 5; z++) { if (( ++x > 2 ) || (++y > 2)) { x++; } } System.out.println(x + " " + y); } }
    • A. 

      5 3

    • B. 

      8 2

    • C. 

      8 3

    • D. 

      8 5

  • 19. 
    Class Test { static int s; public static void main(String [] args) { Test p = new Test(); p.start(); System.out.println(s); } void start() { int x = 7; twice(x); System.out.print(x + " "); } void twice(int x) { x = x*2; s = x; } }
    • A. 

      7 7

    • B. 

      7 14

    • C. 

      14 0

    • D. 

      14 14

  • 20. 
    Int [] a = {1,2,6,"7"};System.out.println(a[3]);
    • A. 

      6

    • B. 

      7

    • C. 

      None of the above

    • D. 

      Option 4