Name:______
Covers Chs 29-31 / Sample Exam

Multiple Choice Questions Only for This Test

Part I: Questions: (1 pts each)

1 You should always invoke the unlock method in the finally clause.

A. true

B. false

2 Given the following code, which set of code can be used to replace the comment so that the program displays time to the console every second?
import java.applet.*;
import java.util.*;
public class Test extends Applet implements Runnable {
public void init() {
Thread t = new Thread(this);
t.start();
}
public void run() {
for(; ;) {
//display time every second
System.out.println(new Date().toString());
}
}
}

A. try { Thread.sleep(1000); } catch(InterruptedException e) { }

B. try { sleep(1000); } catch(InterruptedException e) { }

C. try { Thread.sleep(1000); } catch(RuntimeException e) { }

D. try { t.sleep(1000); } catch(InterruptedException e) { }

3 Which of the following expressions must be true if you create a thread using Thread = new Thread(object)?

A. object instanceof Applet

B. object instanceof Thread

C. object instanceof Frame

D. object instanceof Runnable

4 How do you create a condition on a lock?

A. Condition condition = Lock.getCondition();

B. Condition condition = Lock.newCondition();

C. Condition condition = lock.getCondition();

D. Condition condition = lock.newCondition();

5 Which method on a condition should you invoke to causes the current thread to wait until the condition is signaled?

A. condition.waited();

B. condition.await();

C. condition.waiting();

D. condition.wait();

6 To run an object on a separate thread, the object must be a subclass of Thread.

A. true

B. false

7 The Runnable interface is more general than the Thread class. You can convert any class that extends Thread to a class that implements Runnable.

A. true

B. false

8 An exception occurs if the resume() method is invoked by a finished thread object.

A. true

B. false

9 You can set a priority for an object of ThreadGroup using the setPrority() method.

A. false

B. true

10 You must specify a name for the thread group that you will create.

A. false

B. true

11 You should not directly invoke the run() method of a thread object.

A. true

B. false

12
When creating a server on a port that is already in use, ______.

A. the server encounters a fatal error and must be terminated.

B. the server is blocked until the port is available.

C. the server is created with no problems.

D. java.net.BindException occurs.

13 To obtain an ObjectInputStream from a socket, use ______.

A. socket.objectInputStream()

B. socket.getInputStream()

C. socket.getObjectInputStream()

D. socket.getObjectStream()

14 You can invoke ______on a Socket object, say socket, to obtain an InetAddress object.

A. socket.obtainInetAddress();

B. socket.InetAddress();

C. socket.retrieveInetAddress();

D. socket.getInetAddress();

15 To connect to a server running on the same machine with the client, which of the following can be used for the hostname?

A. InetAddress.getLocalHost(),

B. "127.0.0.1"

C. "localhost"

D. All of the above.

16 The server listens for a connection request from a client using the following statement:

A. Socket s = serverSocket.accept()

B. Socket s = new Socket(ServerName);

C. Socket s = serverSocket.getSocket()

D. Socket s = new Socket(ServerName, port);

17 The server can create a server socket regardless of whether the port is in use or not.

A. true

B. false

18 The client can connect to the server regardless of whether the port is in use or not.

A. true

B. false

19 You cannot get instances of InputStream or OutputStream because InputStream and OutputStream are abstract classes.

A. true

B. false

20 An applet cannot connect to a server program on a Web server where the applet was loaded.

A. false

B. true

21 You can transmit objects over the socket.

A. false

B. true

22 The URL constructor throws MalformedURLException if the URL is syntactically incorrect.

A. true

B. false

23 getInputStream() and getOutputStream() are used to produce InputStream and OutputStream on the socket.

A. false

B. true

24 Which of the following statements are true?

A. You can create an instance of NumberFormat using the static factory methods in NumberFormat.

B. You can create an instance of DecimalFormat using new DecimalFormat(Local).

C. DecimalFormat is a subclass of NumberFormat.

D. An instance created using the static factory methods in NumberFormat is also an instance of DecimalFormat.

E. You can create an instance of NumberFormat using new NumberFormat(Local).

25 Which of the following are in the java.text package?

A. DateFormat

B. SimpleDateFormat

C. DateFormatSymbols

D. Date

E. Locale

26 Which of the following code is correct to obtain hour from a Calendar object cal?

A. cal.getHour();

B. cal.get(Hour);

C. cal.get(Calendar.HOUR);

D. cal.hour();

27 How do you create a locale for the United States?

A. new Locale("en", "US");

B. new Locale("US", "en");

C. Locale.getLocale("en", "US")

D. Locale.US;

28 A resource bundle is ______

A. a Java class file or a text file that provides locale-specific information.

B. a Java source code the contains image, audio, and text files.

C. an audio file.

D. an image file.

29 To display number in desired format, you have to use the NumberFormat class or its subclasses.

A. true

B. false

30 You can find all the available locales from a Swing object.

A. true

B. false

31 The locale property is in the Component class, thus, every Java Swing component has the locale property.

A. true

B. false

32 You can get year, month, day, hour, minute, and second from an instance of GregorianCalendar.

A. true

B. false

33 You can get hour, minute and second from the Date class.

A. false

B. true

34 The TimeZone class has a static method for obtaining all the available time zone IDs.

A. false

B. true

35 You can get all the available locales from an instance of Calendar, Collator, DateFormat, or NumberFormat.

A. false

B. true

Keys:

1. A
2. A
3. D
4. D
5. B
6. A
7. A
8. A
9. A
10. B
11. A
12. D
13. D
14. D
15. D
16. A
17. B
18. A
19. B
20. B
21. B
22. B
23. B
24. ACD
25. ABC
26. C
27. AD
28. A
29. A
30. B
31. A
32. A
33. A
34. B
35. B

1