Wednesday, 17 September 2008

Java 2 Certification By Phillip Heller, Simon Roberts - Part1

Yesterday i started reading Complete Java 2 Certification Study Guide, 5th Edition By Phillip Heller, Simon Roberts.

I wanted to share few important quotations i found from the 1st 2 chapters of the book.

1) Assessment Test contains some basic java questions totally 30 questions.

Some questions to share are:

a) Which of the following statements are true? (Select all that apply.)
A. A final object’s data cannot be changed.
B. A final class can be subclassed.
C. A final method cannot be overloaded.
D. A final object cannot be reassigned a new address in memory.
E. None of the above.

b) Given the following code, what will be the outcome?


public class Funcs extends java.lang.Math {
public int add(int x, int y) {
return x + y;
}
public int sub(int x, int y) {
return x - y;
}
public static void main(String [] a) {
Funcs f = new Funcs();
System.out.println("" + f.add(1, 2));
}
}


A. The code compiles but does not output anything.
B. “3” is printed out to the console.
C. The code does not compile.
D. None of the above.

c) Given the following code, what is the expected outcome?


public class Test {
public static void main(String [] a) {
int [] b = [1,2,3,4,5,6,7,8,9,0];
System.out.println("a[2]=" + a[2]);
}
}



A. The code compiles but does not output anything.
B. “a[2]=3” is printed out to the console.
C. “a[2]=2” is printed out to the console.
D. The code does not compile.
E. None of the above.

d) Given the following code, which of the results that follow would you expect?


1. package mail;
2.
3. interface Box {
4. protected void open();
5. void close();
6. public void empty();
7. }
A. The code will not compile because of line 4.
B. The code will not compile because of line 5.
C. The code will not compile because of line 6.
D. The code will compile.




e) What will be the output of the following code?


public class StringTest {
public static void main(String [] a) {
String s1 = "test string";
String s2 = "test string";
if (s1 == s2) {
System.out.println("same");
} else {
System.out.println("different");
}
}
}



A. The code will compile but not run.
B. The code will not compile.
C. “different” will be printed out to the console.
D. “same” will be printed out to the console.
E. None of the above

f) Suppose you are writing a class that provides custom deserialization. The class implements java.io.Serializable (and not java.io.Externalizable). What method should implement the custom deserialization, and what is its access mode?

A. private readObject
B. public readObject()
C. private readExternal()
D. public readExternal()

2) An identifier is a word used by a programmer to name a variable, method, class, or label.
Keywords and reserved words may not be used as identifiers. An identifier must begin with a
letter, a dollar sign ($), or an underscore (_); subsequent characters may be letters, dollar signs,underscores, or digits.

Some examples are
foobar // legal
BIGinterface // legal: embedded keywords are ok
$incomeAfterTaxes // legal
3_node5 // illegal: starts with a digit
!theCase // illegal: bad 1st char

3) Variables and Initialization
Java supports variables of three different lifetimes:

Member variable A member variable of a class is created when an instance is created, and it
is destroyed when the object is destroyed. Subject to accessibility rules and the need for a reference to the object, member variables are accessible as long as the enclosing object exists.
Automatic variable An automatic variable of a method is created on entry to the method and
exists only during execution of the method, and therefore it is accessible only during the execution of that method.

Class variable A class variable (also known as a static variable) is created when the class is
loaded and is destroyed when the class is unloaded. There is only one copy of a class variable, and it exists regardless of the number of instances of the class, even if the class is never instantiated.

4) How to Cause Leaks in a Garbage Collection System

The nature of automatic garbage collection has an important consequence: you can still get memory leaks. If you allow live, accessible references to unneeded objects to persist in your programs, then those objects cannot be garbage collected. Therefore, it may be a good idea to explicitly assign null into a variable when you have finished with it. This issue is particularly noticeable if you are implementing a collection of some kind.

In this example, assume the array storage is being used to maintain the storage of a stack. This pop() method is inappropriate:

a. public Object pop() {
b. return storage[index--];
c. }

If the caller of this pop() method abandons the popped value, it will not be eligible for garbage collection until the array element containing a reference to it is overwritten. This might take a long time. You can speed up the process like this:

a. public Object pop() {
b. Object returnValue = storage[index];
c. storage[index--] = null;
d. return returnValue;
e. }

5) In Java, you never explicitly free memory that you have allocated; instead, Java provides automatic garbage collection. The runtime system keeps track of the memory that is allocated and is able to determine whether that memory is still useable. This work is usually done in the background by a low-priority thread that is referred to as the garbage collector. When the garbage collector finds memory that is no longer accessible from any live thread (the object is out of scope), it takes steps to release it back into the heap for re-use. Specifically, the garbage collector calls the class destructor method called finalize() (if it is defined) and then frees the
memory.

6) Below is the table for Order of Precedence.











CategoryOperators
Unary++ -- + - ! ~(type)
Arithmetic* / % + -(type)
Shift<< >> >>>
Comparison< <= > >= instanceof == !=
Bitwise& ^ |
Short-circuit&& ||
Conditional?:
Assignment= op=


7) Evaluation Order

In Java, the order of evaluation of operands in an expression is fixed. Consider this code fragment:
1. int [] a = { 4, 4 };
2. int b = 1;
3. a[b] = b = 0;

In this case, it might be unclear which element of the array is modified: Which value of b is used to select the array element,0 or 1 ? An evaluation from left to right requires that the leftmost expression,a[b], be evaluated first, so it is a reference to the element a[1]. Next,b is evaluated, which is simply a reference to the variable called b. The constant expression 0 is evaluated next, which clearly does not involve any work. Now that the operands have been evaluated,the operations take place. This is done in the order specified by precedence and associativity. For assignments, associativity is right to left, so the value 0 is first assigned to the variable called b; then the value 0 is assigned into the last element of the array a.

8) Arithmetic Promotion of Operands

Arithmetic promotion of operands takes place before any binary operator is applied so that all numeric operands are at least int type. This promotion has an important consequence for the unsigned right-shift operator when applied to values that are narrower than int. The diagram in Figure 2.2 shows the process by which a byte is shifted right. First the byte is promoted to an int, which is done treating the byte as a signed quantity. Next, the shift occurs, and 0 bits are indeed propagated into the top bits of the result—but these bits are not
part of the original byte. When the result is cast down to a byte again, the high-order bits of that byte appear to have been created by a signed shift right, rather than an unsigned one. This is why you should generally not use the logical right-shift operator with operands smaller than an int: It is unlikely to produce the result you expected.

About the Authors

Philip Heller is a technical author, novelist, public speaker, and consultant. He has been instrumental in the creation and maintenance of the Java Programmer and Developer exams. His popular seminars on certification have been delivered internationally. He is also the author of Ground-Up Java (available from Sybex), which uses interactive animated illustrations to present fundamental concepts of Java programming to new programmers.

Simon Roberts worked for Sun Microsystems for nine years as an instructor, an authority on the Java language, and the key player in the development of the entire Java certification program. He is now a consultant and instructor, specializing in Java and security. He is also a flight instructor.

No comments: