Navigation |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Topic: Declarations and access control
Question #61 |
|
What will be the result when you attempt to compile this program? |
public class Rand
{
public static void main(String argv[])
{
int iRand;
iRand = Math.random();
System.out.println(iRand);
}
}
|
- Compile time error referring to a cast problem
- A random number between 1 and 10
- A random number between 0 and 1
- A compile time error about random being an unrecognised method
|
Click here to see the answer
|
The Correct Answer is 1: Compile time error referring to a cast problem
|
Click here to see explanation
|
This is a bit of a sneaky one as the Math.random method returns a pseudo random number between 0 and 1, and thus option 3 is a plausible Answer. However the number returned is a double and so the compiler will complain that a cast is needed to convert a double to an int.
|
Question #62 |
|
Given the following code, What code would be most likely for the body of the ioCall method |
- public void ioCall ()throws IOException{
DataInputStream din = new DataInputStream(System.in);
din.readChar();
- public void ioCall ()throw IOException{
DataInputStream din = new DataInputStream(System.in);
din.readChar();
- public void ioCall (){
DataInputStream din = new DataInputStream(System.in);
din.readChar();
}
- public void ioCall throws IOException(){
DataInputStream din = new DataInputStream(System.in);
din.readChar();
}
|
Click here to see the answer
|
The Correct Answer is 1: public void ioCall ()throws IOException{
DataInputStream din = new DataInputStream(System.in);
din.readChar();
|
Click here to see explanation
|
If a method might throw an exception it must either be caught within the method with a try/catch block, or the method must indicate the exception to any calling method by use of the throws statement in its declaration. Without this, an error will occur at compile time.
|
Question #63 |
|
What will happen when you compile and run the following code? |
public class Scope
{
private int i;
public static void main(String argv[])
{
Scope s = new Scope();
s.amethod();
}//End of main
public static void amethod()
{
System.out.println(i);
}//end of amethod
}//End of class
|
- A value of 0 will be printed out
- Nothing will be printed out
- A compile time error
- A compile time error complaining of the scope of the variable i
|
Click here to see the answer
|
The Correct Answer is 3: A compile time error
|
Click here to see explanation
|
Because only one instance of a static method exists not matter how many instance of the class exists it cannot access any non static variables. The JVM cannot know which instance of the variable to access. Thus you will get an error saying something like
Can't make a static reference to a non static variable
|
Question #64 |
|
Which of the following can you perform using the File class? |
- Change the current directory
- Return the name of the parent directory
- Delete a file
- Find if a file contains text or binary information
|
Click here to see the answer
|
The Correct Answer is 2: Return the name of the parent directory 3: Delete a file
|
Click here to see explanation
|
It is surprising that you can't change the current directory. It is not so surprising that you can't tell if a file contains text or binary information.
|
Question #65 |
|
Which statement is true of the following code? |
public class Rpcraven
{
public static void main(String argv[])
{
Pmcraven pm1 = new Pmcraven("One");
pm1.run();
Pmcraven pm2 = new Pmcraven("Two");
pm2.run();
}
}
class Pmcraven extends Thread
{
private String sTname="";
Pmcraven(String s)
{
sTname = s;
}
public void run()
{
for(int i =0; i < 2 ; i++)
{
try
{
sleep(1000);
}
catch(InterruptedException e){}
yield();
System.out.println(sTname);
}
}
}
|
- Compile time error, class Rpcraven does not import java.lang.Thread
- Output of One One Two Two
- Output of One Two One Two
- Compilation but no output at runtime
|
Click here to see the answer
|
The Correct Answer is 2: Output of One One Two Two
|
Click here to see explanation
|
Answer 3 would would be true if the code called the start method instead of the run method (well it is on my Windows machine anyway, I'm not sure it would be for ever implementation of Java Threads). If you call the run method directly it just acts as any other method and does not return to the calling code until it has finished executing.
|
Question #66 |
|
You want to ensure that the Java Virtual Machine will run its garbage collection just before you start a complex routine. What can you do to be certain that garbage collection will run when you want . |
- You cannot be certain when garbage collection will run
- Use the Runtime.gc() method to force garbage collection
- Ensure that all the variables you require to be garbage collected are set to null
- Use the System.gc() method to force garbage collection
|
Click here to see the answer
|
The Correct Answer is 1: You cannot be certain when garbage collection will run
|
Click here to see explanation
|
Although there is a Runtime.gc(), this only suggests that the Java Virtual Machine does its garbage collection. You can never be certain when the garbage collector will run. Roberts and Heller is more specific abou this than Boone. This uncertainty can cause consternation for C++ programmers who wish to run finalize methods with the same intent as they use destructor methods.
|
Question #67 |
|
Which of the following most closely describes a bitset collection? |
- A class that contains groups of unique sequences of bits
- A method for flipping individual bits in instance of a primitive type
- An array of boolean primitives that indicate zeros or ones
- A collection for storing bits as on-off information, like a vector of bits
|
Click here to see the answer
|
The Correct Answer is 4: A collection for storing bits as on-off information, like a vector of bits
|
Click here to see explanation
|
This is the description given to a bitset in Bruce Eckels "Thinking in Java" book. The reference to unique sequence of bits was an attempt to mislead because of the use of the word Set in the name bitset. Normally something called a set implies uniqueness of the members, but not in this context.
|
Question #68 |
|
You have these files in the same directory. What will happen when you attempt to compile and run Class1.java if you have not already compiled Base.java |
//Base.java
package Base;
class Base
{
protected void amethod()
{
System.out.println("amethod");
}//End of amethod
}//End of class base
package Class1;
//Class1.java
public class Class1 extends Base
{
public static void main(String argv[])
{
Base b = new Base();
b.amethod();
}//End of main
}//End of Class1
|
- Compile Error: Methods in Base not found
- Compile Error: Unable to access protected method in base class
- Compilation followed by the output "amethod"
- Compile error: Superclass Class1.Base of class Class1.Class1 not found
|
Click here to see the answer
|
The Correct Answer is 4: Compile error: Superclass Class1.Base of class Class1.Class1 not found
|
Click here to see explanation
|
Using the package statement has an effect similar to placing a source file into a different directory. Because the files are in different packages they cannot see each other. The stuff about File1 not having been compiled was just to mislead, java has the equivalent of an "automake", whereby if it was not for the package statements the other file would have been automatically compiled.
|
Question #69 |
|
What will happen when you attempt to compile and run the following code? |
class Base
{
private void amethod(int iBase)
{
System.out.println("Base.amethod");
}
}
class Over extends Base
{
public static void main(String argv[])
{
Over o = new Over();
int iBase=0;
o.amethod(iBase);
}
public void amethod(int iOver)
{
System.out.println("Over.amethod");
}
}
|
- Compile time error complaining that Base.amethod is private
- Runtime error complaining that Base.amethod is private
- Output of "Base.amethod"
- Output of "Over.amethod"
|
Click here to see the answer
|
The Correct Answer is 4: Output of "Over.amethod"
|
Click here to see explanation
|
The names of parameters to an overridden method is not important, but as the version of amethod in class Base is set to be private it is not visible within Over (despite Over extending Base) and thus does not take part in overriding.
|
Question #70 |
|
The following code will print |
if( new Boolean("true") == new Boolean("true"))
System.out.println("True");
else
System.out.println("False");
|
- Compilation error.
- No compilation error, but runtime exception.
- Prints "True".
- Prints "False".
|
Click here to see the answer
|
The Correct Answer is 4: Prints "False".
|
Click here to see explanation
|
No explanation available.
|
|
|
|
|
|
|
|
Today, there have been 1 visitors (1 hits) on this page! |
|
|
|
|
|
|
|