Questions

  • October 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Questions as PDF for free.

More details

  • Words: 5,197
  • Pages: 32
1. int [] arr = { 1 ,2 ,3 ,4 ,5}; int [] arr2 = new int[4]; arr2 = arr; System.out.println(arr2[4]);

a. Compile error b. Runtime Exception. ArrayOutOfBounds c. Prints 4 d. Prints 5 e. Compiles with warning ----------------------------------------------------------------------------------------2. int [][] arr = new int[3][3]; int [] arr2 = { 1 ,2 }; arr[0] = arr2; System.out.println( arr[0][0] + " " + arr[0][1] ); a. Compile error b. Runtime Exception. ArrayOutOfBounds c. Prints 1 2 d. Prints null e. Compiles with warnings ----------------------------------------------------------------------------------------3. class test { { System.out.println("one"); } static { System.out.println("static one"); } public static void main(String[] args) { new test().run(); } private test(){ { System.out.println("two"); }} { System.out.println("three"); } static { }

System.out.println("static two");

public void run() { System.out.println("hmm..."); }}

a. Compile error b. Runtime error. c. Prints static one static two one two three hmm... d. Prints static one static two one three two hmm... e. Prints static one static two two three one hmm... -----------------------------------------------------------------------------------------

4. int[] a = null , b [] = null; b = a; System.out.println( b ); a. Prints null b. Runtime NullpointerExcepion c. Compile time error. ----------------------------------------------------------------------------------------5. class test { public static void main(String[] args) { test inst_test = new test(); inst_test.method ( 1 , 1 , 1); inst_test.method( new Integer(1) , new Integer(2) , new Integer(3) ); inst_test.method ( 1 , new Integer(5) ); inst_test.method ( new Integer(10) , 1 ); } public void method( Integer... I ) { System.out.println("Eye in the sky"); } public void method( int... i ) { System.out.println("Fly in the pie"); } } a. Fly in the pie Eye in the sky Eye in the sky Eye in the sky b. Eye in the sky Eye in the sky Eye in the sky Eye in the sky c. Compile error d. Runtime Exception e. None of the above. -----------------------------------------------------------------------------------------

6. class test { public static void main(String[] args) { test inst_test = new test(); String pig[][] = { {"one little piggy"}, {"two little piggies"}, {"three little piggies"} }; for ( Object []oink : pig ) { for ( Object piggy : oink ) { System.out.println(piggy); } } } } a. one little piggy two little piggies three little piggies b. Compile Error incompatible types. c. java.lang.String;@187c6c7 java.lang.String;@187c6c8 java.lang.String;@187c6c9 ( or something like that ) d. Runtime NullpointerException e. Prints nothing ----------------------------------------------------------------------------------------7. class test { public static void main(String[] args) { test inst_test = new test(); int i1 = 2000; int i2 = 2000; int i3 = 2; int i4 = 2; Integer Ithree = new Integer(2); // 1 Integer Ifour = new Integer(2); // 2 System.out.println( Ithree == Ifour ); inst_test.method( i3 , i4 ); inst_test.method( i1 , i2 ); } public void method( Integer i , Integer eye ) { System.out.println(i == eye ); } } a. true false true b. false true false c. false false false d. true true false e. Compile error -----------------------------------------------------------------------------------------

8. interface face { void smile(); } class test implements face { public static void main(String[] args) { test evil_laugh = new test(); evil_laugh.smile(); } void smile() { System.out.println("Muahahahahahhahaha"); } } a. Prints Muahahahahahhahaha b. Compile error c. Runtime Exception d Prints nothing e. None of the above. ----------------------------------------------------------------------------------------9. abstract class ab { abstract private void smile(); } class test extends ab { public static void main(String[] args) { test inst_test = new test(); } } a. Compile with warnings indicating smile() should be public. b. Compiles without warnings. c. Compiles with warnings indicating smile() is not inherited. d. Compile error e. Runtime Exception. ----------------------------------------------------------------------------------------10. given that ex is a class that extends from Exception public void blah() throws IOException , ex { throw new ex(); throw new IOException(); }

a. Compile error unreachable code b. Comepile error: cant throw two exceptions at the same time c. Compile error: both Exceptions need to be handled in try catch block d. Runtime error e. No errors. Compiles fine without warnings ----------------------------------------------------------------------------------------11. Drag and Drop either “true” or “false” above the following options that are based on this code fragment. ( Imagine that true and false are two fragments on the screen that can be dragged onto the options shown ) Code Fragment : List < ? > l = new ArrayList< Integer > (); Options: a. This code fragment will not compile b. Nothing can be added to this List. c. Any reference type could replace Integer on the right hand side. 12. If two instances, test_one and test_two of Serializable class test are written to a file named “serial.serial” in that order, which of the following are true ? a. b. c. d.

When reading back from the file, test_two is the first object that is read. When reading back from the file, test_one is the first object that is read. The object cannot be read from a file named “serial.serial”. None of the above.

13. class test { public static void main ( String [] args ) { final Integer x4 = 8; final int x = 10; switch ( x ) { case x4: System.out.println("4"); break; case x: System.out.println("x"); break; } } }

a. b. c. d. e.

Compile error Runtime error Prints x Prints 4 None of the above.

14. class num { int x=9; } interface blob { final num n = new num(); final Integer number = 1; } class test implements blob { public static void main ( String [] args ) { n.x = 10; // 1 number++; // 2 } } a. b. c. d. e.

Compile error at 1 Runtime error at 1 Compiler error at 2 Runtime error at 2 Compiles and runs fine

15. enum cafe { BIG ( 10 ) , SMALL ( 1 ), MED ( 5 ) int mySize = 0; cafe ( int size ) { mySize = size; } } What happens when this enum is in the code outside a class ? a. Compiles fine

b. Compiler error c. Runtime Exception occurs if mySize is accessed. 16. class Animal { void method throws IOException {} } class dog extends Animal { void method throws FileNotFoundException {} } a. b. c. d. e.

Compile error : Incompatible types. Runtime error Compiles fine Compile error : incorrect syntax None of the above

17. class Animal { void method () throws Exception {} Animal( String name ) { System.out.println(name); } } class dog extends Animal { void method () throws Exception {} } class test { public static void main ( String [] args ) { new Animal("Giraffe"); } } a. Prints Giraffe b. Compile error c. Runtime error

d. Prints nothing e. None of the above 18. class test { int x; public static void main ( String [] args ) { final int i; i = 127; byte b = i; System.out.println(b); } } a. Compile error: loss of precision b. No error. Compiles fine. Prints 0 c. Runtime error d. Compiles with a warning e. No error. Compiles fine. Prints 127. 19. class test { public static void main ( String [] args ) { methodOne(20); } static void methodOne( long l ) { System.out.println("long"); } static void methodOne( float f ) { System.out.println("float"); } } a. b. c. d. e.

Prints long Prints float Compile error: Too ambiguous Runtime error None of the above

20. import java.util.*; class test { public static void main ( String [] args )

{ List < String > least = new ArrayList < String > (); List list = new ArrayList(); meth(list); seth(least); } public static void meth(List < String > list) { System.out.println("List"); } public static void seth(List list) { System.out.println("Mist"); } } Which function call(s) is/are allowed? Here allowed refers to legality, and that compilations succeeds. a. b. c. d. e.

Both are allowed and there are no warnings Both are not allowed - they don’t compile Both are allowed but the code compiles with warnings Meth is allowed but seth is not Seth is allowed but meth is not.

21. import java.util.*; class test { public static void main ( String [] args ) { List < Integer > list = new ArrayList < Integer > (); for ( int i = 1 ; i<10 ; i++ ) { list.add(i); } list.remove( new Integer(4) ); // 1 list.remove( 1 ); // 2 } } Lines marked 1 and 2 refer to the remove method.

a. Both methods remove the 4th and 1st Integer objects. b. Both methods remove the Integer 4 and 1 from list. c. Line one removes Integer object 4 and Line 2 removes Integer at index one d. Line one removes Integer at index 4 and Line 2 removes the Integer Object one 22. import java.util.*; class test

{ public static void main ( String [] args ) { Map < Integer , String > map = new LinkedHashMap < Integer , String > (); Map < Integer , String > sap = new HashMap < Integer , String > (); populate( map ); populate( sap ); System.out.println( map.get(1) + sap.get(1) ); } static void populate ( Map m ) { for ( int i = 0 ; i < 10 ; i++ ) { m.put(i,i); } } } a. b. c. d. e.

Prints 11 Prints 2 Compile error Runtime error nullnull

23. import java.util.*; class test { int Identify; public static void main( String args[] ) { Set set = new HashSet (); System.out.println( set.add( "new" ) ); System.out.println( set.add( "new" ) ); System.out.println( set.add( new test(127) ) ); System.out.println( set.add( new test(127) ) ); } test(int ID) { Identify = ID; } } a. b. c. d. e. 24.

Prints true false true false Prints true true true true Prints true false true true Prints false true false true None of the above.

import java.util.*; class test implements Comparator < test > { int testNumber; public static void main( String args[] ) { Set < test > s1 = new TreeSet < test > (); s1.add(new test()); s1.add(new test()); } public int compare( test t1 , test t2 ) { return t1.testNumber - t2.testNumber; } } Choose 3 that apply. a. b. c. d. e.

Compiles without warnings Compiles with warnings Only one test Object is added, the other one is considered a duplicate. A Runtime Exception occurs. TreeSet can only accept objects of type test. ( Given that test has no sub classes )

25. import java.util.*; import java.io.*; class test implements Runnable { public static void main( String args[] ) { Thread t = new Thread(this); try { t.start(); } catch ( IOException e) { System.out.println(e); } } public void run() throws IOException { File f = new File("f"); FileWriter fw = new FileWriter(f); } }

a. b. c. d. e.

One Compile error Runtime error Compiles with warnings. Two Compiler errors None of the above.

26. class test { public static void main( String args[] ) { test( new int[] { 1 , 2 } ); } public static void test (int[] input) { System.out.println("int[]"); } public static void test (int... input) { System.out.println("int ..."); } } a. b. c. d. e.

Prints int[] Prints int… Compile error Runtime error None of the above

27. class face { public void meth() { System.out.println("hello"); } } class test { public static void main( String args[] ) { seth( new face(){} ); // 1 } public static void seth( face f ) { f.meth(); // 2 }

} a. b. c. d. e.

Compilations fails at 2 Compilations fails at 1 Runtime error at 2 Prints hello after successfully compiling. Compilations fails at 1 and 2.

28. class test { public static void main( String args[] ) { new test<String>().meth("hello"); // 1 } public void meth(T type) // 2 { System.out.println(type); // 3 } } a. b. c. d. e.

Compilations fails at 1. Illegal syntax Compilations fails at 2. Cannot find T Compilations fails at 3. Cannot find symbol println( T ) Prints hello Runtime error.

29. class test { public static void main( String args[] ) { new test().meth(new Integer(42)); // 1 new test<String>().meth("hello"); // 2 new test<String>().meth(new Object() ); // 3 } public void meth(T type) { System.out.println(type); } } Compilation at which line(s) fails? a. 1

b. c. d. e.

2 3 Compiles without errors. Compiles without errors. But Runtime errors exist.

30. public List meth(List type) { System.out.println(type); // 1 return new ArrayList<String>(); // 2 } This code… a. b. c. d. e.

Will compile Will not Compile at Line 1 Has Runtime errors. Will not Compile at Line 2 Will not Compile at Line 1 and 2.

31. public static void main( String args[] ) { List type = new ArrayList(); // 1 for ( Integer n : type ) // 2 { System.out.println(n); // 3 } } public void seth(List type) // 4 { type.add("hi"); // 5 } a. b. c. d. e.

Lines 2 and 5 have Compile time errors Lines 1 and 5 have Compile time errors. Lines 2 and 5 and 1 have Compile time errors Lines 4 and 5 have Compile time errors None of the above.

32. Integer eye = new Integer(42); Double d = new Double(42.0); int i = 42; double dd = 42.0; System.out.println(i==eye); //1 System.out.println(eye.equals(d)); //2 System.out.println(eye == 42 ); //3 System.out.println(i == 42); //4

System.out.println( i == dd );

//5

Which line Prints false ? a. b. c. d. e.

1 2 3 4 5

33. Which of the following modifiers can be used with a method local inner class ? a. b. c. d. e. f. g.

static abstract final private protected public default

34. class test{ int x = 10; static test tester = this; public static void main( String args[] ){ System.out.println( tester.x ); } } a. b. c. d. e.

Prints 10 Prints 0 Compile error Runtime error None of the above

35. Integer i = new Integer(10); int i2 = 10; System.out.println( i == i2 ); // 1 With source as 1.4 what does this code print ? a. b. c. d. e.

Prints false Prints true Compile error at 1 Runtime error at 1 None of the above

36. interface face { void meth(); } class tester extends test implements face { public void meth(){System.out.println("hello");}}

class test { public static void main( String args[] ) { test test_one = new tester(); face f = test_one; // 1 f.meth(); } } Choose all that apply. a. b. c. d. e.

Compile error at 1 Runtime error at 1 Compiles without errors Will compile if Line one includes a cast for face. None of the above.

37. List list = new ArrayList <String > (); // 1 list.add(new Object()); // 2 Drag and Drop the following options over Line 1 and Line 2. If the Line will not compile drag option a over the line. If it will compile, but will throw Exceptions drag b over the line. Drag c over the line if both a and b do not fit the description. a. Compile time error b. Compiles fine but throws a Runtime Exception. c. Compiles fine with no Runtime Exceptions. 38. class test { public static void main( String... args ) { Byte b = new Byte(1); System.out.println("" + b + "20" ); } } a. b. c. d. e.

Compile error Runtime Exceptions are thrown. Prints 120 Prints 12 None of the above.

39. public static void main(String [] args) { System.out.println(m(2));

} static int m(int i) { int ret; if( i == 2 ) { ret = i; } return ret; } a. b. c. d. e.

Prints 2 Prints 0 Compile error Runtime error None of the above

40. class superb { int x=1; static int x2=2; void meth(){System.out.println("non static Superb");} static void meth2(){System.out.println("static Superb");} } class test extends superb { int x=3; static int x2=4; void meth(){System.out.println("non static test");} static void meth2(){System.out.println("static test");} public static void main(String [] args) { test t = new test(); System.out.println(t.x + " " + t.x2); superb s = new superb(); System.out.println(s.x + " " + s.x2); t.meth(); t.meth2(); s.meth(); s.meth2(); superb st = new test(); System.out.println( st.x + " " + st.x2 ); st.meth(); st.meth2(); } } Drag and Drop the 9 output literals in the order in which they appear. Note that options can be used more than once. ( Imagine nine empty spaces on the right hand side of these options). a. 3 4 b. 1 2 c. non static test d. static test e. non static Superb f. static Superb 41

package packed; public class pack { static public int x1 = 7; static protected int x2 = 8; static int x3=9; static private int x4 =10; } import packed.pack; class test extends pack { public static void main( String args[] ) { pack p = new pack(); System.out.println( pack.x2 ); } } a. b. c. d. e.

Prints 8 Compile error Runtime error Prints nothing None of the above.

42. int x=1; assert (x=2) : x; This code… a. b. c. d. e.

Compiles without errors when assertion is disabled by default Compiles without errors when the javac –da option is used. Will throw compile time errors when the –ea option is used. Will throw runtime exception. None of the above.

43. Queue q = new PriorityQueue(); q.offer(new String("hello ")); q.offer(new String("hi ") ); q.offer(new String("bye ")); for ( Object o : q ) { System.out.println(q.poll() + " " + q.size() ); } a. b. c. d.

Prints hello 3 hi 2 bye 1 Prints hello 2 hi 1 bye 0 Prints bye 3 hello 2 hi 1 Prints bye 2 hello 1 hi 0

e. Prints bye 2 followed by an Exception. f. None of the above. 44. int []arr = {1,2,3,4}; for ( int i : arr ) { arr[i] = 0; } for ( int i : arr ) { System.out.println(i); } a. b. c. d. e. f.

Prints 0 1 2 3 Prints 0 0 0 0 Prints 0 0 3 0 Prints 0 2 0 0 Compile error Runtime Exception occurs.

45. import java.util.*; class test implements Comparator { private int x; test(int input) { x = input; } public static void main( String args[] ) { List list = new ArrayList(); list.add(new test(2)); list.add(new test(2)); Collections.sort(list); } public int compare( test t1 , test t2 ) { return t1.x - t2.x; } } This code… a. b. c. d.

Sorts test instances based on variable x Sorts the instances in ascending order Sorts the instances in descending order. None of the above.

46. boolean [] arr = { true , false , true }; Arrays.sort(arr); System.out.println( Arrays.binarySearch(arr,true) ) ;

a. b. c. d. e. f.

Does not compile Compiles but has Runtime exceptions Prints 0 Prints 2 Prints 1 None of the above.

47. List l = Arrays.asList( new int[] {1,2,3,4} ); for ( int i : l ) { System.out.println(i); } a. b. c. d. e.

Prints 1 2 3 4 Prints 4 3 2 1 Prints nothing Compile error Runtime error

48. List l = Arrays.asList( new int[] {1,2,3,4} ); // 1 List list = new List (); // 2 Drag and drop the following options over the code fragments 1 and 2. One option may be used more than once. Options a. Compiles b. Does not compile. 49. ArrayList <Exception> l2 = new ArrayList <Exception>(); l2.add(new Exception()); l2.add(new NumberFormatException() ); a. Compiles with no runtime exceptions b. Does not compile c. Compiles but has runtime exceptions. 50. class test { int x; test(int input) { x = input; } public static void main( String args[] ) { System.out.println( new test(3).equals(new test(3)) );

} public boolean equals( Object o) { return ( (test)o ).x == x; } }

a. b. c. d. e.

Prints true Prints false Compile error Runtime Exceptions are encountered None of the above.

51. public static void main( String args[] ) { Map m = new HashMap(); String str = null; m.put(new test() , "mill" ); m.put(new test() , "sill" ); System.out.println(m.size()); } public boolean equals( Object o) { return false; // 1 } public int hashCode() { return 0; } Choose all that apply. a. b. c. d.

Prints 1 Prints 2 If Line 1 is replaced with return true; then m.size() returns 1. If hashCode() is not overridden then regardless of whether line 1 returns true or false 2 will be printed on the screen. e. Compile time error f. Runtime exception is encountered. g. If Line 1 is replaced with return true; then size() will not return any number greater than 1. 52. System.out.println( Arrays.equals( new int[]{1,2,3,4} , new Integer[]{1,2,3,4} )); a. b. c. d.

Compile error Runtime Exception Prints true Prints false

53. Which of the following Thread class methods are static ? a. b. c. d. e. f.

sleep join yield wait notify notifyAll

54. Given these two classes and that you want to promote better coupling between them. What changes are required to achieve good coupling ? Also given that the int variable main is to be used when initializing coupler’s variables. class test { int main=10; public static void main ( String... blah ) { coupler c = new coupler(); c.setMain( c.getMain() ); // 1 } int getMain() { return main; } } class coupler { int pain; void setMain(int mane) { pain = mane; } int getMain() { return new test().main; } // 2 } a. b. c. d. e.

Replace line 1 with main = c.getMain(); Replace line 1 with a call to coupler’s setMain method with the argument - main. Replace line 2 with return pain; and make no other changes to the program. No changes are required. None of the above.

55. public static void main( String args[] ) { Set <String> set = new HashSet<String>(); System.out.println(set.add("duplicate"));

System.out.println(set.add("duplicate")); } public boolean equals(Object o) { return false; } public int hashCode() { return 0; } a. b. c. d. e. f.

Compile error Runtime error Prints true true Prints true false Prints false true Prints false false.

56. import java.util.regex.*; class test { public static void main( String args[] ) { search("asf jgds8 93 qn" , "\\d\\w"); // 1 search("asf jgds8 93 qn" , ".*\\d"); // 2 search("asf jgds8 93 qn" , "\\d.*"); // 3 search("asf jgds8 93 qn" , "\\w.*\\d"); // 4 } public static void search(String mat , String pat) { Pattern p = Pattern.compile(pat); Matcher m = p.matcher(mat); while(m.find()) { System.out.println(m.group()); } } } a. 93 asf jgds8 93 8 93 qn asf jgds8 93 b. 93 asf jgds8 93 8 93 qn asf jgds8 c. 93

asf jgds8 93 asf jgds8 93 asf jgds8 d.None of the above. 57. Which of the following are true about the meta characters used with the regex package ? a. b. c. d.

d searches for a digit w searches for a white space. s searches for a string. b searches for either 1 or 0.

58. class test { public static void main ( String [] blah ) { System.out.printf("%s", new test()); } public String toString() { return "testing something"; } } a. Prints testing something b. Gives a runtime exception c. Prints nothing d. Prints test@13234 or something like that. e. Compile error. 59. class test { public static void main ( String [] blah ) { System.out.printf("%1$b", "123"); } public String toString() { return "testing something"; } }

a. b. c. d. e.

Prints false Prints true Runtime Exception Compile error None of tha above.

60. class sup { static void method(){System.out.println("Super");} // 1 } class test extends sup { public static void main(String args[]) {} static void method(){System.out.println("test");} } What class modifier(s) can be inserted at line 1 to prevent method() from being overridden (hidden ) without causing compile time errors ? a. b. c. d. e. f.

final private protected default transient Hiding cannot be prevented.

61. class sup { void method() throws Exception

{} }

class test extends sup { public static void main( String [] args) // 1 { sup s = new test(); s.method(); } void method() // 2 {} } What can be done to avoid a compile error ? a. b. c. d. e. 62.

Add a “throws Exception” clause at line 1 Add a “throws Exception” clause at line 2 Use a try catch block when calling s.method(). There is no compile error. None of the above.

enum num {BERT("CAFE") , KATHY("BABE")} What happens when this enum is compiled ? a. b. c. d. e.

Runtime Exceptions are thrown Compile time error BERT is equated to CAFÉ and KATHY is equated to BABE. Either, BERT is equated to CAFÉ or KATHY is equated to BABE. None of the above.

63. public class test { public static void main( String [] args ) { new test(); } test() { test(2); } test(int x) { System.out.println(x); } } a. b. c. d. e.

Prints 2 Prints 0 Does not compile Runtime Exception. None of the above.

64. class test { public static void main( String args[] ) { class ma { final int x; ma () { x = 10; System.out.println( this.x); } } new ma(); } }

What is the output ? a. b. c. d. e.

Fails to compile Runtime Exceptions are encountered Prints 10 Prints 0 None of the above.

65. StringBuffer sb = new StringBuffer("abcdef"); System.out.println( sb.append("123").delete(0,5).reverse().insert(1,"1") ); What is the output of this code fragment ? a. b. c. d. e. f. g.

321 3121 3121f 321f This fragment will not compile This fragment will throw an IndexOutOfBoundsException None of the above.

66. Which of the following are not legal identifiers ? a. b. c. d. e.

String #baby; char c123; Byte $wombat; Long long; Short ~english;

67. class test { public static void main( String args[] ) { new test().method((short)1); } void method(int... i) { System.out.println("int"); } void method(Integer i) { System.out.println("pint"); } void method(byte i) { System.out.println("bite"); } } What is printed ?

a. b. c. d. e.

pint bite int Nothing is printed None of the above.

68. Cow is an Animal and has a tail is represented by which of these classes ? a. b. c. d. e.

Class cow is a Animal { tail t; } Class cow extends Animal { String tail; } Class cow implements Animal { tail string; } Class cow extends tail { Animal ani; } None of the above.

69. class test { test tester; test( test t ) { tester = t; } test() {} public static void main( String args[] ) { test t = null; t = new test(); t = new test(t); t = null; // 1 } } How many objects are eligible for garbage collection after line //1 executes ? a. b. c. d. e.

One Two Zero This code will not compile None of the above.

70. class test { test() { try { throw new RuntimeException(); }

finally { System.out.println("Damn !"); } } public static void main( String args[] ) { try { new test(); } catch ( Throwable t ) { System.out.println("Caught"); } } } What is the output ? a. b. c. d. e. f.

Damn ! RuntimeException. Damn ! Caught RuntimeException RuntimeException caught Damn ! Caught Caught. None of the above.

71. Which of the following are valid declarations of a for in loop that loops through an int array named j? a. b. c. d. e.

foreach ( int i : j ) {} for( int I : []j ) {} for ( int I : j ) {} for ( int I : j[] ) {} None of the above.

72. Drag and drop the correct relation that is exhibited by this class. Drag the correct option on the class definition. Ignore the class names and member variable names. Choose the option based on is a and has a relationships. class a extends b implements c { int d; long e; } a. b. c. d. e.

Subaru is a car, Subaru is a vehicle, Subaru has a wheel. Chocolate is a candy, Chocolate has a taste. Girl is a hotchick, Girl is a barbie, Girl is a Girly. MonsterTruck is a truck, MonsterTruck is a Monster, MonsterTruck has 2 doors. None of the above.

Here are some extra questions on generics.

1. class test { public static void main( String args[] ) { new test().meth(new Integer(42)); // 1 new test<String>().meth("hello"); // 2 new test<String>().meth(new Object() ); // 3 } public void meth(T type) { System.out.println(type); } } Compilation at which line(s) fail ? a. b. c. d. e.

1 2 3 No compile errors Runtime error.

2. public List meth(List type) { System.out.println(type); // 1 return new ArrayList<String>(); // 2 } This code… a. b. c. d. e.

Will compile Will not Compile at Line 1 Has Runtime errors. Will not Compile at Line 2 Will not Compile at Line 1 and 2.

3. public List meth(List type) { System.out.println(type); // 1 return new ArrayList<String>(); // 2 } a. Will compile b. Will not Compile at Line 1 c. Has Runtime errors. d. Will not Compile at Line 2

e. Will not Compile at Line 1 and 2. 4. public List meth(List type) { System.out.println(type); // 1 return new ArrayList(); // 2 } a. Will compile b. Will not Compile at Line 1 c. Has Runtime errors. d. Will not Compile at Line 2 e. Will not Compile at Line 1 and 2. 5. import java.util.*; class test { List tester = new ArrayList < String >(); public static void main( String args[] ) { seth( tester ); // 2 } public static void seth(List type) { type.add("hi"); // 1 } } a. b. c. d. e.

Compile Error occurs when adding “hi” at Line 1 Runtime error Compiles without warnings and adds “hi” successfully. Compiles with warnings and adds “hi” successfully. Compile Error occurs at 2.

6. List list = new ArrayList <String > (); for ( Object o : list ) { System.out.println(o); } Choose all that are applicable. a. b. c. d.

Compile error Runtime error Nothing can be added to list Only a String reference can point to list. Hence a String reference is required in the loop.

e. None of the above. 7. List <String> list = new ArrayList <String>; // 1 list.add("hello"); // 2 list.add("My dear"); // 3 for ( Object o : list ) // 4 { System.out.println(o); // 5 } a. b. c. d. e.

Prints hello My dear Prints My dear hello Cannot predict the sequence in which the strings are printed. Compile error Runtime error.

Some non generic questions: 1. int []arr = new int[]{1,2,3,4} ; int []arr2 = new int[]{new Integer(1),2,3,4}; System.out.println( Arrays.equals( arr,arr2 )); a. b. c. d. e.

Prints true Prints false Prints nothing Compile error Runtime error.

2 int []arr = new int[]{1,2,3,4} ; String []arrstr = Arrays.toString(arr); for ( String s : arrstr ) { System.out.println(s); } a. b. c. d. e.

Prints 1 2 3 4 Prints nothing Compile error Runtime error None of the above

Related Documents

Questions
September 2019 34
Questions
October 2019 21
Questions
November 2019 17
Questions
April 2020 19
Questions
May 2020 6
Questions
October 2019 18