SCJP Exam Solutions - Page -1



1)














1.The file “file.txt” exists on the file system and contsins ASCII text.

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. The code does not compile.
b. The code runs and no change is made to the file.
c. The code runs and sets the length of the file to 0.
d. An exception is thrown because the file is not closed.
e. The code runs and deletes the file from the file system.
2.)
Which is a valid identifier?
a. false
b. default
c. _object
  1. a-class
3).
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;
}
4).
Given:
      11. int index = 1;
      12. boolean[] test = new Boolean[3];
      13. boolean foo = test [index];

What is the result?

a. Foo has the value of 0.
b. Foo has the value of null.
    c. Foo has the value of true.
    d. Foo has the value of false.
    e. An exception is thrown.
    f. The code will not compile.
5).
What is the numerical range of a byte?

a. 0...32767
b. 0...65535
c. -128...127
d. -256...255
e. Range is platform dependent
6).
Which two statements are true? (Choose Two)

a. An anonymous inner class can be declared inside of a method
b. An anonymous inner class constructor can take arguments in some situation.
c. An anonymous inner class that is a direct subclass that is a direct subclass of Object can implement multiple interfaces.
d. 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.
e. 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.
7).
Exhibit

1. public class SyncTest{
2. public static void main(String[] args) {
3. final StringBuffer s1=new StringBuffer();
4. final StringBuffer s2=new StringBuffer();
5. new Thread () {
6. public void run() {
7. synchronized (s1) {
8. s2.append(“A”);
9. synchronized(s2) {
10. s2.append(“B”);
11. System.out.print(s1);
12. System.out.print(s2);
13. }
14. }
15. }
16. } .start();
17. new Thread() {
18. public void run() {
19. synchronized (s2) {
20. s2.append (“C”);
21. synchronized(s1) {
22. s1.append (“D”);
23. System.out.print(s2);
24. System.out.print(s1);
25. }
26. }
27. }
28. } .start();
29. }
30.}

Which two statements are true? (Choose two)

a. The program prints “ABBCAD”
b. The program prints “CDDACB”
c. The program prints “ADCBADBC”
d. The output is a non-deterministic point because of a possible deadlock condition.
e. The output is dependent on the threading model of the system the program is running on.
8.)
Given:
1. public class Foo {
2. public void main (String [] args) {
3. system.out.println(“Hello World.”);
4. }
5. }

What is the result?

a. An exception is thrown.
b. The code does not compile.
c. “Hello World.” is printed to the terminal.
d. The program exits without printing anything.
9.)
Given:
1. public class ExceptionTest {
2. class TestException extends Exception {}
3. public void runTest () throws TestException {}
4. public void test () /* Point X*/ {
5. runTest ();
6.}
7.}

At point X on line 4, which code can be added to make the code compile?

a. Throws Exception.
b. Catch (Exception e).
c. Throws RuntimeException.
d. Catch (TestException e).
e. No code is necessary.
10.)
Given:
1. public class WhileFoo {
2. public static void main (String [] args) {
3. int x=1, y = 6;
4. while (y--) {x--;}
5. sy stem.out.println(“x=” +x “y =” +y);
6. }
7. }

What is the result?

a. The output is x = 6 y = 0
b. The output is x = 7 y = 0
c. The output is x = 6 y = -1
d. The output is x = 7 y = -1
e. Compilation will fail.
Next