NOTES
 - all tokens must be present for each question
 - the "question" token is different, simply to improve human readability!
 - any text should immediately follow the relevant token
 - the correct answer list is comma-separated, and 1-based (not zero based)
 - don't add in new line characters ('/n' or '/r') to text unless you mean it!
 It'll screw up the displaying of question and answer text in Jxam (this is why the text is allowed to run off the end of the right hand margin of the page... :-)


General Comments...

- Some people have pointed out that JARs and HTML tags do not appear to be 
on te exam sylabus. Fair point. However, I've included some questions on
these issues, since both books I used for study (Roberts/Hellor and Boone)
covered these topics.








##------------------------------question------------------------------##
Which of the following statements are valid, given the following variable declarations:  boolean a; boolean b; int c;

##ans1##
(a | b)

##ans2##
(a || a)

##ans3##
(a ^ b) | c

##ans4##
(a & c)

##ans5##
(a && c)

##correct##
1,2

##multipleSelection##
TRUE

##explanation##
Answers 3, 4 and 5 are incorrect because these operations requires that an int (c) be converted to a boolean, or the boolean be converted to an int. Both of these conversions are illegal.


##------------------------------question------------------------------##
Which of the following can be applied to constructors:

##ans1##
final

##ans2##
static

##ans3##
synchronized

##ans4##
native

##ans5##
None of these.

##correct##
5

##multipleSelection##
TRUE

##explanation##
A constructor cannot be abstract, final, native, static or synchronized. Refer to the Java Language Specification (section 8.6.3, version 1.0).


##------------------------------question------------------------------##
Which of the following retain their preferred size (width and height) when added (individually) to the North section of a container with a BorderLayout (assume that no other components or containers are present in the North section).

##ans1##
TextArea

##ans2##
Button

##ans3##
TextField

##ans4##
Checkbox

##ans5##
None. All of these mentioned components will only retain their preferred height.

##correct##
4

##multipleSelection##
TRUE

##explanation##
In general, all components are resized and only retain their preferred height in the NORTH region. However, the CheckBox is not deformable, so it retains it's preferred size.


##------------------------------question------------------------------##
Which of the following are legal names for variables.

##ans1##
_int

##ans2##
%large

##ans3##
$fred

##ans4##
Integer

##ans5##
2much

##correct##
1,3,4

##multipleSelection##
TRUE

##explanation##
Variable names must begin with one of the following:
* a letter [a-z,A-Z]
* a $ character [$]
* an under-score [_]

All other characters are illegal and will cause an error at compile-time. Refer to the Java Language Specification.


##------------------------------question------------------------------##
Which of the following are correct ways to create a font.

##ans1##
Font f = new Font("Serif", Font.BOLD, 24);

##ans2##
Font f = new Font(Font.SERIF, "Bold", 24);

##ans3##
Font f = new Font("Serif", "Bold", 24);

##ans4##
Font f = new Font(Font.SERIF, Font.BOLD, 24);

##ans5##


##correct##
1

##multipleSelection##
TRUE

##explanation##
Refer to the standard JDK API documentation.


##------------------------------question------------------------------##
Select the correct statements regarding the following piece of code.

File f = new File("c:\\large.txt");

##ans1##
On execution, a file called "large.txt" will be created on the local harddisk.

##ans2##
The code fails to compile on a UNIX machine, because the directory separator is not correct.

##ans3##
A file is NOT created on the harddisk when this code is executed.

##ans4##
An exception is thrown at runtime if the file "large.txt" already exists.

##ans5##
The code fails to compile, since this is not a valid constructor for the File class.

##correct##
3

##multipleSelection##
TRUE

##explanation##
When a File class is constructed, it simply creates a handle to a File object. No files are created on the local harddisk.

As the JDK API documentation points out, the File class is "an abstract representation of file and directory pathnames".

The File class also contains directory separator attributes, which are platform specific. Therefore, the directory separator used is irrelevant.

Also, since the File class simply creates a reference to a file object, no exception is thrown if the file already exists on the disk.


##------------------------------question------------------------------##
Which of the following statements are correct regarding the RandomAccessFile class?

##ans1##
An IOException is thrown if the specified file doesn't exist when created using the "r" mode.

##ans2##
This class has a method which allows a file to be deleted from the harddisk.

##ans3##
It is possible to use this class in conjunction with the DataInputStream class.

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

##ans5##
There are methods to read and write primatives (eg, readInt(), writeInt(), etc).

##correct##
1,4,5

##multipleSelection##
TRUE

##explanation##
Answer 1 is correct, because in read mode, the file must exist if any other operations are to be performed on it.

The RandomAccessFile is not compatible with the Stream or reader/writer classes, so Answer 3 is incorrect.

Refer to the JDK API documentation for further information on this class.


##------------------------------question------------------------------##
Consider the following piece of code and select the correct statement from the following.

1.String s = new String("abcdefgh");
2.s.replace('d', 'q');
3.System.out.println(s);

##ans1##
The code fails to compile, reporting an error at line 2. Strings are immutable, and therefore a replace() method is meaningless.

##ans2##
The code compiles correctly, and displays the text "abcqefgh".

##ans3##
The code compiles correctly, and displays the text "abcdefgh".

##ans4##
The code compiles, but an exception is thrown when line 2 is executed.

##ans5##
The code compiles, but an exception is thrown at line 3.


##correct##
3

##multipleSelection##
FALSE

##explanation##
Strings are immutable, and so they cannot be modified. However, as the JDK API documentation will show, the String class has a replace() method, so answer 1 is incorrect.

If line 2 read as "s = s.replace('d', 'q')" then a new object would be created and assigned to s. The value of this new String object would be "abcqefgh".  However, as line 2 is presented, no new object is created, and so 's' remains unchanged.



##------------------------------question------------------------------##
Which of the following keywords can be applied to the variables or methods of an interface.

##ans1##
static

##ans2##
private

##ans3##
synchronised

##ans4##
protected

##ans5##
public

##correct##
1,5

##multipleSelection##
TRUE

##explanation##
Refer to the Java language specification.


##------------------------------question------------------------------##
True or False.

Only Frames can contain menu bars or pull-down menus.

##ans1##
True

##ans2##
False.

##ans3##


##ans4##


##ans5##


##correct##
2

##multipleSelection##
FALSE

##explanation##
This question has been the source of much correspondence between myself and numerous people who have used Jxam, so you may be justified in feeling confused - join the club :-)

I came across the statement during my study for the exam, and simply noted it as fact. I never really looked too deeply into it - so please don't email me for an explanation :-)

For further reference, this question is taken indirectly from the book "Java 1.1 Certification Study Guide" by Simon Roberts and Philip Heller (ISBN 0-7821-2069-5), chapter 9, Section "The Menu Components", page 267.

By the way, I would highly recommend this book (or its 1.2 successor) as a study guide! If you only get one book for the exam, get this one!


##------------------------------question------------------------------##
Consider the following piece of code and select the correct statement(s):

1. class A{
2.     protected int method(){
3.     }
4. }
5.
6. class B extends A{
7.     int method(){
8.     }
9. }

##ans1##
The code fails to compile, because you can't override a method to be more private than its parent.

##ans2##
The code fails to compile, because method() is declared as protected, and is therefore not available to any subclass.

##ans3##
The code compiles correctly, but throws a NullPointerException at runtime.

##ans4##
The code fails to compile. However, it can be made to compile correctly by prefixing line 7 with the access qualifier "public".

##ans5##
The code fails to compile. However, it can be made to compile correctly by prefixing line 7 with the access qualifier "protected".

##correct##
1,4,5

##multipleSelection##
TRUE

##explanation##
The access hierarchy in increasing levels of accessibility is:

private->"friendly"->protected->public

[where "friendly" is used to mean that there is no access qualifier for the method]

Therefore, Answer 1 is correct, because class B attempts to override method() to be less public (ie, protected to "friendly").

Answer 2 is incorrect, because a protected variable or method is always available to child classes (this is the purpose of the protected keyword).

Answer 3 is incorrect, because all the information needed to verify this code is available at compile time.

Answers 4 and 5 are correct because adding either "public" or "protected" to line 7 actually moves it higher up the access hierarchy (ie, more public than friendly).


##------------------------------question------------------------------##
True or False.

The Throwable class is the superclass of all exceptions in the Java language.

##ans1##
True

##ans2##
False

##ans3##


##ans4##


##ans5##


##correct##
1

##multipleSelection##
FALSE

##explanation##
Refer to the JDK API documentation.


##------------------------------question------------------------------##
Consider the following piece of code (assume the Graphics context g is defined correctly):

g.setBackground(Color.red);
g.setForeground(Color.white);
g.drawLine(10, 10, 50, 10);
g.setForeground(Color.blue);
g.drawRect(100, 100, 50, 50);

What is displayed when this code is executed.

##ans1##
A blue line from (10,10) to (50,10) and a blue rectangle with upper left corner at (100,100).

##ans2##
A white line from (10,10) to (50,10) and a blue square with top left corner at (100,100).

##ans3##
A white line from (10,10) to (10,50) and a blue square with lower left corner at (100,100).

##ans4##
A red line from (10, 10) to (50,10) and a red square with upper left corner at (100,100).

##ans5##
Nothing is displayed. You must first issue a repaint() command.

##correct##
2

##multipleSelection##
FALSE

##explanation##
Setting the colours only affects subsequent drawing operations.

Refer to the JDK API documentation for descriptions of the methods.


##------------------------------question------------------------------##
Consider the following piece of code.

class Test{
     public static void main(String [] args){
         System.out.println(args[3]);
     }
}

When the following is typed at the command line, what is displayed:

java Test Metallica Justice For All


##ans1##
All

##ans2##
For

##ans3##
Justice

##ans4##
Nothing.

##ans5##
Nothing. An ArrayIndexOutOfBoundsException is thrown

##correct##
1

##multipleSelection##
FALSE

##explanation##
The command line args are passed in, indexed from zero. The args excludes the program name ("Test").


##------------------------------question------------------------------##
Consider the following piece of code.

1. String s = "abcd";
2. Integer x = new Integer(3);
3. String s2 = s + 4;
4. s2 = null;
5. s = null;

Following the execution of which line above, is the object referenced by s2 available for garbage collection.

##ans1##
Line 5

##ans2##
It is not possible to say when an object is available for garbage collection.

##ans3##
Line 4

##ans4##
The objects are not available until the executing thread is ended.

##ans5##


##correct##
3

##multipleSelection##
FALSE

##explanation##
Setting an object reference to null indicates to the JVM that the object is no long referenced, and so available for garbage collection.

Garbage collection is not related to the execution of the thread, so answer 4 is incorrect.


##------------------------------question------------------------------##
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;
     }
}


##ans1##
Nothing. The code fails to compile because the object b is not created in a valid way.

##ans2##
2
3

##ans3##
2
2

##ans4##
3
3

##ans5##
3
2

##correct##
2

##multipleSelection##
FALSE

##explanation##
The rule for accessing methods and variables in a class hierarchy 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).

Therefore answer 2 is correct.


##------------------------------question------------------------------##
What is displayed when the following is executed:

String s1 = "aaa";
s1.concat("bbb");
System.out.println(s1);


##ans1##
The string "aaa".

##ans2##
The string "aaabbb".

##ans3##
Nothing. concat() is not a valid method in the String class.

##ans4##
The string "bbbaaa".

##ans5##
The string "bbb".

##correct##
1

##multipleSelection##
FALSE

##explanation##
Strings are immutable, so concatenating a string will not modify the object reference.


##------------------------------question------------------------------##
True or False.

The following is a valid way to construct a StringBuffer.

StringBuffer sb1 = "abcd";


##ans1##
True

##ans2##
False

##ans3##


##ans4##


##ans5##


##correct##
2

##multipleSelection##
FALSE

##explanation##
Refer to the JDK documentation.


##------------------------------question------------------------------##
What is the output of the following piece of code:

1. int x = 6;
2. double d = 7.7;
3.
4. System.out.println((x > d) ? 99.9 : 9);


##ans1##
9

##ans2##
9.0

##ans3##
99.9

##ans4##
Nothing. An ArithmeticException is thrown at line 4.

##ans5##
6

##correct##
2

##multipleSelection##
FALSE

##explanation##
The statement at line 4 reads as:

if x > d, result is 99.9, else result is 9

I had much correspondance about this question, but I think the correct explanation must go to Dave. Thanks Dave - here is it (para-phrased)...

The reason 9 is promoted to a double is because 99.9 is a double and the operation must return (assign) the same type.  Trying different numbers gives (eg, 8 and 9):

                       int x = 6;
                       double d = 7.7;
                       System.out.println((x > d) ?  8 : 9);

This prints 9,  not 9.0

A further example to clarify...

                       int x = 6;
                       double d = 7.7;
                       System.out.println((x > d) ? true : 4);

This gives  "Error: Incompatible type for ?:. Can't convert boolean to int"

From the above, the conversion is performed because the result is a double, and NOT because "x" is promoted to a double for the comparison with "d".

Thanks again, Dave.


##------------------------------question------------------------------##
Which of the following applet tags are mandatory (select all the correct answers).

##ans1##
CODE

##ans2##
WIDTH

##ans3##
HEIGHT

##ans4##
PARAM

##ans5##
ARCHIVE

##correct##
1,2,3

##multipleSelection##
TRUE

##explanation##
Refer to the relevant JDK documentation.


##------------------------------question------------------------------##
What is printed out following the execution of the code below:

1. class Test{
2.      static String s;
3.      public static void main(String []args){
4.           int x = 4;
5.           if (x < 4)
6.                System.out.println("Val = " + x);
7.           else
8.                System.out.println(s);
9.          }
10.     }


##ans1##
Nothing. The code fails to compile because the String s isn't declared correctly.

##ans2##
The text "Val = null" is displayed.

##ans3##
The string "Val = " is displayed.

##ans4##
The text "null" is displayed.

##ans5##
A blank line of text is printed.

##correct##
4

##multipleSelection##
FALSE

##explanation##
The String s is initialised to null (all static and non-method variables are initialised to zero or equivalent), therefore, line 8 prints out "null".

As to why the string "null" is printed, rather than simply a blank line, refer to the JDK API for the System class.


##------------------------------question------------------------------##
True or False.

The StringBuffer class does not have a concat() method.

##ans1##
True

##ans2##
False

##ans3##


##ans4##


##ans5##


##correct##
1

##multipleSelection##
FALSE

##explanation##
Refer to the JDK API documentation.


##------------------------------question------------------------------##
What is displayed when the following piece of code is executed (assume the graphics context, g, is correctly set up):

g.drawString("abc", 10, 10);

##ans1##
The text "abc" with the lower left part of "a" located at x = 10, y = 10.

##ans2##
The text "abc" with the upper left part of "a" located at x = 10, y = 10.

##ans3##
Nothing. This is not a valid method.

##ans4##


##ans5##


##correct##
1

##multipleSelection##
FALSE

##explanation##
The drawString() method puts the baseline of the first character at the co-ordinated x = 10, y = 10 (refer to the JDK API documentation).


##------------------------------question------------------------------##
True or False.

Anonymous classes cannot have constructors.

##ans1##
True

##ans2##
False

##ans3##


##ans4##


##ans5##


##correct##
1

##multipleSelection##
FALSE

##explanation##
Anonymous classes are classes which do not have names. Therefore, constructors are meaningless.


##------------------------------question------------------------------##
To reference a JAR from a web page, which of the following keywords are used:

##ans1##
jar

##ans2##
class

##ans3##
zip

##ans4##
archive

##ans5##
package

##correct##
4

##multipleSelection##
FALSE

##explanation##
Refer to the standard JDK documentation.

I've had some comments about this question, regarding the validation of including it in Jxam (apparently, some people were unclear if JARs were on the exam syllabus).

I've left it in because in the Roberts/Hellor book, there is mention of Jars and applets on page 365 (of the 1.1 book).



##------------------------------question------------------------------##
Analyse the following 2 classes and select the correct statements.

class A{
     private int x = 0;
     static int y = 1;
     protected int q = 2;
}

class B extends A{
     void method(){
         System.out.println(x);
         System.out.println(y);
         System.out.println(q);
     }
}


##ans1##
The code fails to compile because the variable x is not available to class B.

##ans2##
The code compiles correctly, and the following is displayed:  0 1 2

##ans3##
The code fails to compile because you can't subclass a class with private variables.

##ans4##
Removing the line "System.out.println(x)" will allow the code to compile correctly.

##ans5##
The compiler will complain that the variable x in class B is undefined.

##correct##
1,4,5

##multipleSelection##
TRUE

##explanation##
Answer 1 is correct, because x is declared as private to class A and is therefore not available to any other class (even subclasses).

Answer 3 is totally meaningless, but sounds vaguely plausible!

Answer 4 is correct, because the only problem with the code is the attempt to reference the variable x.

Answer 5 is correct (see answer 4).


##------------------------------question------------------------------##
Which of the following interfaces can be used to manage a collection of elements, with no duplication.

##ans1##
List

##ans2##
Vector

##ans3##
Set

##ans4##


##ans5##


##correct##
3

##multipleSelection##
FALSE

##explanation##
Lists and Vectors allow duplication (so answers 1 and 2 are incorrect).


##------------------------------question------------------------------##
Which of the following statements are true regarding inner classes.

##ans1##
Variables defined inside inner classes cannot be static.

##ans2##
Variables defined inside inner classes cannot be static unless the inner class itself is static.

##ans3##
Non-static inner classes (which are not defined in a method) have access to all class and instance variables, regardless of the access qualifier of those variables.

##ans4##
An inner class can actually be a subclass of the outer class

##ans5##
Inner classes can be declared as private. Top level, outer classes cannot.

##correct##
2,3,4,5

##multipleSelection##
TRUE

##explanation##
Answer 1 is incorrect, since static variables can exist inside an inner class if the inner class is also static (see answer 2).


##------------------------------question------------------------------##
Which of the following are valid ways to define an abstract method.

##ans1##
abstract void Test();

##ans2##
abstract void Test() {}

##ans3##
static abstract void Test();

##ans4##
final abstract void Test();

##ans5##
Methods cannot be defined as abstract, only variables can be abstract.

##correct##
1

##multipleSelection##
TRUE

##explanation##
Answer 2 is incorrect, since the {} defines an empty method (but still a method) so it is not abstract.

Abstract methods cannot be static, so answer 3 is incorrect.

Abstract and final are almost opposite in meaning and therefore do not form a valid keyword combination (final means a method can't be overridden, abstract means a method must be overridden), so answer 4 is incorrect.


##------------------------------question------------------------------##
Consider the following:

class A extends Integer{
     int x = 0;
}

Select all valid statements.

##ans1##
The code will compile correctly.

##ans2##
The code will not compile because Integer is final and cannot be subclassed.

##ans3##
The code will not compile because class A has no methods or constructor.

##ans4##
The code will compile correctly, but will throw an ArithmeticException at runtime.

##ans5##


##correct##
2

##multipleSelection##
TRUE

##explanation##
Answer 3 is incorrect. A class does not need a method or constructors to exist.

Answer 4 is a meaningless comment.


##------------------------------question------------------------------##
Consider the following and select the correct statement(s):

interface A{
     int x = 0;
     A(){
         x= 5;
     }
     A(int s){
         x = s;
     }
}

##ans1##
This is a valid piece of code and it compiles correctly.

##ans2##
The default constructor is not required since the compiler will create one for you.

##ans3##
The code fails to compile because interfaces cannot have more than 1 constructor.

##ans4##
The code fails to compile because an interface cannot have any constructors.

##ans5##
The code fails to compile, because a class must have more than 1 character in it's name.

##correct##
4

##multipleSelection##
TRUE

##explanation##
Interfaces cannot have constructors, only classes have constructors.

Answer 2 is a valid statement when dealing with classes, but not in the context of the code in the question.

Answer 5 is totally untrue, but potentially confusing :-)



##------------------------------question------------------------------##
True or False.

A try block always needs a catch or a finally block (either or both, but not none).

##ans1##
True

##ans2##
False

##ans3##


##ans4##


##ans5##


##correct##
1

##multipleSelection##
FALSE

##explanation##
Refer to the Java Language Specification.


##------------------------------question------------------------------##
Which of the following are valid ways to declare the main() method which is used to start a Java program.

##ans1##
public static void main(String [] args)

##ans2##
static public void main(String [] args)

##ans3##
public void main(String args [])

##ans4##
public static void main(String args[])

##ans5##
public static void main(String args)

##correct##
1,2,4

##multipleSelection##
TRUE

##explanation##
Answer 3 is actually a correct method definition and will not cause a compile error. However, because it is not declared as static, it will not be visible to the JVM as a means of launching a program. In effect, the compiler sees this as simply another method.

Answer 5 is incorrect, because the signature of main() must take an array of strings as the parameter list.

Answers 1,2 and 4 are all valid. The order of the keywords "static" and "public" is not relevant. Also, the way the args[] is defined is valid in all cases.


##------------------------------question------------------------------##
Consider the following piece of code:

boolean b = true;
System.out.println(b);

What is displayed when this code is executed?

##ans1##
The text "true" is displayed.

##ans2##
The text "1" is displayed

##ans3##
The code fails to compile, because conversion string conversion in ths System.out.println() method only applies to integers.

##ans4##
The code compiles but nothing is displayed upon execution.

##ans5##
The code fails to compile. However, changing the first line to "boolean b = TRUE;" will correctly declare a boolean, and the code will compile and display "1".

##correct##
1

##multipleSelection##
FALSE

##explanation##
Answer 2 is incorrect, because booleans can't have integer values (in this case, "1").

Answer 3 is an meaningless statement.

Answer 5 is incorrect, attempting to confuse C/C++ and Java ways of defining true and false boolean values.


##------------------------------question------------------------------##
Which of the following pieces of code compiles without any errors?

##ans1##
StringBuffer sb1 = "abcd";

##ans2##
Boolean b = new Boolean("abcd");

##ans3##
byte b = 255;

##ans4##
int x = 0x1234;

##ans5##
float fl = 1.2;

##correct##
2,4

##multipleSelection##
TRUE

##explanation##
Answer 1 is not a valid way to construct a StringBuffer.

Answer 2 is a valid construction of a Boolean (any string other than "true" or "false" will result in a Boolean with a value of false - see the JDK API documentation).

Answer 3 will fail to compile because the valid range for a byte is -128 to +127 (ie, 8 bits, signed).

Answer 5 fails to compile because the compiler interprets 1.2 as a double being assigned to a float (down-casting). You either need an explicit cast (as in "(float)1.2") or "1.2f", to indicate a float.


##------------------------------question------------------------------##
Which of the following are valid statements regarding the following piece of code?

1. String s1 = "abcd";
2. StringBuffer sb1 = new StringBuffer("abcd");
3. int val = 6;
4. System.out.println(s1 + val);
5. System.out.println(sb1 + val);


##ans1##
The text "abcd6" is displayed followed by "abcd6".

##ans2##
The code fails to compile because String conversion does not apply to StringBuffer.

##ans3##
The code compiles but upon execution, throws a NullPointerException at line 5.

##ans4##
The code fails to compile at line 2, because this is not a valid way to create a StringBuffer.

##ans5##
The code fails to compile at line 1,  because this is not a valid way to create a String.

##correct##
2

##multipleSelection##
TRUE

##explanation##
Answers 4 and 5 sound plausible, but are incorrect.


##------------------------------question------------------------------##
True or False.

Abstract methods can be declared as static.

##ans1##
True

##ans2##
False

##ans3##


##ans4##


##ans5##


##correct##
2

##multipleSelection##
FALSE

##explanation##
Abstract methods cannot be declared as static, final or private. Refer to the Java Language Specification (section 8.4.3.1, version 1.0).


##------------------------------question------------------------------##
FlowLayout is the default layout manager for which of the following containers:

##ans1##
Panel

##ans2##
Applet

##ans3##
Frame

##ans4##
Window

##ans5##
Dialog

##correct##
1,2

##multipleSelection##
TRUE

##explanation##
Frame, Window and Dialog all use BorderLayout as their default layout managers.


##------------------------------question------------------------------##
In which class are the following methods defined:
        - wait()
        - notify()
        - notifyAll()

##ans1##
Thread

##ans2##
Runnable

##ans3##
Object

##ans4##
Event

##ans5##
Synchronize

##correct##
3

##multipleSelection##
FALSE

##explanation##
Runnable is an interface, not a class. Synchronize is not a class, it's a keyword.


##------------------------------question------------------------------##
Which one of the following creates an instance of Vector with an initial capacity of 10, and an  incremental capacity of 5.

##ans1##
new Vector(10, 5);

##ans2##
new Vector(5,10);

##ans3##
None. There is no constructor of Vector which provides this feature.

##ans4##
Vector is declared as final, and it is therefore not possible to instantiate it.

##ans5##


##correct##
1

##multipleSelection##
FALSE

##explanation##
Refer to the standard JDK API documentation.


##------------------------------question------------------------------##
True of False.

CheckboxGroup is a subclass of Component.

##ans1##
True

##ans2##
False

##ans3##


##ans4##


##ans5##


##correct##
2

##multipleSelection##
FALSE

##explanation##
CheckboxGroup is a direct subclass of Object (refer to the JDK API documentation).


##------------------------------question------------------------------##
Which statements(s) below are true about the following piece of code.

class Test implements Runnable{
      public static void main(String [] args){
           Thread t = new Thread(new Test());
           t.start();
           }
      public void run(int limit){
           for (int x = 0; x < limit; x++)
                System.out.println(x);
           }
      }

##ans1##
All the numbers up to (but not including) "limit" are printed out.

##ans2##
Nothing is displayed. There is no explicit call to the run() method.

##ans3##
The code fails to compile because the Runnable interface is not implemented correctly.

##ans4##
The code can be made to compile by declaring the class Test to be abstract.

##ans5##
The code can be made to compile by removing the words "implements Runnable".

##correct##
3

##multipleSelection##
TRUE

##explanation##
The signature for the run() method is "public void run()". As presented in the question, the compiler interprets the run(int) method as simply another method in the class, and assumes that the Runnable interface is not correctly implemented.


##------------------------------question------------------------------##
Consider the following code and select the statement(s) which are true:

1. class Test extends Frame{
2.
3.     public static void main(String [] args){
4.          Test t = new Test();
5.          }
6.
7.     Test(){
8.          Button b = new Button("Hello");
9.          add(b, BorderLayout.SOUTH);
10.         }
11.
12.    }


##ans1##
The code compiles. When executed, nothing is displayed.

##ans2##
The code compiles. When executed, a button with the text "Hello" is located at the bottom on the screen. The button is as tall as the text, but is the width of the frame.

##ans3##
Adding in the following two lines between lines 9 and 10 will display a frame with a button at the bottom of the frame:  setSize(100, 100); setVisible(true);

##ans4##
The code fails to compile, because a layout manager was not specified.

##ans5##
The code fails to compile because you cannot subclass the Frame class.

##correct##
1,3

##multipleSelection##
TRUE

##explanation##
1 is correct, because the code is fine, and the default size for a Frame is no size (ie, not visible).

3 is correct, because when these lines are added, the frame becomes visible and is of size 100,100.


##------------------------------question------------------------------##
Before which of the following can the keyword "synchronized" be placed, without causing a compile error.

##ans1##
class methods

##ans2##
instance methods

##ans3##
any block of code within a method

##ans4##
variables

##ans5##
a class

##correct##
1,2,3

##multipleSelection##
TRUE

##explanation##
Variables and classes cannot be synchronized.  Any code block within a method can be synchronized by enclosing it in {}.


##------------------------------question------------------------------##
Consider the following class definitions:

class Base{}

class Subclass1 extends Base{}

class Subclass2 extends Base();


Now consider the following declarations:

Base b = new Base();
Subclass1 s1 = new Subclass1();
Subclass2 s2 = new Subclass2();


Now, consider the following assignment:

s1 = (Subclass1)s2;

Which of the following statements are correct regarding this assignment (select one).

##ans1##
The assignment is legal and compiles without an error. No exception is thrown at runtime.

##ans2##
The code fails to compile. The compiler complains that the assignment "s1 = (Subclass1)s2" is illegal.

##ans3##
The code compiles but ClassCastException is thrown at runtime.

##ans4##
The code fails to compile. You cannot subclass a parent class more than once.

##ans5##


##correct##
2

##multipleSelection##
FALSE

##explanation##
In all cases, two sibling classes derived from a common base class, cannot be equated or assigned.


##------------------------------question------------------------------##
Select all the valid ways of initialising an array.

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

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

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

##ans4##
int []x = {0,0,0};

##ans5##
char c[] = {'a', 'b'};

##correct##
1,2,4,5

##multipleSelection##
TRUE

##explanation##
Refer to the Java language Specification.


##------------------------------question------------------------------##
What is the valid declaration for the finalize() method.

##ans1##
protected void finalize() throws Throwable

##ans2##
final finalize()

##ans3##
public final finalize()

##ans4##
private boolean finalize()

##ans5##
private final void finalize() throws Exception

##correct##
1

##multipleSelection##
FALSE

##explanation##
Refer to the JDK API documentation.


##------------------------------question------------------------------##
What is the method used to retrieve a parameter passed into an applet using the PARAM tag.

##ans1##
getParam()

##ans2##
getParameter()

##ans3##
getVariable()

##ans4##
getVar()

##ans5##
There is no method available. You must use "String [] args" approach.

##correct##
2

##multipleSelection##
FALSE

##explanation##
Refer to the relevant Java documentation.


##------------------------------question------------------------------##
You have a button, which is in a panel. The panel is inside a frame.  You assign the Frame a 24-point font and a background colour of yellow. You set the panel to have a background colour of red.  Which of the following statements are true (select all valid statements).

##ans1##
The font size of the button is 24-point.

##ans2##
The background colour of the button is the same as that of the frame.

##ans3##
The panel has a font size of 8-point.

##ans4##
The button inherits the font from the panel.

##ans5##
This is not a valid configuration. It is not valid to place a panel into a frame.

##correct##
1,4

##multipleSelection##
TRUE

##explanation##
In all cases, unless a component's attributes (font, font size, foreground colour, background colour, etc) are explicitly defined, they are inherited from the surrounding container.


##------------------------------question------------------------------##
Consider the following piece of code and select the correct statement(s):

public class Test{
     final int x = 0;
     Test(){
         x = 1;
     }
     final int aMethod(){
         return x;
     }
}


##ans1##
The code fails to compile. The compiler complains because there is a final method ("aMethod") in a non-final class.

##ans2##
The code compiles correctly. On execution, an exception is thrown when the Test() constructor is executed.

##ans3##
The code fails to compile because an attempt is made to alter the value of a final variable.

##ans4##
Removing the "final" keyword from the line "final int x = 0" will allow the code to compile correctly.

##ans5##
The code fails to compile because only methods can be declared as final (and therefore "final int x = 0" is not valid).


##correct##
3,4

##multipleSelection##
TRUE

##explanation##
Answer 1 is incorrect, because a class can have final methods and variables without the need for the entire class to be final.

Answer 2 is incorrect, because the type of error in this code is detectable at compile time.

Answer 5 is invalid. Final can be applied to primitives.

##------------------------------question------------------------------##
What is displayed when the following code fragment is compiled and executed (assume that the enveloping class and method is correctly declared and defined):

StringBuffer sb1 = new StringBuffer("abcd");
StringBuffer sb2 = new StringBuffer("abcd");
String s1 = new String("abcd");
String s2 = "abcd";
System.out.println(s1==s2);
System.out.println(s1=s2);
System.out.println(sb1==sb2);
System.out.println(s1.equals(sb1));
System.out.println(sb1.equals(sb2));


##ans1##
The code fails to compile, complaining that the line

System.out.println(s1=s2);

is illegal.

##ans2##
The code fails to compile because the equals() method is not defined for the StringBuffer class.

##ans3##
false
true
true
false
false

##ans4##
false
abcd
false
false
false

##ans5##
false
true
false
false
false

##correct##
4

##multipleSelection##
FALSE

##explanation##
Answer 1 is incorrect, because this is a valid operation. The object references are assigned as specified (s1 = s2), which results in "abcd" as the output.

Answer 2 is incorrect, because the equals() method is available to all classes, since it is defined in the Object class.

One point to note, is that StringBuffer does not override the equals() method, and so will always only compare the object references. String, however, overrides it, and will do a "deep" compare (ie, will compare the objects themselves).

Keeping this in mind, the following points are worth noting:

s1==s2 -> false (s1 and s2 are different objects)

s1=s2 -> "abcd" (see above comment)

sb1==sb2 -> shallow compare, different object references

s1.equals(sb1) -> deep compare, different objects

sb1.equals(sb2) -> not overridden, shallow compare, different objects


##------------------------------question------------------------------##
What is the default layout manager for applets and panels?

##ans1##
FlowLayout

##ans2##
BorderLayout

##ans3##
GridBagLayout

##ans4##
GridLayout

##ans5##
None of these

##correct##
1

##multipleSelection##
FALSE

##explanation##
Refer to the JDK API documentation.


##------------------------------question------------------------------##
Which of the following statements will compile without an error?

##ans1##
Boolean b = new Boolean("abcd");

##ans2##
float f = 123;

##ans3##
byte b = 127;

##ans4##
int x = (int)(1.23);

##ans5##
short s = 128;

##correct##
1,2,3,4,5

##multipleSelection##
TRUE

##explanation##
Answer 1 is correct. Any string other than "true" or "false" will create a Boolean object with a false state (see the JDK API documentation).

Answer 2 is correct, because 123 is an integer which gets automatically promoted by the compiler to a float.

Answer 3 is correct, because the valid range for a byte type is -128 to +127.

Answer 4 is correct because the cast will convert 1.23 to 1, which is a valid integer (omitting the explicit cast will cause a compile error).

Answer 5 is correct, because the short type is a 16-bit value, with a range -32768 to +32767.


##------------------------------question------------------------------##
True or False.

Menus can be added to containers.

##ans1##
True

##ans2##
False

##ans3##


##ans4##


##ans5##


##correct##
2

##multipleSelection##
FALSE

##explanation##
Menus can only be added to menu-bars.


##------------------------------question------------------------------##
Which of the following statements are true regarding the graphical methods paint(), repaint() and update().

##ans1##
paint() schedules a call to repaint().

##ans2##
repaint() schedules a call to update().

##ans3##
update() calls paint().

##ans4##
update() schedules a call to repaint().

##ans5##
repaint() calls paint() directly.

##correct##
2,3

##multipleSelection##
TRUE

##explanation##
The sequence of operation of these three methods is:

-repaint() schedules update()
-update() calls paint()


##------------------------------question------------------------------##
To which of the following can a menubar component be added?

##ans1##
Applet

##ans2##
Panel

##ans3##
Frame

##ans4##
Canvas

##ans5##


##correct##
3

##multipleSelection##
FALSE

##explanation##
Menubars can only be added to Frames.

This question (and another like it elsewhere in Jxam) have been the source of much correspondence between myself and numerous people who have used Jxam :-)

I came across the statement during my study for the exam, and simply noted it as fact. I never really looked too deeply into it.

For further reference, this question is taken indirectly from the book "Java 1.1 Certification Study Guide" by Simon Roberts and Philip Heller (ISBN 0-7821-2069-5), chapter 9, Section "The Menu Components", page 267.

By the way, I would highly recommend this book as a study guide!


##------------------------------question------------------------------##
With regard to HTML tags, which of the following statements are correct?

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

##ans2##
CODE and CODEBASE are case insensitive, and the .class extension is optional.

##ans3##
The PARAM tag is case insensitive.

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

##ans5##
The CODE tag is the only mandatory tag.

##correct##
1,3,4

##multipleSelection##
TRUE

##explanation##
Refer to the relevant java documentation.


##------------------------------question------------------------------##
Consider the following piece of code and select the correct statements.

1. Object o = new String("abcd");
2. String s = o;
3. System.out.println(s);
4. System.out.println(o);


##ans1##
The following is displayed:
abcd
abcd

##ans2##
The code fails to compile at line 1.

##ans3##
The code fails to compile at line 2

##ans4##
The code fails to compile at line 4.

##ans5##
The code can be made to compile by changing line 1 to the following:
String o = new String("abcd");

##correct##
3

##multipleSelection##
TRUE

##explanation##
The code fails to compile because line 2 is attempting to assign objects "down" the hierarchy.


##------------------------------question------------------------------##
Which of the following are legal methods for the String class?

##ans1##
length()

##ans2##
toUpper()

##ans3##
toUpperCase()

##ans4##
toString()

##ans5##
equals()

##correct##
1,3,4,5

##multipleSelection##
TRUE

##explanation##
Refer to the standard JDK API documentation.


##------------------------------question------------------------------##
What is the output from the following piece of code:

loop1:
     for(int i = 0; i < 3; i++){
         loop2:
             for(int j = 0; j < 3; j++){
                 if (i == j){
                     break loop2;
                 }
                 System.out.print("i = " + i + " j = " + j + " ");
             }
     }


##ans1##
i = 1
j = 0

##ans2##
i = 1
j = 0
i = 2
j = 1

##ans3##
i = 0
j = 1
i = 0
j = 2
i = 1
j = 0
i = 2
j = 0
i = 2
j = 1

##ans4##
i = 1
j = 0
i = 2
j = 0
i = 2
j = 1

##ans5##
i = 1 j = 0 i = 2 j = 0 i = 2 j = 1

##correct##
5

##multipleSelection##
FALSE

##explanation##
The statement "break loop2;" causes the currently executing loop to terminate and control is passed back to the outer loop.

Note that Answer 5 is correct, because the code for printing is:

        System.out.print()

rather than the more usual

        System.out.println()

In the real exam, be careful of this type of question, where the focus is on one area (the "break" statement in this case), but you incorrectly make assumptions about the remainder of the code segment!


##------------------------------question------------------------------##
What is displayed when the following piece of code is executed:

loop1:
     for(int i = 0; i < 3; i++){
         loop2:
             for(int j = 0; j < 3; j++){
                 if (i == j){
                     continue loop2;
                 }
                 System.out.println("i = " + i + " j = " + j);
            }
     }

##ans1##
i = 0
j = 0
i = 0
j = 1
i = 1
j = 0
i = 1
j = 1
i = 2
j = 0
i = 2
j = 1

##ans2##
i = 0
j = 0
i = 1
j = 1
i = 2
j = 2

##ans3##
i = 0
j = 1
i = 0
j = 2
i = 1
j = 0
i = 1
j = 2
i = 2
j = 0
i = 2
j = 1

##ans4##
None of the above.

##ans5##


##correct##
3

##multipleSelection##
FALSE

##explanation##
The statement "continue loop2;" causes the current iteration to terminate, but the loop continues with the next iteration.


##------------------------------question------------------------------##
Consider the following piece of code, and select the correct statement(s):

long val = 2;
...
...

Switch (val){
     default:
         System.out.println("Default");
         break;
     case 1:
         System.out.println("1");
         break;     
     case 2:
         System.out.println("2");
     case 3:
         System.out.println("3");
         break;
}

##ans1##
The following is displayed:
2
3

##ans2##
The following is displayed:  
2

##ans3##
The code fails to compile because the default case must be the last case in the switch statement.

##ans4##
The code fails to compile because the argument for a switch statement cannot be a variable of type long.

##ans5##
The following is displayed:  
2 
3 
Default

##correct##
4

##multipleSelection##
TRUE

##explanation##
Answer 3 is incorrect, because the default case can reside anywhere in the switch statement.

Asnwer 4 is correct because the valid parameter types for a switch statement are:
-byte
-char
-short
-int


##------------------------------question------------------------------##
Consider the following piece of code, and select the correct statement(s):

public class Test extends Applet{
     public void init(){
         setLayout(new GridLayout(1,2));
         add(new Button("#1"));
         add(new Button("#2"));
         add(new Button("#3"));
         add(new Button("#4"));
     }
}


##ans1##
The Gridlayout is created with 1 row and 2 columns.

##ans2##
Adding the button with the label "#3" will cause an exception to be thrown.

##ans3##
Adding the button with label "#3" will cause it to overwrite the button with label "#2".

##ans4##
The layout automatically extends to accommodate the additional buttons.

##ans5##
Only 2 buttons are displayed, one with label "#1" and one with label "#4".

##correct##
1,4

##multipleSelection##
TRUE

##explanation##
The GridLayout increases in size when new components are added.


##------------------------------question------------------------------##
Which of the following layout managers will retain the preferred width and height of the contained components.

##ans1##
GridLayout

##ans2##
GridBagLayout

##ans3##
BoxLayout

##ans4##
FlowLayout

##ans5##
BorderLayout

##correct##
4

##multipleSelection##
FALSE

##explanation##
Refer to the JDK API documentation for further information on each of the Layout Managers.


##------------------------------question------------------------------##
You construct a List by calling List(5, false).

Which statements below are correct (assume the layout managers do not modify the List properties).

##ans1##
The list supports multiple selection.

##ans2##
The list has 5 visible items.

##ans3##
A vertical scroll bar will be added automatically if needed.

##ans4##
The code fails to compile. The given constructor is not a valid one.

##ans5##


##correct##
2,3

##multipleSelection##
TRUE

##explanation##
1 is incorrect, because the second parameter (false) indicates that multiple selection is not permitted.



##------------------------------question------------------------------##
Which of the following will definitely stop a thread from executing:

##ans1##
wait()

##ans2##
notify()

##ans3##
yield()

##ans4##
suspend()

##ans5##
sleep()

##correct##
1,3,4,5

##multipleSelection##
TRUE

##explanation##
Only notify() will not explicitly stop an executing thread from running. Yield() returns control back to the JVM, which may or may not stop the thread from running.


##------------------------------question------------------------------##
Which of the following is the correct method to call to change the layout manager for a container:

##ans1##
setLayoutManager()

##ans2##
setLayManager()

##ans3##
changeLayout()

##ans4##
You can't change the layout manager for a container.

##ans5##
setLayout()

##correct##
5

##multipleSelection##
FALSE

##explanation##
Refer to the JDK API documentation.


##------------------------------question------------------------------##
Which is the correct way to add a button, referenced by b, to a panel, referenced by p.

##ans1##
add(p, b);

##ans2##
p.add(b)

##ans3##
b.add(p)

##ans4##
Buttons can't be added to panels.

##ans5##
add(p)

##correct##
2

##multipleSelection##
FALSE

##explanation##
2 is the only correct answer. While the other answers appear valid, they are in fact incorrect.


##------------------------------question------------------------------##
What is displayed when the following code is compiled and executed:

long val = 2;
      switch(val){
           case 1:
               System.out.println("1");
           case 2:
               System.out.println("2");
           default:
               System.out.println("default");
           }

##ans1##
default

##ans2##
2
default

##ans3##
The code fails to compile because there are no break statements in the case clauses.

##ans4##
The code fails to compile because a long data type is not a valid parameter for a switch statement.

##ans5##


##correct##
4

##multipleSelection##
FALSE

##explanation##
Only the following data types are valid arguments for a switch statement:
- byte
- short
- char
- int


##------------------------------question------------------------------##
What is displayed following the execution of the code below:

1. class Test{
2.      String s;
3.      public static void main(String []args){
4.           int x = 4;
5.           if (x < 4)
6.                System.out.println("Val = " + x);
7.          else
8.                System.out.println(s);
9.          }
10.     }

##ans1##
Nothing. The code doesn't compile because the variable s wasn't initialised.

##ans2##
The string "null" is displayed.

##ans3##
The code runs, but a NullPointerException is thrown at line 8.

##ans4##
The code compiles. No exception is thrown but nothing is displayed.

##ans5##
Nothing. The code doesn't compile because you can't make a static reference to a non-static variable.

##correct##
5

##multipleSelection##
FALSE

##explanation##
You can't make a static reference (the method main() is static) to a non-static variable (the String variable s).

One way to rectify the problem is to declare String s as static, as in:

      static String s;


##------------------------------question------------------------------##
Consider the following piece of code:

class Test{
          int x;
          String s;
          float fl;
          boolean [] b = new boolean [5];

          public static void main(String []args){
               System.out.println(x);
               System.out.println(s);
               System.out.println(fl);
               System.out.println(b[2]);
               }
          }

What is displayed when this code is executed.


##ans1##
0 
null 
0.0 
false

##ans2##
0 
" " 
0.0 
true

##ans3##
Nothing. The code fails to compile because the boolean array is incorrectly declared.

##ans4##
Nothing.  The code will not compile because the static method cannot access the variables since there is no instance of the Test class.

##ans5##


##correct##
4

##multipleSelection##
FALSE

##explanation##
You can't make a static reference (the method main() is static) to a non-static variable (all the variables concerned are non-static). In order to access the instance variables (x, s, fl, b) an instance of the class Test must exist.


##------------------------------question------------------------------##
What is displayed when the following code is compiled and executed:

String s = "abcd";
String s1 = new String(s);
if (s == s1)
          System.out.println("the same");
if (s.equals(s1))
          System.out.println("equals");


##ans1##
the same
equals

##ans2##
equals

##ans3##
the same

##ans4##
The code compiles, but nothing is displayed upon execution.

##ans5##
The code fails to compile.

##correct##
2

##multipleSelection##
FALSE

##explanation##
The object references s and s1 are different (the standard optimisation applied to immutable strings does not apply when a String is created using "new").

Therefore, "the same" is not displayed.

However, the String class overrides the equals() method, and a "deep compare" is performed on the contents of the objects. Since the contents of the objects are the same, the "equals" string is displayed.


##------------------------------question------------------------------##
What is the legal range for the byte data type:

##ans1##
0 to +255

##ans2##
-127 to +128

##ans3##
-128 to +127

##ans4##
0 to 65535

##ans5##
-32767 to +32768

##correct##
3

##multipleSelection##
FALSE

##explanation##
the byte type is 8-bits, signed. Refer to the Java language Specification.


##------------------------------question------------------------------##
What is displayed when the following code is executed:

String s = "abcdef";
System.out.println(s.charAt(4));

##ans1##
d

##ans2##
e

##ans3##
Nothing. An ArrayIndexOutOfBoundsException is thrown

##ans4##
The code does not compile. charAt() is not a valid method of the String class.

##ans5##
The code does not compile because the string s is not created correctly.

##correct##
2

##multipleSelection##
FALSE

##explanation##
Indices are zero based, so the character at index 4 is "e".


##------------------------------question------------------------------##
True or False.

The String class does not have an append() method.

##ans1##
True

##ans2##
False

##ans3##


##ans4##


##ans5##


##correct##
1

##multipleSelection##
FALSE

##explanation##
Refer to the standard JDK API documentation.


##------------------------------question------------------------------##
Consider the following code segment.

double d = -11.1;
double d1 = method(d);
System.out.println(d1);

If the output of this code segment is -12.0, then what methods could be called in method() above.

##ans1##
floor()

##ans2##
ceil()

##ans3##
round()

##ans4##
abs()

##ans5##
min()

##correct##
1

##multipleSelection##
TRUE

##explanation##
2 is incorrect, because ceil(-11.1) results in the value -11.0
3 is incorrect because round(-11.1) produces -11.
4 is incorrect since abs(-11.1) produces 11.1
5 fails to compile.

I specifically remember this type of question coming up in the real exam, so knowing the difference between these methods is important :-) It is especially important to know which methods operate on floats, and which operate on integers.


##------------------------------question------------------------------##
What outputs are possible from invoking Math.random().

##ans1##
-0.12 
0.56E3

##ans2##
0.12 
1.1E1

##ans3##
-23.45 
0.0

##ans4##
0.356 
0.03

##ans5##
1.00 
0.99

##correct##
4

##multipleSelection##
TRUE

##explanation##
The output for the random() method is a random number greater than or equal to 0.0 and less than 1.0


##------------------------------question------------------------------##
In a thread, the wait() method must be called inside which of the following:

##ans1##
A while() loop

##ans2##
The run() method

##ans3##
synchronised code

##ans4##
The constructor

##ans5##
It doesn't matter where the wait() is called from.

##correct##
3

##multipleSelection##
TRUE

##explanation##
The wait() method can only be called inside synchronised code. Refer to the Java Language Specification.


##------------------------------question------------------------------##
Consider the following piece of code:

public class Test extends Applet{
          public void init(){
               setLayout(new BorderLayout());
               add("South", new Button("B1"));
               add("North", new Button("B2"));
               add("South", new Button("B3"));
               }
          }

Select the correct statements regarding the above.

##ans1##
There are 2 buttons displayed in the SOUTH section, B1 and B3.

##ans2##
Only B3 is displayed in the SOUTH section.

##ans3##
An exception is thrown when an attempt is made to add a second component to SOUTH.

##ans4##
Only B1 is displayed in the SOUTH section.

##ans5##
You can't add buttons to applets.

##correct##
2

##multipleSelection##
TRUE

##explanation##
You can only add a single component to the peripheral areas of the BorderLayour. However, adding more than 1 will simply result in the most recent being displayed.


##------------------------------question------------------------------##
What is displayed when the following piece of code is executed:

1. String val = null;
2. int x = Integer.parseInt(val);
3.
4. System.out.println(x);

##ans1##
0

##ans2##
null

##ans3##
A NumberFormatException is thrown at line 2.

##ans4##
Nothing is displayed

##ans5##


##correct##
3

##multipleSelection##
FALSE

##explanation##
An exception is thrown because the string is null, and can therefore not be parsed into an integer.


##------------------------------question------------------------------##
Consider the following piece of code:

class A{
     int x = 0;
     A(int w){
         x = w;
     }
}

class B extends A{
     int x = 0;
     B(int w){
         x = w +1;
     }
}

##ans1##
The code compiles correctly.

##ans2##
The code fails to compile, because both class A and B do not have valid constructors.

##ans3##
The code fails to compile because there is no default no-args constructor for class A.

##ans4##


##ans5##


##correct##
3

##multipleSelection##
FALSE

##explanation##
There is no explicit call in class B to a constructor for class A. Therefore, the compile adds in a call to the default no-args constructor for A. 

However, this doesn't exist in class A, so the compile operation fails.  Note that the compiler only puts in a call to the no-args constructor if no other constructors are present.


##------------------------------question------------------------------##
Given an object myListener (whose class implements the ActionListener interface), which of the following are valid ways to enable myListener to receive all action events from component smallButton?

##ans1##
smallButton.add(myListener);

##ans2##
smallButton.addListener(myListener);

##ans3##
smallButton.addActionListener(myListener);

##ans4##
smallButton.addItem(myListener);

##ans5##


##correct##
3

##multipleSelection##
FALSE

##explanation##
Refer to the JDK API documentation.  The standard way to add a listener is to call addXXXListener, where XXX is the type of listener (Mouse, Action, etc).


##------------------------------question------------------------------##
Which of the following are valid listener interfaces?

##ans1##
ActionListener

##ans2##
MouseMotionListener

##ans3##
SystemEventListener

##ans4##
MouseClickListener

##ans5##
WindowListener

##correct##
1,2,5

##multipleSelection##
TRUE

##explanation##
3 and 4 are fictitious listeners.


##------------------------------question------------------------------##
Which of the following is the correct way to set the foreground colour of a component, c.

##ans1##
c.setForeground("red");

##ans2##
c.setColor("Color.red");

##ans3##
c.Foreground(Color.red);

##ans4##
c.setForegroundColor("red");

##ans5##
c.setForeground(Color.red);

##correct##
5

##multipleSelection##
FALSE

##explanation##
Refer to the JDK API documentation for the setForeground() method.


##------------------------------question------------------------------##
Which of the following are valid ways to create a Map collection.

##ans1##
Map m = new Map();

##ans2##
Map m = new Map(init capacity, increment capacity);

##ans3##
Map m = new Map(new Collection());

##ans4##
Map is an interface, and cannot be instantiated.

##ans5##


##correct##
4

##multipleSelection##
TRUE

##explanation##
Refer to the JDK API documentation.


##------------------------------question------------------------------##
Select all the correct statements relating to the following piece of code?

1.  public class A{
2.      abstract int method();
3.      void anotherMethod(){
4.       }
5.
6.  class B extends A{
7.      int method(){
8.          return 2;
9.      }
10. }


##ans1##
Changing "extends" to "implements" on line 6 will allow the code to compile correctly.

##ans2##
The method() in class A cannot be abstract without the entire class being declared as abstract.

##ans3##
Declaring class A to be abstract will allow the code to compile correctly.

##ans4##
The class A must have an explicit default constructor in order for it to be subclassed correctly.

##ans5##
The code fails to compile, because class B does not implement anotherMethod().

##correct##
2,3

##multipleSelection##
TRUE

##explanation##
There are several things wrong with this code:

- class A must be declared as abstract, since method() is declared as abstract
- if class A is declared as abstract, then the code will compile correctly
- if the abstract declaration of method() in class A is removed, the code will compile (however, the entire line 2 must now read as follows:
    int method(){}
In other words, a method body must now be supplied since the method is no longer abstract.


##------------------------------question------------------------------##
Which layout manager do the comments below refer to:

All components contained in it have the same width and height. Contained components are stretched to have the same dimensions.

##ans1##
FlowLayout

##ans2##
BorderLayout

##ans3##
GridLayout

##ans4##


##ans5##


##correct##
3

##multipleSelection##
FALSE

##explanation##
FlowLayout retains the preferred sizes of components.

Borderlayout will retain the preferred height of components located in NORTH or SOUTH, and the preferred width for components located in EAST or WEST.


##------------------------------question------------------------------##
Select all valid statements.

##ans1##
When typing text into a TextField, scroll bars will automatically appear when the TextField becomes full.

##ans2##
When typing text into a TextArea, scroll bars will appear when text is typed past the boundaries of the TextArea.

##ans3##
TextFields can have more than 1 row of text.

##ans4##
The class TextArea is a super class of the class TextField.

##ans5##
Both TextAreas and TextFields have a method called setEditable(), which can enable or disable editingof the component.

##correct##
2,5

##multipleSelection##
TRUE

##explanation##
TextFields are 1 row of text high, and 'n' character columns.

Scroll bars do not appear on TextFields.

TextFields and TextAreas are derived from java.awt.TextComponent.


##------------------------------question------------------------------##
In which class is the paint() method defined?

##ans1##
Object

##ans2##
Applet

##ans3##
Component

##ans4##
Thread

##ans5##
Graphics

##correct##
3

##multipleSelection##
FALSE

##explanation##
Refer to the standard JDK API documentation.


##------------------------------question------------------------------##
Select the statement which most closely describes the user interface of an applet with the following init() method:

public void init(){
     Panel p = new Panel();
     p.setLayout(new BorderLayout());
     p.add(new Button("Hello"), BorderLayout.EAST);
     p.add(new Button("Bye"), BorderLayout.WEST);
     add(p);
}


##ans1##
Two buttons are displayed. A button with "Hello" is on the right side of the applet. The button with "Bye" is on the left side of the applet. Both buttons extend from the centre of the applet to each side, and are the height of the applet.

##ans2##
Two buttons are displayed. A button with "Hello" is on the right side of the applet. The button with "Bye" is on the left side of the applet. Both buttons are only as wide as the text on the button, but are the height of the applet.

##ans3##
Two buttons are displayed. A button with "Hello" is on the right side of the applet. The button with "Bye" is on the left side of the applet. Both buttons are only large enough to support the associated button texts.

##ans4##


##ans5##


##correct##
2

##multipleSelection##
FALSE

##explanation##
BorderLayout preserves the width of components located in the EAST or WEST regions. However they are stretched to the height of the applet.


##------------------------------question------------------------------##
What is wrong about the following piece of code (assume the relevant import statements are present):

public class Test{
     public static void main (String [] args) throws IOException{
         if (args[0] == "hello")
             throw new IOException();
     }
}


##ans1##
Nothing. The code compiles correctly.

##ans2##
The code fails to compile. You can't throw an exception from the main() method.

##ans3##
The code fails to compile. IOException is a system exception, and cannot be thrown by application code.

##ans4##


##ans5##


##correct##
1

##multipleSelection##
FALSE

##explanation##
You can throw an exception from the main() method, so answer 2 is incorrect.  There are no such things as "system exceptions" so answer 3 is incorrect.




##------------------------------question------------------------------##
Consider the following piece of code, and select the statements which are true.

class Test{
     int x = 5;
     static String s = "abcd";

     public static void method(){
         System.out.println(s + x);
     }
}


##ans1##
The code compiles and displays "abcd5".

##ans2##
The code compiles, but throws an exception at runtime, because variable x isn't declared as static.

##ans3##
The code fails to compile because you can't make a static reference to a non-static variable.

##ans4##
The code will compile and display "abcd5" if x is declared to be static.

##ans5##
The code will compile by removing the static keyword from the declaration of method().

##correct##
3,4,5

##multipleSelection##
TRUE

##explanation##
The whole problem with this piece of code revolves around accessing static and non-static data and methods.

Since no Test object is created in method(), x does not exist and cannot be accessed by method(). Declaring x as static avoids this problem.

Similarly, removing the static declaration from method() means that x is visible.



##------------------------------question------------------------------##
Select all legal code fragments from the following.

##ans1##
char c = "c";

##ans2##
Boolean b = new Boolean("qwerty");

##ans3##
String s = "null";

##ans4##
int q;
for (int p = 0, q = 0; p < 5; p++){
     System.out.println("Val = " + p + q);
}

##ans5##
int x = 3; 
Float f = new Float(x);

##correct##
2,3,5

##multipleSelection##
TRUE

##explanation##
Answer 1 is incorrect because "c" is a string declaration (where as 'c' is a character declaration).

Answer 2 is correct, because any string other than true or false, will result in a Boolean with a state of false (refer to the API documentation for the Boolean class).

Answer 3 is valid.

Answer 4 is incorrect, because you can't mix the initialisation of a local loop variable declaration in the 'for' loop, with the initialisation of another loop variable.

Answer 5 is valid because the variable x will be promoted to a float when used as a parameter.


##------------------------------question------------------------------##
Which of the following are legal ways to construct a RandomAccessFile?

##ans1##
RandomAccessFile("file", "r");

##ans2##
RandomAccessFile("r", "file");

##ans3##
RandomAccessFile('r', "file");

##ans4##
RandomAccessFile("file", 'r');

##ans5##
RandomAccessFile("file", "rw");

##correct##
1,5

##multipleSelection##
TRUE

##explanation##
Refer to the JDK API documentation.


##------------------------------question------------------------------##
Consider the following code and select the most appropriate statements.

1. class Test{
2.    public int doubleValue(int a)
3.         System.out.println(a);
4.         return (int)(a * 2);
5     }
6.
7     public float doubleValue(int a){
8.         System.out.println(a);
9.         return (float)(a * 2);
10.   }
11.}

##ans1##
The code compiles since the two doubleVal() methods  have different return types.

##ans2##
The code doesn't compile because the compiler sees two methods with the same signature.

##ans3##
The code can be made to compile by redefining the parameter for doubleVal() on line 7 to be "float a" instead of "int a".

##ans4##
The code can be made to compile by replacing the  public declaration on line 7 with private.

##ans5##


##correct##
2,3

##multipleSelection##
TRUE

##explanation##
Return types are not sufficient to distinguish between methods.

Also, changing the scope of doubleValue() does not avoid this requirement.


##------------------------------question------------------------------##
What is displayed when the following piece of code is executed:

class Test extends Thread{
     public void run(){
         System.out.println("1");
         yield();
         System.out.println(2");
         suspend();
         System.out.println("3");
         resume();
         System.out.println("4");
     }

     public static void main(String []args){
         Test t = new Test();
         t.start();
     }
}


##ans1##
1 
2 
3 
4

##ans2##
1 
2 
3

##ans3##
1 
2

##ans4##
Nothing. This is not a valid way to create and start a thread.

##ans5##
1

##correct##
3

##multipleSelection##
FALSE

##explanation##
The code will run, but the thread suspends itself after displaying "2".

Threads cannot un-suspend themselves (since they ARE suspended and therefore not running!).


##------------------------------question------------------------------##
Consider the following piece of code, and select the most appropriate statements.

TextField t = new TextField("Hello", 20);

##ans1##
The user will be able to edit the string.

##ans2##
The field will be 20 characters wide.

##ans3##
The text field will be same size on all platforms, since Java is platform independent.

##ans4##
When entering text into the field, only 20 characters can be entered.

##ans5##
If the font is changed, the size of the textfield will adjust to allow 20 characters to be displayed.

##correct##
1,2

##multipleSelection##
TRUE

##explanation##
By default, text fields are editable, and the 20 refers to the number of displayed characters.

Answer 3 is incorrect, because the dimensions of displayable characters vary from platform to platform.

Answer 4 is incorrect, because when entering text, the text field scrolls when additional characters are entered. The size is simply the number of displayed characters.

Answer 5 is incorrect, because text fields do not adjust their size according to font changes.


##------------------------------question------------------------------##
True of False.

Using the instanceof operator on an interface will cause a runtime exception.

##ans1##
True

##ans2##
False

##ans3##


##ans4##


##ans5##


##correct##
2

##multipleSelection##
FALSE

##explanation##
The instanceof operator can be used as follows:

- on classes to see if a particular class is derived from another (ie, is an instance of)

- on interfaces to see if a class implements a particular interface


##------------------------------question------------------------------##
Which of the following are valid ways to create a Button component?

##ans1##
new Button();

##ans2##
new Button(30, 10);  where 30 is the width of the button and 10 is the height

##ans3##
new Button("hello");

##ans4##
new Button(new String("hello"));

##ans5##
new Button(String("hello"));

##correct##
1,3,4

##multipleSelection##
TRUE

##explanation##
Answer 5 is incorrect, because the "new" declaration is missing.  For all other answers, refer to the JDK API documentation.


##------------------------------question------------------------------##
Which of the following are NOT valid subclasses of AWTEvent?

##ans1##
MouseClickEvent

##ans2##
OutputEvent

##ans3##
MouseMotionEvent

##ans4##
KeyAdapter

##ans5##
WindowMinimizeEvent

##correct##
1,2,4,3,5

##multipleSelection##
TRUE

##explanation##
Answers 1, 2, 3 and 5 are fictitious events and do not exist within Java.

KeyAdapter exists, but directly extends Object.


##------------------------------question------------------------------##
Which of the following are valid methods for the Graphics class?

##ans1##
drawOval(int x, int y, int width, int height)

##ans2##
toString()

##ans3##
drawArc(int x, int y, int width, int height, int startAngle, int arcAngle)

##ans4##
drawPolygon(int[] xPoints, int[] yPoints, int nPoints)

##ans5##
fillArc(int x, int y, int width, int height, int startAngle, int arcAngle)

##correct##
1,2,3,4,5

##multipleSelection##
TRUE

##explanation##
Refer to the JDK API documentation.


##------------------------------question------------------------------##
Which of the following are valid methods in the Math class?

##ans1##
arcTan(double a)

##ans2##
atan(double a)

##ans3##
sqrt(double a)

##ans4##
min(int a, int b)

##ans5##
cosine(double a)

##correct##
2,3,4

##multipleSelection##
TRUE

##explanation##
Refer to the JDK API documentation.


##------------------------------question------------------------------##
Which of the following will display the string "ica", given:      

     String s = "Metallica";

##ans1##
System.out.println(s.subString(7));

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

##ans3##
System.out.println(s.subString(6, 8));

##ans4##
System.out.println(s.substring(7, 9));

##ans5##
None of these.

##correct##
2

##multipleSelection##
TRUE

##explanation##
The subString() method uses zero-based indices. Therefore Answers 1 and 4 are incorrect.

Also, a second parameter (as in subString(a, b)) 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".


##------------------------------question------------------------------##
Which of the following are defined in the Object class?

##ans1##
toString()

##ans2##
equals(Object o)

##ans3##
public static void main(String [] args)

##ans4##
System.out.println()

##ans5##
wait()

##correct##
1,2,5

##multipleSelection##
TRUE

##explanation##
Refer to the JDK API documentation.


##------------------------------question------------------------------##
Select all valid statements relating to the paint() method defined in the Component class.

##ans1##
It takes 4 int types as parameters, representing the x and y co-ordinates, width and height, respectively.

##ans2##
It is declared as final, and so it cannot be overridden.

##ans3##
It is declared as static.

##ans4##
It takes an instance of the Graphics component as a parameter.

##ans5##
When overriding the paint() method, you must handle the identification and repair of damaged components.

##correct##
4

##multipleSelection##
TRUE

##explanation##
Answer 1 is incorrect, because paint() takes an instance of the Graphics class.

Paint() is declared as public and void, so answers 2 and 3 are incorrect.

Answer 5 is incorrect. Repainting damaged areas is the responsibility of the graphics handlers, and the paint() method should simply repaint the entire component.


##------------------------------question------------------------------##
Which of the following keywords can be applied to a method?

##ans1##
transient

##ans2##
volatile

##ans3##
private

##ans4##
protected

##ans5##
final

##correct##
3,4,5

##multipleSelection##
TRUE

##explanation##
"transient" is a keyword used for preventing variables in an object from being serialised. It cannot be applied to methods.

"volatile" is used to inform the compiler that a variable may be changed asynchronously with regard to it's current definition (eg, from a native method outside the JVM). The compiler will not preform any optimisations on a variable declared as volatile.

Methods can be :

private (not accessable outside the containing class)
protected (only accessible to the containing class and subclasses)
final (not allowed to be overridden).


##------------------------------question------------------------------##
Which of the following keywords can be applied to a top-level class (i.e., not an inner class)?

##ans1##
private

##ans2##
protected

##ans3##
transient

##ans4##
public

##ans5##
final

##correct##
4,5

##multipleSelection##
TRUE

##explanation##
"transient" is a keyword used for preventing variables in an object from being serialised. It cannot be applied to classes.

Top-level classes cannot be declared as private.

"protected" only applies to variables and methods, and is used to restrict access to the containing class or subclasses.



##------------------------------question------------------------------##
Consider the following piece of code, and select all relevant statements:

1. class Test{
2.     public static void main(String [] args){
3.         aMethod();
4.     }
5.
6.     static void aMethod(){
7.         try{
8.             System.out.println("abcd");
9.             return;
10.        } finally {
11.            System.out.println("1234");
12.        }
13.    }
14.}


##ans1##
An exception is thrown at line 9.

##ans2##
The code fails to compile. The compiler complains about the return statement at line 9.

##ans3##
abcd
1234

##ans4##
abcd

##ans5##
The code fails to compile, complaining that a 'catch' clause is missing from line 10.

##correct##
3

##multipleSelection##
TRUE

##explanation##
Answers 1 and 2 are incorrect, since it is legal to issue a return inside a try block.

Answer 4 is incorrect, because the finally block is ALWAYS executed, regardless of the presence of the return statement at line 9.

Note, that the only time the finally block is not executed is if the program terminates. For example, calling System.exit(0) at line 9, will mean the finally block is not executed.

Answer 5 is incorrect, because a 'try' clause needs either a 'catch' or a 'finally', one or both, but not neither - it must have one of them


##------------------------------question------------------------------##
Which of the following statements are true.

##ans1##
The type "int" is a 32-bit signed integer value.

##ans2##
The type "short" is a 16-bit unsigned integer value.

##ans3##
The type "char" is a 16-bit Unicode character.

##ans4##
The type "float" is a 64-bit floating point value.

##ans5##
The type "long" is a 64-bit signed integer value.

##correct##
1,3,5

##multipleSelection##
TRUE

##explanation##
By default, all integer variable types are signed, so Answer 2 is incorrect.

The type float is 32-bits in size (double is 64-bits), so Answer 4 is incorrect.


##------------------------------question------------------------------##
Which layout manager is described by the following:

It aligns components vertically and horizontally, without requiring that the components be of the same size. It maintains a dynamic rectangular grid of cells, with each component occupying one or more cells, called its display area.

##ans1##
GridLayout

##ans2##
FlowLayout

##ans3##
GridBagLayout

##ans4##
BoxLayout

##ans5##


##correct##
3

##multipleSelection##
FALSE

##explanation##
Refer to the JDK API documentation.


##------------------------------question------------------------------##
True of False.

The StringBuffer class overrides the toString() method to return a String representation of the StringBuffer.

##ans1##
True

##ans2##
False

##ans3##


##ans4##


##ans5##


##correct##
1

##multipleSelection##
FALSE

##explanation##
Refer to the JDK API documentation.


##------------------------------question------------------------------##
Select the correct statements from the following:

##ans1##
The class RandomAccessFile is a subclass of the java.io.File class.

##ans2##
The class java.io.FileWriter contains methods to write primitives (eg, writeInt(), writeFloat(), etc)

##ans3##
The class java.io.BufferedOutputStream contains methods to write primitives (eg, writeInt(), writeFloat(), etc)

##ans4##
The File class defines a method which can be used to delete files and directories from a disk.

##ans5##
The RandomAccessFile class defines a method which can be used to delete files and directories from a disk.

##correct##
4

##multipleSelection##
TRUE

##explanation##
Refer to the JDK API documentation.


##------------------------------question------------------------------##
True or False.

A class can implement more than one interface, but can only inherit from a single parent class.

##ans1##
True

##ans2##
False

##ans3##


##ans4##


##ans5##


##correct##
1

##multipleSelection##
FALSE

##explanation##
A class can implement multiple interface as follows:

class A implements x, y{...}

where x and y are interfaces.

However, the Java language specification only allows a class to inherit from a single parent class. Accordingly, the following is invalid:

class A extends w, z {...}

where w and z are classes.

Combining the 2 types, results in a valid declaration, as follows:

class A extends w implements x, y {...}

Here, class A inherits from a single parent class (w), but implements 2 interfaces (x and y).


##------------------------------question------------------------------##
What is the result of compiling and executing the following code.

public class ThreadTest extends Thread {
     public void run() {
         System.out.println("In run");
         yield();
         System.out.println("Leaving run");
     }

     public static void main(String args []) {
         (new ThreadTest()).start();
     }
}

##ans1##
The code fails to compile in the main() method.

##ans2##
The code fails to compile in the run() method.

##ans3##
Only the text "In run" will be displayed.

##ans4##
The text "In run" followed by "Leaving run" will be displayed.

##ans5##
The code compiles correctly, but nothing is displayed.

##correct##
4

##multipleSelection##
TRUE

##explanation##
Answer 1 is incorrect. The main() method is defined correctly, and the line  (new ThreadTest()).start();  is a valid way to create and start a ThreadTest thread (since the class ThreadTest is a subclass of Thread).

Answer 2 is incorrect, since the run() method is declared correctly. The yield() method simply returns control back to the JVM, and unless there is a higher priority thread ready to run, this thread will continue or resume execution.

Answer 3 is incorrect, since the yield() only has a temporary effect on execution.

Answer 5 is incorrect, since the start() method starts the thread. On doing so, it is the run() method which is executed. Therefore, the text is displayed.


##------------------------------question------------------------------##
Which of the following will compile without errors.

##ans1##
Byte b = new Byte(123);

##ans2##
Byte b = new Byte("123");

##ans3##
Byte b = new Byte(); 
b = 123;

##ans4##
Byte b = new Byte((int)123.4);

##ans5##
Byte b = new Byte(0x123);

##correct##
2

##multipleSelection##
TRUE

##explanation##
1 will not compile, because 123 defaults to type int, and there is no constructor of the Byte class which takes type int as a parameter.

2 compiles correctly, because a parseable string is a valid parameter for the Byte constructor.

3 fails to compile, because there is no no-args constructor for the Byte class.

4 fails to compile. While the cast to an int is valid, the same reasons as Answer 1 apply.

5 fails to compile, because the value 0x123 is a hex value, and equates to a decimal value of 291. This is not within the valid range for the Byte value.


##------------------------------question------------------------------##
Consider the following piece of code and select the correct statement(s):

1. public class Test{
2.     static int x;
3.     public static void main(String [] args){
4.         x = x + 1;
5.         System.out.println("Value is " + x);
6.     }
7. }


##ans1##
The code fails to compile. The compiler complains that the variable x is used before it is initialised.

##ans2##
The code compiles correctly and displays "Value is 1" when executed.

##ans3##
The code compiles correctly, but throws a NullPointerException at line 5.

##ans4##
The code fails to compile because the 'main' method is incorrectly declared.

##ans5##


##correct##
2

##multipleSelection##
TRUE

##explanation##
Member variables are initialised to 0 at compile time, so x is correctly initialised.


##------------------------------question------------------------------##
Which of the following are termed "short circuit" logical operators.

##ans1##
&

##ans2##
&&

##ans3##
|

##ans4##
||

##ans5##
^

##correct##
2,4

##multipleSelection##
TRUE

##explanation##
The term short-circuit refers to the ability to prematurely terminate evaluation if the result is definitely known.

For example,

boolean a = true;
boolean b = true;

if (a || b){...}

In the above, the expression (a || b) is true if either a OR b is true. Therefore, the evaluation of the expression can be terminated without evalating the right side of the '||' operator, if the left side is true.

In the above example, the code is trivial. However, if 'a' and 'b' were complex expressions resulting in a boolean value, then short-circuiting is a valid way to improve performance.


##------------------------------question------------------------------##
Which of the following are valid expressions.

##ans1##
Object o = new String("abcd");

##ans2##
Boolean b = true;

##ans3##
Panel p = new Frame();

##ans4##
Applet a = new Panel()

##ans5##
Panel p = new Applet()

##correct##
1,5

##multipleSelection##
TRUE

##explanation##
1 is correct because String is derived from Object, and this operation is "down the hierarchy".

2 is incorrect, because Boolean is a class which needs a constructor (as in Boolean b = new Boolean("true")).

3 is incorrect because Frame is not derived from Panel (it is derived from Window).

4 is incorrect, because this operation is "up the hierarchy".

5 is correct, because Applet is derived from Panel, and the operation is "down the hierarchy".


##------------------------------question------------------------------##
Which of the following is a valid way to declare an abstract method which is intended to be public.

##ans1##
public abstract method();

##ans2##
public abstract void method();

##ans3##
public abstract void method(){}

##ans4##
public virtual void method();

##ans5##
public void method() implements abstract;

##correct##
2

##multipleSelection##
FALSE

##explanation##
1 is incorrect, because method() has no return value.

3 is incorrect because the {} braces indicate that the method is actually implemented, and is therefore not abstract. In other words, even empty braces are a valid way to define a method.

4 is incorrect, since virtual is not a Java keyword - this is an attempt to confuse C++ programmers:-)

5 is totally incorrect, even though all the keywords actually exist in Java.


##------------------------------question------------------------------##
Which of the following are valid ways to define a constructor for class Test.

##ans1##
public void Test(){}

##ans2##
public Test(){}

##ans3##
private Test(){}

##ans4##
public static Test(){}

##ans5##
final Test(){}

##correct##
2,3

##multipleSelection##
TRUE

##explanation##
1 is incorrect, because the presence of a return type (void) indicates that Test() is actually a method (constructors do not have a return type).

4 is incorrect because constructors cannot be static.

5 is incorrect because constructors cannot be final.

In general, constructors cannot be:
-final
-static
-synchronized
-abstract



##------------------------------question------------------------------##
Which of the following statements are valid for a method which is overriding the following:

protected void method(int x){..}

##ans1##
The overriding method must take an int as its only parameter.

##ans2##
The overrding method must return a void.

##ans3##
The overriding method can be declared as private.

##ans4##
The overriding method can return any value it wishes.

##ans5##


##correct##
1,2

##multipleSelection##
TRUE

##explanation##
When overriding a method, the overriding method must return the same as the original method, and must have the same signature (parameter list) as the original.

Therefore, 1 and 2 are correct.

3 is incorrect, because you cannot override a method to be more private than the original. It is therefore not possible to override a protected method to be either "friendly" or private.


##------------------------------question------------------------------##
True or False.

An inner class can implement an interface or extend a class.

##ans1##
True

##ans2##
False

##ans3##


##ans4##


##ans5##


##correct##
1

##multipleSelection##
FALSE

##explanation##
Inner classes are real classes, and can therefore be manipulated the same as "top-level" classes.


##------------------------------question------------------------------##
Which of the following are valid return types for listener methods.

##ans1##
boolean

##ans2##
void

##ans3##
An object of type Event

##ans4##
An object of type Object

##ans5##
A long value, representing the Object ID.

##correct##
2

##multipleSelection##
TRUE

##explanation##
All listener methods return void (refer to the JDK API for further information).


##------------------------------question------------------------------##
Consider the following piece of code and select the correct statements.

1.  public class Test{
2.      public static void main(String [] args){
3.          print();
4.      }
5.
6.      public static void print(){
7.         System.out.println("Test");
8.      }
9.
10.     public void print(){
11.         System.out.println("Another Test");
12      }
13. }


##ans1##
The code compiles successfully and displays "Test".

##ans2##
Changing the code at line 10 to the following, will allow the code to compile correctly:  

     public void print(int x){

##ans3##
The code fails to compile. The compiler complains about a static reference to a non-static method, print().

##ans4##
The code fails to compile. The compiler complains about duplicate methods.

##ans5##
Changing the return type on line 10 from 'void' to 'int' will allow the code to compile correctly.

##correct##
2,4

##multipleSelection##
TRUE

##explanation##
The code appears to be attempting to overload the print() method.

However, the only differentiating feature between the methods is the static keyword. This is insufficient for overloading.

Therefore, 1 is incorrect because the code fails to compile due to duplicate methods.

2 is correct because the methods are different overloaded versions of print().

3 is incorrect because of 1 above.

4 is correct.

5 is incorrect because return parameters are not sufficient to differentiate overloaded methods.


##------------------------------question------------------------------##
Consider the following list of tags and attributes of tags and select the ones which can legally be placed between the <applet> and </applet> delimiters.

##ans1##
JAVAC

##ans2##
JAR

##ans3##
CODE

##ans4##
NAME

##ans5##
PARAM

##correct##
3,4,5

##multipleSelection##
TRUE

##explanation##
Javac is the java compiler, and is not a valid tag.

JAR is a java archive. This is a means of storing java classes. While a JAR can be accessed from an applet, the mechanism is to use the ARCHIVE tag.

CODE specifies the applet class to execute.

NAME is used for identifying variables for passing into the applet.

PARAM is used for passing the value of the variable.


##------------------------------question------------------------------##
Which variables can an inner class access from the class which encapsulates it.

##ans1##
All static variables

##ans2##
All final variables

##ans3##
All instance variables

##ans4##
Only final instance variables

##ans5##
Only final static variables

##correct##
1,2,3

##multipleSelection##
TRUE

##explanation##
Inner classes have access to all variables declared within the encapsulating class.


##------------------------------question------------------------------##
Using a FlowLayout manager, which of the following is the correct way to add a conponent referenced by the variable "c" to a container:

##ans1##
add(c);

##ans2##
add("Center", c);

##ans3##
c.add();

##ans4##
c.set();

##ans5##
set(c);

##correct##
1

##multipleSelection##
TRUE

##explanation##
Thanks to Prasad for rightly pointing out that Answer 2 above also works for a container which uses the Flow Layout manager (even though it doesn't have a "Center" area").

This can be explained by the fact that a container such as a Panel inherits from java.awt.Container, which has a method with a signature as follows:

     add(String, Component)

However, while this is symantically correct (ie, it will compile and function correctly), it can't be considered as a valid answer since Flow layout doesn't have the concept of a "Center" area - at least, that's my opinion :-)

However, this raises an interesting point - where possible, you should use static class variables, where a choice is available - eg, use BorderLayout.NORTH instead of "NORTH" when adding components to a container. This will ensure that any errors are trapped at compile time, rather than at run-time :-)



##------------------------------question------------------------------##
Assuming there is a class X which implements the ActionListener interface.

Which method should be used to register this with a Button?

##ans1##
addListener(new X());

##ans2##
addActionListener(new X());

##ans3##
addButtonListener(new X());

##ans4##
addActionAdapter(new X());

##ans5##
(new X()).addActionListener();

##correct##
2

##multipleSelection##
FALSE

##explanation##
All listener objects are associated with event sources using the following method naming template:

     addEEEListener(new X());

where examples of EEE are:
- Mouse
- MouseMotion
- Item
- Adjustment

Refer to the JDK API documentation.


##------------------------------question------------------------------##
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");
}

##ans1##
The code fails to compile at the "if" statement.

##ans2##
An exception is thrown at run-time at the "if" statement.

##ans3##
The text "true" is displayed.

##ans4##
The text "false" is displayed.

##ans5##
Nothing is displayed.

##correct##
3

##multipleSelection##
TRUE

##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 and Answer 3 is correct.


##------------------------------question------------------------------##
Which of the following is not a subclass of java.awt.Component?

##ans1##
Frame

##ans2##
Container

##ans3##
CheckboxGroup

##ans4##
Canvas

##ans5##
Applet

##correct##
3

##multipleSelection##
TRUE

##explanation##
Frame, Container, Canvas and Applet are all subclasses of Component (with Canvas and Container being direct or immediate subclasses, while Frame and Applet are further down the hierarchy).

CheckboxGroup is a direct subclass of java.lang.Object.

Refer to the JDK API documentation.


##------------------------------question------------------------------##
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. }


##ans1##
The code fails to compile at line 5, because the variable "j" is not declared correctly.

##ans2##
The code fails to compile at line 2, because you can't have a static method in a non-static class.

##ans3##
The code compiles correctly, but throws an exception at line 5.

##ans4##
The code compiles correctly but throws an exception at line 2.

##ans5##
The code compiles correctly and displays the following when executed:  
i = 0 : j = 10 
i = 1 : j = 9 
i = 2 : j = 8 
Total 30

##correct##
5

##multipleSelection##
TRUE

##explanation##
Answer 1 is incorrect, because 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.

Answer 2 is incorrect. It is possible to have static methods in a non-static class.

Answers 3 and 4 are incorrect (see above).


##------------------------------question------------------------------##
Which of the following will create a TextField capable of displaying 10 characters, with the initial text "hello"?

##ans1##
TextField t = new TextField("hello", 10);

##ans2##
TextField t = new TextField(10, "hello");

##ans3##
TextField t = new TextField(); 
t.setCols(10); 
t.setText("hello");

##ans4##
TextField t = new TextField(); 
t.setFieldSize(10); 
t.setText("hello");

##ans5##
None of the above.

##correct##
1

##multipleSelection##
FALSE

##explanation##
Refer to the JDK API documentation.

Answers 3 and 4 are totally fictitious since the methods setCols() and setFieldSize() do not exist for TextField (however, setColumns() does exist).


##------------------------------question------------------------------##
What is the effect of adding the sixth element to a Vector created using the following code:

new Vector(5, 10);

##ans1##
An IndexOutOfBounds exception is thrown.

##ans2##
The vector grows in size to a capacity of 10 elements.

##ans3##
The vector grows in size to a capacity of 15 elements.

##ans4##
Nothing. The Vector will have grown when the fifth element was added, because Vector elements are zero-based.

##ans5##
Nothing. This is not a valid way to create a Vector.

##correct##
3

##multipleSelection##
FALSE

##explanation##
One of the constructors for Vector is:

     Vector(int initialCapacity, int capacityIncrement)

Therefore, the Vector is created with an initial capacity of 5. When the sixth element is added, the capacity is increased by 10, to 15.  Therefore, Answer 3 is correct.

Answer 4 is incorrect. While elements are addressable as zero-based (eg, elements 0 to 4), there is still 5 elements in the initial Vector.


##------------------------------question------------------------------##
When is the text "Hi there" displayed?

public class StaticTest {
     static {
         System.out.println("Hi there");
         }

     public void print() {
         System.out.println("Hello");
     }

     public static void main(String args []) {
         StaticTest st1 = new StaticTest();
         st1.print();

         StaticTest st2 = new StaticTest();
         st2.print();
     }
}

##ans1##
Never.

##ans2##
Each time a new object of type StaticTest is created.

##ans3##
Once when the class is loaded into the Java virtual machine.

##ans4##
Only when the main() method is executed.

##ans5##


##correct##
3

##multipleSelection##
FALSE

##explanation##
The code in this segment is what is known as a static initialiser. Static initialiser code is executed once when the class is loaded into the JVM. Multiple static initialiser blocks in a single class are executed in the order they are defined.

Static initialisers are not related to the main() method. Therefore, Answers 1, 2 and 4 are incorrect.


##------------------------------question------------------------------##
Which of the following are valid ways to assign the value of 5 to a variable of type "int" called testValue?

##ans1##
int testValue = 0x5;

##ans2##
int testValue = (int)(2.1F + 3.4D);

##ans3##
int testValue = (octal)5;

##ans4##
int testValue = (0x0A >> 1);

##ans5##
int testValue = (0x0A >>> 1);

##correct##
1,2,4,5

##multipleSelection##
TRUE

##explanation##
Answer 1 is correct, since "0x5" is the hexidecimal representation of 5.

Answer 2 is correct. The value of (2.1F + 3.4D) is 5.5D (the float value is promoted to a double). However, when cast to type "int", the value becomes 5.

Answer 3 is incorrect, because "octal" is not a valid keyword in Java.

Answers 4 and 5 are also correct. The value of 0x0A is decimal 10. Both operations shift right by 1 (which is the same as dividing by 2). The >> and >>> operators yield the same result when a number is positive.


##------------------------------question------------------------------##
Which of the following operations will cause an ActionEvent to be generated.

##ans1##
Clicking a checkbox.

##ans2##
Selecting an option from a Choice box.

##ans3##
Scrolling a scroll bar.

##ans4##
Clicking a button.

##ans5##
Hitting ENTER when typing in a text field.

##correct##
4,5

##multipleSelection##
TRUE

##explanation##
ActionEvents are generated when the user performs the following operations:

- double-clicking a list item
- selecting an menu item
- pressing ENTER when typing in a text field
- clicking on a button

Therefore, Answers 4 and 5 are correct.

Answer 1 and 2 are incorrect, because these operations generate an ItemEvent.

Answer 3 is incorrect, because this operation generates an AdjustmentEvent.


##------------------------------question------------------------------##
True or False.

All Listener interfaces are declared as public and void.

##ans1##
True

##ans2##
False

##ans3##


##ans4##


##ans5##


##correct##
1

##multipleSelection##
FALSE

##explanation##
Refer to the JDK API documentation.


##------------------------------question------------------------------##
True or False.

In a GridLayout, the width and height of all cells are the same.

##ans1##
True

##ans2##
False

##ans3##


##ans4##


##ans5##


##correct##
1

##multipleSelection##
FALSE

##explanation##
In the GridLayout, components are stretched both vertically and horizontally such that they all acquire the same dimensions.


##------------------------------question------------------------------##
True or False.

The BorderLayout manager only allows a single component to be added to each sector.

##ans1##
True

##ans2##
False

##ans3##


##ans4##


##ans5##


##correct##
1

##multipleSelection##
FALSE

##explanation##
This is true. However, in order to add multiple components to a sector, these components can be added to a single container (such as a panel), which is then added to the sector.


##------------------------------question------------------------------##
You want to extend the functionality of the String class, and decide that the best approach is to subclass String.  Which of the following statements are correct.

##ans1##
The approach fails, because the String class is declared as final and cannot be subclassed.

##ans2##
The approach succeeds, and it is therefore possible to override the standard functionality provided by String.

##ans3##
The approach fails, because the String class is declared as abstract, and therefore it cannot be subclassed.

##ans4##


##ans5##


##correct##
1

##multipleSelection##
TRUE

##explanation##
Answer 1 is correct. String is declared as final, so it cannot be extended.

Answer 2 is incorrect (see Answer 1 explanation above).

Answer 3 is incorrect in several areas. Firstly, the String class is not abstract. Secondly, even it it was abstract, it can still be subclassed (abstract means that an instance cannot be created - it doesn't affect subclassing).


##------------------------------question------------------------------##
Which of the following are valid ways to determine the number of elements in an array defined as follows:

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

##ans1##
intArray.size;

##ans2##
intArray.size();

##ans3##
intArray.length;

##ans4##
intArray.length();

##ans5##
intArray.getSize();

##correct##
3

##multipleSelection##
TRUE

##explanation##
Refer to the Java Language Specification.


##------------------------------question------------------------------##
You have a check box within a panel, and the panel is contained within an applet. There are no other components within the applet.

Using "setBackground(Color.white)", you assign the background colour of the applet to white.

Which of the following statements are true.

##ans1##
The background colour of the panel is white.

##ans2##
The foreground colour of the checkbox is the same as the foreground colour of the applet.

##ans3##
It is impossible to determine the background colour of the checkbox from the imformation supplied.

##ans4##
You can't put a panel inside an applet.

##ans5##


##correct##
1,2

##multipleSelection##
TRUE

##explanation##
1 and 2 are correct. In all cases, nested conponents take on the attributes of enclosing components unless explicitly assigned otherwise. This is true for colours, fonts, etc.

3 is incorrect, because of the explanation for 1 and 2 above.

4 is incorrect.


##------------------------------question------------------------------##
Which of the following can be added to a menu?

##ans1##
A panel

##ans2##
A button

##ans3##
A menu

##ans4##
A checkbox

##ans5##
A menubar

##correct##
3

##multipleSelection##
TRUE

##explanation##
A menu can contain any of the following:

- a menu (really a sub-menu)
- a check box menu item, but not a checkbox
- menu items

Therefore, answers 1, 2, 4 and 5 are incorrect.


##------------------------------question------------------------------##
You construct an application by making the following call:

Frame app = new Frame();
app.setSize(100,100);

Which of the following statements are correct.

##ans1##
An empty frame of size 100x100 is displayed, located in the top left corner of the screen.

##ans2##
An empty frame of size 100x100 is displayed, located in the centre of the screen.

##ans3##
Nothing is displayed. There is no setSize() method in the Frame class.

##ans4##
Nothing is displayed. The frame will only be displayed by setting the forground and background colours.

##ans5##
Nothing is displayed. A frame is created by default with zero size, and invisible.

##correct##
5

##multipleSelection##
TRUE

##explanation##
When a frame is created, it is created by default to have a zero size, and to be invisible.

It is therefore necessary to set it's size AND issue the setVisible(true) statement.  Therefore, answer 5 is correct.


##------------------------------question------------------------------##
Which of the following are valid methods in the java.awt.Graphics class?

##ans1##
drawString()

##ans2##
drawOval()

##ans3##
fillRect()

##ans4##
drawPolyline()

##ans5##
fillPolyline()

##correct##
1,2,3,4

##multipleSelection##
TRUE

##explanation##
1, 2, 3 and 4 are all valid methods.

fillPolyline() is not valid, but fillPolygon() is valid!!!

Refer to the JDK API documentation.


##------------------------------question------------------------------##
Which of the following are valid command line options for the JAR utility:

##ans1##
c (create a new or empty archive)

##ans2##
m (indicates that first argument is the name of a user-supplied manifest)

##ans3##
M (do not create a manifest)

##ans4##
z (ZIP the contents of the archive)

##ans5##
L (list the archive contents)

##correct##
1,2,3

##multipleSelection##
TRUE

##explanation##
Refer to the Java documentation.

I'm not 100% sure this type of stuff is on the exam syllabus, but there's no harm in being familiar with it!



##------------------------------question------------------------------##
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. }

##ans1##
The code fails to compile because a class cannot have more than 1 constructor.

##ans2##
The code fails to compile because the class FinalTest has no constructors.

##ans3##
The code fails to compile because the an attempt is made to initialise a final variable at lines 6 and 10.

##ans4##
The code compiles correctly without any warnings or errors.

##ans5##
The code fails to compile because the final variable q is not initialised correctly at line 2.

##correct##
4

##multipleSelection##
TRUE

##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).


##------------------------------question------------------------------##
True or False.

It is valid to throw (and re-throw) an exception inside a catch{} clause.

For example:

     ...
     catch (Exception e){
           throw e;
     }


##ans1##
True

##ans2##
False

##ans3##


##ans4##


##ans5##


##correct##
1

##multipleSelection##
FALSE

##explanation##
This is valid. It will cause the exception to be passed to the handlers at the next-higher level. All further catch clauses are ignored in the current try block.


##------------------------------question------------------------------##
True or False.

A scrollbar will appear in a TextField if more text is typed than can be displayed within the TextField.

##ans1##
True

##ans2##
False

##ans3##


##ans4##


##ans5##


##correct##
2

##multipleSelection##
FALSE

##explanation##
Only a TextArea has this property. TextFields do not display scroll bars.


##------------------------------question------------------------------##
True or False.

It is valid to declare an inherited method as abstract.

##ans1##
True

##ans2##
False

##ans3##


##ans4##


##ans5##


##correct##
1

##multipleSelection##
FALSE

##explanation##
It is valid. However, there is no way to get to behaviour which is located above the abstract method in the hierarchy. In effect, you will block access to parent methods further up the hierarchy.


##------------------------------question------------------------------##
Consider the following code segments and select the correct statement:

Segment 1:
1.  int a = 3;
2.  int b = 0;
3.  int c = a/b;

Segment 2:
4.  float a = 1.0F;
5.  float b = 0.0F;
6.  float c = a/b;

##ans1##
When executed, both segments will cause an exception to be thrown.

##ans2##
When executed, neither segment will cause an exception to be thrown.

##ans3##
Only Segment 1 will throw an exception.

##ans4##
Only Segment 2 will throw an exception.

##ans5##


##correct##
3

##multipleSelection##
FALSE

##explanation##
A divide by zero will cause an exception ONLY with integer arithmetic. Floating point arithmetic will simply assign the result to be NaN and no exception is thrown.


##------------------------------question------------------------------##
Which of the following classes override the equals() method.

##ans1##
String

##ans2##
Integer

##ans3##
Double

##ans4##
Date

##ans5##
File

##correct##
1,2,3,4,5

##multipleSelection##
TRUE

##explanation##
Refer to the JDK documentation.


##------------------------------question------------------------------##
Which of the following is the default layout manager for a panel.

##ans1##
BorderLayout

##ans2##
FlowLayout

##ans3##
GridBagLayout

##ans4##
GridLayout

##ans5##
None of the above.

##correct##
2

##multipleSelection##
FALSE

##explanation##
Refer to JDK documentation.


##------------------------------question------------------------------##
True or False.

The StringBuffer class inherits from the String class.

##ans1##
True

##ans2##
False

##ans3##


##ans4##


##ans5##


##correct##
2

##multipleSelection##
FALSE

##explanation##
Refer to JDK documentation.


##------------------------------question------------------------------##
True or False.

The class String has a concat() method, while the StringBuffer class has an append() method.

##ans1##
True

##ans2##
False

##ans3##


##ans4##


##ans5##


##correct##
1

##multipleSelection##
FALSE

##explanation##
Refer to JDK documentation


##------------------------------question------------------------------##
True or False.

The primative wrapper classes Integer, Double and Float all inherit directly from the java.lang.Wrapper class.

##ans1##
True

##ans2##
False

##ans3##


##ans4##


##ans5##


##correct##
2

##multipleSelection##
FALSE

##explanation##
They all inherit directly from the class Number.

However, note that the wrapper class Boolean inherits directly from the Object class.


##------------------------------question------------------------------##
Which of the following are valid methods for the Applet class.

##ans1##
start()

##ans2##
stop()

##ans3##
init()

##ans4##
destroy()

##ans5##
kill()

##correct##
1,2,3,4

##multipleSelection##
TRUE

##explanation##
Refer to the JDK documentation.


##------------------------------question------------------------------##
True or False.

A label can only display a single line of text.

##ans1##
True

##ans2##
False

##ans3##


##ans4##


##ans5##


##correct##
1

##multipleSelection##
FALSE

##explanation##
Multi-line labels are not currently supported in Java.

Refer to the JDK documentation.


##------------------------------question------------------------------##
Which of the following are valid methods for the Math class.

##ans1##
random()

##ans2##
Random()

##ans3##
toDegrees()

##ans4##
square()

##ans5##
sqr()

##correct##
1,3

##multipleSelection##
TRUE

##explanation##
Refer to the JDK documentation.

Random() is incorrect, because all methods begin with a lower case letter (standard style guide for Java programming).
square() and sqr() do not exist, since the same functionality can be provided by pow().


##------------------------------question------------------------------##
True or False.

JDK 1.2 contains an Arrays class which provides various methods for manipulating arrays (such as searching and sorting).

##ans1##
True

##ans2##
False

##ans3##


##ans4##


##ans5##


##correct##
1

##multipleSelection##
FALSE

##explanation##
Refer to the JDK documentation for 1.2  This class was introduced in 1.2.


##------------------------------question------------------------------##
True or False.

The Class class has no public constructor.

##ans1##
True

##ans2##
False

##ans3##


##ans4##


##ans5##


##correct##
1

##multipleSelection##
FALSE

##explanation##
Refer to the JDK documentation.

To quote... "Class has no public constructor. Instead Class objects are constructed automatically by the Java Virtual Machine as classes are loaded and by calls to the defineClass method in the class loader."


##------------------------------question------------------------------##
Which of the following statements is true with regard to persistence.

##ans1##
"Serializable" is a keyword within the Java language.

##ans2##
"Serializable" is an interface, which classes can implement.

##ans3##
"Serializable" is a class, which other classes can extend.

##ans4##
None of the above.

##ans5##


##correct##
2

##multipleSelection##
FALSE

##explanation##
Serializable is an abstract interface which has no methods or fields. To quote the JDK, it "serves only to identify the semantics of being serializable".


##------------------------------question------------------------------##
Which of the following statements is correct with regard to threads.

##ans1##
"Runnable" is a keyword within the Java language, used to identify classes which can exist as separate threads.

##ans2##
"Runnable" is an interface, which classes can implement when they wish to execute as a separate thread.

##ans3##
"Runnable" is a class, which classes can extend when they wish to execute as a separate thread.

##ans4##
None of the above.

##ans5##
java does not support threads.

##correct##
2

##multipleSelection##
FALSE

##explanation##
Runnable is an abstract interface.

Refer to the JDK documentation.


##------------------------------question------------------------------##
Select the statements which are valid from the list below.

##ans1##
The primative data type "char" is 16-bits wide.

##ans2##
Unicode uses 16-bits to represent a character.

##ans3##
Unicode uses 32-bits to represent a character.

##ans4##
UTF uses 24-bits to represent a character.

##ans5##
UTF uses as many bits as are needed to represent a character.

##correct##
1,2,5

##multipleSelection##
TRUE

##explanation##
UTF is an acronym for UCS (Universal Character Set) Transformation Format. The number of bits is variable - fewer for smaller alphabets, and more for larger (eg, Asian) alphabets.


##------------------------------question------------------------------##
When a Frame is constructed, it has no size and is not displayed. Which of the following methods can be called to display the Frame.

##ans1##
setSize()

##ans2##
setBounds()

##ans3##
setDisplayable()

##ans4##
setVisible()

##ans5##
setState()

##correct##
1,2,4

##multipleSelection##
TRUE

##explanation##
A Frame has zero size by default. It is therefore necessary to first set its size by calling either setSize() or setBounds().

Once it has a size, it is then necessary to make it visible, by calling setVisible().

setDisplayable() is not a valid method.

setState() is used to change between the iconified state and the normal state.




##------------------------------question------------------------------##
Which one of the following is correct (assume A and B are correctly defined).


##ans1##
if (A instanceOf B)

##ans2##
if (A instanceof B)

##ans3##
if (A.instanceOf(B))

##ans4##
if (A instanceof(B))

##ans5##


##correct##
2

##multipleSelection##
FALSE

##explanation##
Answer 2 is correct. All the others are not valid. "instanceof" is a keyword and all lower-case (if it was a method, it would probably be written as "instanceOf(...)").

Refer to the Java Language Specification for further information.




##------------------------------question------------------------------##
What is displayed when the following is executed:

double d1 = -0.5;
System.out.println("Ceil d1 = " + Math.ceil(d1));
System.out.println("floor d1 = " + Math.floor(d1));

##ans1##
Ceil d1 = -0.0 
floor d1 = -1.0

##ans2##
Ceil d1 = 0.0 
floor d1 = -1.0

##ans3##
Ceil d1 = -0.0 
floor d1 = -0.0

##ans4##
Ceil d1 = 0.0 
floor d1 = 0.0

##ans5##
Ceil d1 = 0 
floor d1 = -1

##correct##
1

##multipleSelection##
FALSE

##explanation##
Ceil() is defined as "returns the smallest (closest to negative infinity) double value that is not less than the argument and is equal to a mathematical integer".

Floor() is defined as "returns the largest (closest to positive infinity) double value that is not greater than the argument and is equal to a mathematical integer".

Therefore, only answer 1 is correct.

NOTE : -0.0 is different from 0.0, so answer 2 is incorrect!!!



##------------------------------question------------------------------##
Select the layour manager which is described by the following:

"It arranges its constituent components in horizontal rows. It will attempt to fit as many components as possible into the first row, and remaining components will spill over into subsequent rows. Components always appear left to right, in the order they were added to the container."


##ans1##
Flow Layout

##ans2##
Grid Layout

##ans3##
Card Layout

##ans4##
Transverse Flow Layout

##ans5##
Grid Bag Layout

##correct##
1

##multipleSelection##
FALSE

##explanation##
The "Transverse Flow Layout" manager does not exist. Refer to the JDK documentation for further datails on layout managers.




##------------------------------question------------------------------##
True or False

The argument for a "case" statement must be a constant or a constant expression which can be evaluated at compile time.

##ans1##
True

##ans2##
False

##ans3##


##ans4##


##ans5##


##correct##
1

##multipleSelection##
FALSE

##explanation##
It is not valid to have as an argument of a "case" statement, an expression which is only capable of being evaluated at run-time.

Refer to the Java Language Specification.



##------------------------------question------------------------------##
Select the valid code fragments from the following list:

##ans1##
public transient static final int _FRAMEX = 850;

##ans2##
this("a", "b");
[assuming a constructor of the type xxx(String, String) exists for this class]

##ans3##
private transient static final int _FRAMEX = 850;

##ans4##
boolean b = 0;

##ans5##


##correct##
1,2,3

##multipleSelection##
TRUE

##explanation##
Answer 4 is incorrect, because a boolean must be either true or false (unlike C/C++ where a non-zero value is taken as true, and a 0 is taken as false).

Answers 1 and 3 are correct - they are valid ways of declaring integer variables.

Answer 2 is correct - but only assuming that there is a constructor for the class with the signature "xxx(String, String)".



##------------------------------question------------------------------##
True or False.

The File class can represent either a file or a directory within a file system.

##ans1##
True

##ans2##
False

##ans3##

##ans4##

##ans5##


##correct##
1

##multipleSelection##
FALSE

##explanation##
Refer to the JDK API documentation.


##------------------------------question------------------------------##
True or False.

The argument for a while() statement MUST be a boolean.

##ans1##
True

##ans2##
False

##ans3##

##ans4##

##ans5##


##correct##
1

##multipleSelection##
FALSE

##explanation##
Unlike C/C++, in Java the argument MUST be a boolean value (or an expression that returns a boolean result).


##------------------------------question------------------------------##
True or False.

The right-hand operand of the instanceof operator can be an interface; eg

     a instanceof b

where b is an interface.

##ans1##
True

##ans2##
False

##ans3##

##ans4##

##ans5##


##correct##
1

##multipleSelection##
FALSE

##explanation##
The instanceof operator results in a true value if the left-hand object implements the interface.



##------------------------------question------------------------------##
Consider the following class:

public class Test{
    public int secret;
}

Which of the following is the correct way to make the variable 'secret' read-only.


##ans1##
Declare 'secret' to be private.

##ans2##
Declare the class Test to be private.

##ans3##
Declare 'secret' to be private and write a method getSecret() which returns the value of 'secret'.

##ans4##
Declare 'secret' to be transient.

##ans5##
Declare 'secret' to be static.

##correct##
3

##multipleSelection##
FALSE

##explanation##
Answer 1 is incorrect, because doing this will result in secret being inaccessible from outside the class.

Answer 2 is incorrect, because there is no such thing as a private class (unless it is an inner class).

Answers 4 and 5 are misleading, in that neither if these affect the scope or accessibility of the variable.




##------------------------------question------------------------------##
Which one of the following methods of the Math class can be used to find the square root of a number.

##ans1##
squareRoot()

##ans2##
sqrt()

##ans3##
root()

##ans4##
sqr()

##ans5##
None of the above. You use the pow() method.

##correct##
2

##multipleSelection##
FALSE

##explanation##
Refer to the JDK API documentation.




##------------------------------question------------------------------##
To which layout manager does the following statement refer:

"it preserves the preferred size (width and height) of each component".

##ans1##
Flow Layout

##ans2##
Border Layout

##ans3##
Grid Layout

##ans4##
Grid Bag Layout

##ans5##
None of the above. All layout managers alter the dimensions of their containing components.

##correct##
1

##multipleSelection##
FALSE

##explanation##
Only the Flow Layout manager retains the preferred size.





##------------------------------question------------------------------##
Consider the following code fragment, and select the correct statements(s):

1. public class Test extends MyBase implements MyInterface{
2.      int x = 0;
3. 
4.      public Test(int inVal) throws Exception{
5.           if (inVal != this.x){
6.                throw new Exception("Invalid input");
7.           }
8.      }
9. 
10.     public static void main(String args[]){
11.          Test t = new Test(4);
12.     }
13.}
        


##ans1##
The code fails to compile at line 1. It is not valid to both implement an interface and extend from a parent class simultaneously.

##ans2##
The code fails to compile at line 4. It is not valid for constructors to throw exceptions.

##ans3##
The code fails to compile at line 6, because this is not valid way to throw an exception.

##ans4##
The code fails to compile at line 11. The compiler complains that there is an uncaught exception.

##ans5##
The code fails to compile at line 5, because this is not a valid way to reference variable x.

##correct##
4

##multipleSelection##
FALSE

##explanation##
This piece of code is probably the most complicated you're likely to see in the real exam :-)

It contains many elements of the Java language in a single question, where the real exam tends to keep questions narrowly focused on a particular issue.

Anyway....

Answer 1 is incorrect, because it is indeed valid to both extend a parent class and implement one or more interfaces simultaneously.

Answer 2 is incorrect, because it is valid for a constructor to throw an exception.

Answer 3 is incorrect, because this is the correct way to throw an exception. Also, there is a constructor for the Exception class which takes a string as an argument.

Answer 4 is correct. In order to make the code compile, you need to wrap the creation of the object in a try...catch block.

Answer 5 is incorrect. It is valid to use the word "this" to reference any variables or methods of the local object.




##------------------------------question------------------------------##
Consider the following real-life relationships, and select the code fragment which most closely represents it.

An employee is a person. An employee has zero or more dependants.

[for the code fragments, assume that all relevant packages are imported, and all relevant class definitions exist].


##ans1##
class Employee extends Dependants{
    Person p;
}


##ans2##
class Person extends Employee{
     Vector dependants;
}


##ans3##
class Employee extends Person{
     Vector dependants;
}

##ans4##
abstract class Person extends Dependants{
     Employee e;
}


##ans5##
class Dependant implements Employee{
     Vector person;
}

##correct##
3

##multipleSelection##
FALSE

##explanation##
This questions attempts to test your understanding of real-world situations when mapped to the Java language and object oriented.

Answer 1 is incorrect, because it describes the following:

an employee is a dependant, and an employee has a person.

Answer 2 is incorrect, because it describes the following:

a person is an employee, and a person has zero or more dependants.

Answer 4 is incorrect because it describes the following (the 'abstract' keyword is simply an attempt to confuse the situation):

a person is a dependant (or at least can behave as a dependant), and a person has an employee.

Answer 5 is incorrect and describes the following:

a dependant is an employee (or can behave as an employee), and a dependant has zero or more persons.




##------------------------------question------------------------------##
Which of the following are valid declarations for a native method.


##ans1##
public native void aMethod();

##ans2##
private native String aMethod();

##ans3##
private native boolean aMethod(int val){};

##ans4##
public native int aMethod(String test);

##ans5##
private native boolean aMethod(boolean flag, Integer val);

##correct##
1,2,4,5

##multipleSelection##
TRUE

##explanation##
The 'native' does not affect the access qualifiers, so a native method can be public or private.

It is also valid to return primitives as well as objects from a native method (so returning a String is valid) - it is also valid to pass in objects as well as primitives.

Answer 3 is incorrect, because it attempts to define the actual method, by using '{...}'.





##------------------------------question------------------------------##
Which of the following are valid ways to define an octal literal of value 17 (octal).


##ans1##
private final int theNumber = 0x17;

##ans2##
private final int theNumber = 017;

##ans3##
public int theNumber = 017;

##ans4##
public int theNumber = (octal)17;

##ans5##
public int THE_NUMBER = 017;

##correct##
2,3,5

##multipleSelection##
TRUE

##explanation##
The answers contain various spurious keywords in an attempt to confuse :-)

Answer 1 is incorrect, because it defines a hexidecimal number (0x17).

Answer 4 is incorrect because there is no such keyword as "octal".





##------------------------------question------------------------------##
Consider the following piece of code and select all statements which yield a boolean value of true as a result.


     Double d1=new Double(10.0);
     Double d2=new Double(10.0);
     int x=10;
     float f=10.0f;


##ans1##
d1 == d2;

##ans2##
d1 == x;

##ans3##
f == x;

##ans4##
d1.equals(d2);

##ans5##
None of the above

##correct##
3,4

##multipleSelection##
TRUE

##explanation##

d1==d2 is false, since they are different objects - only the object references are compared.

d1 == x is an invalid comparison, and will cause a compile error.

f == x is valid and will result in a true value. This is because x will be promoted to a float for the comparison, and they are the same value when this occurs.

d1.equals(d2) returns true, because a "deep compare" is done, and this will compare the actual objects themselves.




##------------------------------question------------------------------##
Given the following definition:
      String s = null;

Which code fragment will cause an exception of type NullPointerException to be thrown.


##ans1##
if ((s != null) & (s.length()>0))

##ans2##
if ((s != null) && (s.length()>0))

##ans3##
if ((s != null) | (s.length()>0))

##ans4##
if ((s != null) || (s.length()>0))

##ans5##
None of the above.

##correct##
1,2,3,4

##multipleSelection##
TRUE

##explanation##
This question revolves around the short-circuit logical operators (at least some of it does!).

For the following, LHS is left-hand side, RHS = right hand side.

Answer 1 must be fully evaluated. The LHS is true and so the RHS needs to be evaluated to ensure the entire expression is true. This is an AND operation, and if the LHS is true, then the RHS must be evaluated for the expression to be true.

Answer 2 is the same as above. Both sides need to be evaluated for an AND expression to be true (if the LHS is true, that is)..  

Answer 3 must be fully evaluated because an OR expression is only true if one or the other is true, but not both. Therefore, since the LHS is true, the RHS must be evaluated.

Answer 4 is similar to answer 3 above.

For someone like me with 14+ years of software experience, this was a tough question! It requires that you are familiar with AND and OR truth tables, as well as Java.






##------------------------------question------------------------------##
Which of the following are valid declarations for a 2x2 array of integers.

##ans1##
int a[][] = new int[10,10];

##ans2##
int a[][] = new int [10][10];

##ans3##
int a[10,10] = new int [10][10];

##ans4##
int [][]a = new int [10][10];

##ans5##
int []a[] = new int [10][10];

##correct##
2,4,5

##multipleSelection##
TRUE

##explanation##
Refer to the Java Language Specification.





##------------------------------question------------------------------##
Which values of variable x will show "Message 2".

     switch(x)
     {
          case 1 :
               System.out.println("Message 1");
          case 2 :
          case 3 :
               System.out.println("Message 2");
          default :
               System.out.println("End");
     }

##ans1##
1

##ans2##
2

##ans3##
3

##ans4##
4

##ans5##
none

##correct##
1,2,3

##multipleSelection##
TRUE

##explanation##
There is no 'break' statement after 'case 1', so a value of 1 will "fall down into" case 2 (as well as display "Message 1").





##------------------------------question------------------------------##
What is the result of the following code :

     public class SuperEx {
          String r;
          String s;

          public SuperEx(String a,String b) {
               r = a;
               s = b;
          }

          public void aMethod() {
               System.out.println("r :" + r);
          }
     }


     public class NewSuper extends SuperEx {

          public NewSuper(String a,String b) {
               super(a,b);
          }

          public static void main(String args []) {
               SuperEx a = new SuperEx("Hi","Tom");
               SuperEx b = new NewSuper("Hi","Bart");

               a.aMethod();
               b.aMethod();
          }

          public void aMethod() {
               System.out.println("r :" + r + "  s:" + s);
          }
     }

##ans1##
The following is displayed:
r:Hi
s:Hi


##ans2##
Compiler error at the line "SuperEx b = new NewSuper("Hi","Bart");"

##ans3##
The following is displayed:
r:Hi
r:Hi s:Bart

##ans4##
The following is displayed
r:Hi s:Tom
r:Hi s:Bart

##ans5##

##correct##
3

##multipleSelection##
FALSE

##explanation##
None.






##------------------------------question------------------------------##
You have a class with a certain variable and you don't want that variable to be accessible to ANY other class but your own. Your class must be sub-classable.

Which keyword do you add to the variable.

##ans1##
private

##ans2##
public

##ans3##
transient

##ans4##
final

##ans5##
abstract

##correct##
1

##multipleSelection##
FALSE

##explanation##
'public' means everyone sees it.

'transient' relates to being serializable or not.

'final' for a variable means that it's value cannot be changes once set.

'abstract' relates to class templates.







##------------------------------question------------------------------##
You get this description of a class :

Employee is a person. For every employee, we keep a vector with the working hours, an integer for the salary and a variable that can be true or false whether or not the employee has a company car.

Indicate which of the following would be used to define the class members.

##ans1##
Vector

##ans2##
Employee

##ans3##
Object

##ans4##
boolean

##ans5##
int

##correct##
1

##multipleSelection##
TRUE

##explanation##
A Vector is used for storing the working hours (see the question text!).

A boolean is used for the company car flag.

An int is used for the salary.






##------------------------------question------------------------------##
Which line of code can be inserted in place of the comments, to perform the initialisation described by the comments:

     public class T {
          int r;
          int s;

          T(int x, int y) {
               r = x;
               s = y;
          }
     }

     class S extends T {
          int t;

          public S(int x, int y, int z){
               // insert here the code
               // that would do the correct initialisation
               // r= x  and s= y
               t=z;
          }
     }



##ans1##
T(x, y);

##ans2##
this(x, y);

##ans3##
super(x, y);

##ans4##
super(x, y, z);

##ans5##
None

##correct##
3

##multipleSelection##
FALSE

##explanation##
What's needed here is to call the parent constructor (ie, of class T) and pass in the relevant parameters).

"this(x, y)" attempts to call a constructor for the local class S, which isn't defined.

"super(x, y, z)" attempts to call itself.




##------------------------------question------------------------------##
Suppose a MyException should be thrown if Condition() is true, which statements do you have to insert ?

1: public aMethod {
2:
3:      if (Condition) {
4:
5:      }
6:
7: }



##ans1##
throw new Exception() at line 4

##ans2##
throws new MyException() at line 4

##ans3##
throw new MyException() at line 6

##ans4##
throws new Exception() at line 2

##ans5##
throws MyException at line 1

##correct##
2,5

##multipleSelection##
TRUE

##explanation##
You need to do 2 things...declare the method to throw an exception (answer 5) and then throw the exception (answer 2).








##------------------------------question------------------------------##
Suppose a NullPointerException is thrown by test(). Which message is displayed?

     void Method(){
          try {
               test();
               System.out.println("Message1");
          }
          catch (ArrayOutOfBoundsException e) {
               System.out.println("Message2");
          }
          finally {
               System.out.println("Message3");
          }

          System.out.println("Message4");
     }


##ans1##
Message1

##ans2##
Message2

##ans3##
Message3

##ans4##
Message4

##ans5##
None of the above.

##correct##
3

##multipleSelection##
FALSE

##explanation##
The exception thrown will mean that "Message1" is not displayed (that code is not executed).

Message2 will not be displayed because the catch clause will not catch a NullPointerException.

As normal, the finally clause is executed, so Message3 is displayed.

However, the exception remains uncaught, so execution of the method terminates, and Message4 never gets displayed.





##------------------------------question------------------------------##
Choose the class that can hold multiple equal objects in an ordered way.


##ans1##
Map

##ans2##
Collection

##ans3##
List

##ans4##
Set

##ans5##
Vector

##correct##
3

##multipleSelection##
FALSE

##explanation##
Refer to the JDK API documentation for further information.




##------------------------------question------------------------------##
Consider the following variables definitions.

     Float f1 = new Float("10F");
     Float f2 = new Float("10F");
     Double d1 = new Double("10D");

Which of the following yields a boolean value of true.


##ans1##
f1 == f2

##ans2##
f1.equals(f2)

##ans3##
f2.equals(d1)

##ans4##
f2.equals(new Float"10")

##ans5##


##correct##
2,4

##multipleSelection##
TRUE

##explanation##
Answer 1 is comparing object references, and f1 and f2 reference different objects.

Answer 2 is correct, because the Float class overrides the equals() method. This means that a "deep compare" is performed.

Answer 4 is correct, for the same reasons that answer 2 is correct.

I've no idea why answer 3 returns false, do you :-)




##------------------------------question------------------------------##
What does the getID() method of AWTEvent tell us.

##ans1##
Tells us the x and y coords of the mouse at the event time.

##ans2##
Tells us which Object the event originated from.

##ans3##
Tells us which special keys were pressed at the event time.

##ans4##
Tells us the event type.

##ans5##
Tells us the how many pixels were between the mouse pointer and text on a Canvas.

##correct##
4

##multipleSelection##
FALSE

##explanation##
Refer to the JDK API documentation.




##------------------------------question------------------------------##
What is the return type of an Eventlistener ?

##ans1##
int

##ans2##
NULL

##ans3##
void

##ans4##
boolean

##ans5##
Object

##correct##
3

##multipleSelection##
FALSE

##explanation##
Refer to the JDK API documentation.


##------------------------------question------------------------------##
You create a Long object using the following:

     Long val = new Long("11");

Which of the following will return the value of the object as a byte value?

##ans1##
byte b = (byte)val;

##ans2##
byte b = val.getByte();

##ans3##
byte b = (byte)val.getValue();

##ans4##
byte b = val.byteValue();

##ans5##
None of the above.

##correct##
4

##multipleSelection##
False

##explanation##
Answer 1 is incorrect, because it is attempting to cast an object to a primitive.

Refer to the JDK API documentation for the remaining explanations.



##------------------------------question------------------------------##
Consider the following.

String s1 = "abc";
String s2 = "def";

String s3 = new String(s2);
s2 = s1;

What is the value of s3 after the final line of code is executed?

##ans1##
"abc"

##ans2##
"def"

##ans3##
null

##ans4##
An exception is thrown when "s2 = s1" is executed.

##ans5##


##correct##
2

##multipleSelection##
False

##explanation##
Once s3 is created, it is unaffected by anything happening to s2.



##------------------------------question------------------------------##
What tags are mandatory when creating HTML to display an applet

##ans1##
name, width, height

##ans2##
code, name

##ans3##
codebase, height, width

##ans4##
code, width, height

##ans5##
None of the above

##correct##
4

##multipleSelection##
False

##explanation##
Only code, width and height are mandatory. All other tags are optional.




##------------------------------question------------------------------##
What is displayed when the following code is executed:

String s1 = "   hello world   ";
String s2 = s1.trim();

System.out.println(s2);


##ans1##
"   hello world"

##ans2##
"hello world   "

##ans3##
"hello world"

##ans4##
"   hello world   "

##ans5##
None of the above

##correct##
3

##multipleSelection##
False

##explanation##
The trim() method removed leading and trailing white spaces.




##------------------------------question------------------------------##
Which of the following are methods within the String class.


##ans1##
substring()

##ans2##
startsWith()

##ans3##
toString()

##ans4##
toUpperCase()

##ans5##
indexOf()

##correct##
1,2,3,4,5

##multipleSelection##
TRUE

##explanation##
Refer to the JDK API documentation.


##------------------------------question------------------------------##
True or False.

<APPLET> tags must appear between <BODY> and </BODY> tags.


##ans1##
True

##ans2##
False

##ans3##


##ans4##


##ans5##


##correct##
1

##multipleSelection##
FALSE

##explanation##
Refer to the relevant JDK documentation.

