Prévia do material em texto
Certified Java Programmer Mock Exam 19
10-c d e f -The ready state to the running state. The running state to the Not-Runnable state. The running state to the ready state. The Not-
Runnable state to the ready state. -A dead thread can not be restarted.
11-a -The number printed is greater than or equal to 0. -The a1 is a daemon thread so the program can run to completion even if thread a1 is
still running, waiting or sleeping. The notify method is never invoked on Thread a1. If Thread a1 were not a daemon thread then the program
would wait forever. However, the program will run to completion without waiting for thread a1.
12-e -Compile-time error. -Remember that the Thread.start method is an instance method and can not be invoked from a static context.
13-e -Prints: T1T1T3 -The Thread.currentThread method returns a reference to the currently executing thread. When the run method is
invoked directly it does not start a new thread so T1 is printed twice.
14-a b c d -Thread.interrupted is a static method. Thread.isInterrupted is an instance method. Thread.interrupted clears the interrupt status
flag but Thread.isInterrupted does not. The boolean value false will be returned if Thread.interrupted or Thread.isInterrupted is invoked after the
InterruptedException has been thrown. -Thread.interrupted is a static method that determines if the currently executing thread has been
interrupted. Thread.isInterrupted is an instance method that determines if the referenced thread instance has been interrupted.
Thread.interrupted clears the interrupted status flag, but Thread.isInterrupted does not clear the status flag. If either method is invoked after the
InterruptedException has been thrown the returned value will be the boolean value false.
15-b -ready -
16-a c -primitives A local variable that is an object reference. -Primitives don't have locks and therefore can not be used to synchronize
threads. A local variable can not be used to synchronize threads because each thread has its own copy of local variables and their locks.
17-c -The printed number must always be greater than 10000. -If the interrupt method is invoked on a thread that is sleeping or waiting, then
the thread will wake up and then throw an InterruptedException when it starts to run. If the interrupt method is invoked on a thread that is not
sleeping or waiting, then no exception is thrown. Instead, the interrupted flag is set and the thread may check the flag at its own discretion using
the interrupted method. In this code example the thread never checks the interrupted flag so the interrupt is just ignored and the run method
runs to completion. The elapsed time printed by the main method will therefore be greater than 10000.
18-b c -The thread moves out of the Not-Runnable state. An InterruptedException is thrown when the thread moves into the running state. -A
sleeping thread is in the Not-Runnable state. If the interrupt method is invoked on a sleeping thread it will move out of the Not-Runnable state.
When it reaches the running state it will throw an InterruptedException and the interrupt status flag will be cleared. The Thread.interrupted
method will return false if it is invoked in the catch clause that catches the InterruptedException.
19-e -An IllegalThreadStateException is thrown at run-time. -Invoking the start method on a running thread will generate an
IllegalThreadStateException. The exam might have a question that invokes the start method on the same thread twice. The correct answer will
state that an exception is thrown at run-time. In reality, the JVM would probably ignore the second attempt to start the thread and would
probably not throw the exception if the thread were already dead. This code example includes a lot of thread synchronization code that ensures
that the thread is not dead when the start method is invoked the second time. You can run this example yourself and get the correct result. On
the real exam, the synchronization code will not be included.
Capítulo 10 – The java.lang Package (A)
No.-Answer-Remark
1-a -Prints: long primitive -The Long.parseLong method returns a long primitive.
2-c -Prints: int,int,int -The Math.min method is overloaded. There are versions that accept parameters of types int, long, float, and double. The
type of the return value is the same as the type of the input parameter. If a byte or a short is passed to the Math.min method as an input
parameter, then it will be promoted to a type that is accepted by Math.min. The type that it is promoted to will depend on the type of the other
parameter. If both parameters are of type byte, then both are promoted to an int. Similarly, if both parameters are of type short, then both are
promoted to int. If one is of type byte or short and the other is of type int, then the byte or short is promoted to an int.
3-e g -parseDouble valueOf -
4-f -None of the above. -None of these statements that pass a String as an argument will generate a compile-time error but some will generate
a run-time error. The Short wrapper class has two constuctors. One accepts an argument of type short and the other accepts a String. A String
argument must represent an integral primitive type. A leading minus sign can be added to indicate a negative value. A leading zero added to
represent an octal value will be ignored and the String will be interpreted as a decimal value. A leading zero and letter x added to a String to
indicate a hexadecimal value will result in a run-time error.
5-g -Runtime Error -The Long.parseLong method accepts a String parameter that represents a numeric value. Long.parseLong assumes that
the input String represents a decimal value unless a second parameter is provided to specify the radix. All of the characters in the String must
be digits of the specified radix. The parseLong method does not determine the type of the numeric value based on a suffix such as l, L, f, F, d, or
D. Use of any such suffix generates a NumberFormatException at run time.
6-f -Run-time Error -The parseByte and valueOf methods use a second parameter, radix, to specify the base of the number system. For
example, if the radix is two then the number is binary. If the radix is 8 then the number is octal. The Byte constructor does not accept a radix
parameter. The only Byte method that is able to decode a prefix such as 0x to determine the radix is the Byte.decode method, but that method
will not appear on the exam. None of the methods on the exam, parseByte, valueOf, or the constructor, are able to decode a prefix that specifies
the radix.
7-c -Compile-time Error -Integer has two constructors. The first accepts a parameter of type primitive int. The second accepts a parameter of
type String. The attempt to pass primitiveLong and primitiveFloat to a constructor of the Integer class generates a compiler error because
method invocation type conversions do not include implicit narrowing conversions such as the narrowing of a long or float to an int.
8-d -Prints: true,true -The expression (b1.equals(b2)) compares the values of two instances of byte objects. Since both instances contain the
value one the return value of the method call is true. The expression (a==b) compares the hash codes of two instances of Byte objects. Since
the two Byte objects contain the same value the result is true.
9-h -Prints: true,true,true -NaN is the only value that is not equal to itself. The Double.isNaN method returns the result of the expression (v !=
v).
10-c d -3. 4. -The boolean literals are written with lower case letters. The boolean literals can not be written with upper case letters. A String
representation of a boolean is acceptable in both upper and lower case letters.
11-c -Prints: 1,-1,0 -String.indexOf returns negative one if the argument is not found in the String. Zero is returned if the argument is anempty
String.
12-d -Prints: true,true -The Math.random method returns a primitive of type double. The value is equal to or greater than zero and less than
1.0.
13-k -None of the above. -The program compiles and runs without error. The output is 1.0,1.0,Infinity,-Infinity,NaN. The String parameter can
represent any floating point literal including Infinity and NaN. However, integral literals that are not floating point literals are not accepted in
String format. For example, the suffix used to declare a long, L, is not accepted in String format. However, a long literal value that is not in String
format is promoted to a float primitive and is accepted by the constructor that has a float parameter.
14-b d f -Float f2 = new Float("A"); Float f4 = new Float("1L"); Float f6 = new Float("0x10"); -The Float constructor is overloaded. One
accepts a primitive of type float. One accepts a primitive of type double. One accepts a String representation of a floating point literal. The
acceptable string representations of numeric values are more restricted than the acceptable primitive literal representations. The char primitive
literal is promoted to a float and is accepted by the constructor with a parameter of type float. The String containing the character A can not be