Chapter 1: Language fundamentals

Tuesday, March 31, 2009

Chapter 1:
Language fundamentals

1.1) Which of the following lines are valid declarations? [1] [3]
(a)char a = ‘\u0061’;
(b)char \u0061 = ‘a’;
(c)ch\u0061r a = ‘a’;
(d)char a = (char) 65;

1.2) Is an empty file a valid source file? [1]
(a) True.
(b) False.

1.3) Which of these are valid declaration of the main() method? [1]
(a) static void main(String args[]) {/* … */}(b) public static int main(String args[]) {/* … */}(c) public static void main(String args) {/* … */}(d) final static public void main(String[] arguments) {/* … */}(e) public int main(String args[], int argc) {/* … */}(f) public void main(String args[]) {/* … */}
1.4) Which one of the following are not valid character constants? [8]
Select any two.
(a) char c = '\u00001' ;
(b) char c = '\101';
(c) char c = 65;
(d) char c = '\1001' ;



Answers:
1.1) (a), (b), (c), (d)
All are valid declarations. The \uxxxx notation can be used anywhere in the source to represent Unicode characters, and also casted integer to a char primitive type.

1.2) (a)
Although nonsensical, an empty file ia a valid source file. A source file can contain an optional package declaration, any number of import statements and any number of class and interface definitions.

1.3) (d)
A valid declaration of the main() method must be public and static, have void as return type and take a single array of String objects as arguments. The order of the static and public keywords is irrelevant. Also, declaring the method final does not affect the method’s potential to be used as a main() method.

1.4) (a), (d)
You cannot use five digits after \u."char c = 65" is valid because it will take it as ascii value. '\101' is representing a octal value which is equivalent to 65 which is valid but '\1001' is not valid as its decimal value is 513 and you can give only those values which represent 0 to 255 in decimal.

0 comments:

Post a Comment