Saturday, 26 September 2009

Java Certification Question 0105

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


int Output=10;

boolean b1 = false;

if((b1==true) && ((Output+=10)==20)){

System.out.println("We are equal "+Output);

}else

{

System.out.println("Not equal! "+Output);

}


1) Compile error, attempting to peform binary comparison on logical data type
2) Compilation and output of "We are equal 10"
3) Compilation and output of "Not equal! 20"
4) Compilation and output of "Not equal! 10"





Answer :

4) Compilation and output of "Not equal! 10"

The output will be "Not equal 10". This illustrates that the Output +=10 calculation was never performed because processing stopped after the first operand was evaluated to be false. If you change the value of b1 to true processing occurs as you would expect and the output is "We are equal 20";.

Friday, 25 September 2009

Java Certification Question 0104

Which of the following statements are true?


1) An inner class may be defined as static
2) There are NO circumstances where an inner class may be defined as private
3) An anonymous class may have only one constructor
4) An inner class may extend another class





Answer :

1) An inner class may be defined as static
4) An inner class may extend another class

A static inner class is also sometimes known as a top level nested class. There is some debate if such a class should be called an inner class. I tend to think it should be on the basis that it is created inside the opening braces of another class. How could an anonymous class have a constructor?. Remember a constructor is a method with no return type and the same name as the class. Inner classes may be defined as private

Thursday, 24 September 2009

Java Certification Question 0103

Your chief Software designer has shown you a sketch of the new Computer parts system she is about to create. At the top of the hierarchy is a Class called Computer and under this are two child classes. One is called LinuxPC and one is called WindowsPC.

The main difference between the two is that one runs the Linux operating System and the other runs the Windows System (of course another difference is that one needs constant re-booting and the other runs reliably). Under the WindowsPC are two Sub classes one called Server and one Called Workstation. How might you appraise your designers work?


1) Give the goahead for further design using the current scheme
2) Ask for a re-design of the hierarchy with changing the Operating System to a field rather than Class type
3) Ask for the option of WindowsPC to be removed as it will soon be obsolete
4) Change the hierarchy to remove the need for the superfluous Computer Class.






Answer 103)

2) Ask for a re-design of the hierarchy with changing the Operating System to a field rather than Class type

This question is about the requirement to understand the difference between the "is-a" and the "has-a" relationship. Where a class is inherited you have to ask if it represents the "is-a" relationship. As the difference between the root and the two children are the operating system you need to ask are Linux and Windows types of computers.The answer is no, they are both types of Operating Systems. So option two represents the best of the options. You might consider having operating system as an interface instead but that is another story.

Of course there are as many ways to design an object hierarchy as ways to pronounce Bjarne Strousjoup, but this is the sort of answer that Sun will proabably be looking for in the exam. Questions have been asked in discussion forums if this type of question really comes up in the exam. I think this is because some other mock exams do not contain any questions like this. I assure you that this type of question can come up in the exam. These types of question are testing your understanding of the difference between the is-a and has-a relationship in class design.

Wednesday, 23 September 2009

Java Certification Question 0102

Which of the following statements about threading are true?


1) You can only obtain a mutually exclusive lock on methods in a class that extends Thread or implements runnable
2) You can obtain a mutually exclusive lock on any object
3) A thread can obtain a mutually exclusive lock on a method declared with the keyword synchronized
4) Thread scheduling algorithms are platform dependent








Answer :

1) b=m;
3) d =i;

You can assign up the inheritance tree from a child to a parent but not the other way without an explicit casting. A boolean can only ever be assigned a boolean value.

Tuesday, 22 September 2009

Java Certification Question 0101

Given the following code

class Base{}

public class MyCast extends Base{

static boolean b1=false;
static int i = -1;
static double d = 10.1;

public static void main(String argv[]){

MyCast m = new MyCast();
Base b = new Base();
//Here

}

}

Which of the following, if inserted at the comment //Here will allow the code to compile and run without error

1) b=m;
2) m=b;
3) d =i;
4) b1 =i;



Answer :

2) If multiple listeners are added to a component the events will be processed for all but with no guarantee in the order
4) You may remove as well add listeners to a component.

It ought to be fairly intuitive that a component ought to be able to have multiple listeners. After all, a text field might want to respond to both the mouse and keyboard


Monday, 21 September 2009

Java Certification Question 0100

Which of the following statements are correct?

1) If multiple listeners are added to a component only events for the last listener added will be processed
2) If multiple listeners are added to a component the events will be processed for all but with no guarantee in the order
3) Adding multiple listeners to a comnponent will cause a compile time error
4) You may remove as well add listeners to a component.





Answer :

2) If multiple listeners are added to a component the events will be processed for all but with no guarantee in the order
4) You may remove as well add listeners to a component.

It ought to be fairly intuitive that a component ought to be able to have multiple listeners. After all, a text field might want to respond to both the mouse and keyboard

Sunday, 20 September 2009

Java Certification Question 0099

Which of the following statements are true?

1) At the root of the collection hierarchy is a class called Collection
2) The collection interface contains a method called enumerator
3) The interator method returns an instance of the Vector class
4) The set interface is designed for unique elements




Answer :

4) The set is designed for unique elements.

Collection is an interface, not a class. The Collection interface includes a method called iterator. This returns an instance of the Iterator class which has some similarities with Enumerators.
The name set should give away the purpose of the Set interface as it is analogous to the Set concept in relational databases which implies uniquness.

Saturday, 19 September 2009

Java Certification Question 0098

Given the following main method in a class called Cycle and a command line of

java Cycle one two

what will be output?

public static void main(String bicycle[]){
System.out.println(bicycle[0]);
}

1) None of these options
2) cycle
3) one
4) two




Answer :

3) one

Command line parameters start from 0 and fromt he first parameter after the name of the compile (normally Java)

Wednesday, 16 September 2009

Java Certification Question 0097

What will be output by the following line?

System.out.println(Math.floor(-2.1));

1) -2
2) 2.0
3) -3
4) -3.0



Answer :

4) -3.0

Tuesday, 15 September 2009

Java Certification Question 0096

Which of the following are Java key words?

1)double
2)Switch
3)then
4)instanceof




Answer :

1)double
4)instanceof

Note the upper case S on switch means it is not a keyword and the word then is part of Visual Basic but not Java. Also, instanceof looks like a method but is actually a keyword,

Monday, 14 September 2009

Java Certification Question 0095

Which of the following will compile correctly?

1) short myshort = 99S;
2) String name = 'Excellent tutorial Mr Green';
3) char c = 17c;
4)int z = 015;





Answer

4)int z = 015;

The letters c and s do not exist as literal indicators and a String must be enclosed with double quotes, not single as in this case.

Sunday, 13 September 2009

Java Certification Question 0094

Which option most fully describes will happen when you attempt to compile and run the following code

public class MyAr{

public static void main(String argv[]) {
MyAr m = new MyAr();
m.amethod();
}

public void amethod(){
static int i;
System.out.println(i);
}
}

1) Compilation and output of the value 0
2) Compile time error because i has not been initialized
3) Compilation and output of null
4) Compile time error



Answer)

4) Compile time error

An error will be caused by attempting to define an integer as static within a method. The lifetime of a field within a method is the duration of the running of the method. A static field exists once only for the class. An approach like this does work with Visual Basic.

Saturday, 12 September 2009

Java Certification Question 0093

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

//Demonstration of event handling
import java.awt.event.*;
import java.awt.*;

public class MyWc extends Frame implements WindowListener{

public static void main(String argv[]){
MyWc mwc = new MyWc();
}

public void windowClosing(WindowEvent we){
System.exit(0);
}//End of windowClosing

public void MyWc(){
setSize(300,300);
setVisible(true);
}
}//End of class

1) Error at compile time
2) Visible Frame created that that can be closed
3) Compilation but no output at run time
4) Error at compile time because of comment before import statements







Answer)

1) Error at compile time

If you implement an interface you must create bodies for all methods in that interface. This code will produce an error saying that MyWc must be declared abstract because it does not define all of the methods in WindowListener. Option 4 is nonsense as comments can appear anywhere. Option 3 suggesting that it might compile but not produce output is ment to mislead on the basis that what looks like a constructor is actually an ordinary method as it has a return type.

Friday, 11 September 2009

Java Certification Question 0092

What will happen when you attempt to compile and run this program

public class Outer

public String name = "Outer";

public static void main(String argv[]){
Inner i = new Inner();
i.showName();
}//End of main

private class Inner{
String name =new String("Inner");
void showName(){
System.out.println(name);
}
}//End of Inner class
}

1) Compile and run with output of "Outer"
2) Compile and run with output of "Inner"
3) Compile time error because Inner is declared as private
4) Compile time error because of the line creating the instance of Inner





Answer)

4) Compile time error because of the line creating the instance of Inner


This looks like a question about inner classes but it is also a reference to the fact that the main method is static and thus you cannot directly access a non static method. The line causing the error could be fixed by changing it to say

Inner i = new Outer().new Inner();

Then the code would compile and run producing the output "Inner"



Thursday, 10 September 2009

Java Certification Question 0091

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

import java.io.*;

class Base{

public static void amethod()throws FileNotFoundException{}
}

public class ExcepDemo extends Base{

public static void main(String argv[]){
ExcepDemo e = new ExcepDemo();
}

public static void amethod(){}

protected ExcepDemo(){
try{
DataInputStream din = new DataInputStream(System.in);
System.out.println("Pausing");
din.readChar();
System.out.println("Continuing");
this.amethod();
}catch(IOException ioe) {}
}
}

1)Compile time error caused by protected constructor
2) Compile time error caused by amethod not declaring Exception
3) Runtime error caused by amethod not declaring Exception
4) Compile and run with output of "Pausing" and "Continuing" after a key is hit




Answer )

4) Compile and run with output of "Pausing" and "Continuing" after a key is hit

An overriden method in a sub class must not throw Exceptions not thrown in the base class. In the case of the method amethod it throws no exceptions and will thus compile without complain. There is no reason that a constructor cannot be protected.

Wednesday, 9 September 2009

Java Certification Question 0090

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

public class StrEq{

public static void main(String argv[]){
StrEq s = new StrEq();
}

private StrEq(){
String s = "Marcus";
String s2 = new String("Marcus");
if(s == s2){
System.out.println("we have a match");
}else{
System.out.println("Not equal");
}
}
}

1) Compile time error caused by private constructor
2) Output of "we have a match"
3) Output of "Not equal"
4) Compile time error by attempting to compare strings using ==




Answer)

3) Output of "Not equal"

Despite the actual character strings matching, using the == operator will simply compare memory location. Because the one string was created with the new operator it will be in a different location in memory to the other string.

Tuesday, 8 September 2009

Java Certification Question 0089

What will happen when you attempt to compile and run the following code with the command line "hello there"

public class Arg{

String[] MyArg;
public static void main(String argv[]){
MyArg=argv;
}
public void amethod(){
System.out.println(argv[1]);
}
}

1) Compile time error
2) Compilation and output of "hello"
3) Compilation and output of "there"
4) None of the above





Answer)

1) Compile time error

You will get an error saying something like "Cant make a static reference to a non static variable". Note that the main method is static.

Monday, 7 September 2009

Java Certification Question 0088

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

public class As{

int i = 10;
int j;
char z= 1;
boolean b;

public static void main(String argv[]){
As a = new As();
a.amethod();
}
public void amethod(){
System.out.println(j);
System.out.println(b);
}
}

1) Compilation succeeds and at run time an output of 0 and false
2) Compilation succeeds and at run time an output of 0 and true
3) Compile time error b is not initialised
4) Compile time error z must be assigned a char value




Answer)

1) Compilation succeeds and at run time an output of 0 and false
The default value for a boolean declared at class level is false, and integer is 0;

Sunday, 6 September 2009

Java Certification Question 0087

Which of the following is the correct syntax for suggesting that the JVM performs garbage collection

1) System.free();
2) System.setGarbageCollection();
3) System.out.gc();
4) System.gc();





Answer)

4) System.gc();

Saturday, 5 September 2009

Java Certification Question 0086

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

public class MySwitch{

public static void main(String argv[]){
MySwitch ms= new MySwitch();
ms.amethod();
}

public void amethod(){
int k=10;
switch(k){
default: //Put the default at the bottom, not here
System.out.println("This is the default output");
break;
case 10:
System.out.println("ten");
case 20:
System.out.println("twenty");
break;
}
}
}

1) None of these options
2) Compile time error target of switch must be an integral type
3) Compile and run with output "This is the default output"
4) Compile and run with output of the single line "ten"




Answer)

1) None of these options

Because of the lack of a break statement after the break 10; statement the actual output will be

"ten" followed by "twenty"

Friday, 4 September 2009

Java Certification Question 0085

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

public class Hope{

public static void main(String argv[]){
Hope h = new Hope();
}

protected Hope(){
for(int i =0; i <10; i++ )
System.out.println(i);
}
}
}

1) Compilation error: Constructors cannot be declared protected
2) Run time error: Constructors cannot be declared protected
3) Compilation and running with output 0 to 10
4) Compilation and running with output 0 to 9




Answer)


4) Compilation and running with output 0 to 9

Thursday, 3 September 2009

Java Certification Question 0084

Under what circumstances might you use the yield method of the Thread class

1) To call from the currently running thread to allow another thread of the same or higher priority to run
2) To call on a waiting thread to allow it to run
3) To allow a thread of higher priority to run
4) To call from the currently running thread with a parameter designating which thread should be allowed to run



Answer)


1) To call from the currently running thread to allow another thread of the same or higher priority to run

Option 3 looks plausible but there is no guarantee that the thread that grabs the cpu time will be of a higher priority. It will depend on the threading algorithm of the Java Virtual Machine and the underlying operating system

Wednesday, 2 September 2009

Java Certification Question 0083

For a class defined inside a method, what rule governs access to the variables of the enclosing method?

1) The class can access any variable
2) The class can only access static variables
3) The class can only access transient variables
4) The class can only access final variables





Answer)

4) The class can only access final variables

Tuesday, 1 September 2009

Java Certification Question 0082

What can cause a thread to stop executing?

1) The program exits via a call to System.exit(0);
2) Another thread is given a higher priority
3) A call to the thread's stop method.
4) A call to the halt method of the Thread class


Answer)

1) The program exits via a call to exit(0);
2) The priority of another thread is increased
3) A call to the stop method of the Thread class


Java threads are somewhat platform dependent and you should be carefull when making assumptions about Thread priorities. On some platforms you may find that a Thread with higher priorities gets to "hog" the processor.

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()

Friday, 31 July 2009

Java Certification Question 0050

Given the following code

public class Sytch{

int x=2000;

public static void main(String argv[]){

System.out.println("Ms "+argv[1]+"Please pay $"+x);

}

}

What will happen if you attempt to compile and run this code with the command line

java Sytch Jones Diggle

1) Compilation and output of Ms Diggle Please pay $2000
2) Compile time error
3) Compilation and output of Ms Jones Please pay $2000
4) Compilation but runtime error


Answer 2:

2) Compile time error

The main method is static and cannot access the non static variable x

Thursday, 30 July 2009

Java Certification Question 0049

Which of the following statements are true?

1) The automatic garbage collection of the JVM prevents programs from ever running out of memory
2) A program can suggest that garbage collection be performed but not force it
3) Garbage collection is platform independent
4) An object becomes eligible for garbage collection when all references denoting it are set to null.


Answers 2 and 4:

2) A program can suggest that garbage collection be performed but not force it
4) A reference becomes eligable for garbage collection when it is assigned to null

If a program keeps creating new references without any being discarded it may run out of memory. Unlike most aspects of Java garbage collection is platform dependent.

Wednesday, 29 July 2009

Java Certification Question 0048

Which of the following statements are true?

1) Static methods cannot be overriden to be non static
2) Static methods cannot be declared as private
3) Private methods cannot be overloaded
4) An overloaded method cannot throw exceptions not checked in the base class


Answer 1:

1) Static methods cannot be overriden to be non static

The JDK1.1 compiler will issue an error message "static methods cannot be overriden" if you atempt to do this. There is no logic or reason why private methods should not be overloaded or that static methods should not be declared private. Option 4 is a jumbled up version of the limitations of exceptions for overriden methods

Tuesday, 28 July 2009

Java Certification Question 0047

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

class Base{

Base(){

System.out.println("Base");

}

}

public class Checket extends Base{

public static void main(String argv[]){

Checket c = new Checket();

super();

}

Checket(){

System.out.println("Checket");

}

}

1) Compile time error
2) Checket followed by Base
3) Base followed by Checket
4) runtime error


Answer 1:

1) Compile time error

With the sun JDK it will produce the following error

"Only constructors can invoke constructors".

If you took out the call to super that causes this error the program would compile and at runtime it would output Base and then Checket as constructors are called from the oldest ancestor class downwards.

Monday, 27 July 2009

Java Certification Question 0046

Which of the following statements are true?

1) A method cannot be overloaded to be less public in a child class
2) To be overridden a method must have the same name and parameter types
3) To be overridden a method must have the same name, parameter and return types
4) An overridden method must have the same name, parameter names and parameter types



Answer 3:

3) To be overriden a method must have the same name, parameter and return types

Option 1 is a sneaky one in that it should read overriden not overloaded. An overriden method must also have the same return type. Parameter names are purely a programmer convenience and are not a factor in either overloading and overriding. Parameter order is a factor however.

Sunday, 26 July 2009

Java Certification Question 0045

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

public class Sandys{

private int court;

public static void main(String argv[]){

Sandys s = new Sandys(99);

System.out.println(s.court);

}

Sandys(int ballcount){

court=ballcount;

}

}


1) Compile time error, the variable court is defined as private
2) Compile time error, s is not initialized when the System.out method is called
3) Compilation and execution with no output
4) Compilation and run with an output of 99



Answer 4:

4) Compilation and run with an output of 99

The fact that the variable court is declared as private does not stop the constructor from being able to initialise it.

Saturday, 25 July 2009

Java Certification Question 0044

Given a reference called

t

to to a class which extends Thread, which of the following will cause it to give up cycles to allow another thread to execute.

1) t.yield();
2) yield()
3) yield(100) //Or some other suitable amount in milliseconds
4) yield(t);


Answer :

yield is a static method inherited from Thread and causes whatever thread is currently executing to yield its cycles.

2) yield()

Friday, 24 July 2009

Java Certification Question 0043

Which of the following statements are true

1) constructors cannot be overloaded
2) constructors cannot be overridden
3) a constructor can return a primitive or an object reference
4) constructor code executes from the current class up the hierarchy to the ancestor class



Answer 2: constructors cannot be overriden

Overloading constructors is a key technique to allow multiple ways of initialising classes. By definition constructors have no return values so option 3 makes no sense. Option 4 is the inverse of what happens as constructor code will execute starting from the oldest ancestor class downwards. You can test this by writing a class that inherits from a base class and getting the constructor to print out a message. When you create the child class you will see the order of constructor calling.


Thursday, 23 July 2009

Java Certification Question 0042

Given the following declaration


Integer i=new Integer(99);


How can you now set the value of i to 10?


1) i=10;
2) i.setValue(10);
3) i.parseInt(10);
4) none of the above


Answer 4: none of the above

The wrapper classes are immutable. Once the value has been set it cannot be changed. A common use of the wrapper classes is to take advantage of their static methods such as Integer.parseInt(String s) that will returns an integer if the String contains one.

Wednesday, 22 July 2009

Java Certification Question 0041

You are creating an application that has a form with a text entry field used to enter a persons age. Which of the following is appropriate for capturing this information.


1) Use the Text field of a TextField and parse the result using Integer
2) Use the getInteger method of the TextField
3) Use the getText method of a TextBox and parse the result using the getInt method of Integer class
4) Use the getText method of a TextField and use the parseInt method of the Integer class


Answer
4: Use the getText method of a Textfield and use the parseInt method of the Integer class

Here is an example of how you might do this

Integer.parseInt(txtInputValue.getText());

I'm not sure that a question on this actually will come up in the exam but it is a very useful thing to know in the real world.

Monday, 20 July 2009

Java Certification Question 0040

Given the following code

class Base{
static int oak=99;
}

public class Doverdale extends Base{

public static void main(String argv[]){
Doverdale d = new Doverdale();
d.amethod();
}
public void amethod(){
//Here
}
}

Which of the following if placed after the comment //Here, will compile and modify the value of the variable oak?
1) super.oak=1;
2) oak=33;
3) Base.oak=22;
4) oak=50.1;



Answer :
1) super.oak=1;
2) oak=33;
3) Base.oak=22;
Because the variable oak is declared as static only one copy of it will exist. Thus it can be changed either through the name of its class or through the name of any instance of that class. Because it is created as an integer it canot be assigned a fractional component without a cast.

Sunday, 19 July 2009

Java Certification Question 0039

Given the following class definition
public class Droitwich{

class one{
private class two{
public void main(){
System.out.println("two");
}
}
}
}

Which of the following statements are true

1) The code will not compile because the classes are nested to more than one level
2) The code will not compile because class two is marked as private
3) The code will compile and output the string two at runtime
4) The code will compile without error



Answer :
4) The code will compile without error
There are no restrictions on the level of nesting for inner/nested classes. Inner classes may be marked private. The main method is not declared as public static void main, and assuming that the commandline was java Droitwich it would not be invoked anyway.

Saturday, 18 July 2009

Java Certification Question 0038

Which of the following statements are true?
1) Code must be written to cause a frame to close on selecting the system close menu
2) The default layout for a Frame is the BorderLayout Manager
3) The layout manager for a Frame cannot be changed once it has been assigned
4) The GridBagLayout manager makes extensive use of the the GridBagConstraints class.



Answer :
1) Code must be written to cause a frame to close on selecting the system close menu
2) The default layout for a Frame is the BorderLayout Manager
4) The GridBagLayout manager makes extensive use of the the GridBagConstraints class.
You can change the layout manager for a Frame or any other container whenever you like.

Friday, 17 July 2009

Java Certification Question 0037

Given the following class definition
public class Upton{

public static void main(String argv[]){

}

public void amethod(int i){}
//Here
}

Which of the following would be legal to place after the comment //Here ?
1) public int amethod(int z){}
2) public int amethod(int i,int j){return 99;}
3) protected void amethod(long l){ }
4) private void anothermethod(){}



Answer:
2) public int amethod(int i, int j) {return 99;}
3) protected void amethod (long l){}
4) private void anothermethod(){}
Option 1 will not compile on two counts. One is the obvious one that it claims to return an integer. The other is that it is effectivly an attempt to redefine a method within the same class. The change of name of the parameter from i to z has no effect and a method cannot be overriden within the same class.

Thursday, 16 July 2009

Java Certification Question 0036

Assuming any exception handling has been set up, which of the following will create an instance of the RandomAccessFile class
1) RandomAccessFile raf=new RandomAccessFile("myfile.txt","rw");
2) RandomAccessFile raf=new RandomAccessFile( new DataInputStream());
3) RandomAccessFile raf=new RandomAccessFile("myfile.txt");
4) RandomAccessFile raf=new RandomAccessFile( new File("myfile.txt"));



Answer :
1) RandomAccessFile raf=new RandomAccessFile("myfile.txt","rw");
The RandomAccessFile is an anomaly in the Java I/O architecture. It descends directly from Object and is not part of the Streams architecture.

Wednesday, 15 July 2009

Java Certification Question 0035

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

public class Borley extends Thread{

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

public void run(){
System.out.println("Running");
}
}

1) Compilation and run but no output
2) Compilation and run with the output "Running"
3) Compile time error with complaint of no Thread target
4) Compile time error with complaint of no access to Thread package



Answer :
2) Compilation and run with the output "Running"
This is perfectly legitimate if useless sample of creating an instnace of a Thread and causing its run method to execute via a call to the start method. The Thread class is part of the core java.lang package and does not need any explicit import statement. The reference to a Thread target is an attempt to mislead with a reference to the method of using the Runnable interface instead of simply inheriting from the Thread super class.

Tuesday, 14 July 2009

Java Certification Question 0034

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

import java.io.*;

class ExBase{
abstract public void martley(){
}
}

public class MyEx extends ExBase{

public static void main(String argv[]){
DataInputStream fi = new DataInputStream(System.in);
try{
fi.readChar();
}catch(IOException e){
System.exit(0);
}
finally {System.out.println("Doing finally");}
}
}

1) Compile time error
2) It will run, wait for a key press and then exit
3) It will run, wait for a keypress, print "Doing finally" then exit
4) At run and immediately exit



Answer :
1) Compile time error
It wil produce an error like "Abstract and native method can't have a body. This is typical of the more misleading question where you might think it is asking you about the circumstances under which the finally clause runs, but actually it is about something else.

Monday, 13 July 2009

Java Certification Question 0033

Which of the following statements are true?
1) Java uses a system called UTF for I/O to support international character sets
2) The RandomAccessFile is the most suitable class for supporting international character sets
3) An instance of FileInputStream may not be chained to an instance of FileOutputStream
4) File I/O activities requires use of Exception handling



Answer :
1) Java uses a system called UTF for I/O to support international character sets
3) An instance of FileInputStream may not be chained to an instance of FileOutputStream
4) File I/O activities requires use of Exception handling
Internally Java uses Unicode which are 16 bit characters. For I/O Java uses UTF which may be more thatn 16 bits per character.
Generally InputStreams can only be chained to other InputStreams and OutputStreams can only be chained to other OutputStreams. The piped streams are an exception to this.

Sunday, 12 July 2009

Java Certification Question 0032

Which of the following statements are true about a variable created with the static modifier?

1) Once assigned the value of a static variable may not be altered
2) A static variable created in a method will keep the same value between calls
3) Only one instance of a static variable will exist for any amount of class instances
4) The static modifier can only be applied to a primitive value



Answer :
3) Only one instance of a static variable will exist for any amount of class instances
Option 1) is more a description of a final variable. Option 2 is designed to fool Visual Basic programmers like me as this is how you can use the keyword static in VB. The modifier static can be applied to a class, method or variable.

Saturday, 11 July 2009

Java Certification Question 0031

Which of the following statements are true?
1) The default layout manager for an Applet is FlowLayout
2) The default layout manager for a Frame is FlowLayout
3) A layout manager must be assigned to an Applet before the setSize method is called
4) The FlowLayout manager attempts to honor the preferred size of any components



Answer :
1) The default layout manager for an Applet is FlowLayout
4) The FlowLayout manager attempts to honor the preferred size of any components
The default layout manager fror an Application is BorderLayout. An applet will use the default of FlowLayout if one is not specifically applied.

Friday, 10 July 2009

Java Certification Question 0030

Which of the following statements are true?
1) The % is used to calculate a percentage thus: 10 % 20=50
2) The / operator is used to divide one value by another
3) The # symbol may not be used as the first character of a variable
4) The $ symbol may not be used as the first character of a variable



Answer :
2) The / operator is used to divide one value by another
3) The # symbol may not be used as the first character of a variable
The % is the modulo operator and returns the remainder after a division. Thus 10 % 3=1
The $ symbol may be used as the first character of a variable, but I would suggest that it is generally not a good idea. The # symbol cannot be used anywhere in the name of a variable. Knowing if a variable can start with the # or $ characters may seem like arbitrary and non essential knowlege but questions like this do come up on the exam.

Thursday, 9 July 2009

Java Certification Question 0029

Which of the following most closely describes the process of overriding?
1) A class with the same name replaces the functionality of a class defined earlier in the hierarchy
2) A method with the same name completely replaces the functionality of a method earlier in the hierarchy
3) A method with the same name but different parameters gives multiple uses for the same method name
4) A class is prevented from accessing methods in its immediate ancestor



Answer :
2) A method with the same name completly replaces the functionality of a method earlier in the hierarchy
Option 3 is more like a description of overloading. I like to remind myself of the difference between overloading and overriding in that an overriden method is like something overriden in the road, it is squashed, flat no longer used and replaced by something else. An overloaded method has been given extra work to do (it is loaded up with work), but it is still being used in its original format. This is just my little mind trick and doesn't match to anything that Java is doing.

Wednesday, 8 July 2009

Java Certification Question 0028

You are given a class hierarchy with an instance of the class Dog. The class Dog is a child of mammal and the class Mammal is a child of the class Vertebrate. The class Vertebrate has a method called move which prints out the string "move". The class mammal overrides this method and prints out the string "walks". The class Dog overrides this method and prints out the string "walks on paws". Given an instance of the class Dog,. how can you access the ancestor method move in Vertebrate so it prints out the string "move";
1) d.super().super().move();
2) d.parent().parent().move();
3) d.move();
4) none of the above;



Answer :
4) none of the above;
You may access methods of a direct parent class through the use of super but classes further up the hierarchy are not visible.

Tuesday, 7 July 2009

Java Certification Question 0027

Given the following class
public class Ombersley{

public static void main(String argv[]){
boolean b1 = true;
if((b1 ==true) || place(true)){
System.out.println("Hello Crowle");
}
}

public static boolean place(boolean location){
if(location==true){
System.out.println("Borcetshire");
}
System.out.println("Powick");
return true;
}
}

What will happen when you attempt to compile and run it?

1) Compile time error
2) Output of "Hello Crowle"
3) Output of Hello Crowle followed by Borcetshire and Powick
4) No output



Answer :
2) Output of "Hello Crowle"
This code is an example of a short circuited operator. Because the first operand of the || (or) operator returns true Java sees no reason to evaluate the second. Whatever the value of the second the overall result will always be true. Thus the method called place is never called.

Monday, 6 July 2009

Java Certification Question 0026

Which of the following statements are true?
1) The elements in a Java array can only be of primitive types, not objects
2) Arrays are initialized to default values wherever they are created
3) An array may be dynamically resized using the setSize method
4) You can find out the size of an array using the size method



Answer :
2) Arrays are initialized to default values wherever they are created

You can find the size of an array using the length field. The method length is used to return the number of characters in a String. An array can contain elements of any type but they must all be of the same type. The size of an array is fixed at creation. If you want to change its size you can of course create a new array and assign the old one to it. A more flexible approach can be to use a collection class such as Vector.

Sunday, 5 July 2009

Java Certification Question 0025

Which of the following statements are true?
1) The following statement will produce a result of 1. System.out.println( -1 >>>2);
2) Performing an unsigned left shift (<<<) on a negative number will always produce a negative number result
3) The following statement will produce a result of zero, System.out.println(1 >>1);
4) All the integer primitives in java are signed numbers



Answer :
3) The following statement will produce a result of zero, System.out.println(1 >>1);
Although you might not know the exact result of the operation -1 >>> 2 a knowledge of the way the bits will be shifted will tell you that the result is not plus 1. (The result is more like 1073741823 ) There is no such Java operator as the unsigned left shift. Although it is normally used for storing characters rather than numbers the char Java primitive is actually an unsigned integer type.

Saturday, 4 July 2009

Java Certification Question 0024

Which of the following statements are true?
1) A method in an interface must not have a body
2) A class may extend one other class plus at most one interface
3) A class may extends at most one other class plus implement many interfaces
4) An class accesses an interface via the keyword uses



Answer :
1) A method in an interface must not have a body
3) A class may extends one other class plus many interfaces
A class accesses an interface using the implements keyword (not uses)

Friday, 3 July 2009

Java Certification Question 0023

Which of the following statements are true?
1 ) The String class is implemented as a char array, elements are addressed using the stringname[] convention
2) The + operator is overloaded for concatenation for the String class
3) Strings are a primitive type in Java and the StringBuffer is used as the matching wrapper type
4) The size of a string can be retrieved using the length property



Answer :
2) The + operator is overloaded for concatenation for the String class
In Java Strings are implemented as a class within the Java.lang package with the special distinction that the + operator is overloaded. If you thought that the String class is implemented as a char array, you may have a head full of C/++ that needs emptying. There is not "wrapper class" for String as wrappers are only for primitive types.
If you are surprised that option 4 is not a correct answer it is because length is a method for the String class, but a property for and array and it is easy to get the two confused.

Thursday, 2 July 2009

Java Certification Question 0022

Which of the following statements are true?
1) All of the variables in an interface are implicitly static
2) All of the variables in an interface are implicitly final
3) All of the methods in an interface are implicitly abstract
4) A method in an interface can access class level variables



Answer :
1) All of the variables in an interface are implicitly static
2) All of the variables in an interface are implicitly final
3) All of the methods in an interface are implictly abstract
All the variables in an interface are implicitly static and final. Any methods in an interface have no body, so may not access any type of variable

Wednesday, 1 July 2009

Java Certification Question 0021

Which of the following statements are true?
1) The default constructor has a return type of void
2) The default constructor takes a parameter of void
3) The default constructor takes no parameters
4) The default constructor is not created if the class has any constructors of its own.



Answer :
3) The default constructor takes no parameters
4) The default constructor is not created if the class has any constructors of its own.
Option 1 is fairly obviously wrong as constructors never have a return type. Option 2 is very dubious as well as Java does not offer void as a type for a method or constructor.

Tuesday, 30 June 2009

Java Certification Question 0020

Which of the following are Java keywords?
1) sizeof
2) main
3) transient
4) volatile



Answer :
3) transient
4) volatile
Option 1, sizeof is designed to catch out the C/C++ programmers. Java does not have a sizeof keyword as the size of primitives should be consistent on all Java implementations. Although a program needs a main method with the standard signature to start up it is not a keyword. The real keywords are less commonly used and therefore might not be so familiar to you.

Monday, 29 June 2009

Java Certification Question 0019

Which of the following are true?
1) A component may have only one event listener attached at a time
2) An event listener may be removed from a component
3) The ActionListener interface has no corresponding Adapter class
4) The processing of an event listener requires a try/catch block



Answer :
2) An event listener may be removed from a component
3) The ActionListener interface has no corresponding Adapter class
A component may have multiple event listeners attached. Thus a field may need to respond to both the mouse and the keyboard, requiring multiple event handlers. The ActionListener has not matching Adapter class because it has only one method, the idea of the Adapter classes is to eliminate the need to create blank methods.

Sunday, 28 June 2009

Java Certification Question 0018

Which of the following will compile without error?
1)
char c='1';
System.out.println(c>>1);
2)
Integer i=Integer("1");
System.out.println(i>>1);
3)
int i=1;
System.out.println(i<<<1);
4)
int i=1;
System.out.println(i<<1);



Answer :
1)
char c='1';
System.out.println(c>>1);
4)
int i=1;
System.out.println(i<<1);
Be aware that Integer (not the upper case I) is a wrapper class and thus cannot be treated like a primitive. The fact that option 1 will compile may be a surprise, but although the char type is normally used to store character types, it is actually an unsigned integer type. The reason option 3 does not compile is that Java has a >>> operator but not a <<< operator.

Saturday, 27 June 2009

Java Certification Question 0017

Which of the following statements are true?
1) static methods do not have access to the implicit variable called this
2) A static method may be called without creating an instance of its class
3) A static method may not be overriden to be non-static
4) A static method may not be overloaded



Answer :
1) static methods do not have access to the implicit variable called this
2) A static method may be called without creating an instance of its class
3) a static may not be overriden to be non-static
The implicit variable this refers to the current instance of a class and thus and by its nature a static method cannot have access to it.

Friday, 26 June 2009

Java Certification Question 0016

What will happen when you attempt to compile and run the following class?
class Base{
Base(int i){
System.out.println("Base");
}
}

class Severn extends Base{
public static void main(String argv[]){
Severn s = new Severn();
}
void Severn(){
System.out.println("Severn");
}
}
1) Compilation and output of the string "Severn" at runtime
2) Compile time error
3) Compilation and no output at runtime
4) Compilation and output of the string "Base"



Answer :
2) Compile time error
An error occurs when the class Severn attempts to call the zero parameter constructor in the class Base Because the Base class has an integer constructor Java does not provide the "behind the scenes" zero parameter constructor.

Thursday, 25 June 2009

Java Certification Question 0015

Objective 6.2)
Which of the following statements are true?
1) Constructors cannot have a visibility modifier
2) Constructors can be marked public and protected, but not private
3) Constructors can only have a primitive return type
4) Constructors are not inherited



Answer :
4) Constructors are not inherited
Constructors can be marked public, private or protected. Constructors do not have a return type.

Wednesday, 24 June 2009

Java Certification Question 0014

Objective 1.2)
Which of the following are valid methods?
1) public static native void amethod(){}
2) public static void amethod(){}
3) private protected void amethod(){}
4) static native void amethod();



Answer :
2) public static void amethod(){}
4) static native void amethod();
Option 1 is not valid because it has braces and the native modifier means that the method can have no body. This is because the body must be implemented in some other language (often C/C++). Option 3 is not valid because private and protected contradict themselves.

Tuesday, 23 June 2009

Java Certification Question 0013

Which of the following statements are true?
1) If a class has any abstract methods it must be declared abstract itself.
2) All methods in an abstract class must be declared as abstract
3) When applied to a class, the final modifier means it cannot be sub-classed
4) transient and volatile are Java modifiers



Answer :
1) If a class has any abstract methods it must be declared abstract itself.
3) The final modifier means that a class cannot be sub-classed
4) transient and volatile are Java modifiers
An abstract class may have non abstract methods. Any class that descends from an abstract class must implement the abstract methods of the base class or declare them as abstract itself.

Monday, 22 June 2009

Java Certification Question 0012

You have a public class called myclass with the main method defined as follows
public static void main(String parm[]){
System.out.println(parm[0]);
}
If you attempt to compile the class and run the program as follows
java myclass hello
What will happen?
1) Compile time error, main is not correctly defined
2) Run time error, main is not correctly defined
3) Compilation and output of java
4) Compilation and output of hello


Answer :
4) Compilation and output of hello
This type of question is particularly calculated to catch out C/C++ programmers who might expect parameter zero to be the name of the compiler.

Sunday, 21 June 2009

Java Certification Question 0011

What will happen when you attempt to compile and run the following code
class Base{
public void Base(){
System.out.println("Base");
}
}
public class In extends Base{
public static void main(String argv[]){
In i=new In();
}
}

1) Compile time error Base is a keyword
2) Compilation and no output at runtime
3) Output of Base
4) Runtime error Base has no valid constructor



Answer :
2) Compilation and no output at runtime
Because the method in Base called Base has a return type it is not a constructor and there for does not get called on creation of an instance of its child class In

Saturday, 20 June 2009

Java Certification Question 0010

Which of the following statements are true?
1) A byte can represent between -128 to 127
2) A byte can represent between -127 to 128
3) A byte can represent between -256 to 256
4) A char can represent between -2x2 pow 16 2 x2 pow 16 - 1


Answer :
1) A byte can represent between -128 to 127
The char type is the only unsigned type in Java and thus cannot represent a negative number.

Friday, 19 June 2009

Java Certification Question 0009

Which of the following are methods of the Runnable interface
1) run
2) start
3) yield
4) stop


Answer :
1) The Runnable interface has only one method run that needs to be created in any class that implements it. The start method is used to actually call and start the run method executing.

Thursday, 18 June 2009

Java Certification Question 0008

Which of the following are valid statements
1) public class MyCalc extends Math
2) Math.max(s);
3) Math.round(9.99,1);
4)Math.mod(4,10);


Answer :
Objective 9.1)
None of these are valid statements. The Math class is final and cannot be extended. The max method takes two parameters, round only takes one parameter and there is no mod parameter. You may get questions in the exam that have no apparently correct answer. If you are absolutely sure this is the case, do not check any of the options.

Wednesday, 17 June 2009

Java Certification Question 0007

Which of the following statements are true?
1) An interface can only contain method and not variables
2) Interfaces cannot have constructors
3) A class may extend only one other class and implement only one interface
4) Interfaces are the Java approach to addressing its lack of multiple inheritance, but require implementing classes to create the functionality of the Interfaces.

Answer:
2) Interfaces cannot have constructors
If you try to create a constructor for an Interface the compiler will give you an error message something like "interface can't have constructors".
4) Interfaces are the Java approach to addressing the single inheritance model, but require implementing classes to create the functionality of the Interfaces.
An interface may contain variables as well as methods. However any variables are final by default and must be assigned values on creation. A class can only extend one other class (single inheritance) but may implement as many interfaces as you like (or is sensible).

Tuesday, 16 June 2009

Java Certification Question 0006

Which of the following statements are true?
1) The instanceof operator can be used to determine if a reference is an instance of a class, but not an interface.
2) The instanceof operator can be used to determine if a reference is an instance of a particular primitive wrapper class
3) The instanceof operator will only determine if a reference is an instance of a class immediately above in the hierarchy but no further up the inheritance chain
4) The instanceof operator can be used to determine if one reference is of the same class as another reference thus



Answer:
2) The instanceof operator can be used to determine if a reference is an instance of a particular primitive wrapper class
The instanceof operator can only be used to make a static comparison with a class type. Java1.1 added the isInstance method to the class Class to allow you to dynamically determine a class type. The exam does not test you on isInstance.

Monday, 15 June 2009

Java Certification Question 0005

Which of the following are true statements?
1) I/O in Java can only be performed using the Listener classes
2) The RandomAccessFile class allows you to move directly to any point a file.
3) The creation of a named instance of the File class creates a matching file in the underlying operating system only when the close method is called.
4) The characteristics of an instance of the File class such as the directory separator, depend on the current underlying operating system



Answer:
(Not on the official sub objectives but this topic does come up on the exam)
2) The RandomAccessFile class allows you to move directly to any point a file.
4) The characteristics of an instance of the File class such as the directory separator, depend on the current underlying operating system
The File class can be considered to represent information about a file rather than a real file object. You can create a file in the underlying operating system by passing an instance of a file to a stream such as FileOutputStream. The file will be created when you call the close method of the stream.

Sunday, 14 June 2009

Java Certification Question 0004

Which of the following statements are true?
1) The garbage collection algorithm in Java is vendor implemented
2) The size of primitives is platform dependent
3) The default type for a numerical literal with decimal component is a float.
4) You can modify the value in an Instance of the Integer class with the setValue method



Answer:
1) The garbage collection algorithm in Java is vendor implemented. Threading and garbage collection are two of the few areas that are platform dependent. This is one of the reasons why Java is not suitable for realtime programming. It is not a good idea use it to control your plane or nuclear power station. Once an instance of the Integer class has a value it cannot be changed.