Monday, 31 August 2009

Java Certification Question 0081

How does the set collection deal with duplicate elements?

1) An exception is thrown if you attempt to add an element with a duplicate value
2) The add method returns false if you attempt to add an element with a duplicate value
3) A set may contain elements that return duplicate values from a call to the equals method
4) Duplicate values will cause an error at compile time


Answer)

2) The add method returns false if you attempt to add an element with a duplicate value

I find it a surprise that you do not get an exception.


Sunday, 30 August 2009

Java Certification Question 0080

Which most closely matches a description of a Java Map?

1) A vector of arrays for a 2D geographic representation
2) A class for containing unique array elements
3) A class for containing unique vector elements
4) An interface that ensures that implementing classes cannot contain duplicate keys



Answer)

4) An interface that ensures that implementing classes cannot contain duplicates

Saturday, 29 August 2009

Java Certification Question 0079

When using the GridBagLayout manager, each new component requires a new instance of the GridBagConstraints class. Is this statement

1) true
2) false




Answer)

2) false

You can re-use the same instance of the GridBagConstraints when added successive components.

Friday, 28 August 2009

Java Certification Question 0078

What will happen when you attempt to compile and run the following code?

public class Bground extends Thread{

public static void main(String argv[]){
Bground b = new Bground();
b.run();
}

public void start(){
for (int i = 0; i <10; i++)
System.out.println("Value of i = " + i);
}
}
}

1) A compile time error indicating that no run method is defined for the Thread class
2) A run time error indicating that no run method is defined for the Thread class
3) Clean compile and at run time the values 0 to 9 are printed out
4) Clean compile but no output at runtime



Answer)

4) Clean compile but no output at runtime


This is a bit of a sneaky one as I have swapped around the names of the methods you need to define and call when running a thread. If the for loop were defined in a method called

public void run()

and the call in the main method had been to b.start()

The list of values from 0 to 9 would have been output.

Thursday, 27 August 2009

Java Certification Question 0077

Which statements are correct about the anchor field?

1) It is a field of the GridBagLayout manager for controlling component placement
2) It is a field of the GridBagConstraints class for controlling component placement
3) A valid setting for the anchor field is GridBagConstraints.NORTH
4) The anchor field controls the height of components added to a container


Answer)

2) It is a field of the GridBagConstraints class for controlling component placement
3) A valid settting for the anchor field is GridBagconstraints.NORTH

Wednesday, 26 August 2009

Java Certification Question 0076

What most closely matches the appearance when this code runs?

import java.awt.*;

public class CompLay extends Frame{

public static void main(String argv[]){
CompLay cl = new CompLay();
}

CompLay(){
Panel p = new Panel();
p.setBackground(Color.pink);
p.add(new Button("One"));
p.add(new Button("Two"));
p.add(new Button("Three"));
add("South",p);
setLayout(new FlowLayout());
setSize(300,300);
setVisible(true);
}
}

1) The buttons will run from left to right along the bottom of the Frame
2) The buttons will run from left to right along the top of the frame
3) The buttons will not be displayed
4) Only button three will show occupying all of the frame


Answer)

2) The buttons will run from left to right along the top of the frame

The call to the setLayout(new FlowLayout()) resets the Layout manager for the entire frame and so the buttons end up at the top rather than the bottom.


Tuesday, 25 August 2009

Java Certification Question 0075

Which of the following are fields of the GridBagConstraints class?

1) ipadx
2) fill
3) insets
4) width


Answers)

1) ipadx
2) fill
3) insets

Monday, 24 August 2009

Java Certification Question 0074

How do you change the current layout manager for a container

1) Use the setLayout method
2) Once created you cannot change the current layout manager of a component
3) Use the setLayoutManager method
4) Use the updateLayout method


Answer 1) Use the setLayout method

Sunday, 23 August 2009

Java Certification Question 0073

How do you indicate where a component will be positioned using Flowlayout?

1) North, South,East,West
2) Assign a row/column grid reference
3) Pass a X/Y percentage parameter to the add method
4) Do nothing, the FlowLayout will position the component




Answer 4) Do nothing, the FlowLayout will position the component

Saturday, 22 August 2009

Java Certification Question 0072

What best describes the appearance of an application with the following code?

import java.awt.*;

public class FlowAp extends Frame{

public static void main(String argv[]){

FlowAp fa=new FlowAp();

fa.setSize(400,300);

fa.setVisible(true);

}

FlowAp(){

add(new Button("One"));

add(new Button("Two"));

add(new Button("Three"));

add(new Button("Four"));

}//End of constructor

}//End of Application

1) A Frame with buttons marked One to Four placed on each edge.
2) A Frame with buutons marked One to four running from the top to bottom
3) A Frame with one large button marked Four in the Centre
4) An Error at run time indicating you have not set a LayoutManager


Answer 3) A Frame with one large button marked Four in the Centre

The default layout manager for a Frame is the BorderLayout manager. This Layout manager defaults to placing components in the centre if no constraint is passed with the call to the add method.

Friday, 21 August 2009

Java Certification Question 0071

You want to loop through an array and stop when you come to the last element. Being a good java programmer and forgetting everything you ever knew about C/C++ you know that arrays contain information about their size. Which of the following can you use?


1)myarray.length();
2)myarray.length;
3)myarray.size
4)myarray.size();



Answer 2) myarray.length;

The String class has a length() method to return the number of characters. I have sometimes become confused between the two.

Thursday, 20 August 2009

Java Certification Question 0070

You want to find out the value of the last element of an array. You write the following code. What will happen when you compile and run it.?

public class MyAr{

public static void main(String argv[]){

int[] i = new int[5];

System.out.println(i[5]);

}

}

1) An error at compile time
2) An error at run time
3) The value 0 will be output
4) The string "null" will be output


Answer 2) An error at run time

This code will compile, but at run-time you will get an ArrayIndexOutOfBounds exception. This becuase counting in Java starts from 0 and so the 5th element of this array would be i[4].

Remember that arrays will always be initialized to default values wherever they are created.



Wednesday, 19 August 2009

Java Certification Question 0069

What happens when you attempt to compile and run these two files in the same directory?

//File P1.java

package MyPackage;

class P1{

void afancymethod(){

System.out.println("What a fancy method");

}

}

//File P2.java

public class P2 extends P1{

afancymethod();

}

1) Both compile and P2 outputs "What a fancy method" when run
2) Neither will compile
3) Both compile but P2 has an error at run time
4) P1 compiles cleanly but P2 has an error at compile time



Answer 4) P1 compiles cleanly but P2 has an error at compile time

The package statement in P1.java is the equivalent of placing the file in a different directory to the file P2.java and thus when the compiler tries to compile P2 an error occurs indicating that superclass P1 cannot be found.

Tuesday, 18 August 2009

Java Certification Question 0068

What will happen when you attempt to compile and run this code?

private class Base{}

public class Vis{

transient int iVal;

public static void main(String elephant[]){

}

}

1)Compile time error: Base cannot be private
2)Compile time error indicating that an integer cannot be transient
3)Compile time error transient not a data type
4)Compile time error malformed main method



Answer 1) Compile time error: Base cannot be private

A top level (non nested) class cannot be private.

Monday, 17 August 2009

Java Certification Question 0067

What will happen when you attempt to compile and run this code?

public class Mod{

public static void main(String argv[]){

}

public static native void amethod();

}

1) Error at compilation: native method cannot be static
2) Error at compilation native method must return value
3) Compilation but error at run time unless you have made code containing native amethod available
4) Compilation and execution without error



Answer 4) Compilation and execution without error

It would cause a run time error if you had a call to amethod though.

Sunday, 16 August 2009

Java Certification Question 0066

What will happen when you attempt to compile and run this code?

class Base{

public final void amethod(){

System.out.println("amethod");

}

}

public class Fin extends Base{

public static void main(String argv[]){

Base b = new Base();

b.amethod();

}

}

1) Compile time error indicating that a class with any final methods must be declared final itself
2) Compile time error indicating that you cannot inherit from a class with final methods
3) Run time error indicating that Base is not defined as final
4) Success in compilation and output of "amethod" at run time.



Answer 4) Success in compilation and output of "amethod" at run time.

A final method cannot be ovverriden in a sub class, but apart from that it does not cause any other restrictions.

Saturday, 15 August 2009

Java Certification Question 0065

Why might you define a method as native?

1) To get to access hardware that Java does not know about
2) To define a new data type such as an unsigned integer
3) To write optimised code for performance in a language such as C/C++
4) To overcome the limitation of the private scope of a method




Answers:

1) To get to access hardware that Java does not know about
3) To write optimised code for performance in a language such as C/C++

Friday, 14 August 2009

Java Certification Question 0064

What will happen when you attempt to compile and run this code?

class Base{

abstract public void myfunc();

public void another(){

System.out.println("Another method");

}

}

public class Abs extends Base{

public static void main(String argv[]){

Abs a = new Abs();

a.amethod();

}

public void myfunc(){

System.out.println("My func");

}

public void amethod(){

myfunc();

}

}

1) The code will compile and run, printing out the words "My Func"
2) The compiler will complain that the Base class is not declared as abstract.
3) The code will compile but complain at run time that the Base class has non abstract methods
4) The compiler will complain that the method myfunc in the base class has no body, nobody at all to love it



Answer 2) The compiler will complain that the Base class is not declared as abstract.

If a class contains abstract methods it must itself be declared as abstract

Thursday, 13 August 2009

Java Certification Question 0063

Which of the following are Java modifiers?

1) public
2) private
3) friendly
4) transient
5) vagrant



Answer :

1) public
2) private
4) transient

The keyword transient is easy to forget as is not frequently used. Although a method may be considered to be friendly like in C++ it is not a Java keyword.

Wednesday, 12 August 2009

Java Certification Question 0062

What will happen when you attempt to compile and run this code?

public class MyMain{

public static void main(String argv){

System.out.println("Hello cruel world");

}

}

1) The compiler will complain that main is a reserved word and cannot be used for a class
2) The code will compile and when run will print out "Hello cruel world"
3) The code will compile but will complain at run time that no constructor is defined
4) The code will compile but will complain at run time that main is not correctly defined




Answer 4) The code will compile but will complain at run time that main is not correctly defined

In this example the parameter is a string not a string array as needed for the correct main method

Tuesday, 11 August 2009

Java Certification Question 0061

What will happen when you attempt to compile and run this code?

abstract class Base{

abstract public void myfunc();

public void another(){

System.out.println("Another method");

}

}

public class Abs extends Base{

public static void main(String argv[]){

Abs a = new Abs();

a.amethod();

}

public void myfunc(){

System.out.println("My Func");

}

public void amethod(){

myfunc();

}

}

1) The code will compile and run, printing out the words "My Func"
2) The compiler will complain that the Base class has non abstract methods
3) The code will compile but complain at run time that the Base class has non abstract methods
4) The compiler will complain that the method myfunc in the base class has no body, nobody at all to love it



Answer 1) The code will compile and run, printing out the words "My Func"

A class that contains an abstract method must be declared abstract itself, but may contain non abstract methods.

Monday, 10 August 2009

Java Certification Question 0060

Given the following code

import java.io.*;

public class Ppvg{

public static void main(String argv[]){

Ppvg p = new Ppvg();

p.fliton();

}

public int fliton(){

try{

DataInputStream din = new DataInputStream(System.in);

din.readChar();

}catch(IOException ioe){

System.out.println("flytwick");

return 99;

}finally{

System.out.println("fliton");

}

return -1;

}

}

Which of the following statements are true if you try to compile and run the program?

1) The program will run and output only "flytwick"
2) The program will run and output only "fliton"
3) The program will run and output both "fliton" and "flytwick"
4) An error will occur at compile time because the method fliton attempts to return two values


Answer 2:

2) The program will run and output only "fliton"

This question tests your knowledge of the principle that the finally clause will almost always run.

Sunday, 9 August 2009

Java Certification Question 0059

Which of the following statements are true?

1) The x,y coordinates of an instance of MouseEvent can be obtained using the getX() and getY() methods
2) The x,y coordinates of an instance of MouseEvent can be obtained using the X and Y integer fields
3) The time of a MouseEvent can be extracted using the getTime() method
4) The time of a MouseEvent can be extracted using the when parameter of the MouseEvent constructor


Answer 1 and 4:

1) The x,y coordinates of an instance of MouseEvent can be obtained using the getX() and getY() methods
4) The time of a MouseEvent can be extracted using the when parameter of the MouseEvent constructor

If you chose option 4, referring to the mythical getTime method you have made a reasonable guess based on the normal conventions of Java. However the conventions do not always hold true. If you chose option 3 perhaps you are not as aware of the conventions as you should be.

Saturday, 8 August 2009

Java Certification Question 0058

What will happen when you attempt to compile and run the following code.

public class Pvf{

static boolean Paddy;

public static void main(String argv[]){

System.out.println(Paddy);

}

}

1) Compile time error
2) compilation and output of false
3) compilation and output of true
4) compilation and output of null


Answer 2:

2) compilation and output of false

A variable defined at class level will always be given a default value and the default value for the primitive type boolean is false

Friday, 7 August 2009

Java Certification Question 0057

Given the following code

class Base {}

class Agg extends Base{

public String getFields(){

String name = "Agg";

return name;

}

}

public class Avf{

public static void main(String argv[]){

Base a = new Agg();

//Here

}

}

What code placed after the comment //Here will result in calling the getFields method resulting in the output of the string "Agg"?

1) System.out.println(a.getFields());
2) System.out.println(a.name);
3) System.out.println((Base) a.getFields());
4) System.out.println( ((Agg) a).getFields());


Answer 4:

4) System.out.println( ((Agg) a).getFields());

The Base type reference to the instance of the class Agg needs to be cast from Base to Agg to get access to its methods.The method invoked depends on the object itself, not on the declared type. So, a.getField() invokes getField() in the Base class, which displays Base. But the call to ((Agg)a).getField() will invoke the getField() in the Agg class. You will be unlucky to get a question as complex as this on the exam.

Thursday, 6 August 2009

Java Certification Question 0056

Given the following class

public class ZeroPrint{

public static void main(String argv[]){

int i =0;

//Here

}

}

Which of the following lines if placed after the comment //Here will print out 0.

1) System.out.println(i++);
2) System.out.println(i+'0');
3) System.out.println(i);
4) System.out.println(i--);


Answer 1, 3 and 4:

1) System.out.println(i++);
3) System.out.println(i);
4) System.out.println(i--);

The options for this question might look suspiciously easy if you are not aware of the effects of the post-increment operators. The ++ and -- operations for examples 1 and 4 only come into effect after the output operations, ie after whatever else is done to them on that line of code. Option 2 should be fairly obvious as you should know that the single quote characters indicate a char value, ie storing the character rather than the numberical value for 0.

Wednesday, 5 August 2009

Java Certification Question 0055

What will happen when you attempt to compile and run the following code?


public class Agg{

static public long i=10;

public static void main(String argv[]){

switch(i){

default:

System.out.println("no value given");

case 1:

System.out.println("one");

case 10:

System.out.println("ten");

case 5:

System.out.println("five");

}

}

}

1) Compile time error
2) Output of "ten" followed by "five"
3) Output of "ten"
4) Compilation and run time error because of location of default



Answer 1:

1) Compile time error

This might be considered a "gocha" or deliberate attempt to mislead you because i has been given the data type of long and the parameter must be either a byte, char, short or int. If you attempt to compile this code with JDK 1.2 you will get an error that says something like "Incompatible type for switch, Explicit cast needed to convert long to int. Answering with option 2 would have been reasonable because if the parameter had been an integer type the lack of break statements would have caused this output. If you gave either of the answers you should probably revise the subject.

Tuesday, 4 August 2009

Java Certification Question 0054

What will happen when you attempt to compile and run the following code?

public class Inc{

public static void main(String argv[]){

Inc inc = new Inc();

int i =0;

inc.fermin(i);

i = i++;

System.out.println(i);

}

void fermin(int i){

i++;

}

}

1) Compile time error
2) Output of 2
3) Output of 1
4) Output of 0


Answer 4:

4) Output of 0

The method fermin only receives a copy of the variable i and any modifications to it are not reflected in the version in the calling method. The post increment operator ++ effectivly modifes the value of i after the initial value has been assiged to the left hand side of the equals operator. This can be a very tricky conept to understand

Monday, 3 August 2009

Java Certification Question 0053

You need to read in the lines of a large text file containing tens of megabytes of data. Which of the following would be most suitable for reading in such a file

1) new FileInputStream("file.name")
2) new InputStreamReader(new FileInputStream("file.name"))
3) new BufferedReader(new InputStreamReader(new FileInputStream("file.name")));
4) new RandomAccessFile raf=new RandomAccessFile("myfile.txt","+rw");


Answer 3:

3) new BufferedReader(new InputStreamReader(new FileInputStream("file.name")));

The key to this question is that it asks about tens of megabytes of data, implying that performance is an issue. A Buffered Reader will optimise the performance of accessing a file. Although the objectives do not specifically mention it questions on I/O do come up on the exam.

Sunday, 2 August 2009

Java Certification Question 0052

You have been asked to create a scheduling system for a hotel and catering organsiation.

You have been given the following information and asked to create a set of classes to represent it.

On the catering side of the organsiation they have

Head Chefs
Chefs
Apprentice Chefs

The system needs to store an employeeid, salary and the holiday entitlement

How would you best represent this information in Java

1) Create classes for Head Chef, Chef, Apprentice Chef and store the other values in fields
2) Create an employee class and derive sub classes for Head Chef, Chef, Apprentice Chef and store the other values in fields.
3) Create and employee class with fields for Job title and fields for the other values.
4) Create classes for all of the items mentioned and create a container class to represent employees



Answer 3:

3) Create and employee class with fields for Job title and fields for the other values.

These questions can appear tricky as the whole business of designing class structures is more art than science. It is asking you to decide if an item of data is best represented by the "Is a" or "Has a" relationship. Thus in this case any of the job titles mentioned will always refer to something that "Is a" employee. However the employee "has a" job title that might change.

One of the important points is to ask yourself when creating a class "Could this change into another class at some point in the future". Thus in this example an apprentice chef would hope one day to turn into a chef and if she is very good will one day be head chef. Few other mock exams seem to have this type of questions but they di come up in the real exam.

Saturday, 1 August 2009

Java Certification Question 0051

What will happen when you attempt to compile and run the following code

class Base{

protected int i = 99;

}

public class Ab{

private int i=1;

public static void main(String argv[]){

Ab a = new Ab();

a.hallow();

}

abstract void hallow(){

System.out.println("Claines "+i);

}

}

1) Compile time error
2) Compilation and output of Claines 99
3) Compilation and output of Claines 1
4) Compilation and not output at runtime


Answer 1:

1) Compile time error

When compiled with JDK 1.1 the following error is produced.

Abstract and native methods can't have a body: void hallow() abstract void hallow()