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