Saturday, February 9, 2008

Practice Questions

SCJP/OCPJP Practice Exam 1
Question 1
Consider the following classes:
<pre class="brush: java">
class Animal extends Object {
public boolean ok = true;
 }


class Horse extends Animal {
public boolean ok = false;
 }


public class Oharada {
  public static void main( String args[] ) {
    Horse palm = new Horse;
   Animal vap = palm;
   System.out.println( vap.ok );
}
 }
</pre>


What will be the result of trying to compile and execute the class?
a.Some address in memory will be printed to the standard output.
b.true will be printed to the standard output.
c.false will be printed to the standard output.
d.The compiler will object line 8.
e.A runtime exception will be thrown in the main method.


Question 2
Which of the following are interfaces?
a.java.util.List
b.java.util.TreeMap
c.java.util.AbstractList
d.java.util.SortedMap
e.java.util.Iterator


Question 3
If an instance of the class File is created in order to represent a file, which isnt exist, the file will be created.
a.True.
b.False.


Question 4
Consider the following Animal class:
<pre class="brush: java">
  public class Animal {
    static String[] nameList={ McNealy , Gates , Thompson , Ritchie };
    public static int findName( String name ) {
             for(int i=0; i<nameList.length; i++) 
                   if (name.equals( nameList[i] )) 
break;
                  return i;
}
   public static void main( String args[] ) {
               System.out.print( findName( "Zindell" ));
    }
 }
</pre>
What will be the result of trying to compile and execute the class?
a."3" will be printed to the standard output.
b.A runtime exception will be thrown in the main method.
c.A compiler error will prevent compilation.
d.The program will run, but nothing will be produced to the standard output.
e.None of above.


Question 5
Which method is in charge for searching in memory for pre-existing String objects with similar text?


a.String.intern()
b.String.equals()
c.StringNameHelper.id()
d.Runtime.freeMemory()
e.MemoryHandler.push()


Question 6
Which of the following anonymous inner classes is written correctly?
<pre class="brush: java">
a.button.addActionListener(
    new ActionListener(){
      public void actionPerformed(ActionEvent evt){
           doAction();
     }
});


b.button.addActionListener(
     new Object implements ActionListener(){
       public void actionPerformed(ActionEvent evt) {
          doAction();}
});


c.button.addActionListener(
new ActionListener(){
doAction();
} );


d.button.addActionListener(
ActionListener Animal = new ActionListener(){
public void actionPerformed(ActionEvent evt){
doAction();}
} );


e.button.addActionListener(
new Animal extends Object implements ActionListener(){
public void actionPerformed(ActionEvent evt){
doAction();
}
} );
</pre>


Question 7
Consider the following hierarchy of classes:
Exception
|
+--RuntimeException

+--BadException
|
+--VeryBadException
|
+--AwfulException
|
+--DisastrousException


Suppose that you have a method X that could throw both awful and disastrous exceptions. Assuming that X does not have try-catch statements, which of the following statements are correct?
a.The declaration of X must include throws AwfulException, DisastrousException.
b.If a method calling X catches VeryBadException, both awful and disastrous exceptions are caught.
c.If the declaration of X includes throws VeryBadException, any calling method must use a try-catch block.
d.The declaration for X does not have to mention exceptions.
e.The declaration for X has to mention at least one of the exceptions in given hierarchy.


Question 8
In order to create an Applet, the programmer must derive its class from java.applet.Applet. Which of the following methods expects the JVM to find when creates an instance of this class?
a.run()
b.init()
c.stop()
d.pause()
e.start()


Question 9
Consider the following classes:
<pre class="brush: java">
class Animal{
private String name;
private int age;
public Animal( String name , int age ){
this.name=name;
this.age=age;
}
}


 public class Oharada extends Animal {
private boolean nationality;
public Oharada ( String name , int age , boolean  nationality ) {
this.nationality=nationality;
super( name , age );
}
 public static void main( String args[] ) {
Animal oh = new Oharada( Zaharya , 118 ,  true );
System.out.print( oh.nationality );
   }
}
</pre>


What will be the result of trying to compile and execute the class?
a."true" will be printed to the standard output.
b.A runtime exception will be thrown in the main method.
c.A compiler will object line 16.
d.A compiler will object line 21.
e.A compiler will search for Animal() constructor and wont find it.


Question 10
Consider the following code:
1 byte b = 0;
2 b += 27;
3 b = 0;
4 b = b + 27;


Line 2 is interchangeable with line 4.
a.True.
b.False.


Question 11
Consider the following code:


<pre class="brush: java">
final Boolean flag = new Boolean( true );
final boolean flaggy = false;
</pre>


Which of the following statements are false?
a.The value that object, which reference is stored in flag variable, contains can not be changed.
b.The reference, stored in flag variable, can be changed.
c.The flaggy variable can not be stored in a Vector, while the flag can.
d.The check ( flaggy || !flag ) is legal.
e.When you want to convert boolean to String you have to use a Boolean object.




Question 12
Consider the following Animal class:


<pre class="brush: java">
public class Animal{
 public static void main(String args[]){ 
byte b = 3;
Byte bb = new Byte( b );
byte c = 3;
Byte cc = new Byte( c );
boolean r = ( b == c );
Boolean rr = new Boolean( bb == cc );
System.out.print( r );
System.out.print(   );
System.out.println( rr );
   }
}
</pre>
What will be result of the execution?
a.The program will produce true true to the standard output.
b.A compiler error will prevent compilation, objecting line 13.
c.The program will produce true false to the standard output.
d.A runtime exception will be thrown in the main method.
e.A compiler error will prevent compilation, objecting line 10.






Question 13
Here is a method that takes an array of Object references, adds them to a Vector, and calls the action method on the Vector (assume that the action method does not keep a reference to any of the objects involved):


<pre class="brush :java">
 public void Animal( Object[] arr ) {
    Vector vec = new Vector();
for(int i=0; i<arr.length; i++) {
vec.addElement( arr[ I ] );
}
    action(vec);
 }
 </pre>


Which of the following statements are correct?
a.The Vector object can be garbage collected after line 9.
b.The array can be garbage collected after line 9.
c.The content of the array can be garbage collected after line 9.
d.The references from array prevent the Vector from being garbage collected after line 9.
e.It is enough to add a reference to Vector to the array to prevent them both from being garbage collected after line 9.


Question 14
Regarding a method which throws exceptions, which of the following statements about its overriding methods are true?
a.An overriding method may legitimately throw fewer exceptions than its original, but it may not throw more. 
b.An overriding method must be able to throw exactly the same number of exceptions as the original one.
c.An overriding method may throw and exception that is a subclass of the original.
d.An overriding method must be able to throw exactly the same exceptions as the original one.
e.An overriding method does not have to throw exceptions at all.


Q)15
Which is the right hierarchy of RandomAccessFile?
a.java.Object
|
+--java.io.File
|
+--java.io.RandomAccessFile
b.java.Object
|
+--java.io.DataInput
|
+--java.io.RandomAccessFile


c.java.Object
|
+--java.io.RandomAccessFile


d.java.Object
|
+--java.io.DataInputStream
|
+--java.io.RandomAccessFile
e.java.Object
|
+--java.io.Reader
|
+--java.io.RandomAccessFile