Cheatsheet - Sun Java2 Scjp

  • November 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 Cheatsheet - Sun Java2 Scjp as PDF for free.

More details

  • Words: 10,782
  • Pages: 50
Cheet Sheets: Sun Java Certified Professional (310-025) Word 97 Edition Copyright Cheet Sheets 2001 Last Updated: 10/01/01 **The following questions have appeared on actual exams. Situational details (ie, names, locations, and quantities) have been changed to avoid copyright infringement. Memorize as many of these questions as possible in the 5 nights prior to your exam. Good luck. –Keen Interactive

1. 1. 2. 3. 4. 5. 6. 7.

Given:

public class test ( public static void main (String args[]) int i = 0xFFFFFFF1; int j = ~i;

{

} )

What is the decimal value of j at line 5? A. B. C. D. E. F.

0 1 14 –15 An error at line 3 causes compilation to fail. An error at line 4 causes compilation to fail. 2.

Given:

Integer i = new Integer (42); Long 1 = new Long (42); Double d = new Double (42.0); Which two expressions evaluate to True? (Choose Two) A. B. C. D. E. F.

(i ==1) (i == d) (d == 1) (i.equals (d)) (d.equals (i)) (i.equals (42)) 3.

1. 2. 3. 4. 5. 6. 7. 8.

Click the exhibit button:

public class test ( private static int j = 0; private static boolean methodB(int k) ( j += k; return true; )

1

9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19.

public static void methodA(int i) { boolean b: b = i < 10 | methodB (4); b = i < 10 || methodB (8); ) public static void main (String args[] } ( methodA (0); system.out.printIn(j); ) )

What is the result? A. B. C. D. E.

The program prints “0” The program prints “4” The program prints “8” The program prints “12” The code does not complete 4.

1. 2. 3. 4. 5.

Given:

Public class test ( Public static void main (String args[]) ( System.out.printIn (6 ^ 3); ) )

What is the output? Ans:

5. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12.

Given:

public class Foo { public static void main (String [] args) { StringBuffer a = new StringBuffer (“A”); StringBuffer b = new StringBuffer (“B”); operate (a,b); system.out.printIn{a + “,” +b}; ) static void operate (StringBuffer x, StringBuffer y) { x.append {y}; y = x; ) }

What is the result? A. B. C. D. E. F.

The code compiles and prints “A,B”. The code compiles and prints “A,A”. The code compiles and prints “B,B”. The code compiles and prints “AB,B”. The code compiles and prints “AB,AB”. The code does not compile because “+” cannot be overloaded for StringBuffer.

2

6. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19.

Click the exhibit button:

Public class test ( Public static void stringReplace (String text) ( Text = text.replace (‘j’ , ‘i’); ) public static void bufferReplace (StringBuffer text) ( text = text.append (“C”) ) public static void main (String args[]} ( String textString = new String (“java”); StringBuffer text BufferString = new StringBuffer (“java”); stringReplace (textString); BufferReplace (textBuffer); System.out.printIn (textString + textBuffer); } )

What is the output? Ans:

7.

Click the Exhibit button:

1. public class test { 2. public static void add3 (Integer i) } 3. int val = i.intValue ( ); 4. val += 3; 5. i = new Integer (val); 6. } 7. 8. public static void main (String args [ ] ) { 9. Integer i = new Integer (0); 10. add3 (i); 11. system.out.printIn (i.intValue ( ) ); 12. } 13. ) What is the result? A. B. C. D.

Compilation will fail. The program prints “0”. The program prints “3”. Compilation will succeed but an exception will be thrown at line 3. 8.

1. 2. 3. 4.

Given:

public class ConstOver { public ConstOver (int x, int y, int z) { } }

3

Which two overload the ConstOver constructor? (Choose Two) A. B. C. D. E.

ConstOver ( ) { } Protected int ConstOver ( ) { } Private ConstOver (int z, int y, byte x) { } public Object ConstOver (int x, int y, int z) { } public void ConstOver (byte x, byte y, byte z) { } 9.

Given:

1. 2. 3. 4.

public class MethodOver { public void setVar (int a, int b, float c) { } }

Which two overload the setVar method? (Choose Two) A. B. C. D. E.

private void setVar (int a, float c, int b) { } protected void setVar (int a, int b, float c) { } public int setVar (int a, float c, int b) (return a;) public int setVar (int a, int b, float c) (return a;) protected float setVar (int a, int b, float c) (return c;) 10. Given:

1. 2. 3. 4. 5. 6. 7. 8.

class BaseClass { Private float x = 1.0f ; protected float getVar ( ) ( return x;) } class Subclass extends BaseClass ( private float x = 2.0f; //insert code here )

Which two are valid examples of method overriding? (Choose Two) A. B. C. D. E.

float getVar ( ) { return x;} public float getVar ( ) { return x;} float double getVar ( ) { return x;} protected float getVar ( ) { return x;} public float getVar (float f ) { return f;} 11. Which two demonstrate an “is a” relationship? (Choose Two)

A. B. C. D. E.

public interface Person { } public class Employee extends Person { } public interface Shape { } public class Employee extends Shape { } public interface Color { } public class Employee extends Color { } public class Species { } public class Animal (private Species species;) interface Component { } Class Container implements Component ( Private Component[ ] children; )

4

12. Which statement is true? A. B. C. D. E.

An anonymous inner class may be declared as final An anonymous inner class can be declared as private An anonymous inner class can implement multiple interfaces An anonymous inner class can access final variables in any enclosing scope Construction of an instance of a static inner class requires an instance of the enclosing outer class.

13. Given: 1. 2. 3. 4. 5. 6.

package foo; public class Outer ( public static class Inner ( ) )

Which statement is true? A. B. C. D.

An instance of the Inner class can be constructed with “new Outer.Inner ()” An instance of the inner class cannot be constructed outside of package foo An instance of the inner class can only be constructed from within the outer class From within the package bar, an instance of the inner class can be constructed with “new inner ()”

14. Click the exhibit button: 1. 2. 3. 4. 5. 6. 7. 8. 9.

public class enclosingone ( public class insideone{} ) public class inertest( public static void main (string[]args)( enclosingone eo= new enclosingone (); //insert code here ) )

Which statement at line 7 constructs an instance of the inner class? A. B. C. D.

InsideOnew ei= eo.new InsideOn(); Eo.InsideOne ei = eo.new InsideOne(); InsideOne ei = EnclosingOne.new InsideOne(); EnclosingOne.InsideOne ei = eo.new InsideOne(); 15. Click the exhibit button:

5

1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14.

interface foo { int k = 0; ] public class test implements Foo ( public static void main(String args[]) ( int i; Test test = new test (); i= test.k; i= Test.k; i= Foo.k; ) )

What is the result? A. B. C. D. E.

Compilation succeeds. An error at line 2 causes compilation to fail. An error at line 9 causes compilation to fail. An error at line 10 causes compilation to fail. An error at line 11 causes compilation to fail.

16. Given: 1. 2. 3. 4. 5.

6. 7. 8.

//point X public class foo ( public static void main (String[]args) throws Exception { printWriter out = new PrintWriter (new java.io.outputStreamWriter (System.out), true; out.printIn(“Hello”); } )

Which statement at PointX on line 1 allows this code to compile and run? A. B. C. D. E.

import java.io.PrintWriter; include java.io.PrintWriter; import java.io.OutputStreamWriter; include java.io.OutputStreamWriter; no statement is needed.

17. Which two statements are reserved words in Java? (Choose Two) A. B. C. D.

run import default implement

18. Which three are valid declarations of a float? (Choose Three) A. B. C. D.

float foo = -1; float foo = 1.0; float foo = 42e1; float foo = 2.02f;

6

E. F.

float foo = 3.03d; float foo = 0x0123;

19. Given: 8. int index = 1; 9. boolean[] test = new Boolean[3]; 10. boolean foo= test [index]; What is the result? A. B. C. D. E. F.

foo has the value of 0 foo has the value of null foo has the value of true foo has the value of false an exception is thrown the code will not compile

20. Given: 1. 2. 3. 4. 5. 6. 7.

public class test( public static void main(string[]args){ string foo = args [1]; string foo = args [2]; string foo = args [3]; } }

And the command line invocation: Java Test red green blue What is the result? A. B. C. D. E. F. G.

baz has the value of “” baz has the value of null baz has the value of “red” baz has the value of “blue” bax has the value of “green” the code does not compile the program throws an exception

21. Given: 8. 9. 10. 11.

int index = 1; int [] foo = new int [3]; int bar = foo [index]; int baz = bar + index;

What is the result? A. B. C. D. E.

baz has the value of 0 baz has the value of 1 baz has the value of 2 an exception is thrown the code will not compile

22. Given:

7

1. 2. 3. 4. 5. 6.

public class foo { public static void main (String[]args) { String s; system.out.printIn (“s=” + s); } }

What is the result? A. B. C. D. E.

The code compiles and “s=” is printed. The code compiles and “s=null” is printed. The code does not compile because string s is not initialized. The code does not compile because string s cannot be referenced. The code compiles, but a NullPointerException is thrown when toString is called.

23. Which will declare a method that forces a subclass to implement it? A. B. C. D. E.

Public double methoda(); Static void methoda (double d1) {} Public native double methoda(); Abstract public void methoda(); Protected void methoda (double d1){} 24. You want subclasses in any package to have access to members of a superclass. Which is the most restrictive access modifier that will accomplish this objective?

A. B. C. D. E.

Public Private Protected Transient No access modifier is qualified 25. Given: 1. 2. 3. 4. 5. 6. 7.

abstract class abstrctIt { abstract float getFloat (); ) public class AbstractTest extends AbstractIt { private float f1= 1.0f; private float getFloat () {return f1;} }

What is the result? A. B. C. D.

Compilation is successful. An error on line 6 causes a runtime failure. An error at line 6 causes compilation to fail. An error at line 2 causes compilation to fail.

8

26. Click the exhibit button: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13.

public class test( public int aMethod()[ static int i=0; i++; return I; ) public static void main (String args[]){ test test = new test(); test.aMethod(); int j = test.aMethod(); System.out.printIn(j); ] }

What is the result? A. B. C. D.

Compilation will fail. Compilation will succeed and the program will print “0”. Compilation will succeed and the program will print “1”. Compilation will succeed and the program will print “2”. 27. Given: 1. 2. 3. 4. 5. 6. 7.

class super { public float getNum() {return 3.0f;} ) public class Sub extends Super { )

Which method, placed at line 6, will cause a compiler error? A. B. C. D.

public float getNum() {return 4.0f; } public void getNum () { } public void getNum (double d) { } public double getNum (float d) {retrun 4.0f; } 28. Which declaration prevents creating a subclass of an outer class? A. B. C. D. E.

static class FooBar{} private class FooBar{} abstract public class FooBar{} final public class FooBar{} final abstract class FooBar{}

29. Given: 1. 2. 3.

byte [] arry1, array2[]; byte array3 [][]; byte[][] array4;

If each array has been initialized, which statement will cause a compiler error?

9

A. B. C. D. E. F.

array2 = array1; array2 = array3; array2 = array4; both A and B both A and C both B and C

30. Click the exhibit button: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18.

class super ( public int I = 0; public super (string text) ( I=1 ) ) public class sub extends super ( public sub (string text) ( i= 2 ) public static void main (straing args[]) ( sub sub = new sub (“Hello”); system.out. PrintIn(sub.i); ) )

What is the result? A. Compilation will fail. B. Compilation will succeed and the program will print “0”. C. Compilation will succeed and the program will print “1”. D. Compilation will succeed and the program will print “2”. 31. Given: 1. 2. 3. 4. 5.

public class returnIt ( returnType methodA(byte x, double y) ( return (short) x/y * 2; ) )

What is the valid returnType for methodA in line 2? A. B. C. D. E. F.

int byte long short float double

32. Given the ActionEvent, which method allows you to identify the affected component? A.

getClass

10

B. C. D. E.

getTarget getSource getComponent getTargetComponent

33. Which is a method of the MouseMotionListener interface? A. B. C. D. E.

Public void mouseMoved(MouseEvent) Public boolean mouseMoved(MouseEvent) Public void mouseMoved(MouseMotionEvent) Public boolean MouseMoved(MouseMotionEvent) Public boolean mouseMoved(MouseMotionEvent)

11

34. Click the exhibit button: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29.

import java.awt*; public class X extends Frame ( public static void main(string []args) ( X x = new X (); X.pack(); x.setVisible(true); ) public X () ( setlayout (new GridLayout (2,2)); Panel p1 = new panel(); Add(p1); Button b1= new Button (“One”); P1.add(b1); Panel p2 = new panel(); Add(p2); Button b2= new Button (“Two”); P2.add(b2); Button b3= new Button (“Three”); add(b3); Button b4= new Button (“Four”); add(b4); ) )

Which two statements are true? (Choose Two) A. B. C. D.

All the buttons change height if the frame height is resized. All the buttons change width if the Frame width is resized. The size of the button labeled “One” is constant even if the Frame is resized. Both width and height of the button labeled “Three” might change if the Frame is resized.

35. You are assigned the task of building a panel containing a TextArea at the top, a label directly below it, and a button directly below the label. If the three components are added directly to the panel, which layout manager can the panel use to ensure that the TextArea absorbs all of the free vertical space when the panel is resized? A. B. C. D. E.

GridLayout CardLayout FlowLayout BorderLayout GridBagLayout

36. Which of the following gets the name of the parent directory file “file.txt”? A. B. C.

String name= File.getParentName(“file.txt”); String name= (new File(“file.txt”)).getParent(); String name = (new File(“file.txt”)).getParentName();

12

D. E.

String name= (new File(“file.txt”)).getParentFile(); Directory dir=(new File (“file.txt”)).getParentDir(); String name= dir.getName();

37. Which can be used to encode charS for output? A. B. C. D. E.

java.io.OutputStream java.io.OutputStreamWriter java.io.EncodeOutputStream java.io.EncodeWriter java.io.BufferedOutputStream

38. The file “file.txt” exists on the file system and contains ASCII text. Given: 38. try { 39. File f = new File(“file.txt”); 40. OutputStream out = new FileOutputStream(f, true); 41. } 42. catch (IOException) {} What is the result? A. B. C. D. E.

The code does not compile. The code runs and no change is made to the file. The code runs and sets the length of the file to 0. An exception is thrown because the file is not closed. The code runs and deletes the file from the file system.

39. Which constructs a DataOutputStream? A. B. C. D. E. F.

new dataOutputStream(“out.txt”); new dataOutputStream(new file(“out.txt”)); new dataOutputStream(new writer(“out.txt”)); new dataOutputStream(new FileWriter(“out.txt”)); new dataOutputStream(new OutputStream(“out.txt”)); new dataOutputStream(new FileOutputStream(“out.txt”));

40. What writes the text “<end>” to the end of the file “file.txt”? A. B. C. D.

OutputStream out= new FileOutputStream (“file.txt”); Out.writeBytes (“<end>/n”); OutputStream os= new FileOutputStream (“file.txt”, true); DataOutputStream out = new DataOutputStream(os); out.writeBytes (“<end>/n”); OutputStream os= new FileOutputStream (“file.txt”); DataOutputStream out = new DataOutputStream(os); out.writeBytes (“<end>/n”); OutputStream os= new OutputStream (“file.txt”, true); DataOutputStream out = new DataOutputStream(os); out.writeBytes (“<end>/n”);

41. Given: 1. 2.

public class X ( public object m () {

13

3. 4. 5. 6. 7. 8. 9.

object o = new float (3.14F); object [] oa = new object [1]; oa[0]= o; o = null; return oa[0]; } }

When is the float object created in line 3, eligible for garbage collection? A. B. C. D.

Just after line 5 Just after line 6 Just after line 7 (that is, as the method returns) Never in this method

14

42. Given: 3. string foo = “ABCDE”; 4. foo.substring(3); 5. foo.concat(“XYZ”); 6. Type the value of foo at line 6. Ans:

43. Which method is an appropriate way to determine the cosine of 42 degrees? A. B. C. D. E.

double d = Math.cos(42); double d = Math.cosine(42); double d = Math.cos(Math.toRadians(42)); double d = Math.cos(Math.toDegrees(42)); double d = Math.cosine(Math.toRadians(42));

44. You need to store elements in a collection that guarantees that no duplicates are stored and all elements can be accessed in natural order. Which interface provides that capability? A. B. C. D. E. F.

java.util.Map java.util.Set java.util.List java.util.StoredSet java.util.StoredMap java.util.Collection

45. Which statement is true for the class java.util.HashSet? A. B. C. D. E.

The elements in the collection are ordered. The collection is guaranteed to be immutable. The elements in the collection are guaranteed to be unique. The elements in the collection are accessed using a unique key. The elements in the collections are guaranteed to be synchronized.

46. Given: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10.

public class IfTest ( public static void main(string[]args) { int x = 3; int y = 1; if (x = y) system.out.printIn(“Not equal”); else system.out.printIn(“Equal”); } )

What is the result? A. B.

The output is “Equal”. The output in “Not Equal”.

15

C. D.

An error at line 5 causes compilation to fall. The program executes but does not print a message.

47. Click the exhibit button: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12.

public class test ( public static void main(string args[]) { int 1= 0; while (i) { if (i==4) { break; ) ++i; ) ) )

What is the value of i at line 10? A. B. C. D. E.

0 3 4 5 the code will not compile

48. Given: 3. int i= 1, j= 10 ; 4. do ( 5. if (i++> --j) continue; 6. ) while (i<5); After execution, what are the values for I and j? A. B. C. D. E.

i = 6 and j= 5 i = 5 and j= 5 i = 6 and j= 4 i = 5 and j= 6 i = 6 and j= 6

49. Given: 1. 2. 3. 4.

switch (i) { default: System.out.printIn(“Hello”); )

What are the two acceptable types for the variable i? (Choose Two) A. B. C. D. E.

char byte float double object

16

50. Given: 1. 2. 3. 4. 5. 6.

public class foo { public static void main (string[]args) try {return;} finally {system.out.printIn(“Finally”);} } )

What is the result? A. B. C. D.

The program runs and prints nothing. The program runs and prints “Finally”. The code compiles, but an exception is thrown at runtime. The code will not compile because the catch block is missing.

17

51. Click the exhibit button. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15.

import java.io.IOException; public class ExceptionTest( public static void main (String[]args) try ( methodA(); ) catch (IOException e) ( system.out.printIn(“Caught IOException”); ) catch (Exception e) ( system.out.printIn(“Caught Exception”); ) ) public void methodA () { throw new IOException (); ) )

What is the result? A. The code will not compile. B. The output is caught exception. C. The output is caught IOException. D. The program executes normally without printing a message. 52. Click the exhibit button: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26.

public class test { public static string output = “” public static void foo(int i) { try { if(i= =1) { throw new Exception (); } output += “1”; ) catch(Exception e) { output += “2”; return; ) finally ( output += “3”; ) output += “4”; ) public static void main (string args[]) ( foo(0); foo(1); ) )

18

What is the value of the variable output at line 24? Ans:

53. Given: 1. 2. 3. 4. 5. 6. 7. 8.

public class Foo implements Runnable ( public void run (Thread t) { system.out.printIn(“Running.”); } public static void main (String[] args) { new thread (new Foo()).start(); ) )

What is the result? A. B. C. D. E.

An exception is thrown The program exists without printing anything An error at line 1 causes compilation to fail. An error at line 2 causes the compilation to fail. “Running” is printed and the program exits

54. Which statement is true? A. B. C. D.

If only one thread is blocked in the wait method of an object, and another thread executes the modify on that same object, then the first thread immediately resumes execution. If a thread is blocked in the wait method of an object, and another thread executes the notify method on the same object, it is still possible that the first thread might never resume execution. If a thread is blocked in the wait method of an object, and another thread executes the notify method on the same object, then the first thread definitely resumes execution as a direct and sole consequence of the notify call. If two threads are blocked in the wait method of one object, and another thread executes the notify method on the same object, then the first thread that executed the wait call first definitely resumes execution as a direct and sole consequence of the notify call.

55. Which two CANNOT directly cause a thread to stop executing? (Choose Two) A. B. C. D. E.

Calling the yield method Calling the wait method on an object Calling the notify method on an object Calling the NotifyAll method on an object Calling the start method on another Thread object

56. Which two can be used to create a new Thread? (Choose Two) A. B. C. D. E.

Extend java.lang.Thread and override the run method. Extend java.lang.Runnable and override the start method. Implement java.lang.thread and implement the run method. Implement java.lang.Runnable and implement the run method. Implement java.lang.Thread and implement the start method.

57. Given:

19

1. 2. 3. 4. 5. 6. 7. 8.

public class SyncTest ( private int x; private int y; private synchronized void setX (int i) (x=1;) private synchronized void setY (int i) (y=1;) public void setXY(int 1)(set X(i); setY(i);) public synchronized Boolean check() (return x !=y;) )

Under which conditions will check () return true when called from a different class? A. B. C. D.

Check() can never return true Check() can return true when setXY is called by multiple threads Check() can return true when multiple threads call setX and setY separately. Check() can only return true if SyncTest is changed to allow x and y to be set separately.

58. Click the exhibit button: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22.

class A implements runable ( int i; public void run () ( try ( thread.sleep(5000); i= 10; ) catch(InterruptedException e) {} ) ) public class Test { public static void main (string args[]) ( try ( A a = new A (); Thread t = new Thread (a); t.start(); int j= a.i; ) catch (Exception e) {} ) )

Which statement al line 17 will ensure that j=10 at line 19? A. B. C. D. E. F. G.

a.wait(); t.wait(); t.join(); t.yield(); t.notify(); a.notify(); t.interrupt();

59. Click the exhibit button: 1.

public class X implements Runnable (

20

2. private int x; 3. private int y; 4. 5. public static void main(String [] args) ( 6. X that = new X(); 7. (new Thread(that)) . start( ); 8. (new Thread(that)) . start( ); 9. ) 10. 11. public synchronized void run( ) ( 12. for (;;) ( 13. x++; 14. y++; 15. System.out.printIn(“x = “ + x + “, y = “ + y); 16. ) 17. ) 18. ) What is the result? A. B.

An error at line 11 causes compilation to fail. Errors at lines 7 and 8 cause compilation to fail. C. The program prints pairs of values for x and y that might not always be the same on the same line (for example, “x=2, y=1”) D. The program prints pairs of values for x and y that are always the same on the same line (for example, “x=1, y=1”. In addition, each value appears twice (for example, “x=1, y=1” followed by “x=1, y=1”) E. The program prints pairs of values for x and y that are always the same on the same line (for example, “x=1, y=1”. In addition, each value appears twice (for example, “x=1, y=1” followed by “x=2s, y=2”)

60. Which two CANNOT directly cause a thread to stop executing? (Choose Two) A. B. C. D. E.

Existing from a synchronized block Calling the wait method on an object Calling notify method on an object Calling read method on an InputStream object Calling the SetPriority method on a Thread object

61. Click the exhibit button:

21

1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30.

public class SyncTest{ public static void main(String[] args) { final StringBuffer s1= new StringBuffer(); final StringBuffer s2= new StringBuffer(); new Thread () { public void run() { synchronized(s1) { s2.append(“A”); synchronized(s2) { s2.append(“B”); System.out.print(s1); System.out.print(s2); } } } }.start(); new Thread() { public void run() { synchronized(s2) { s2.append(“C”); synchronized(s1) { s1.append(“D”); System.out.print(s2); System.out.print(s1); } } } }.start(); } }

Which two statements are true? (Choose Two) A. B. C. D. E.

The program prints “ABBCAD” The program prints “CDDACB” The program prints “ADCBADBC” The output is a non-deterministic point because of a possible deadlock condition The output is dependent on the threading model of the system the program is running on.

62. Which method in the Thread class is used to create and launch a new thread of execution? A. B. C. D. E. F.

run(); start(); execute(); run(Runnable r); start(Runnable r); execute(Thread t);

63. Given: 5. 6. 7. 8.

String foo = “base”; foo.substring(0,3); foo.concat(“ket”)

22

Type the value of foo at line 8. Ans:

64. Which code determines the int value foo closest to, but not greater than, a double value bar? A. B. C. D. E. F.

int foo = (int) Math.max(bar); int foo = (int) Math.min(bar); int foo = (int) Math.abs(bar); int foo = (int) Math.ceil(bar); int foo = (int) Math.floor(bar); int foo = (int) Math.round(bar);

65. Which statement is true? A. B. C. D. E.

A flow layout can be used to position a component that should resize horizontally when the container is resized. A grid layout can be used to position a component that should maintain a constant size even when the container is resized. A border layout can be used to position a component that should maintain a constant size even when the container is resized. The grid bag layout can be used to give a grid-like layout which differs from the normal grid in that individual rows and columns can have unique sizes. If two components are placed in the same column of a grid bag layout, and one component resizes horizontally, then the other component must resize horizontally.

66. Given an ActionEvent, which method allows you to identify the affected Component? A. B. C. D. E. F.

public class getClass() public Object getSource() public Component getSource() public Component getTarget() public Component getComponent() public Component getTargetComponent()

67. Click the exhibit button: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15.

import java.awt.*; public class Test extends Frame { public Test() { add(new Label(“Hello”) ); add(new TextField(“Hello”) ); add(new Button(“Hello”) ); pack(); show(); } public static void main(String args[]) { new Test (); } )

23

What is the result? A. B. C. D. E. F.

The code will not compile. A Window will appear containing only a Button. An IllegalArgumentException is thrown at line 6. A Window button will appear but will not contain the Label, TextField, or Button. A Window will appear containing a Label at the top, a TextField below the Label, and a Button below the TextField. A Window will appear containing a Label on the left, a TextField to the right of the Label, and a button to the right of the TextField.

68. Click the exhibit button: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16.

class A { public int getNumber(int a) { return a + 1; } } class B extends A { public int getNumber (int a) { return a + 2 } public static void main (String args[]) { A a = new B(); System.out.printIn(a.getNumber(0)); } }

What is the result? A. B. C. D. E.

Compilation succeeds and 1 is printed. Compilation succeeds and 2 is printed. An error at line 8 causes compilation to fail. An error at line 13 causes compilation to fail. An error at line 14 causes compilation to fail.

69. Given: 1. 2. 3. 4. 5. 6. 7. 8.

class BaseClass{ private float x= 1.0f; protected void setVar (float f) {x = f;} } class SubClass exyends BaseClass { private float x = 2.0f; //insert code here }

Which two are valid examples of method overriding? (Choose Two) A. B. C.

void setVar(float f) {x = f;} public void setVar(int f) {x = f;} public void setVar(float f) {x = f;}

24

D. E. F.

public double setVar(float f) {x = f;} public final void setVar(float f) {x = f;} protected float setVar() {x=3.0f; return 3.0f; }

70. Which statement about static inner classes is true? A. B. C. D.

An anonymous class can be declared as static. A static inner class cannot be a static member of the outer class A static inner class does not require an instance of the enclosing class. Instance members of a static inner class can be referenced using the class name of the static inner class.

71. Click the exhibit button: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16.

class A { public byte getNumber () { return 1; } } class B extends A { public short getNumber() { return 2; } public static void main (String args[]) { B b = new B (); System.out.printIn(b.getNumber()) } }

What is the result? A. B. C. D. E.

Compilation succeeds and 1 is printed. Compilation succeeds and 2 is printed. An error at line 8 causes compilation to fail. An error at line 14 causes compilation to fail. Compilation succeeds but an exception is thrown at line 14.

72. Given: AnInterface is an interface. AnAdapter0 is a non-abstract, non-final class with a zero argument constructor. AnAdapter1 is a non-abstract, non-final class without a zero argument constructor, but with a constructor that takes one int argument. Which two construct an anonymous inner class? (Choose Two) A. B. C. D. E.

AnAdapter1 aa=new AnAdapter1(){} AnAdapter0 aa=new AnAdapter0(){} AnAdapter0 aa=new AnAdapter0(5){} AnAdapter1 aa=new AnAdapter1(5){} AnInterface a1=new AnInterface(5){}

73. Which two statements are true? (Choose Two)

25

A. B. C. D. E.

An inner class may be declared as static An anonymous inner class can be declared as public. An anonymous inner class can be declared as private. An anonymous inner class can extend an abstract class. An anonymous inner class can be declared as protected.

74. Click the exhibit button: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13.

public class Mycircle { public double radius; public double diameter; public void setRadius(double radius) this.radius = radius; this.diameter= radius * 2; } public double getRadius() { return radius; } }

Which statement is true? A. B. C. D.

The Mycircle class is fully encapsulated. The diameter of a given MyCircle is guaranteed to be twice its radius. Lines 6 and 7 should be in a synchronized block to ensure encapsulation. The radius of a MyCircle object can be set without affecting its diameter.

75. You want to limit access to a method of a public class to members of the same class. Which access modifier accomplishes this objective? A. B. C. D. E.

Public Private Protected Transient No access modifier is required

76. Click the exhibit button.

26

ClassOne.java 1. package com.abc.pkg1; 2. public class ClassOne { 3. private char var = ‘a’; 4. char getVar() {return var;} 5. } ClassTest.java 1. package com.abc.pkg2; 2. import com.abc.pkg1.ClassOne; 3. public class ClassTest extends ClassOne { 4. public static void main(String[]args) { 5. char a = new ClassOne().getVar(); 6. char b = new ClassTest().getVar(); 7. } 8. } What is the result? A. B. C. D.

Compilation will fail. Compilation succeeds and no exceptions are thrown. Compilation succeeds but an exception is thrown at line 5 in ClassTest.java. Compilation succeeds but an exception is thrown at line 6 in ClassTest.java.

77. Given: 1. 2. 3. 4. 5. 6. 7. 8.

public class ArrayTest { public static void main (String[]args) { float f1[], f2[]; f1 = new float [10]; f2 = f1; System.out.printIn (“f2[0]=” + f2[0]); } }

What is the result? A. B. C. D. E.

It prints f2[0] = 0.0. It prints f2[0] = NaN. An error at line 5 causes compile to fail. An error at line 6 causes compile to fail. An error at line 6 causes an exception at runtime.

78. Which two statements are true regarding the creation of a default constructor? (Choose Two) A. B. C. D. E.

The default constructor initializes method variables. The compiler always creates a default constructor for every class. The default constructor invokes the no-parameter constructor of the superclass. The default constructor initializes the instance variables declared in the class. When a class has only constructors with parameters, the compiler does not create a default constructor.

79. Click the exhibit button:

27

1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14.

class super { public int getLength() {return 4;} } public class Sub extends Super { public long getLength() {return 5;} public static void main (String[]args) { super sooper = new Super (); Sub sub = new Sub(); System.out.printIn( sooper.getLength()+ “,” + sub.getLength() }; } }

What is the output? A. B. C. D. E.

4, 4 4, 5 5, 4 5, 5 the code will not compile

80. Given: 1. 2. 3. 4. 5. 6. 7. 8.

public abstract class Test { public abstract void methodA(); public abstract void methodB() { System.out.printIn(“Hello”); } }

Which three changes (made independently) allow the code to compile? (Choose Three) A. B. C. D. E. F.

Add a method body to methodA. Replace lines 5-7 with a semicolon (“.”). Remove the abstract qualifier from the declaration of Test. Remove the abstract qualifier from the declaration of methodB. Remove the abstract qualifier from the declaration of methodA. Remove methodB in its entirely and change class o interface in line 1.

81. Which determines if “prefs” is a directory and exists on the file system? A. Boolean exists=Directory.exists (“prefs”); B. Boolean exists=(new File(“prefs”)).isDir(); C. Boolean exists=(new Directory(“prefs”)).exists(); D. Boolean exists=(new File(“prefs”)).isDirectory(); E. Boolean exists=true; Try{ Directory d = new Directory(“prefs”); } catch (FileNotFoundException e) { exists = false;

28

} 82. Which two create an InputStream and open file the “file.txt” for reading? (Choose Two) A. B. C. D. E.

InputStream in=new FileReader(“file.txt”); InputStream in=new FileInputStream(“file.txt”); InputStream in=new InputStreamFileReader (“file.txt”, “read”); FileInputStream in=new FileReader(new File(“file.txt”)); FileInputStream in=new FileInputStream(new File(“file.txt”));

83. Which two construct an OutputSream that appends to the file “file.txt”? (Choose Two) A. B. C. D. E.

OutputStream out=new FileOutputStream(“file.txt”); OutputStream out=new FileOutputStream(“file.txt”, “append”); FileOutputStream out=new FileOutputStream(“file.txt”, true); FileOutputStream out=new FileOutputStream(new file(“file.txt”)); OutputStream out=new FileOutputStream(new File(“file.txt”)true);

84. Which constructs a BufferedInputStream? A. B. C. D. E. F.

New BufferedInputStream(“in.txt”); New BufferedInputStream(new File(“in.txt”)); New BufferedInputStream(new Writer(“in.txt”)); New BufferedInputStream(new Writer(“in.txt”)); New BufferedInputStream(new InputStream(“in.txt”)); New BufferedInputStream(new FileInputStream(“in.txt”)); 85. Which is a valid identifier? A. B. C. D.

false default _object a-class

86. Click the exhibit button:

1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19.

package foo; import java.util.Vector; private class MyVector extends Vector { int i = 1; public MyVector() { i = 2; } } public class MyNewVector extends MyVector { public MyNewVector () { i = 4; } public static void main (String args []) { MyVector v = new MyNewVector(); } }

29

The file MyNewVector.java is shown in the exhibit. What is the result? A. B. C. D. E.

Compilation will succeed Compilation will fail at line 5 Compilation will fail at line 6 Compilation will fail at line 14 Compilation will fail at line 17

87. Given: 1. 2. 3. 4. 5. 6. 7. 8.

public class Test { public static void main (String[]args) { String foo = args[1]; String bar = args[2]; String baz = args[3]; System.out.printIn(“baz = ” + baz); } }

And the output: Baz = 2 Which command line invocation will produce the output? A. B. C. D.

java Test 2222 java Test 1 2 3 4 java Test 4 2 4 2 java Test 4 3 2 1

88. Given: 8. int index = 1; 9. String [] test = new String[3]; 10. String foo = test[index]; What is the result? A. B. C. D.

foo has the value “” foo has the value null an exception is thrown the code will not compile

89. Given: 1. 2. 3.

public interface Foo{ int k = 4; }

Which three are equivalent to line 2? (Choose Three) A. B. C. D.

final int k = 4; public int k = 4; static int k = 4; private int k = 4;

30

E. F. G. H.

abstract int k = 4; volatile int k = 4; transient int k = 4; protected int k = 4;

90. Given: 1. 2. 3. 4. 5. 6.

public class foo { static String s; public static void main (String[]args) { system.out.printIn (“s=” + s); } }

What is the result? A. B. C. D. E.

The code compiles and “s=” is printed. The code compiles and “s=null” is printed. The code does not compile because string s is not initialized. The code does not compile because string s cannot be referenced. The code compiles, but a NullPointerException is thrown when toString is called.

91. Which of the following two are valid declarations of a char? (Choose Two) A. B. C. D. E. F. G.

char ch = “a”; char ch = ‘\’ ‘; char ch = ‘cafe’; char ch = “cafe”; char ch = ‘\ucafe’; char ch = ‘\u10100’; char ch = (char) true;

92. Given: 1. 2. 3. 4. 5.

String foo = “blue”; Boolean[]bar = new Boolean [1]; if (bar[0]) { foo = “green”; }

What is the result? A. B. C. D. E. F.

foo has the value of “” foo has the value of null foo has the value of “blue” foo has the value of “green” an exception is thrown the code will not compile

93. Click the exhibit button:

31

1. 2. 3. 4. 5. 6. 7. 8. 9.

public class X { public static void main (String[]args) { String s1 = new String (“true”); Boolean b1 = new Boolean (true); if (s2.equals(b1)) { System.out.printIn(“Equal”); } } }

What is the result? A. B. C. D.

The program runs and prints nothing. The program runs and prints “Equal.” An error at line 5 causes compilation to fail. The program runs but aborts with an exception.

94. Given: 1. 2. 3. 4. 5. 6. 7. 8. 9.

public class Foo { public static void main (String []args) { int i = 1; int j = i++; if ((i>++j) && (i++ ==j)) { i +=j; } } }

What is the final value of i? A. B. C. D. E.

1 2 3 4 5

95. Click the exhibit button: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11.

public class X { public static void main (String[]args) { string s = new string (“Hello”); modify(s); System.out.printIn(s); } public static void modify (String s) { s += “world!”; } }

What is the result?

32

A. B. C. D.

The program runs and prints “Hello”. An error causes compilation to fail. The program runs and prints “Hello world!”. The program runs but aborts with an exception.

96. Which two are equivalent? (Choose Two) A. B. C. D. E. F.

16>4 16/2 16*4 16>>2 16/2^2 16>>>2

97. Click the exhibit button 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11.

public class X { public static void main (String[]args) { int [] a = new int [1] modify(a); System.out.printIn(a[0]); } public static void modify (int[] a) { a[0] ++; } }

What is the result? A. B. C. D. E.

The program runs and prints “0”. The program runs and prints “1”. The program runs but aborts with an exception. An error “possible undefined variable” at line 4 causes compilation to fail. An error “possible undefined variable” at line 9 causes compilation to fail.

98. Given: 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24.

public class Foo { public static void main (String [] args) { StringBuffer a = new StringBuffer (“A”); StringBuffer b = new StringBuffer (“B”); operate (a,b); system.out.printIn{a + “,” +b}; ) static void operate (StringBuffer x, StringBuffer y) { y.append {x}; y = x; ) }

What is the result? A. B.

The code compiles and prints “A,B”. The code compiles and prints “A, BA”.

33

C. D. E. F.

The code compiles and prints “AB, B”. The code compiles and prints “AB, AB”. The code compiles and prints “BA, BA”. The code does not compile because “+” cannot be overloaded for stringBuffer.

99. Given: 1. 2. 3. 4. 5. 6. 7.

public class X { public static void main (String[] args) { byte b = 127; byte c = 126; byte d = b + c; } }

Which statement is true? A. B. C. D. E.

Compilation succeeds and d takes the value 253. Line 5 contains an error that prevents compilation. Line 5 throws an exception indicating “Out of range”. Line 3 and 4 contain error that prevent compilation. The compilation succeeds and d takes the value of 1.

100.Given: 1. 2. 3. 4. 5. 6. 7.

public class WhileFoo { public static void main (String []args) { int x= 1, y = 6; while (y--) {x--;} system.out.printIn(“x=” + x “y =” + y); } }

What is the result? A. B. C. D. E.

The output is x = 6 y = 0 The output is x = 7 y = 0 The output is x = 6 y = -1 The output is x = 7 y = -1 Compilation will fail

101.Which statement is true? A. B. C. D. E.

The Error class is a untimeException. No exceptions are subclasses of Error. Any statement that may throw an Error must be enclosed in a try block. Any statement that may throw an Exception must be enclosed in a try block. Any statement that may thro a runtimeException must be enclosed in a try block.

102.Click the exhibit button:

34

1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16.

int I=1, j=0 switch(i) { case 2: j+=6; case 4: j+=1; default: j +=2; case 0: j +=4; }

What is the value of j at line 16? A. B. C. D. E.

0 1 2 4 6

103.Given: 1. switch (i) { 2. default: 3. System.out.printIn(“Hello”); 4. ) What is the acceptable type for the variable i? A. B. C. D. E. F.

G.

byte long float double object A and B C and D

104.You need to store elements in a collection that guarantees that no duplicates are stored. Which two interfaces provide that capability? (Choose Two) A. B. C. D. E. F.

java.util.Map java.util.Set java.util.List java.util.StoredSet java.util.StoredMap java.util.Collection

105.Which statement is true for the class java.util.ArrayList? A.

The elements in the collection are ordered.

35

B. C. D. E.

The collection is guaranteed to be immutable. The elements in the collection are guaranteed to be unique. The elements in the collection are accessed using a unique key. The elements in the collections are guaranteed to be synchronized.

106.Click the exhibit button: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18.

public class X implements Runnable( private int x; private int y; public static void main(String[]args) X that = new X(); (new Thread(that)).start(); (new Thread(that)).start(); ) public void run() ( for (;;) ( x++; y++; System.out.printIn(“x=” + x + “, y = ” + y); ) ) )

What is the result? A. B. C. D.

Errors at lines 7 and 8 cause compilation to fail. The program prints pairs of values for x and y that might not always be the same on the same line (for example, “x=2, y=1”). The program prints pairs of values for x and y that are always the same on the same line (for example, “x=1, y=1”. In addition, each value appears twice (for example, “x=1, y=1” followed by “x=1, y=1”). The program prints pairs of values for x and y that are always the same on the same line (for example, “x=1, y=1”. In addition, each value appears only for once (for example, “x=1, y=1” followed by “x=2, y=2”).

107.Given: 1. 2. 3. 4. 5. 6. 7. 8.

public class SyncTest { private int x; private int y; public synchronized void setX (int i) (x=1;) public synchronized void setY (int i) (y=1;) public synchronized void setXY(int 1)(set X(i); setY(i);) public synchronized Boolean check() (return x !=y;) )

Under which conditions will check () return true when called from a different class? A. B. C. D.

check() can never return true check() can return true when setXY is called by multiple threads check() can return true when multiple threads call setX and setY separately. Check() can only return true if SyncTest is changed to allow x and y to be set separately. 108.Which is a method of the MouseMotionListener interface?

36

A. B. C. D. E.

Public void mouseDragged(MouseEvent) Public boolean mouseDragged(MouseEvent) Public void mouseDragged(MouseMotionEvent) Public boolean MouseDragged(MouseMotionEvent) Public boolean mouseDragged(MouseMotionEvent)

109.Given: 1. 2. 3. 4. 5.

String foo = “base”; foo.substring(0,3); foo.concat(“ket”); foo += “ball”;

Type the value of foo at line 8. 110.Given: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10.

public class Test { public static void leftshift(int i, int j) { i<<=j; } public static void main(String args[]) { int i = 4, j = 2; leftshift(i, j); System.out.printIn(i); } }

What is the result? A. B. C. D. E.

2 4 8 16 The code will not compile

111.Given: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11.

public class Foo { private int val; public foo(int v) (val = v;) } public static void main (String [] args) { Foo a = new Foo (10); Foo b = new Foo (10); Foo c = a; int d = 10; double e = 10.0; } }

Which three logical expressions evaluate to true? (Choose Three) A. B. C.

(a ==c) (d ==e) (b ==d)

37

D. E. F.

(a ==b) (b ==c) (d ==10.0)

112.Click the exhibit button: 1. public class X { 2. private static int a; 3. 5. public static void main (String[] args) { 6. modify (a); 7. } 8. 9. public static void modify (int a) { 10. a++; 11. } 12. } What is the result? A. B. C. D. E.

The program runs and prints “0”. The program runs and prints “1”. The program runs but aborts with an exception. En error “possible undefined variable” at line 5 causes compilation to fail. En error “possible undefined variable” at line 10 causes compilation to fail.

113.Click the exhibit button: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11.

public class Test { public static void replaceJ(string text) { text.replace (‘j’, ‘l’); } public static void main(String args[]) { string text = new String (“java”) replaceJ(text); system.out.printIn(text); } }

What is the result? A. B. C. D.

The program prints “lava”. The program prints “java”. An error at line 7 causes compilation to fail. Compilation succeeds but the program throws an exception.

114.Which two are equivalent? (Choose Two) A. B. C. D. E. F.

3/2 3<2 3*4 3<<2 3*2^2 3<<<2

115.What is the numerical range of a char?

38

A. B. C. D. E.

0 . . . 32767 0 . . . 65535 –256 . . . 255 –32768 . . . 32767 range is platform dependent

116.Given: 1. 2. 3. 4. 5. 6. 7.

public class Test { public static void main (String []args) { unsigned byte b = 0; b--; } }

What is the value of b at line 5? A. B. C. D. E.

-1 255 127 compilation will fail compilation will succeed but the program will throw an exception at line 4

117.Given: 1. 2. 3. 4. 5.

public class Foo { public void main (String [] args) { system.out.printIn(“Hello World.”); } }

What is the result? A. B. C. D.

An exception is thrown. The code does not compile. “Hello World.” is printed to the terminal. The program exits without printing anything.

118.Given: 1. 2. 3. 4. 5.

6. 7. 8.

//point X public class foo ( public static void main (String[]args) throws Exception { java.io.printWriter out = new java.io.PrintWriter ( new java.io.outputStreamWriter (System.out), true; out.printIn(“Hello”); } }

Which statement at PointX on line 1 allows this code to compile and run? A. B. C. D.

import java.io.*; include java.io.*; import java.io.PrintWriter; include java.io.PrintWriter;

39

E. no statement is needed. 119.Which will declare a method that is available to all members of the same package and can be referenced without an instance of the class? A. B. C. D. E.

abstract public void methoda(); public abstract double methoda(); static void methoda(double d1){} public native double methoda() {} protected void methoda(double d1) {}

120.Which type of event indicates a key pressed on a java.awt.Component? A. B. C. D. E.

KeyEvent KeyDownEvent KeyPressEvent KeyTypedEvent KeyPressedEvent

121.Click the exhibit button: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19.

import java.awt.*; public class X extends Frame { public static void main (String [] args) { X x = new X(); x.pack(); x.setVisible(true); } public X() { setLayout (new BordrLayout()); Panel p = new Panel (); add(p, BorderLayout.NORTH); Button b = new Button (“North”); p.add(b): Button b = new Button (“South”); add(b1, BorderLayout.SOUTH): } }

Which two statements are true? (Choose Two) A. B. C. D. E. F.

The buttons labeled “North” and “South” will have the same width The buttons labeled “North” and “South” will have the same height The height of the button labeled “North” can very if the Frame is resized The height of the button labeled “South” can very if the Frame is resized The width of the button labeled “North” is constant even if the Frame is resized The width of the button labeled “South” is constant even if the Frame is resized

122.How can you create a listener class that receives events when the mouse is moved? A. B. C. D. E.

By extending MouseListener. By implementing MouseListener. By extending MouseMotionListener. By implementing MouseMotionListener. Either by extending MouseMotionListener or extending MouseListener.

40

F.

Either by implementing MouseMotion Listener or implementing MouseListener.

123.Which statement is true? A. B. C. D.

A grid bag layout can position components such that they span multiple rows and/or columns. The “North” region of a border layout is the proper place to locate a menuBar component in a Frame. Components in a grid bag layout may either resize with their cell, or remain centered in that cell at their preferred size. A border layout can be used to position a component that should maintain a constant size even when the container is resized.

124.You want a class to have access to members of another class in the same package. Which is the most restrictive access modifier that will accomplish this objective? A. B. C. D. E.

Public Private Protected Transient No access modifier is required

125.Which two statements are true regarding the creation of a default constructor? (Choose Two) A. B. C. D. E.

The default constructor initializes method variables. The default constructor invokes the no-parameter constructor of the superclass. The default constructor initializes the instance variables declared in the class. If a class lacks a no-parameter constructor, but has other constructors, the compiler creates a default constructor. The compiler creates a default constructor only when there are no other constructors for the class.

126.Given: 1. 2. 3. 4.

public class OuterClass { private double d1 1.0; //insert code here }

You need to insert an inner class declaration at line2. Which two inner class declarations are valid? (Choose Two) A. static class InnerOne { public double methoda() {return d1;} } B. static class InnerOne { static double methoda() {return d1;} } C. private class InnerOne { public double methoda() {return d1;} } D. protected class InnerOne { static double methoda() {return d1;} } E. public abstract class InnerOne { public abstract double methoda(); }

41

127.Which two declarations prevent the overriding of a method? (Choose Two) A. B. C. D. E.

final void methoda() {} void final methoda() {} static void methoda() {} static final void methoda() {} final abstract void methoda() {}

128.Given: 1. public class Test { 2. public static void main (String args[]) { 3. class Foo { 4. public int i = 3; 5. } 6. Object o = (Object) new Foo(); 7. Foo foo = (Foo)o; 8. System.out.printIn(foo. i); 9. } 10. } What is the result? A. B. C. D.

Compilation will fail Compilation will succeed and the program will print “3” Compilation will succeed but the program will throw a ClassCastException at line 6 Compilation will succeed but the program will throw a ClassCastException at line 7

129.Which two create an instance of an array? (Choose Two) A. B. C. D. E.

int[] ia = new int [15]; float fa = new float [20]; char[] ca = “Some String”; Object oa = new float[20]; Int ia [][] = (4, 5, 6) (1, 2, 3)

130.Given: 1. 2. 3. 4. 5. 6. 7.

public class ExceptionTest { class TestException extends Exception {} public void runTest () throws TestException {} public void test () /* Point X*/ { runTest (); } }

At point X on line 4, which code can be added to make the code compile? A. B. C. D. E.

throws Exception catch (Exception e) throws RuntimeException catch (TestException e) no code is necessary

131.Click the exhibit button:

42

1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17.

public class SwitchTest { public static void main (String []args) { System.out.PrintIn(“value =” +switchIt(4)); } public static int switchIt(int x) { int j = 1; switch (x) { case 1: j++; case 2: j++; case 3: j++; case 4: j++; case 5: j++; default:j++; } return j + x; } }

What is the output from line 3? A. B. C. D. E. F.

Value = 3 Value = 4 Value = 5 Value = 6 Value = 7 Value = 8

132.Which four types of objects can be thrown using the throw statement? (Choose Four) A. B. C. D. E. F.

error event object exception throwable RuntimeException

133.Given: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11.

public class ForBar { public static void main(String []args) { int i = 0, j = 5; tp: for (;;) { i ++; for(;;) if(i > --j) break tp; } system.out.printIn(“i = ” + i + “, j = “+ j); } }

What is the result? A. B. C. D. E.

The program runs and prints “i=1, j=0” The program runs and prints “i=1, j=4” The program runs and prints “i=3, j=4” The program runs and prints “i=3, j=0” An error at line 4 causes compilation to fail

43

F.

An error at line 7 causes compilation to fail

134.Which two can directly cause a thread to stop executing? (Choose Two) A. B. C. D. E.

Exiting from a synchronized block. Calling the wait method on an object. Calling the notify method on an object. Calling the notifyAll method on an object. Calling the setPriority method on a thread object.

135.Given: 1. 2. 3. 4. 5. 6. 7. 8.

public class Foo implements Runnable ( public void run (Thread t) { system.out.printIn(“Running.”); } public static void main (String[] args) { new thread (new Foo()).start(); ) )

What is the result? A. B. C. D. E.

An exception is thrown, The program exits without printing anything. An error at line 1 causes compilation to fail. An error at line 6 causes the compilation to fail. “Running” is printed and the program exits.

136.Which constructs a DataOutputStream? A. B. C. D. E. F.

new dataInputStream(“in.txt”); new dataInputStream(new file(“in.txt”)); new dataInputStream(new writer(“in.txt”)); new dataInputStream(new FileWriter(“in.txt”)); new dataInputStream(new InputStream(“in.txt”)); new dataInputStream(new FileInputStream(“in.txt”));

137.Which can be used to decode charS for output? A. B. C. D. E.

java.io.InputStream java.io.EncodedReader java.io.InputStreamReader java.io.InputStreamWriter java.io.BufferedInputStream

138.Given: 1. 2. 3. 4. 5. 6. 7. 8.

public class Test { public static void main (String [] args) { string foo = “blue”; string bar = foo; foo = “green”; System.out.printIn(bar); } }

44

What is the result? A. B. C. D. E.

An exception is thrown. The code will not compile. The program prints “null”. The program prints “blue”. The program prints “green”.

139.Which code determines the int value foo closest to a double value bar? A. B. C. D. E. F.

int foo = (int) Math.max(bar); int foo = (int) Math.min(bar); int foo = (int) Math.abs(bar); int foo = (int) Math.ceil(bar); int foo = (int) Math.floor(bar); int foo = (int) Math.round(bar);

140.Which two demonstrate encapsulation of data? (Choose Two) A. B. C. D. E.

Member data has no access modifiers. Member data can be modified directly. The access modifier for methods is protected. The access modifier to member data is private. Methods provide for access and modification of data.

141.Click on the exhibit button: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15.

class A { public String toString () { return “4”; } } class B extends A { 8. public String toString () { return super.toString() + “3”; } } public class Test { public static void main(String[]args) { System.out.printIn(new B()); } }

What is the result? A. B. C. D. E.

Compilation succeeds and 4 is printed. Compilation succeeds and 43 is printed. An error on line 9 causes compilation to fail. An error on line 14 causes compilation to fail. Compilation succeeds but an exception is thrown at line 9

142.Which two statements are true? (Choose Two) A. B. C.

An anonymous inner class can be declared inside of a method. An anonymous inner class constructor can take arguments in some situation. An anonymous inner class that is a direct subclass that is a direct subclass of Object can implement multiple interfaces .

45

D. E.

Even if a class Super does not implement any interfaces, it is still possible to define an anonymous inner class that is an immediate subclass of Super that implements a single interface. Event if a class Super does not implement any interfaces, it is still possible to define an anonymous inner class that is an immediate subclass of Super that implements multiple interfaces.

143.Given: 1. 2. 3. 4. 5. 6. 7. 8. 9.

public class MethodOver { private int x, y; private float z; public void setVar(int a, int b, float c){ x = a; y = b; z = c; } }

Which two overload the setVar method? (Choose Two) A.

B. C. D.

E.

void setVar (int a, int b, float c){ x = a; y = b; z = c; } public void setVar(int a, float c, int b) { setVar(a, b, c); } public void setVar(int a, float c, int b) { this(a, b, c); } public void setVar(int a, float b){ x = a; z = b; } public void setVar(int ax, int by, float cz) { x = ax; y = by; z = cz; }

144.Which statements about static inner classes are true? (Choose Two) A. B. C. D. E.

A static inner class requires a static initializer. A static inner class requires an instance of the enclosing class. A static inner class has no reference to an instance of the enclosing class. A static inner class has access to the non-static members of the outer class. Static members of a static inner class can be referenced using the class name of the static inner class.

145.Given: 1. public class X { 2. public object m () { 3. object o = new float (3.14F); 4. object [] oa = new object [1];

46

5. oa[0]= o; 6. o = null; 7. oa[0] = null; 8.return o; 9. } 10. } When is the float object created in line 3, eligible for garbage collection? A. B. C. D.

Just after line 5 Just after line 6 Just after line 7 Just after line 8(that is, as the method returns)

146.Which two interfaces provide the capability to store objects using a key-value pair? (Choose Two) A. B. C. D. E. A.

java.util.Map java.util.Set java.util.List java.util.StoredSet java.util.StoredMap java.util.Collection

147.Which interface does java.util.Hashable implement? A. B. C. D.

java.util.Map java.util.List java.util.Hashable java.util.Collection

47

Answer Key: 1. C 2. D,E 3. B 4. 5 5. D 6. JavaJavaC 7. B 8. A,C 9. A,C 10. B,D 11. D,E 12. D 13. A 14. D 15. A 16. A 17. B,C 18. A,D,F 19. D 20. F 21. B 22. C 23. D 24. C 25. C 26. A 27. B 28. D 29. F 30. A 31. F 32. C 33. A 34. C,D 35. E 36. B 37. B 38. A 39. F 40. B 41. D 42. A,B,C,D,E 43. C 44. D 45. C 46. C 47. E 48. D 49. A,B 50. B 51. A 52. 13423 53. C 54. B 55. C,D 56. A,D

48

57. B 58. C 59. E 60. A,C 61. D,E 62. B 63. base 64. E 65. D 66. B 67. B 68. B 69. C,E 70. C 71. C 72. B,D 73. A,D 74. B 75. B 76. B 77. A 78. D,E 79. E 80. B,D,F 81. D 82. B,E 83. C,E 84. F 85. C 86. B 87. C 88. B 89. A,B,C 90. B 91. B,E 92. F 93. A 94. B 95. A 96. D,F 97. B 98. B 99. B 100.E 101.D 102.E 103.A 104.B,D 105.A 106.D 107.N/A 108.A 109.baseball 110.B 111.A,B,F 112.A 113.B

49

114.C,D 115.B 116.D 117.A 118.E 119.C 120.A 121.B,E 122.D 123.A 124.E 125.C,E 126.C,E 127.A,D 128.B 129.A,D 130.B 131.A,D 132.A,D,E,F 133.A 134.B,E 135.C 136.F 137.C 138.D 139.F 140.D,E 141.B 142.A,B 143.B,D 144.C,E 145.C 146.A,E 147.A

50

Related Documents

Cheatsheet - Sun Java2 Scjp
November 2019 11
Cheatsheet
May 2020 31
Cheatsheet
December 2019 51
Cheatsheet
December 2019 51
Scjp
October 2019 42