Scjp 1.5 Questions

39
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(){

Transcript of Scjp 1.5 Questions

Page 1: Scjp 1.5 Questions

1.

int [] arr = { 1 ,2 ,3 ,4 ,5};int [] arr2 = new int[4];arr2 = arr;System.out.println(arr2[4]);

a. Compile errorb. Runtime Exception. ArrayOutOfBoundsc. Prints 4d. Prints 5e. 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 errorb. Runtime Exception. ArrayOutOfBoundsc. Prints 1 2d. Prints nulle. 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...");}}

Page 2: Scjp 1.5 Questions

a. Compile errorb. 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 nullb. Runtime NullpointerExcepionc. 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 skyb. Eye in the sky Eye in the sky Eye in the sky Eye in the skyc. Compile errord. Runtime Exceptione. None of the above.-----------------------------------------------------------------------------------------

Page 3: Scjp 1.5 Questions

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 piggiesb. Compile Error incompatible types.c. java.lang.String;@187c6c7 java.lang.String;@187c6c8 java.lang.String;@187c6c9 ( or something like that )d. Runtime NullpointerExceptione. 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); // 1Integer Ifour = new Integer(2); // 2System.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 trueb. false true falsec. false false falsed. true true falsee. Compile error-----------------------------------------------------------------------------------------

Page 4: Scjp 1.5 Questions

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 Muahahahahahhahahab. Compile errorc. Runtime Exceptiond Prints nothinge. 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();

}

Page 5: Scjp 1.5 Questions

a. Compile error unreachable codeb. Comepile error: cant throw two exceptions at the same timec. Compile error: both Exceptions need to be handled in try catch blockd. Runtime errore. 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 compileb. Nothing can be added to this List.c. Anything beside Integer could be replaced 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. When reading back from the file, test_two is the first object that is read.b. When reading back from the file, test_one is the first object that is read.c. The object cannot be read from a file named “serial.serial”.d. 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;

}}

}

Page 6: Scjp 1.5 Questions

a. Compile errorb. Runtime errorc. Prints xd. Prints 4e. 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. Compile error at 1b. Runtime error at 1c. Compiler error at 2d. Runtime error at 2e. 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

Page 7: Scjp 1.5 Questions

b. Compiler errorc. Runtime Exception occurs if mySize is accessed.

16.

class Animal{

void method throws IOException{}

}

class dog extends Animal{

void method throws FileNotFoundException{}

}

a. Compile error : Incompatible types.b. Runtime errorc. Compiles fined. Compile error : incorrect syntaxe. 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 Giraffeb. Compile errorc. Runtime error

Page 8: Scjp 1.5 Questions

d. Prints nothinge. 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 precisionb. No error. Compiles fine. Prints 0c. Runtime errord. Compiles with a warninge. 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. Prints longb. Prints floatc. Compile error: Too ambiguousd. Runtime errore. None of the above

20.

import java.util.*;

class test{

public static void main ( String [] args )

Page 9: Scjp 1.5 Questions

{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. Both are allowed and there are no warningsb. Both are not allowed - they don’t compilec. Both are allowed but the code compiles with warningsd. Meth is allowed but seth is note. 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) ); // 1list.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 oned. Line one removes Integer at index 4 and Line 2 removes the Integer Object one

22.

import java.util.*;

class test

Page 10: Scjp 1.5 Questions

{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. Prints 11b. Prints 2c. Compile errord. Runtime errore. 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. Prints true false true falseb. Prints true true true truec. Prints true false true trued. Prints false true false truee. None of the above.

24.

Page 11: Scjp 1.5 Questions

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. Compiles without warningsb. Compiles with warningsc. Only one test Object is added, the other one is considered a duplicate.d. A Runtime Exception occurs.e. 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);

}}

Page 12: Scjp 1.5 Questions

a. One Compile errorb. Runtime errorc. Compiles with warnings.d. Two Compiler errorse. 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. Prints int[]b. Prints int…c. Compile errord. Runtime errore. 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}

Page 13: Scjp 1.5 Questions

}

a. Compilations fails at 2b. Compilations fails at 1c. Runtime error at 2d. Prints hello after successfully compiling.e. Compilations fails at 1 and 2.

28.class test <T>{

public static void main( String args[] ){

new test<String>().meth("hello"); // 1}

public void meth(T type) // 2{

System.out.println(type); // 3}

}

a. Compilations fails at 1. Illegal syntaxb. Compilations fails at 2. Cannot find Tc. Compilations fails at 3. Cannot find symbol println( T )d. Prints helloe. Runtime error.

29.

class test <T>{

public static void main( String args[] ){

new test().meth(new Integer(42)); // 1new test<String>().meth("hello"); // 2new test<String>().meth(new Object() ); // 3

}

public void meth(T type){

System.out.println(type);}

}

Compilation at which line(s) fails?

a. 1

Page 14: Scjp 1.5 Questions

b. 2c. 3d. Compiles without errors.e. Compiles without errors. But Runtime errors exist.

30.

public <T> List<T> meth(List<?> type){

System.out.println(type); // 1return new ArrayList<String>(); // 2

}

This code…

a. Will compileb. Will not Compile at Line 1c. Has Runtime errors.d. Will not Compile at Line 2e. Will not Compile at Line 1 and 2.

31.

public static void main( String args[] ){

List<? extends Number> type = new ArrayList<Integer>(); // 1for ( Integer n : type ) // 2{

System.out.println(n); // 3}

}

public <T> void seth(List<?> type) // 4{

type.add("hi"); // 5}

a. Lines 2 and 5 have Compile time errorsb. Lines 1 and 5 have Compile time errors.c. Lines 2 and 5 and 1 have Compile time errorsd. Lines 4 and 5 have Compile time errorse. 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); //1System.out.println(eye.equals(d)); //2System.out.println(eye == 42 ); //3System.out.println(i == 42); //4

Page 15: Scjp 1.5 Questions

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

Which line Prints false ?

a. 1b. 2c. 3d. 4e. 5

33.

Which of the following modifiers can be used with a method local inner class ?

a. staticb. abstractc. finald. privatee. protectedf. publicg. default

34.

class test{ int x = 10; static test tester = this;public static void main( String args[] ){

System.out.println( tester.x );}

}

a. Prints 10b. Prints 0c. Compile errord. Runtime errore. 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. Prints falseb. Prints truec. Compile error at 1d. Runtime error at 1e. None of the above

36.

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

Page 16: Scjp 1.5 Questions

class test{

public static void main( String args[] ){

test test_one = new tester();face f = test_one; // 1f.meth();

}}

Choose all that apply.

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

37.

List <? super String> list = new ArrayList <String > (); // 1list.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 errorb. 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. Compile errorb. Runtime Exceptions are thrown.c. Prints 120d. Prints 12e. None of the above.

39.

public static void main(String [] args){

System.out.println(m(2));

Page 17: Scjp 1.5 Questions

}static int m(int i){

int ret;if( i == 2 ){

ret = i;}return ret;

}

a. Prints 2b. Prints 0c. Compile errord. Runtime errore. 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 4b. 1 2c. non static testd. static teste. non static Superbf. static Superb

41

Page 18: Scjp 1.5 Questions

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. Prints 8b. Compile errorc. Runtime errord. Prints nothinge. None of the above.

42.

int x=1;assert (x=2) : x;

This code…

a. Compiles without errors when assertion is disabled by defaultb. Compiles without errors when the javac –da option is used.c. Will throw compile time errors when the –ea option is used.d. Will throw runtime exception.e. 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. Prints hello 3 hi 2 bye 1b. Prints hello 2 hi 1 bye 0c. Prints bye 3 hello 2 hi 1d. Prints bye 2 hello 1 hi 0

Page 19: Scjp 1.5 Questions

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. Prints 0 1 2 3b. Prints 0 0 0 0c. Prints 0 0 3 0d. Prints 0 2 0 0 e. Compile errorf. Runtime Exception occurs.

45.

import java.util.*;class test implements Comparator<test>{

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. Sorts test instances based on variable xb. Sorts the instances in ascending orderc. Sorts the instances in descending order.d. None of the above.

46.

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

Page 20: Scjp 1.5 Questions

a. Does not compileb. Compiles but has Runtime exceptionsc. Prints 0d. Prints 2e. Prints 1f. None of the above.

47.

List l = Arrays.asList( new int[] {1,2,3,4} );for ( int i : l ){

System.out.println(i);}

a. Prints 1 2 3 4b. Prints 4 3 2 1c. Prints nothingd. Compile errore. Runtime error

48.

List <Integer> l = Arrays.asList( new int[] {1,2,3,4} ); // 1List <Object > list = new List <Object> (); // 2

Drag and drop the following options over the code fragments 1 and 2. One option may be used more than once.

Options

a. Compilesb. Does not compile.

49.

ArrayList <Exception> l2 = new ArrayList <Exception>();l2.add(new Exception());l2.add(new NumberFormatException() );

a. Compiles with no runtime exceptionsb. Does not compilec. 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)) );

Page 21: Scjp 1.5 Questions

}public boolean equals( Object o){

return ( (test)o ).x == x;}

}

a. Prints trueb. Prints falsec. Compile errord. Runtime Exceptions are encounterede. 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. Prints 1b. Prints 2c. If Line 1 is replaced with return true; then m.size() returns 1.d. 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 errorf. 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. Compile errorb. Runtime Exceptionc. Prints trued. Prints false

Page 22: Scjp 1.5 Questions

53.

Which of the following Thread class methods are static ?

a. sleepb. joinc. yieldd. waite. notifyf. 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. Replace line 1 with main = c.getMain();b. Replace line 1 with a call to coupler’s setMain method with the argument - main.c. Replace line 2 with return pain; and make no other changes to the program.d. No changes are required.e. None of the above.

55.

public static void main( String args[] ){

Set <String> set = new HashSet<String>();System.out.println(set.add("duplicate"));

Page 23: Scjp 1.5 Questions

System.out.println(set.add("duplicate"));}public boolean equals(Object o){

return false;}public int hashCode(){

return 0;}

a. Compile errorb. Runtime errorc. Prints true trued. Prints true falsee. Prints false truef. Prints false false.

56.

import java.util.regex.*;class test{

public static void main( String args[] ){

search("asf jgds8 93 qn" , "\\d\\w"); // 1search("asf jgds8 93 qn" , ".*\\d"); // 2search("asf jgds8 93 qn" , "\\d.*"); // 3search("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. 93asf jgds8 938 93 qnasf jgds8 93

b.93asf jgds8 938 93 qnasf jgds8

c.93

Page 24: Scjp 1.5 Questions

asf jgds8 93asf jgds8 93asf jgds8

d.None of the above.

57.

Which of the following are true about the meta characters used with the regex package ?

a. d searches for a digitb. w searches for a white space.c. s searches for a string.d. 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 somethingb. Gives a runtime exceptionc. Prints nothingd. 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";}

}

Page 25: Scjp 1.5 Questions

a. Prints falseb. Prints truec. Runtime Exceptiond. Compile errore. 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. finalb. privatec. protectedd. defaulte. transientf. 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. Add a “throws Exception” clause at line 1b. Add a “throws Exception” clause at line 2c. Use a try catch block when calling s.method().d. There is no compile error.e. None of the above.

62.

Page 26: Scjp 1.5 Questions

enum num{BERT("CAFE") , KATHY("BABE")}

What happens when this enum is compiled ?

a. Runtime Exceptions are thrownb. Compile time errorc. BERT is equalted to CAFÉ and KATHY is equated to BABE.d. Either, BERT is equalted to CAFÉ or KATHY is equated to BABE.e. 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. Prints 2b. Prints 0c. Does not compiled. Runtime Exception.e. 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();

}}

Page 27: Scjp 1.5 Questions

What is the output ?

a. Fails to compileb. Runtime Exceptions are encounteredc. Prints 10d. Prints 0e. 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. 321b. 3121c. 3121fd. 321fe. This fragment will not compilef. This fragment will throw an IndexOutOfBoundsExceptiong. None of the above.

66.

Which of the following are not legal identifiers ?

a. String #baby;b. char c123;c. Byte $wombat;d. Long long;e. 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 ?

Page 28: Scjp 1.5 Questions

a. pintb. bitec. intd. Nothing is printede. None of the above.

68.

Cow is an Animal and has a tail is represented by which of these classes ?

a. Class cow is a Animal { tail t; }b. Class cow extends Animal { String tail; }c. Class cow implements Animal { tail string; }d. Class cow extends tail { Animal ani; }e. 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. Oneb. Twoc. Zerod. This code will not compilee. None of the above.

70.class test{

test(){

try{

throw new RuntimeException();}

Page 29: Scjp 1.5 Questions

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. Damn ! RuntimeException.b. Damn ! Caught RuntimeExceptionc. RuntimeException caughtd. Damn ! Caughte. Caught.f. 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. foreach ( int i : j ) {} b. for( int I : []j ) {}c. for ( int I : j ) {}d. for ( int I : j[] ) {}e. 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. Subaru is a car, Subaru is a vehicle, Subaru has a wheel.b. Chocolate is a candy, Chocolate has a taste.c. Girl is a hotchick, Girl is a barbie, Girl is a Girly.d. MonsterTruck is a truck, MonsterTruck is a Monster, MonsterTruck has 2 doors.e. None of the above.

Here are some extra questions on generics.

Page 30: Scjp 1.5 Questions

1. class test{

public static void main( String args[] ){

new test().meth(new Integer(42)); // 1new test<String>().meth("hello"); // 2new test<String>().meth(new Object() ); // 3

}

public <T>void meth(T type){

System.out.println(type);}

}

Compilation at which line(s) fail ?

a. 1b. 2c. 3d. No compile errorse. Runtime error.

2.public <T> List<T> meth(List<?> type){

System.out.println(type); // 1return new ArrayList<String>(); // 2

}

This code…

a. Will compileb. Will not Compile at Line 1c. Has Runtime errors.d. Will not Compile at Line 2e. Will not Compile at Line 1 and 2.

3.public <T> List<?> meth(List<T> type){

System.out.println(type); // 1return new ArrayList<String>(); // 2

}

a. Will compileb. Will not Compile at Line 1c. Has Runtime errors.d. Will not Compile at Line 2

Page 31: Scjp 1.5 Questions

e. Will not Compile at Line 1 and 2.

4.

public <T> List<?> meth(List<?> type){

System.out.println(type); // 1return new ArrayList<?>(); // 2

}

a. Will compileb. Will not Compile at Line 1c. Has Runtime errors.d. Will not Compile at Line 2e. Will not Compile at Line 1 and 2.

5.

import java.util.*;class test {

List<? super String> tester = new ArrayList < String >();public static void main( String args[] ){

seth( tester ); // 2}

public static <T> void seth(List<? super String> type){

type.add("hi"); // 1}

}

a. Compile Error occurs when adding “hi” at Line 1b. Runtime errorc. Compiles without warnings and adds “hi” successfully.d. Compiles with warnings and adds “hi” successfully.e. Compile Error occurs at 2.

6.List <? extends String> list = new ArrayList <String > ();for ( Object o : list ){

System.out.println(o);}

Choose all that are applicable.

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

Page 32: Scjp 1.5 Questions

e. None of the above.

7.List <String> list = new ArrayList <String>; // 1list.add("hello"); // 2list.add("My dear"); // 3for ( Object o : list ) // 4{

System.out.println(o); // 5}

a. Prints hello My dearb. Prints My dear helloc. Cannot predict the sequence in which the strings are printed.d. Compile errore. 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. Prints trueb. Prints falsec. Prints nothingd. Compile errore. 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. Prints 1 2 3 4b. Prints nothingc. Compile errord. Runtime errore. None of the above