Tải bản đầy đủ - 0 (trang)
C: Objectives for the SCJP 1.6 Upgrade Exam

C: Objectives for the SCJP 1.6 Upgrade Exam

Tải bản đầy đủ - 0trang

864



APPENDIX C



1.2



1.3



1.4



1.5



Develop code that declares, initializes, and uses primitives, arrays, enums, and

objects as static, instance, and local variables. Also, use legal identifiers for variable names.





For primitive types, see Section 2.2, p. 28.







For arrays, see Section 3.6, p. 69.







For enums, see Section 3.5, p. 54.







For initialization of static, instance, and local variables, see Section 2.3, p. 31.







For initializers, see Section 9.7, p. 406



Develop code that declares both static and non-static methods and, if appropriate,

use method names that adhere to the JavaBeans naming standards. Also develop

code that declares and uses a variable-length argument list.





For methods, see Section 3.3, p. 44.







For JavaBeans naming standard, see Section 3.2, p. 41.







For varargs, see Section 3.8, p. 90.



Given a code example, determine if a method is correctly overriding or overloading another method, and identify legal return values (including covariant

returns), for the method.





For overloading methods, see Section 3.3, p. 47.







For overloaded method resolution, see Section 7.10, p. 324.







For overriding methods, see Section 7.2, p. 288.







For return values, see Section 6.4, p. 228.







For covariant return, see Section 7.2, p. 290.



Given a set of classes and superclasses, develop constructors for one or more of

the classes. Given a class declaration, determine if a default constructor will be

created and, if so, determine the behavior of that constructor. Given a nested or

non-nested class listing, write code to instantiate the class.





For constructors, see Section 3.4, p. 48.







For default constructors, see Section 3.4, p. 49.







For constructor chaining, see Section 7.5, p. 302, and Section 9.11, p. 416.







For instantiating nested classes, see Chapter 8, p. 351.



Section 2: Flow Control





2.1

2.2



For control flow, see Chapter 6, p. 203.



Develop code that implements an if or switch statement; and identify legal argument types for these statements.

Develop code that implements all forms of loops and iterators, including the use of

for, the enhanced for loop (for-each), do, while, labels, break, and continue; and



explain the values taken by loop counter variables during and after loop execution.



www.it-ebooks.info



APPENDIX C



865



2.3



Develop code that makes use of assertions, and distinguish appropriate from

inappropriate uses of assertions.



2.4



Develop code that makes use of exceptions and exception handling clauses (try,

catch, finally), and declares methods and overriding methods that throw exceptions.



2.5



Recognize situations that will result in any of the following being thrown:

ArrayIndexOutOfBoundsException,

ClassCastException,

IllegalArgumentException,

IllegalStateException, NullPointerException, NumberFormatException, AssertionError,

ExceptionInInitializerError, StackOverflowError or NoClassDefFoundError. Understand which of these are thrown by the virtual machine and recognize situations

in which others should be thrown programmatically.



Section 3: API Contents

3.1



3.2



Develop code that uses the primitive wrapper classes (such as Boolean, Character,

Double, Integer, and so on), and/or autoboxing & unboxing. Discuss the differences between the String, StringBuilder, and StringBuffer classes.





For boxing and unboxing, see Section 5.1, p. 162.







For primitive wrapper classes and string handling classes, see Chapter 10, p. 423.



Given a scenario involving navigating file systems, reading from files, writing to

files, or interacting with the user, develop the correct solution using the following

classes (sometimes in combination), from java.io: BufferedReader, BufferedWriter,

File, FileReader, FileWriter, PrintWriter, and Console.





3.3



Develop code that serializes and/or de-serializes objects using the following APIs

from java.io: DataInputStream, DataOutputStream, FileInputStream, FileOutputStream,

ObjectInputStream, ObjectOutputStream and Serializable.





3.4



For object serialization, see Section 11.6, p. 510.



Use standard J2SE APIs in the java.text package to correctly format or parse

dates, numbers, and currency values for a specific locale; and, given a scenario,

determine the appropriate methods to use if you want to use the default locale or

a specific locale. Describe the purpose and use of the java.util.Locale class.





3.5



For file I/O, see Chapter 11, p. 467.



For using locales, and formatting dates, numbers, and concurrency values, see Chapter

12, p. 531.



Write code that uses standard J2SE APIs in the java.util and java.util.regex

packages to format or parse strings or streams. For strings, write code that uses

the Pattern and Matcher classes and the String.split method. Recognize and use

regular expression patterns for matching (limited to: . (dot), * (star), + (plus), ?, \d,

\s, \w, [], ()). The use of *, +, and ? will be limited to greedy quantifiers, and the

parenthesis operator will only be used as a grouping mechanism, not for capturing content during matching. For streams, write code using the Formatter and



www.it-ebooks.info



866



APPENDIX C



Scanner classes and the PrintWriter.format/printf methods. Recognize and use

formatting parameters (limited to: %b, %c, %d, %f, %s) in format strings.





For string pattern matching using regular expressions, and formatting and tokenizing

values, see Chapter 12, p. 531.



Section 4: Concurrency

4.1



Recognize the states in which a thread can exist, and identify ways in which a

thread can transition from one state to another.





4.2



For thread states, see Section 13.6, p. 634.



Given a scenario, write code that makes appropriate use of object locking to protect static or instance variables from concurrent access problems.





For object locking, see Section 13.5, p. 626.



Section 5: OO Concepts





For OO concepts, see Chapter 7, p. 283.



5.1



Develop code that implements tight encapsulation, loose coupling, and high

cohesion in classes, and describe the benefits.



5.2



Given a scenario, develop code that demonstrates the use of polymorphism. Further, determine when casting will be necessary and recognize compiler vs. runtime errors related to object reference casting.



5.3



Explain the effect of modifiers on inheritance with respect to constructors,

instance or static variables, and instance or static methods.



5.4



Develop code that implements “is-a” and/or “has-a” relationships.



Section 6: Collections / Generics





For generics and collections, see Chapter 14, p. 661, and Chapter 15, p. 747, respectively.



6.1



Given a design scenario, determine which collection classes and/or interfaces

should be used to properly implement that design, including the use of the

Comparable interface.



6.2



Write code that uses the generic versions of the Collections API, in particular, the

Set, List, and Map interfaces and implementation classes. Recognize the limitations

of the non-generic Collections API and how to refactor code to use the generic

versions. Write code that uses the NavigableSet and NavigableMap interfaces.



www.it-ebooks.info



APPENDIX C



6.3



867



Develop code that makes proper use of type parameters in class/interface declarations, instance variables, method arguments, and return types; and write

generic methods or methods that make use of wildcard types and understand the

similarities and differences between these two approaches.





6.4



For generics, see Chapter 14, p. 661.



Use capabilities in the java.util package to write code to manipulate a list by sorting, performing a binary search, or converting the list to an array. Use capabilities

in the java.util package to write code to manipulate an array by sorting, performing a binary search, or converting the array to a list. Use the java.util.Comparator

and java.lang.Comparable interfaces to affect the sorting of lists and arrays. Furthermore, recognize the effect of the “natural ordering” of primitive wrapper

classes and java.lang.String on sorting.



Section 7: Fundamentals

7.1



Given an example of a class and a command-line, determine the expected runtime

behavior.





7.2



For program arguments, see Section 3.9, p. 95.



Given the fully-qualified name of a class that is deployed inside and/or outside a

JAR file, construct the appropriate directory structure for that class. Given a code

example and a classpath, determine whether the classpath will allow the code to

compile successfully.





For classpath and JAR files, see Section 4.3, p. 117, and Section 4.4, p. 120, respectively.



www.it-ebooks.info



This page intentionally left blank



www.it-ebooks.info



Appendix D



Annotated Answers

to Review Questions

1 Basics of Java Programming

1.1



(d)

A method is an operation defining the behavior for a particular abstraction. Java

implements abstractions using classes that have properties and behavior. Behavior

is defined by the operations of the abstraction.



1.2



(b)

An object is an instance of a class. Objects are created from classes that implement

abstractions. The objects that are created are concrete realizations of those abstractions. An object is neither a reference nor a variable.



1.3



(b)

(2) is the first line of a constructor declaration. A constructor in Java is declared like

a method, except that the name is identical to the class name, and it does not specify a return value. (1) is a header of a class declaration, and (3), (4), and (5) are

instance method declarations.



1.4



(b) and (f)

Two objects and three reference variables are created by the code. Objects are normally created by using the new operator. The declaration of a reference variable creates a variable regardless of whether a reference value is assigned to it or not.



1.5



(d)

An instance member is a field or an instance method. These members belong to

an instance of the class rather than the class as a whole. Members which are not

explicitly declared static in a class declaration are instance members.



869



www.it-ebooks.info



870



APPENDIX D



1.6



(c)

An object communicates with another object by calling an instance method of the

other object.



1.7



(d) and (f)

Given the declaration class B extends A {...}, we can conclude that class B extends

class A, class A is the superclass of class B, class B is a subclass of class A, and class B

inherits from class A, which means that objects of class B will inherit the field value1

from class A.



1.8



(d)

The compiler supplied with the JDK is named javac. The names of the source files

to be compiled are listed on the command line after the command javac.



1.9



(a)

Java programs are executed by the Java Virtual Machine (JVM). In the JDK, the

command java is used to start the execution by the JVM. The java command

requires the name of a class that has a valid main() method. The JVM starts the program execution by calling the main() method of the given class. The exact name of

the class should be specified, and not the name of the class file, i.e., the .class

extension in the class file name should not be specified.



2 Language Fundamentals

2.1



(c)

52pickup is not a legal identifier. The first character of an identifier cannot be a digit.



2.2



(e)

In Java, the identifiers delete, thrown, exit, unsigned, and next are not keywords.

Java has a goto keyword, but it is reserved and not currently used.



2.3



(b)

It is a completely valid comment. Comments do not nest. Everything from the start

sequence of a multiple-line comment (/*) to the first occurrence of the end

sequence of a multiple-line comment (*/) is ignored by the compiler.



2.4



(a) and (d)

String is a class, and "hello" and "t" denote String objects. Java has the following

primitive data types: boolean, byte, short, char, int, long, float, and double.



2.5



(a), (c), and (e)

(a) is a boolean data type, while (c) and (e) are floating-point data types.



www.it-ebooks.info



APPENDIX D



2.6



871



(c)

The bit representation of int is 32-bits wide and can hold values in the range –231

through 231–1.



2.7



(a), (c), and (d)

The \uxxxx notation can be used anywhere in the source to represent Unicode

characters.



2.8



(c)

Local variable a is declared but not initialized. The first line of code declares the local variables a and b. The second line of code initializes the local variable b. Local

variable a remains uninitialized.



2.9



(c)

The local variable of type float will remain uninitialized. Fields and static variables are initialized with a default value. Local variables remain uninitialized unless

explicitly initialized. The type of the variable does not affect whether a variable is

initialized or not.



2.10



(e)

The program will compile. The compiler can figure out that the local variable price

will be initialized, since the value of the condition in the if statement is true. The

two instance variables and the two static variables are all initialized to the respective default value of their type.



3 Declarations

3.1



(b)

Only (b) is a valid method declaration. Methods must specify a return type or must

be declared void. This makes (d) and (e) invalid. Methods must specify a list of zero

or more comma-separated parameters enclosed by parentheses, ( ). The keyword

void cannot be used to specify an empty parameter list. This makes (a) and (c)

invalid.



3.2



(a), (b), and (e)

Non-static methods have an implicit this object reference. The this reference cannot be changed, as in (c). The this reference can be used in a non-static context to

refer to both instance and static members. However, it cannot be used to refer to

local variables, as in (d).



www.it-ebooks.info



872



APPENDIX D



3.3



(a) and (d)

The first and the third pairs of methods will compile. The second pair of methods

will not compile, since their method signatures do not differ. The compiler has no

way of differentiating between the two methods. Note that the return type and the

names of the parameters are not a part of the method signatures. Both methods in

the first pair are named fly and, therefore, overload this method name. The methods in the last pair do not overload the method name glide, since only one method

has that name. The method named Glide is distinct from the method named glide,

as identifiers are case-sensitive in Java.



3.4



(a)

A constructor cannot specify any return type, not even void. A constructor cannot

be final, static, or abstract.



3.5



(b) and (e)

A constructor can be declared private, but this means that this constructor can only

be used within the class. Constructors need not initialize all the fields when a class

is instanstiated. A field will be assigned a default value if not explicitly initialized.

A constructor is non-static and, as such, it can directly access both the static and

non-static members of the class.



3.6



(c)

A compilation error will occur at (3), since the class does not have a constructor

accepting a single argument of type int. The declaration at (1) declares a method,

not a constructor, since it is declared as void. The method happens to have the same

name as the class, but that is irrelevant. The class has an implicit default constructor, since the class contains no constructor declarations. This constructor is invoked

to create a MyClass object at (2).



3.7



(a), (b), and (d)

We cannot instantiate an enum type using the new operator. An enum type is

implicitly final. Enum types inherit members from the Object class, as any other

reference type.



3.8



(d)

An enum type can be run as a standalone application. The constants need not be

qualified when referenced inside the enum type declaration. The constants are

static members. The toString() method always returns the name of the constant,

unless it is overridden.



3.9



(c)

An enum type can be run as a standalone application. (1), (2), and (3) define constant-specific class bodies that override the toString() method. For constants that do

not override the toString() method, the name of the constant is returned.



www.it-ebooks.info



APPENDIX D



3.10



873



(d)

An enum type cannot be declared as abstract. (b) is not correct, because without

the enum type name, it would be a call to an instance method in a static context.

Any abstract method must be implemented by each enum constant.



3.11



(c)

All enum types override the equals() method from the Object class. The equals()

method of an enum type compares its constants for equality according to reference

equality (same as with the == operator). This equals() method is final.



3.12



(a) and (d)

Declarations in (a) and (d) are overridden in each constant-specific class body. Declarations in (b) and (c) are not overridden by the declarations in the constant-specific

class bodies, because of the incompatible return type.



3.13



(c), (e), (f), and (g)

Note how the nested enum type constants are accessed. Enum constants of an

enum type can be compared, and an enum constant is an instance of its enum type.



3.14



(d)

Enum constants can be used as case labels and are not qualified with the enum type

name in the case label declaration. The switch expression is compatible with the

case labels, as the reference this will refer to objects of the enum type Scale5, which

is the type of the case labels. The call to the method getGrade() returns a char value,

which in this case is 'C'.



3.15



(d), (f), and (g)

A nested enum type must be declared inside a static member type, like (2), (3) and

(5). Note that a nested enum type is implicitly static, and the keyword static is not

mandatory in this case. An enum type cannot be local, as static member types cannot be declared locally.



3.16



(a), (b), and (d)

The static method values() returns an array with the enum constants for the specified type. The final method name() always returns the name of the enum constant.

There is no names() method for enums in the Java standard library. The loop in (d)

only converts the array of enums to a list, and iterates over this list. The argument

Direction.class is not an array and, therefore, an illegal argument to the asList()

method.



www.it-ebooks.info



874



APPENDIX D



3.17



(b)

A constructor in the enum type is called for each enum constant created, when the

enum type is loaded.



3.18



(d)

In Java, arrays are objects. Each array object has a final field named length that

stores the size of the array.



3.19



(a)

Java allows arrays of length zero. Such an array is passed as argument to the main()

method when a Java program is run without any program arguments.



3.20



(c)

The [] notation can be placed both after the type name and after the variable name

in an array declaration. Multidimensional arrays are created by constructing

arrays that can contain references to other arrays. The expression new int[4][] will

create an array of length 4, which can contain references to arrays of int values. The

expression new int[4][4] will create the same two-dimensional array, but will in

addition create four more one-dimensional arrays, each of length 4 and of the type

int[]. References to each of these arrays are stored in the two-dimensional array.

The expression int[][4] will not work, because the arrays for the dimensions must

be created from left to right.



3.21



(b) and (e)

The size of the array cannot be specified, as in (b) and (e). The size of the array is

given implicitly by the initialization code. The size of the array is never specified

in the declaration of an array reference. The size of an array is always associated

with the array instance (on the right-hand side), not the array reference (on the lefthand side).



3.22



(e)

The array declaration is valid, and will declare and initialize an array of length 20

containing int values. All the values of the array are initialized to their default

value of 0. The for(;;) loop will print all the values in the array, that is, it will print

0 twenty times.



3.23



(d)

The program will print "0 false 0 null" when run. All the instance variables,

including the array element, will be initialized to their default values. When concatenated with a string, the values are converted to their string representation.

Notice that the null pointer is converted to the string "null", rather than throwing

a NullPointerException.



www.it-ebooks.info



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

C: Objectives for the SCJP 1.6 Upgrade Exam

Tải bản đầy đủ ngay(0 tr)

×