Thread Exceptions


 

Q 1)Which of the following require explicit try/catch exception handling by the programmer?

 

(1)

Traversing each member of an array

(2)

Attempting to open a file

(3)

Attempting to open a network socket

(4)

Accessing a method in other clas

2)

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

public class Master{
    boolean bContinue=false;
    public static void main(String argv[]){
            Master m = new Master();
            m.go();
    }
    public void go(){
            Slave s = new Slave(this);
            Thread t1 = new Thread(s);
            t1.start();
            while(bContinue==false){
            }
            s.setPrice(200);
    }
}

class Slave implements Runnable{
    int iPrice =100;
    Master master;
    Slave(Master m){
            master=m;
    }
    synchronized public void setPrice(int iM){
            iPrice=iM;     
    }

    synchronized public void run(){
            master.bContinue=true;
            while(true){
                System.out.println(iPrice);
            }
    }
}

 

(1)

Compilation but no output as the run method of slave is not correct

(2)

Compilation and output of 100 several times followed by 200 several times

(3)

Compilation and repeated output of 100

(4)

Compile time error, while cannot be given an unconditional boolean value

3)

 

What will be the result when you attempt to compile this program?


public class RandomExample{
    public static void main(String argv[]){
        int iRand;
        iRand = Math.random();
        System.out.println(iRand);
    }
}

 

(1)

Compile time error referring to a cast problem

(2)

A random number between 1 and 10

(3)

A random number between 0 and 1

(4)

A compile time error about random being an unrecognised method

 

 

Q4)

Which of the following statements are true?

 

(1)

The vaue of the Integer class can be changed using the = operator

(2)

The value of the Integer class can be changed using the setValue method

(3)

The value of the Integer class can be changed using the setInt method

(4)

Once assigned the value of an instance of Integer cannot be changed

 

 

 

Answer : -------------------------

Q5)

 

Which of the following statements are true?

(1)

A variable declared as public within a method will always be visible from code anywhere else in the class

(2)

A variable declared as public at class level will always be visible from code anywhere else in the class

(3)

A method with no visibility modifier can be less visible than one declared with the protected modifier

(4)

Only one copy will ever exist of a method variable declared as static

 

 

Q6)

 

At what point will the object created on line 8 be eligible for garbage collection?


1 public class RJMould{
2     StringBuffer sb;
3     public static void main(String argv[]){
4          RJMould rjm = new RJMould();
5          rjm.kansas();
6    }
7     public void kansas(){
8          sb = new StringBuffer("Manchester");
9          StringBuffer sb2 = sb;
10        StringBuffer sb3 = new StringBuffer("Chester");
11        sb=sb3;
12        sb3=null;
13        sb2=null;
14     }
15 }

(1)

Line 11

(2)

Line 9

(3)

Line 12

(4)

Line 13

 

 

 

 

Q7)

 

An ArithmeticException is a checked exception. True Or False?

(1)

True

(2)

False

 

 

Q8)

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


class TSamp extends Thread {
    public native String getTime(); 
}

public class Multi implements Runnable {
    boolean bStop;
    public static void main(String argv[]){
            Multi m = new Multi();
            m.go();
    }
    public void go(){
            TSamp ts = new TSamp(this);
            ts.start();
            bStop=true;
           
    }
    public void run(){
            if(bStop==true){
                return;
            }
            System.out.println("running");
    }
}

 

(1)

Compilation, but output at runtime will cannot be exactly determined

(2)

Compilation and output of "running"

(3)

Compilation but no output at runtime

(4)

Compile time error

 

 

 

Answer : -------------------------

Q9

class A extends Thread {
  public void run() {
    try {
      synchronized (this) {
        wait();
      }
    } catch (InterruptedException ie) {
      System.out.print(interrupted());
    }
  }
  public static void main(String[] args) {
    A a1 = new A();
    a1.start();
    a1.interrupt();
  }
}

What are the possible results of attempting to compile and run the program?

 

(1)

Prints: true

(2)

Prints: false

(3)

Compiler error

(4)

Run time error

(5)

None of the above

 

 

 

Answer : -------------------------

Q10)    Read the code below carefully.
  if( "String".endsWith(""))
    Sytem.out.println("True");
else
    System.out.println("False");

   The code produces

  1. True
  2. False

Q11) Read the code below carefully.
      public class TestClass
    {
        public static void main(String Args[])
        {
            StringBuffer sb1 = new StringBuffer("String");
            StringBuffer sb2 = new StringBuffer("String");
            if(sb1.equals(sb2))
            {
                //lots of code
            }
         }
    }

12)Which of the following is the direct base class of java.awt.AWTEvent.
 

  1. java.lang.Object.
  2. java.util.EventObect

Q13) Interface methods can be declared with the following modifiers

  1. public
  2. none (i.e., no access modifier).
  3. private.
  4. static
  5. native
  6. synchronized.

Q14) Read the following code carefully.

public class AStringQuestion
{
    static String s1;
    static String s2;

    public static void main(String args[])
    {
         s2 = s1+s2;

        System.out.println(s2);
    }
}

    Attempting to compile and run the code
  Will cause a compilation error.

  1. Runtime Execption - NullPointerException in the 2nd line of the main method.
  2. Will compile successfully and print nullnull on the screen.
  3. Will compile successfully and print an empty line on the screen.
  4. Will compile successfully and print nothing on the screen.

Q15) Read the following piece of code carefully

public abstract class AbstractClass
{
     public AbstractClass()
     {
          System.out.println("this is an abstract class constructor!");
     }

     public void aMethod()
     {
          System.out.println("This is in the method in the abstract class");
     }

}

Attempting to compile and run the code will cause

  1. Compiler error - abstract classes cannot have constructors.
  2. Compiler error - the method AbstractClass does not have a valid return type.
  3. Compiler error - the class cannot be declared as abstract as it does not have any unimplemented methods.
  4. No compiler error - the class is practically not an abstract class and can be instantiated.
  5. No compiler error - the class cannot be instantiated directly. It has to be extended to an non-abstract class. The constructors of the extended class will call the constructor of the abstract class (implicitly or explicitly).
  6. No compiler error - the class cannot be instantiated directly. It has to be extended to an non-abstract class. The constructors of the abstract class will never be called.

Q16) Read the following piece of code carefully.
 
class Base
{
    Base()
    {
        System.out.println(“Message 1 : In the base class constructor”);
    }
}

abstract class Derived1 extends Base
{
    Derived1()
    {
        System.out.println(“Message 2 : In the abstract class Derived1\’s constructor”);
    }
}

public class Derived2 extends Derived1
{
    public Derived2()
    {
        System.out.println(“Message 3 : In the derived2 class\’s constructor.”);
    }
 
    public static void main(String args[])
    {
        Derived2 d2 = new Derived2();
    }
}

An attempt to compile and run the above code
 

  1. will cause a compiler error. The non-abstract classes cannot be extended to the abstract classes.
  2. will cause a compiler error.  The abstract classes cannot have constructors defined.
  3. will not cause any compiler error. The lines “Message 1…” and “Message 3 … “ are printed on the screen.
  4. will not cause any compiler error. The lines “Message 1…” and “Message 2….” and Message 3….” are printed on the screen.