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
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 extends Number> 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 super String> 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.