Archive for the 'scjp' Category

A SCJP question about Java enumerations

Thursday, January 11th, 2007

Given the following:

1.public enum Wallpaper {
2.  BROWN, BLUE, YELLOW;
3.}

Which of the following are legal?

  1. enum PatternedWallpaper extends Wallpaper {
    STRIPES, DOTS, PLAIN;
    }
  2. Wallpaper wp = Wallpaper.BLUE;
  3. Wallpaper wp = new Wallpaper(Wallpaper.BLUE);
  4. void aMethod(Wallpaper wp) {
    System.out.println(wp);
    }
  5. int hcode = Wallpaper.BLUE.hashCode();

Answer: only items 2, 4 and 6 are corrects. We can´t extend or instantiate an enumeration. The item 2 is the correct way to get a enumeration reference, the item 4 shows a method receiving a enumeration reference, and finally item 6 shows a call for the hashCode() method that all enums inherit from Object.

A SCJP question about arrays

Wednesday, January 10th, 2007

Question: Given the following:

1.public class CommandArgs {
2.   public static void main(String[] args) {
3.     String s1 = args[1];
4.     String s2 = args[2];
5.     String s3 = args[3];
6.     String s4 = args[4];
7.    System.out.println(" args[2] = " + s2);
8.  }
9.}

and the command-line invocation,

java CommandArgs 1 2 3 4

what is the result?

  1. args[2] = 2
  2. args[2] = 3
  3. args[2] = null
  4. args[2] = 1
  5. Compilations fails
  6. An exception is thrown at runtime

Ok, this question is trying to cheat you. Look well. First of all, the first index in a Java array is zero. Then, the command line showed sets args[0] = 1, args[1] = 2, args[2] = 3 and args[3] = 4. The assignment:

6.     String s4 = args[4];

Causes a RuntimeException, or more precisely, an ArrayOutOfBoundsException. The correct answer is 6.

References:

A SCJP question about right shifts and two´s complement aritmetic

Wednesday, January 10th, 2007

Which of the following expressions results in a positive value in x?(Choose all that apply)

  1. int x = -1; x = x >>> 5;
  2. int x = -1; x = x >>> 32;
  3. byte x = -1; x = x >>> 5;
  4. int x = -1; x = x >> 5;

To solve this question, we need to know:

  • All numerical data types are stored as bit patterns.
  • how to represents negative numbers using two´s complement.
  • Integers are signed 32 bits values, bytes are 16 bits values.
  • All numerical operands returns a integer value.
  • >> is a right shift operand (keeps the leftmost bit unchanged), >>> is a unsigned right shift operand (left-bits are zero filled).
  • When the value to be shifted (left-operand) is an int, only the last 5 digit bits of the right-hand operand are used to perform the shift. The actual size of the shift is the value of the right-hand operand masked by 31 (0×1f). ie the shift distance is always between 0 and 31 (if right-operand is >= 32, the shift is the right-operand % 32)

With all this by heart, it becomes easy now.

In the first item, we need to use two´s complement to know what is put into the x variable. To represent a negative number, first we invert each bit, then add 1. In this case, 1 = 00000000000000000000000000000001, inverting becomes 11111111111111111111111111111110, then adding 1, 11111111111111111111111111111111.

The operator >>> is a unsigned right shift. In other words, the shift affects all the bit pattern, including the leftmost bit. Doing 5 unsigned shifts in 11111111111111111111111111111111 returns 00000111111111111111111111111111. I´m too lazy to make the aritmetic, but this number is positive for sure (you see the leftmost bit?). The first alternative is part of the answer.

In the second alternative, how the right-operand is equal 32, the shift is 32 % 32 = 0, then the bit pattern stay unchanged, returning a negative value.

The third is easy. Remember: numerical operands returns integers values, always. Then, without a cast, the third item returns a very nasty:

Type mismatch: cannot convert from int to byte

The fourth item shows a signed shift operand, then, doesn´t matter how many shifts, the result is negative.

Conclusion: only the first item returns a positive value.

Java Certification Path - Class Access - Public Access

Sunday, November 19th, 2006

A class with the public modifier gives to all classes in all packages access to it. In other words, all classes can instantiate, extend or return a class of that type. Example:

package humanity;
public class Person {}

This class is visible to all classes in all packages. But if this class is in a different package, we need to import it yet.
Some exceptions exists, mainly when we use the nonaccess final modifier. I will explain soon.

Java Certification Path - Reserved Words

Saturday, November 18th, 2006

Ok, my most boring post until now. Don’t forget these Java keywords and reserved words:

abstract class extends implements null strictfp true
assert const false import package super try
boolean continue final instanceof private switch void
break default finally int protected synchronized volatile
byte do float interface public this while
case double for long return throw  
catch else goto native short throws  
char enum if new static transient  

Java Certification Path - Class Access - Default Access

Saturday, November 18th, 2006

A class with default access has no modifier preceding it in the declaration. The default access is a package-level access, because a class with default access can be seen only by classes within the same package. If a class has default access, a class in another package won’t be able to create a instance of that class, or even declare a variable or return type. The compiler will complain. For example:

package humanity;
class Person {}

package family;
import humanity.Person;
class Child extends Person {}

Try to compile this 2 sources. As you can see, they are in different packages, and the compilation will fail.

Java Certification Path

Saturday, November 18th, 2006

After thinking a lot about, I take a decision: I will take some certification exams in the Java Language. My object is to take the entry lavel exam Sun Certified Java Programmer (SCJP), and after follow the path until the Sun Certified Enterprise Architecture (SCEA).
I will try to put some notes here, about what I learn while chasing this objectives.
The book Sun Certified Programmer & Developer for Java 2 Study Guide (Exam 310-035 & 310-027) will be my partner for the next months. I will try to get my Java Black Belt, while I too do some mock exams. Cross your fingers!