ASSESSMENT TEST 2

The real exam has 60 questions and you are given three hours. Since this assessment exam has only 14 questions, allow yourself only 42 minutes to complete this exam. On the real exam, and on all of the exams in this book, give yourself credit only for those questions that you answer 100 percent correctly. For instance, if a question has three correct answers and you get two of the three correct, you get zero credit. There is no partial credit. Good luck!

1. Given:

          3. import java.util.*;
          4. public class VLA2 implements Comparator<VLA2> {
          5.   int dishSize;
          6.   public static void main(String[] args) {
          7.     VLA2[] va = {new VLA2(40), new VLA2(200), new VLA2(60)};
          8.
          9.     Arrays.sort(va, va[0]);
         10.     int index = Arrays.binarySearch(va, new VLA2(40), va[0]);
         11.     System.out.print(index + " ");
         12.     index = Arrays.binarySearch(va, new VLA2(80), va[0]);
         13.     System.out.print(index);
         14.   }
         15.   public int compare(VLA2 a, VLA2 b) {
         16.     return b.dishSize - a.dishSize;
         17.   }
         18.   VLA2(int d) { dishSize = d; }
         19. }

What is the result?

A. 0 -2

B. 0 -3

C. 2 -1

D. 2 -2

E. Compilation fails.

F. An exception is thrown at runtime.

2. Given a directory structure:

         - baseDir
           - testDir
             - subDir2
               - Shackelton.txt

and given the following code:

       12. String name = "testDir" + File.pathSeparator + "subDir2"
                       + File.pathSeparator + "Shackelton.txt";
       13. File f = new File(name);
       14. System.out.println("exists " + f.exists());

Assuming the proper import statements and exception handling, which statements must be true in order for the output to be "exists true"? (Choose three.)

A. Line 12 is correct as it stands.

B. Line 14 is correct as it stands.

C. The program must be invoked from the baseDir directory.

D. The program must be invoked from the testDir directory.

E. Line 12 must use File.separator instead of File.pathSeparator.

F. Line 14 must use the method fileExists() instead of exists().

3. Given:

          1. import java.io.*;
          2. import java.util.*;
          3. import static java.lang.Short.*;
          4. import static java.lang.Long.*;
          5. public class MathBoy {
          6.   public static void main(String[] args) {
          7.     long x = 123456789;
          8.     short y = 22766; // maximum value of a short is 32767
          9.     System.out.printf("%1$+10d %2$010d ", x, MAX_VALUE - y);
         10.     System.out.println(new Date());
         11.   }
         12. }

Which are true? (Choose all that apply.)

A. Compilation fails.

B. The output will include "+"

C. The output will include "10001"

D. The output will include "0000010001"

E. The output will include today’s date.

F. The output will include the number of milliseconds from January 1, 1970 until today.

4. Given:

          1. public class WeatherTest {
          2.   static Weather w;
          3.   public static void main(String[] args) {
          4.     System.out.print(w.RAINY.count + " " + w.Sunny.count + " ");
          5.   }
          6. }
          7. enum Weather {
          8.   RAINY, Sunny;
          9.   int count = 0;
         10.   Weather() {
         11.     System.out.print("c ");
         12.     count++;
         13.   }
         14. }

What is the result?

A. c 1 c 1

B. c 1 c 2

C. c c 1 1

D. c c 1 2

E. c c 2 2

F. Compilation fails.

G. An exception is thrown at runtime.

5. Given:

          2. import java.text.*;
          3. public class Gazillion {
          4.   public static void main(String[] args) throws Exception {
          5.     String s = "123.456xyz";
          6.     NumberFormat nf = NumberFormat.getInstance();
          7.     System.out.println(nf.parse(s));
          8.     nf.setMaximumFractionDigits(2);
          9.     System.out.println(nf.format(s));
         10.   }
         11. }

Which are true? (Choose all that apply.)

A. Compilation fails.

B. The output will contain "123.45 "

C. The output will contain "123.456"

D. The output will contain "123.456xyz"

E. An exception will be thrown at runtime.

6. Given that the current directory is bigApp, and the following directory structure:

Image

And the code:

       package com.wickedlysmart;
       public class BigAppMain {
         public static void main(String[] args) {
           System.out.println("big app");
         }
       }

Which will invoke BigAppMain? (Choose all that apply.)

A. java classes/com.wickedlysmart.BigAppMain

B. java classes com/wickedlysmart/BigAppMain

C. java classes.com.wickedlysmart.BigAppMain

D. java -cp classes com.wickedlysmart.BigAppMain

E. java -cp /classes com.wickedlysmart.BigAppMain

F. java -cp .:classes com.wickedlysmart.BigAppMain

G. java -cp classes/com/wickedlysmart com.wickedlysmart.BigAppMain

7. Given:

          2. class Game {
          3.   static String s = "-";
          4.   String s2 = "s2";
          5.   Game(String arg) { s += arg; }
          6. }
          7. public class Go extends Game {
          8.   Go() { super(s2); }
          9.   { s += "i "; }
         10.   public static void main(String[] args) {
         11.     new Go();
         12.     System.out.println(s);
         13.   }
         14.   static { s += "sb "; }
         15. }

What is the result?

A. -sb i s2

B. -sb s2 i

C. -s2 i sb

D. -s2 sb i

E. Compilation fails.

F. An exception is thrown at runtime.

8. Given:

          2. public class Salmon extends Thread {
          3.   public static long id;
          4.   public void run() {
          5.     for(int i = 0; i < 4; i++) {
          6.       // insert code here
          7.          new Thread(new Salmon()).start();
          8.          throw new Error();
          9.       }
         10.       System.out.print(i + " ");
         11.   } }
         12.   public static void main(String[] args) {
         13.     Thread t1 = new Salmon();
         14.     id = t1.getId();
         15.     t1.start();
         16. } }

And the two code fragments:

I. if(i == 2 && id== Thread.currentThread().getId()) {

II. if(i == 2) {

When inserting either fragment, independently at line 6, which are true? (Choose all that apply.)

A. Both fragments produce the same output.

B. Both fragments will end in about the same amount of time.

C. Compilation fails, regardless of which fragment is inserted.

D. Regardless of which fragment is inserted, output ends once the Error is thrown.

E. Regardless of which fragment is inserted, output continues after the Error is thrown.

9. Given:

          2. public class Internet {
          3.   private int y = 8;
          4.   public static void main(String[] args) {
          5.     new Internet().go();
          6.   }
          7.   void go() {
          8.     int x = 7;
          9.     TCPIP ip = new TCPIP();
         10.     class TCPIP {
         11.       void doit() { System.out.println(y + x); }
         12.     }
         13.     ip.doit();
         14.   }
         15. }

What is the result? (Choose all that apply.)

A. Compilation succeeds.

B. Compilation fails due to an error on line 3.

C. Compilation fails due to an error on line 8.

D. Compilation fails due to an error on line 9.

E. Compilation fails due to an error on line 10.

F. Compilation fails due to accessing x on line 11.

G. Compilation fails due to accessing y on line 11.

10. Given:

          4. public static void main(String[] args) {
          5.   try {
          6.     if(args.length == 0) throw new Exception();
          7.   }
          8.   catch (Exception e) {
          9.     System.out.print("done ");
         10.     doStuff(); // assume this method compiles
         11.   }
         12.   finally {
         13.     System.out.println("finally ");
         14.   }
         15. }

Which are possible outputs? (Choose all that apply.)

A. "done "

B. "finally "

C. "done finally "

D. Compilation fails.

E. No output is produced.

11. Given:

         3. class A { }
         4. class B extends A { }
         5. class C extends B { }
         6. public class Carpet<V extends B> {
         7.   public <X extends V> Carpet<? extends V> method(Carpet<? super X> e) {
         8.   // insert code here
         9. } }

Which, inserted independently at line 8, will compile? (Choose all that apply.)

A. return new Carpet<X>();

B. return new Carpet<V>();

C. return new Carpet<A>();

D. return new Carpet<B>();

E. return new Carpet<C>();

12. Given:

          1. class One {
          2.   int x = 0;
          3.   { assert x == 1; }
          4. }
          5. public class Two {
          6.   public static void main(String[] args) {
          7.     int y = 0;
          8.     assert y == 0;
          9.     if(args.length > 0)
         10.       new One();
         11.   }
         12. }

Which of the following will run without error? (Choose all that apply.)

A. java Two

B. java Two x

C. java -ea Two

D. java -ea Two x

E. java -ea:One Two

F. java -ea:One Two x

G. java -ea:Two Two x

13. Given:

          2. class SafeDeposit {
          3.   private static SafeDeposit singleton;
          4.   public static SafeDeposit getInstance(int code) {
          5.     if(singleton == null)
          6.       singleton = new SafeDeposit(code);
          7.     return singleton;
          8.   }
          9.   private int code;
         10.   private SafeDeposit(int c) { code = c; }
         11.   int getCode() { return code; }
         12.  }
         13.  public class BeSafe {
         14.    // insert lots of code here
         25. }

Which are true? (Choose all that apply.)

A. Compilation fails.

B. Class BeSafe can create many instances of SafeDeposit.

C. Class BeSafe CANNOT create any instances of SafeDeposit.

D. Class BeSafe can create only one instance of SafeDeposit.

E. Class BeSafe can create instances of SafeDeposit without using the getInstance() method.

F. Once class BeSafe has created an instance of SafeDeposit, it cannot change the value of the instance’s "code" variable.

14. Given:

          2. public class DMV implements Runnable {
          3.   public static void main(String[] args) {
          4.     DMV d = new DMV();
          5.     new Thread(d).start();
          6.     Thread t1 = new Thread(d);
          7.     t1.start();
          8.   }
          9.   public void run() {
         10.     for(int j = 0; j < 4; j++) { do1(); do2(); }
         11.   }
         12.   void do1() {
         13.     try { Thread.sleep(1000); }
         14.     catch (Exception e) { System.out.print("e "); }
         15.   }
         16.   synchronized void do2() {
         17.     try { Thread.sleep(1000); }
         18.     catch (Exception e) { System.out.print("e "); }
         19. } }

Which are true? (Choose all that apply.)

A. Compilation fails.

B. The program’s minimum execution time is about 8 seconds.

C. The program’s minimum execution time is about 9 seconds.

D. The program’s minimum execution time is about 12 seconds.

E. The program’s minimum execution time is about 16 seconds.

F. Un-synchronizing do2() changes the program’s minimum execution time by only a few milliseconds.

QUICK ANSWER KEY

1. D

2. B, C, E

3. A

4. C

5. C, E

6. D, F

7. E

8. E

9. D, F

10. A, B, C

11. A, B

12. A, B, C, E, G

13. B, F

14. C

ASSESSMENT TEST 2: ANSWERS

1. Given:

          3. import java.util.*;
          4. public class VLA2 implements Comparator<VLA2> {
          5.   int dishSize;
          6.   public static void main(String[] args) {
          7.     VLA2[] va = {new VLA2(40), new VLA2(200), new VLA2(60)};
          8.
          9.     Arrays.sort(va, va[0]);
         10.     int index = Arrays.binarySearch(va, new VLA2(40), va[0]);
         11.     System.out.print(index + " ");
         12.     index = Arrays.binarySearch(va, new VLA2(80), va[0]);
         13.     System.out.print(index);
         14.   }
         15.   public int compare(VLA2 a, VLA2 b) {
         16.     return b.dishSize - a.dishSize;
         17.   }
         18.   VLA2(int d) { dishSize = d; }
         19. }

What is the result?

A. 0 -2

B. 0 -3

C. 2 -1

D. 2 -2

E. Compilation fails.

F. An exception is thrown at runtime.

2. Given a directory structure:

         - baseDir
           - testDir
             - subDir2
               - Shackelton.txt

and given the following code:

       12. String name = "testDir" + File.pathSeparator + "subDir2"
                       + File.pathSeparator + "Shackelton.txt";
       13. File f = new File(name);
       14. System.out.println("exists " + f.exists());

Assuming the proper import statements and exception handling, which statements must be true in order for the output to be "exists true"? (Choose three.)

A. Line 12 is correct as it stands.

B. Line 14 is correct as it stands.

C. The program must be invoked from the baseDir directory.

D. The program must be invoked from the testDir directory.

E. Line 12 must use File.separator instead of File.pathSeparator

F. Line 14 must use the method fileExists() instead of exists()

3. Given:

          1. import java.io.*;
          2. import java.util.*;
          3. import static java.lang.Short.*;
          4. import static java.lang.Long.*;
          5. public class MathBoy {
          6.   public static void main(String[] args) {
          7.     long x = 123456789;
          8.     short y = 22766; // maximum value of a short is 32767
          9.     System.out.printf("%1$+10d %2$010d ", x, MAX_VALUE - y);
         10.     System.out.println(new Date());
         11.   }
         12. }

Which are true? (Choose all that apply.)

A. Compilation fails.

B. The output will include "+"

C. The output will include "10001"

D. The output will include "0000010001"

E. The output will include today’s date.

F. The output will include the number of milliseconds from January 1, 1970 until today.

4. Given:

          1. public class WeatherTest {
          2.   static Weather w;
          3.   public static void main(String[] args) {
          4.     System.out.print(w.RAINY.count + " " + w.Sunny.count + " ");
          5.   }
          6. }
          7. enum Weather {
          8.   RAINY, Sunny;
          9.   int count = 0;
         10.   Weather() {
         11.     System.out.print("c ");
         12.     count++;
         13.   }
         14. }

What is the result?

A. c 1 c 1

B. c 1 c 2

C. c c 1 1

D. c c 1 2

E. c c 2 2

F. Compilation fails.

G. An exception is thrown at runtime.

5. Given:

          2. import java.text.*;
          3. public class Gazillion {
          4.   public static void main(String[] args) throws Exception {
          5.     String s = "123.456xyz";
          6.     NumberFormat nf = NumberFormat.getInstance();
          7.     System.out.println(nf.parse(s));
          8.     nf.setMaximumFractionDigits(2);
          9.     System.out.println(nf.format(s));
         10.   }
         11. }

Which are true? (Choose all that apply.)

A. Compilation fails.

B. The output will contain "123.45 "

C. The output will contain "123.456"

D. The output will contain "123.456xyz"

E. An exception will be thrown at runtime.

6. Given that the current directory is bigApp, and the following directory structure:

Image

And the code :

       package com.wickedlysmart;
       public class BigAppMain {
         public static void main(String[] args) {
           System.out.println("big app");
         }
       }

Which will invoke BigAppMain? (Choose all that apply.)

A. java classes/com.wickedlysmart.BigAppMain

B. java classes com/wickedlysmart/BigAppMain

C. java classes.com.wickedlysmart.BigAppMain

D. java -cp classes com.wickedlysmart.BigAppMain

E. java -cp /classes com.wickedlysmart.BigAppMain

F. java -cp .:classes com.wickedlysmart.BigAppMain

G. java -cp classes/com/wickedlysmart com.wickedlysmart.BigAppMain

7. Given:

          2. class Game {
          3.   static String s = "-";
          4.   String s2 = "s2";
          5.   Game(String arg) { s += arg; }
          6. }
          7. public class Go extends Game {
          8.   Go() { super(s2); }
          9.   { s += "i "; }
         10.   public static void main(String[] args) {
         11.     new Go();
         12.     System.out.println(s);
         13.   }
         14.   static { s += "sb "; }
         15. }

What is the result?

A. -sb i s2

B. -sb s2 i

C. -s2 i sb

D. -s2 sb i

E. Compilation fails.

F. An exception is thrown at runtime.

8. Given:

          2. public class Salmon extends Thread {
          3.   public static long id;
          4.   public void run() {
          5.     for(int i = 0; i < 4; i++) {
          6.     // insert code here
          7.       new Thread(new Salmon()).start();
          8.       throw new Error();
          9.     }
         10.     System.out.print(i + " ");
         11.  } }
         12.  public static void main(String[] args) {
         13.    Thread t1 = new Salmon();
         14.    id = t1.getId();
         15.    t1.start();
         16. } }

And the two code fragments:

I. if(i == 2 && id== Thread.currentThread().getId()) {

II. if(i == 2) {

When inserting either fragment, independently at line 6, which are true? (Choose all that apply.)

A. Both fragments produce the same output.

B. Both fragments will end in about the same amount of time.

C. Compilation fails, regardless of which fragment is inserted.

D. Regardless of which fragment is inserted, output ends once the Error is thrown.

E. Regardless of which fragment is inserted, output continues after the Error is thrown.

9. Given:

          2. public class Internet {
          3.   private int y = 8;
          4.   public static void main(String[] args) {
          5.     new Internet().go();
          6.   }
          7.   void go() {
          8.     int x = 7;
          9.     TCPIP ip = new TCPIP();
         10.     class TCPIP {
         11.       void doit() { System.out.println(y + x); }
         12.     }
         13.     ip.doit();
         14.   }
         15. }

What is the result? (Choose all that apply.)

A. Compilation succeeds.

B. Compilation fails due to an error on line 3.

C. Compilation fails due to an error on line 8.

D. Compilation fails due to an error on line 9.

E. Compilation fails due to an error on line 10.

F. Compilation fails due to accessing x on line 11.

G. Compilation fails due to accessing y on line 11.

10. Given:

          4. public static void main(String[] args) {
          5.   try {
          6.     if(args.length == 0) throw new Exception();
          7.   }
          8.   catch (Exception e) {
          9.     System.out.print("done ");
         10.     doStuff(); // assume this method compiles
         11.   }
         12.   finally {
         13.     System.out.println("finally ");
         14.   }
         15. }

Which are possible outputs? (Choose all that apply.)

A. "done "

B. "finally "

C. "done finally "

D. Compilation fails.

E. No output is produced.

11. Given:

         3. class A { }
         4. class B extends A { }
         5. class C extends B { }
         6. public class Carpet<V extends B> {
         7.   public <X extends V> Carpet<? extends V> method(Carpet<? super X> e) {
         8.     // insert code here
         9. } }

Which, inserted independently at line 8, will compile? (Choose all that apply.)

A. return new Carpet<X>();

B. return new Carpet<V>();

C. return new Carpet<A>();

D. return new Carpet<B>();

E. return new Carpet<C>();

12. Given:

          1. class One {
          2.   int x = 0;
          3.   { assert x == 1; }
          4. }
          5. public class Two {
          6.   public static void main(String[] args) {
          7.     int y = 0;
          8.     assert y == 0;
          9.     if(args.length > 0)
         10.       new One();
         11.   }
         12. }

Which of the following will run without error? (Choose all that apply.)

A. java Two

B. java Two x

C. java -ea Two

D. java -ea Two x

E. java -ea:One Two

F. java -ea:One Two x

G. java -ea:Two Two x

13. Given:

          2. class SafeDeposit {
          3.   private static SafeDeposit singleton;
          4.   public static SafeDeposit getInstance(int code) {
          5.     if(singleton == null)
          6.       singleton = new SafeDeposit(code);
          7.     return singleton;
          8.   }
          9.   private int code;
         10.   private SafeDeposit(int c) { code = c; }
         11.   int getCode() { return code; }
         12.  }
         13.  public class BeSafe {
         14.    // insert lots of code here
         25. }

Which are true? (Choose all that apply.)

A. Compilation fails.

B. Class BeSafe can create many instances of SafeDeposit.

C. Class BeSafe CANNOT create any instances of SafeDeposit.

D. Class BeSafe can create only one instance of SafeDeposit.

E. Class BeSafe can create instances of SafeDeposit without using the getInstance() method.

F. Once class BeSafe has created an instance of SafeDeposit, it cannot change the value of the instance’s "code" variable.

14. Given:

          2. public class DMV implements Runnable {
          3.   public static void main(String[] args) {
          4.     DMV d = new DMV();
          5.     new Thread(d).start();
          6.     Thread t1 = new Thread(d);
          7.     t1.start();
          8.   }
          9.   public void run() {
         10.     for(int j = 0; j < 4; j++) { do1(); do2(); }
         11.   }
         12.   void do1() {
         13.     try { Thread.sleep(1000); }
         14.     catch (Exception e) { System.out.print("e "); }
         15.   }
         16.   synchronized void do2() {
         17.     try { Thread.sleep(1000); }
         18.     catch (Exception e) { System.out.print("e "); }
         19. } }

Which are true? (Choose all that apply.)

A. Compilation fails.

B. The program’s minimum execution time is about 8 seconds.

C. The program’s minimum execution time is about 9 seconds.

D. The program’s minimum execution time is about 12 seconds.

E. The program’s minimum execution time is about 16 seconds.

F. Un-synchronizing do2() changes the program’s minimum execution time by only a few milliseconds.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset