Exception Handling

    1) What is the difference between ‘throw’ and ‘throws’ ?And it’s application?

        Ans : Exceptions that are thrown by java runtime systems can be handled by Try and catch blocks. With throw exception we can handle the exceptions thrown by the program itself. If a method is capable of causing an exception that it does not

        handle, it must specify this behavior so the callers of the method can guard

        against that exception.

    2) What is the difference between ‘Exception’ and ‘error’ in java?

        Ans : Exception and Error are the subclasses of the Throwable class. Exception class is used for exceptional conditions that user program should catch. With exception class we can subclass to create our own custom exception.

        Error defines exceptions that are not excepted to be caught by you program. Example is Stack Overflow.

    3) What is ‘Resource leak’?

        Ans : Freeing up other resources that might have been allocated at the beginning of a method.

    4)What is the ‘finally’ block?

        Ans : Finally block will execute whether or not an exception is thrown. If an exception is thrown, the finally block will execute even if no catch statement match the exception. Any time a method is about to return to the caller from inside try/catch block, via an uncaught exception or an explicit return statement, the finally clause is also execute.

    5) Can we have catch block with out try block? If so when?

        Ans : No. Try/Catch or Try/finally form a unit.

    6) What is the difference between the following statements?

        Catch (Exception e),

        Catch (Error err),

        Catch (Throwable t)

        Ans :

    7) What will happen to the Exception object after exception handling?

        Ans : It will go for Garbage Collector. And frees the memory.

    8) How many Exceptions we can define in ‘throws’ clause?

        Ans : We can define multiple exceptions in throws clause.

        Signature is..

        type method-name (parameter-list) throws exception-list

    9) The finally block is executed when an exception is thrown, even if no catch matches it.

        True/False

        Ans : True

    10) The subclass exception should precede the base class exception when used within the catch clause.

        True/False

        Ans : True

    11) Exceptions can be caught or rethrown to a calling method.

        True/False

        Ans : True

    12) The statements following the throw keyword in a program are not executed.

        True/False

        Ans : True

    13) The toString ( ) method in the user-defined exception class is overridden.

        True/False

Multi Threading

    1) What are the two types of multitasking?

        Ans :

            1.process-based

            2.Thread-based

    2) What are the two ways to create the thread?

        Ans :

            1.by implementing Runnable

            2.by extending Thread

    3) What is the signature of the constructor of a thread class?

        Ans : Thread(Runnable threadob,String threadName)

    4) What are all the methods available in the Runnable Interface?

        Ans : run()

    5) What is the data type for the method isAlive() and this method is available in which class?

        Ans : boolean, Thread

    6) What are all the methods available in the Thread class?

        Ans :

        1.isAlive()

        2.join()

        3.resume()

        4.suspend()

        5.stop()

        6.start()

        7.sleep()

        8.destroy()

    7) What are all the methods used for Inter Thread communication and what is the class in which these methods are defined?

        Ans :

        1. wait(),notify() & notifyall()

        2. Object class

    8) What is the mechanisam defind by java for the Resources to be used by only one Thread at a time?

        Ans : Synchronisation

    9) What is the procedure to own the moniter by many threads?

        Ans : not possible

    10) What is the unit for 1000 in the below statement?

        ob.sleep(1000)

        Ans : long milliseconds

    11) What is the data type for the parameter of the sleep() method?

        Ans : long

    12) What are all the values for the following level?

        max-priority

        min-priority

        normal-priority

        Ans : 10,1,5

    13) What is the method available for setting the priority?

        Ans : setPriority()

    14) What is the default thread at the time of starting the program?

        Ans : main thread

    15) The word synchronized can be used with only a method.

        True/ False

        Ans : False

    16) Which priority Thread can prompt the lower primary Thread?

        Ans : Higher Priority

    17) How many threads at a time can access a monitor?

        Ans : one

    18) What are all the four states associated in the thread?

        Ans : 1. new 2. runnable 3. blocked 4. dead

    19) The suspend()method is used to teriminate a thread?

        True /False

        Ans : False

    20) The run() method should necessary exists in clases created as subclass of thread?

        True /False

        Ans : True

    21) When two threads are waiting on each other and can't proceed the programe is said to be in a deadlock?

        True/False

        Ans : True

    22) Which method waits for the thread to die ?

        Ans : join() method

    23) Which of the following is true?

        1) wait(),notify(),notifyall() are defined as final & can be called only from with in a synchronized method

        2) Among wait(),notify(),notifyall() the wait() method only throws IOException

        3) wait(),notify(),notifyall() & sleep() are methods of object class

            1
            2
            3
            1 & 2
            1,2 & 3

        Ans : D

    24) Garbage collector thread belongs to which priority?

        Ans : low-priority

    25) What is meant by timeslicing or time sharing?

        Ans : Timeslicing is the method of allocating CPU time to individual threads in a priority schedule.

    26) What is meant by daemon thread? In java runtime, what is it's role?

        Ans : Daemon thread is a low priority thread which runs intermittently in the background doing the garbage collection operation for the java runtime system.

    Inheritance

    1) What is the difference between superclass & subclass?

        Ans : A super class is a class that is inherited whereas subclass is a class that does the inheriting.

    2) Which keyword is used to inherit a class?

        Ans : extends

    3) Subclasses methods can access superclass members/ attributes at all times?

        True/False

        Ans : False

    4) When can subclasses not access superclass members?

        Ans : When superclass is declared as private.

    5) Which class does begin Java class hierarchy?

        Ans : Object class

    6) Object class is a superclass of all other classes?

        True/False

        Ans : True

    7) Java supports multiple inheritance?

        True/False

        Ans : False

    8) What is inheritance?

        Ans : Deriving an object from an existing class. In the other words, Inheritance is the process of inheriting all the features from a class

    9) What are the advantages of inheritance?

        Ans : Reusability of code and accessibility of variables and methods of the superclass by subclasses.

    10) Which method is used to call the constructors of the superclass from the subclass?

        Ans : super(argument)

    11) Which is used to execute any method of the superclass from the subclass?

        Ans : super.method-name(arguments)

    12) Which methods are used to destroy the objects created by the constructor methods?

        Ans : finalize()

    13) What are abstract classes?

        Ans : Abstract classes are those for which instances can’t be created.

    14) What must a class do to implement an interface?

        Ans: It must provide all of the methods in the interface and identify the interface in its implements clause.

    15) Which methods in the Object class are declared as final?

        Ans : getClass(), notify(), notifyAll(), and wait()

    16) Final methods can be overridden.

        True/False

        Ans : False

    17) Declaration of methods as final results in faster execution of the program?

        True/False

        Ans: True

    18) Final variables should be declared in the beginning?

        True/False

        Ans : True

    19) Can we declare variable inside a method as final variables? Why?

        Ans : Cannot because, local variable cannot be declared as final variables.

    20) Can an abstract class may be final?

        Ans : An abstract class may not be declared as final.

    21) Does a class inherit the constructors of it's super class?

        Ans: A class does not inherit constructors from any of it's super classes.

    22) What restrictions are placed on method overloading?

        Ans: Two methods may not have the same name and argument list but different return types.

    23) What restrictions are placed on method overriding?

        Ans : Overridden methods must have the same name , argument list , and return type. The overriding method may not limit the access of the method it overridees.The overriding method may not throw any exceptions that may not be thrown by the overridden method.

    24) What modifiers may be used with an inner class that is a member of an outer class?

        Ans : a (non-local) inner class may be declared as public, protected, private, static, final or abstract.

    25) How this() is used with constructors?

        Ans: this() is used to invoke a constructor of the same class

    26) How super() used with constructors?

        Ans : super() is used to invoke a super class constructor

    27) Which of the following statements correctly describes an interface?

        a)It's a concrete class

        b)It's a superclass

        c)It's a type of abstract class

        Ans: c

    28) An interface contains __ methods

        a)Non-abstract

        b)Implemented

        c)unimplemented

        Ans:c