Java Certification Exam Notes

1) Boolean b = new Boolean("abcd");

Will compile with out error

Any string other than "true" or "false" will create a Boolean object with a false state.

2) What is displayed when the following piece of code is compiled and executed:

 

class Test{

public static void main(String [] args){

Base b = new Subclass();

System.out.println(b.x);

System.out.println(b.method());

}

}

class Base{

int x = 2;

int method(){

return x;

}

}

class Subclass extends Base{

int x = 3;

int method(){

return x;

}

}

Answer :

2

3

Explanation :

The rule for accessing method and variable is as follows:

-the object reference is used when accessing variables (so the value of b.x is therefore 2).

-the underlying object is used when accessing methods (so the method called is actually the method defined in Subclass, and in Subclass, x =3).

3) Integer -- is a legeal name for a variable

4) Constructors cannot be:

-final

-static

-synchronized

-abstract

 

5) What is the result of compiling and executing the following fragment of code:

 

boolean flag = false;

if (flag = true) {

System.out.println("true");

} else {

System.out.println("false");

}

Answer:

The text "true" is displayed.

Explanation :

The expression "if (flag = true)" is a valid one.

The result of this is to assign the value "true" to the flag. Since this assignment itself results in a true condition, the first branch of the "if" clause is executed

 

7) Consider the following code segment and select the correct statement(s):

1. class FinalTest{

2. final int q;

3. final int w = 0;

4.

5. FinalTest(){

6. q = 1;

7. }

8.

9. FinalTest(int x){

10. q = x;

11. }

12. }

Answer :

The code compiles correctly without any warnings or errors.

Explanation :

The code segment is valid. It is allowable to initialise a final variable BEFORE IT IS USED, providing it was not assigned a value statically (ie, at declaration).

8) Both TextArea and TextField extend from TextComponent

9) It is valid to declare an inherited method as abstract.

Answer :

True

Explanation :

It is valid. However, there is no way to get to the behavior that is located above the abstract method in the hierarchy.

 

10) Which of the following will display the string "ica", given:

String s = "Metallica";

Answer:

System.out.println(s.substring(6));

Explanation:

A second parameter (as in subString(a, b)--- s.substring(6,8)) refers to the index of the character which terminates the subString. However, this last character is not included in the subString itself. Therefore Answer 3 displays "ic", rather than "ica".

11) Facts about HTML Tags

- The CODE, WIDTH and HEIGHT tags are mandatory and the order is insignificant.

- The PARAM tag is case insensitive.

- It is possible to download multiple JAR's with the ARCHIVE tag (eg, ARCHIVE = "a.jar, b.jar").

12) Inner Class:

static variables can exist inside an inner class if the inner class is also static

13) Valid Method of Array Declaration:

int []x[] = {{1,2,3},{1,2,3}};

14) What is the result when the following piece of code is compiled and executed.

1. public class Calc {

2. public static void main (String args []) {

3. int total = 0;

4.

5. for (int i = 0, j = 10; total < 30; ++i, --j) {

6. System.out.println(" i = " + i + " : j = " + j);

7.

8. total += (i + j);

9.

10. }

11.

12. System.out.println("Total " + total);

13. }

14. }

Answer:

The code compiles correctly and displays the following when executed:

i = 0 : j = 10

i = 1 : j = 9

i = 2 : j = 8

Total 30

Explanation:

The statement "int i = 0, j = 10" is a valid clause. However, if j is defined as "int j" at line 4 (i.e., outside the for loop) then a compile error would result, because you can't mix the declaration of local for-loop variables and external variables.

15) RandomAccessFile

- An IOException is thrown if the specified file doesn't exist when creating using the "r" mode.

- When used with the "rw" mode, the specified file is created on a diskdrive, if it doesn't already exist.

16) The keyword "synchronized" can be placed before the following without causing a compilation error

- class methods

- instance methods

- any block of code within a method

17) Constructors for Byte

Byte (byte value)

Byte (String s) throws NumberFormatException

Constructs a Byte object initialized to the value specified by the String parameter. The radix is assumed to be 10.

The constructors of all the wrapper classes fall in two categories on that accepts the corresponding primitive type and the other which accepts a String. The constructor that accepts a String throws a NumberFormatException.

18) java.lang.Boolean

java.lang.Double

Are both immutable

 

19) The programmer isn't supposed to handle

Programmer can handle only checked exceptions.

Any object that is of class java.lang.Exception, or nay subclass of java.lang.Exception except subclass of java.lang.RuntimeException is a checked exception.

20) If the search for the try block reaches the top of the method call hierarchy ( that is the point where the thread was created ), then the thread is killed and a message and stack trace is dumped to System.err.

21)