The pages of this web site are not formatted to conserve paper, but my new book (ISBN: 0-9745862-0-X) is formatted to save paper, save your printer cartridge, save a loose-leaf binder, and save money. If you prefer to work my exams from printed pages, then give your printer a rest and buy my new book.
Today, you can find my book at BookSurge.com.
Are you a university student studying Java programming? Do you agree that my book would serve as a helpful workbook and companion to be used along with the Java fundamentals textbook that is currently being used in your class? If so, then please ask your professor to consider using my book in future classes.
If you have any questions or comments concerning my mock exams or my book, then please send an e-mail to me at scjpexam2000@yahoo.com.
I would also like to read your response to the following questions.
| No. | Answer | Remark | |
|---|---|---|---|
| 1 | d e | 4 5 | All of the escape sequences used in this question are defined for the C programming language. Those that are not also Java escape sequences result in a compile-time error. Java does not accept the hexadecimal escape sequences of the C programming language. However, Java does accept Unicode escapes (JLS 3.3). |
| 2 | c g | -esa -enablesystemassertions | Two command-line switches used to enable assertions in system classes are -esa and -enablesystemassertions. Remember that all of the letters are lower case. |
| 3 | f | Prints: DDDLDD | The round method does not return either of the two floating point primitive types, float or double. The ceil, sin and sqrt methods return only a value of type double. The abs and max methods are able to return an int, long, float or double depending on the argument type. |
| 4 | d | Prints: false,true,true | ListIterator extends Iterator. |
| 5 | a b d e f | By default assertions are disabled at run-time. Assertions can be selectively enabled for any named class. Assertions can be selectively enabled for any named package. If assertions are selectively enabled for any named package then assertions are automatically enabled for the subpackages. Assertions can be selectively enable for the unnamed package in the current working directory. | |
| 6 | c d | 3 4 | All methods declared within an interface are implicitly abstract and public. Although the abstract and public modifiers can legally be applied to a method declaration in an interface, the usage is redundant and is discouraged. Since all methods declared within an interface are implicitly public, a weaker access level can not be declared. |
| 7 | e f | 5 6 |
The overloaded contructors for the
String
class accept
a parameter of type
String
or
StringBuffer
or an
array of
byte
or
char.
There is no constructor that
will accept a primitive
byte
or
char.
Please note that
if you want to convert a primitive to a
String
then
you can use the static
|
| 8 | e | Prints: DDDD | The floor and ceil methods are not overloaded. There is only one version of each method. The parameter type is double, and the return type is double. |
| 9 | c | AssertionError | An AssertionError is thrown to indicate that an assertion has failed. Don't be fooled by the name AssertionException. |
| 10 | a b c | 1 2 3 | All methods declared within an interface are implicitly abstract and public. Although the abstract and public modifiers can legally be applied to a method declaration in an interface, the usage is redundant and is discouraged. The final, synchronized and native modifiers can not appear in the declaration of an abstract method, and can not be applied to an abstract method declared within an interface. |
| 11 | e | Prints: FDDD | The method name abs is overloaded. There are versions that accept one argument of type int, long, float or double. The type of the return value is the same as the argument type. The sqrt method is not overloaded. The parameter type is double and the return type is double. |
| 12 | f g | LinkedHashMap LinkedHashSet | The HashMap, HashSet and Hashtable classes are all implemented with an internal hashtable that organizes the elements in buckets according to the hashcode and not according to the order of insertion. The LinkedHashMap and LinkedHashSet classes also use an internal hashtable, and both also maintain a linked list through all of the elements. The order of the list is determined by the order of insertion. Optionally, the LinkedHashMap can maintain the order of the list based on the time of the most recent access of each element. The TreeMap and TreeSet classes are both implemented using a tree structure that is ordered based on a Comparator or the Comparable interface. |
| 13 | c d e | 3 4 5 | All methods declared within an interface are implicitly abstract and public. Although the abstract and public modifiers can legally be applied to a method declaration in an interface, the usage is redundant and is discouraged. Since all methods declared within an interface are implicitly public, a weaker access level can not be declared. |
| 14 | d | Prints: IIILFD |
There are four overloaded versions of the
|
| 15 | e | None of the above | All methods declared within an interface are implicitly abstract and public. Although the abstract and public modifiers can legally be applied to a method declaration within an interface, the usage is redundant and is discouraged. The modifiers, final, synchronized and native, can not appear in the declaration of an abstract method, but they can be added to an implementation of an abstract method. |
| 16 | e | The relationship between Cat and Dog is an example of an appropriate use of inheritance. | An appropriate inheritance relationship includes a subclass that "is-a" special kind of the superclass. The relationship between the Dog subclass and the Pet superclass is an example of an appropriate inheritance relationship, because a Dog "is-a" Pet. The relationship between the Cat subclass and the Dog superclass is not an example of an appropriate use of inheritance, because a Cat is not a special kind of a Dog. The goal of the OO paradigm is to develop software models that are accurate and reusable. If the software model is not accurate, then it probably is not reusable and the goals of the OO paradigm are not achieved. Code reuse and maintenance becomes increasingly difficult when inheritance is used to model inappropriate relationships. For example, suppose that somebody implements a herdSheep method in the Dog class. The Cat subclass would inherit the method and suddenly each instance of Cat would acquire the unwanted capability to make an attempt to herd sheep. It is difficult to imagine that a Cat would perform well in that role, so additional maintenance would be required to resolve the problem. |
| 17 | b d | A program will terminate only when all user threads stop running A thread inherits its daemon status from the thread that created it | |
| 18 | d | Prints: 12,true | The valueOf method returns a String representation of the input parameter. The input parameter may be of type Object, char[], or any primitive type. The toString method returns a reference to the existing String instance. It does not create a new instance of the String. |
| 19 | f | LinkedHashMap |
The iteration order of a
Map
is the order in which an iterator moves through the elements of the
Map.
The iteration order of a
LinkedHashMap
is determined by the order in which elements are inserted.
When a reference to an existing
Map
is passed as an argument to the constructor of
LinkedHashMap,
the
|
| 20 | b | Prints: SA SB CA CB | The static initializer of the super class runs before the static initializer of the subclass. The body of the superclass constructor runs to completion before the body of the subclass constructor runs to completion. |
| 21 | a | Prints: A |
If a
Runnable
target object is passed to the constructor of the
Thread
class, then the
|
| 22 | a | LinkedHashMap | The requirement to store key/value pairs is directly satisfied by a concrete implementation of the Map interface. The List and Set interfaces recognize objects, but do not recognize keys and values. The requirement to allow null elements is not satisfied by a Hashtable. The LinkedHashMap offers the option to remove the least recently used (LRU) element when a new element is added. The LinkedHashSet does not offer the LRU option. |
| 23 | e | Prints: T1T1T3 |
The |
| 24 | e | Compile-time error |
The
|
| 25 | c | Prints: 6,6 | Suppose the print statement, System.out.print("(" + i + "," + j + ")");, is inserted at the top of the loop. The output of the program would be as follows: (0,9)(2,8)(4,7)6,6. The variable, i, is incremented twice with each pass through the loop. The variable, j, is decremented once with each pass. The loop terminates when i reaches six. |
| 26 | e | Prints: 9,4 | Suppose the print statement, System.out.print("(" + i + "," + j + ")");, is inserted at the top of the loop. The output of the program would be as follows: (1,8)(3,7)(5,6)(7,5)9,4. The variable, i, is incremented twice with each pass through the loop. The variable, j, is decremented once with each pass. The boolean expression of the while loop, i++ <= j--, is false when i reaches 8 and j reaches 5. The value of i is subsequently incremented and j is decremented yielding 9 and 4 respectively. Those values are printed. |
| 27 | f | Prints: 8,4 | Suppose the print statement, System.out.print("(" + i + "," + j + ")");, is inserted at the top of the loop. The output of the program would be as follows: (0,9)(2,8)(4,7)(6,6)(7,5)8,4. The initial value of j is 9. With each pass through the loop, the expression, j-- < 7, decrements j. The initial value of variable i is 0. With each pass through the loop, the expression, i++ < 7, increments variable i. Inside the body of the loop, i is also incremented as long as j is greater than or equal to 7. When j is less than seven, variable i is no longer incremented inside the body of the loop, but it is still incremented by the expression, i++ < 7. |
| 28 | c | Prints: 3,1 | Suppose the print statement, System.out.print("("+i+","+j+","+k+")");, is inserted before the switch statement. The output of the program would be as follows: (1,0,1)(2,0,2)(2,1,3)(3,1,4)3,1. On the first iteration, case 1 is processed. The "continue label1;" statement causes control to go directly to the top of the for-loop following the label, label1. The boolean expression of the do-loop is not processed. On the second iteration, case 2 is processed. The break statement causes the switch statement to complete, and control passes to the boolean expression of the do-loop. On the third iteration, case 3 is processed. The break with label statement, "break label2;", completes abruptly; and the do-loop completes abruptly without processing the loop expression, ++j<5, so j is not incremented. On the fourth iteration, case 4 is processed. The break with label statement, "break label1;", completes abruptly, and the for-loop completes abruptly. |