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.