This post contains interviews questions regading String Handling, Exception Handling,
Multithreading,Synchronization,Nested Class and Interface.
Que: What is the meaning of immutable in terms of String class ?
Ans: The simple meaning of immutable is unmodifiable or unchangeable. Once string object has been created, its value can't be changed.To see more click on String Handling
Que: Why String objects are immutable in java ?
Ans: Because java uses the concept of string literal. Suppose there are 10 reference variables,all referes to one object "vishwa".If one reference variable changes the value of this object,it will be affected to all the reference variables. That is why string objects are immutable in java.To see more click on String Handling
Que: How many ways we can create the String object ?
Ans: There are two ways to create the string object, by string literal and by new keywod.
To see more click on String Handling
Que: Wow many objects will be created in the following code ?
String name="java";
String name="java";
String name="java";
String name="java";
Ans: Only one object.To see more click on String Handling
Que: Why java uses the concept of string literal ?
Ans: To make Java more memory efficient (because no new objects are created if it exists already in String constant pool area).
Que: How many objects will be created in the following code ?
String name=new String("java");
Ans: Two objects, one in String constant pool and other in non-pool(heap).
Que: How can we create immutable class in java ?
Ans: We can create immutable class as the String class by defining any class as final.
Que: What is the basic difference between String and Stringbuffer object ?
Ans: String is an immutable object. StringBuffer is a mutable object.
Que: What is the difference between StringBuffer and StringBuilder ?
Ans: StringBuffer is synchronized whereas StringBuilder is not synchronized.
Que: What is the purpose of toString() method in java ?
Ans: The toString() method returns the String representation of any object. If you print any object, java compiler internally invokes the toString() method on that object. So overriding the toString() method, returns the desired output, it can be the state of an object etc. depends on your implementation.
Que: What is Exception Handling ?
Ans: Exception Handling is a mechanism to handle runtime errors.It is mainly used to handle checked exceptions.
Que: What is difference between Checked Exception and Unchecked Exception ?
Ans:
Checked Exception:
The classes that extend Throwable class except RuntimeException and Error are known as checked
exceptions e.g.ServletException,IOException,SQLException etc. Checked exceptions are checked at compile-time.
Unchecked Exceptions:
The classes that extend RuntimeException are known as unchecked exceptions e.g.ArrayIndexOutOfBoundExceptionArithmeticException,NumberFormatException,
NullPointerException etc. Unchecked exceptions are not checked at compile-time.
Que: What is the base class(parent class) for Error and Exception ?
Ans: Throwable class.
Que: Is it necessary that each try block must be followed by a catch block ?
Ans: It is not necessary that each try block must be followed by a catch block. It should be followed by either a catch block OR a finally block.
Que: What is finally block ?
Ans: finally block is a block that is always executed either exception occured or not.
Que: Can finally block be used without catch ?
Ans: Yes, by try block. finally must be followed by either try or catch block.
Que: Is there any case when finally will not be executed ?
Ans: finally block will not be executed if program exits(either by calling System.exit() method or by causing a fatal error that causes the process to abort).
Que: What is difference between throw and throws ?
Ans: Throw Throws
1)throw is used to explicitly throw an exception. hrows is used to declare an exception.
2)checked exceptions can not be propagated with checked exception can be propagated throw only. with throws.
3)throw is followed by an instance. throws is followed by class.
4)throw is used within the method. throws is used with the method signature. 5)You cannot throw multiple exception. You can declare multiple exception e.g.
public void method()throws IOException,SQLException.
Que: Can an exception be rethrown ?
Ans: Yes.
Que: Can subclass overriding method declare an exception if parent class method doesn't
throw an exception ?
Ans: Yes but only unchecked exception not checked.
Que: What is exception propagation ?
Ans: Forwarding the exception object to the invoking method is known as exception propagation.
Que: What is nested class ?
Ans: A class which is declared inside another class is known as nested class. There are 4 types of nested class member inner class, local inner class, annonymous inner class and static nested class.
Que: Is there any difference between nested classes and inner classes ?
Ans: Yes ofcourse inner classes are non-static nested classes i.e. inner classes are the part of nested classes. to see more click on Nested Classes
Que: Can we access the non-final local variable, inside the local inner class ?
Ans: No, local variable must be constant if you want to access it in local inner class.
Que: What is nested interface ?
Ans: Any interface i.e. declared inside the interface or class, is known as nested interface. It is static by default.
Que: Can a class have an interface ?
Ans: Yes, it is known as nested interface.
Que: Can an Interface have a class ?
Ans: Yes, they are static implicitely.
Que: What is multithreading ?
Ans: Multithreading is a process of executing multiple threads simultaneously.Its main advantage is:
Threads share the same address space.
Thread is lightweight.
Cost of communication between process is low.
Que: What is thread ?
Ans: A thread is a lightweight subprocess.It is a separate path of execution.It is called separate path of execution because each thread runs in a separate stack frame.
Que: What is the difference between preemptive scheduling and time slicing ?
Ans: Under preemptive scheduling, the highest priority task executes until it enters the waiting or dead states or a higher priority task comes into existence. Under time slicing, a task executes for a predefined slice of time and then reenters the pool of ready tasks. The scheduler then determines which task should execute next, based on priority and other factors.
Que: What does join() method ?
Ans: The join() method waits for a thread to die. In other words, it causes the currently running threads to stop executing until the thread it joins with completes its task.
Que: Is it possible to start a thread twice ?
Ans: No, there is no possibility to start a thread twice. If we does,it throws an exception as IllegalThreadStateException.
Que: Can we call the run() method instead of start() ?
Ans: yes, but it will not work as a thread rather it will work as a normal object so there will not be contextswitching between the threads.
Que: What about the daemon threads ?
Ans: The daemon threads are basically the low priority threads that provides the background support to the user threads. It provides services to the user threads.
Que: Can we make the user thread as daemon thread if thread is started ?
Ans: No, if you do so, it will throw IllegalThreadStateException.
Que: What is difference between wait() and sleep() method ?
Ans: wiat() sleep()
1)The wait() method is defined in Object class. The sleep() method is defined in Thread class.
2) wait() method releases the lock. The sleep() method doesn't releases the lock.
Que: What is shutdown hook ?
Ans: The shutdown hook is basically a thread i.e. invoked implicitely before JVM shuts down. So we can use it perform clean up resource.
Que: When should we interrupt a thread ?
Ans: We should interrupt a thread if we want to break out the sleep or wait state of a thread.
Que: What is the difference between notify() and notifyAll() ?
Ans: notify() is used to unblock one waiting thread whereas notifyAll() method is used to unblock all the threads in waiting state.
Que: What is deadlock ?
Ans: Deadlock is a situation when two threads are waiting on each other to release a resource. Each thread waiting for a resource which is held by the other waiting thread.
Que: What is synchronization ?
Ans: Synchronization is the capabilility of control the access of multiple threads to any shared resource.It is used:
1. To prevent thread interference.
2. To prevent consistency problem.
Que: What is the purpose of Synchnorized block ?
Ans: Synchronized block is used to lock an object for any shared resource. Scope of synchronized block is smaller than the method.
Que: Can Java object be locked down for exclusive use by a given thread ?
Ans: Yes. You can lock an object by putting it in a "synchronized" block. The locked object is inaccessible to any thread other than the one that explicitly claimed it.
Que: What is static synchronization ?
Ans: If you make any static method as synchronized, the lock will be on the class not on object.
Multithreading,Synchronization,Nested Class and Interface.
Que: What is the meaning of immutable in terms of String class ?
Ans: The simple meaning of immutable is unmodifiable or unchangeable. Once string object has been created, its value can't be changed.To see more click on String Handling
Que: Why String objects are immutable in java ?
Ans: Because java uses the concept of string literal. Suppose there are 10 reference variables,all referes to one object "vishwa".If one reference variable changes the value of this object,it will be affected to all the reference variables. That is why string objects are immutable in java.To see more click on String Handling
Que: How many ways we can create the String object ?
Ans: There are two ways to create the string object, by string literal and by new keywod.
To see more click on String Handling
Que: Wow many objects will be created in the following code ?
String name="java";
String name="java";
String name="java";
String name="java";
Ans: Only one object.To see more click on String Handling
Que: Why java uses the concept of string literal ?
Ans: To make Java more memory efficient (because no new objects are created if it exists already in String constant pool area).
Que: How many objects will be created in the following code ?
String name=new String("java");
Ans: Two objects, one in String constant pool and other in non-pool(heap).
Que: How can we create immutable class in java ?
Ans: We can create immutable class as the String class by defining any class as final.
Que: What is the basic difference between String and Stringbuffer object ?
Ans: String is an immutable object. StringBuffer is a mutable object.
Que: What is the difference between StringBuffer and StringBuilder ?
Ans: StringBuffer is synchronized whereas StringBuilder is not synchronized.
Que: What is the purpose of toString() method in java ?
Ans: The toString() method returns the String representation of any object. If you print any object, java compiler internally invokes the toString() method on that object. So overriding the toString() method, returns the desired output, it can be the state of an object etc. depends on your implementation.
Que: What is Exception Handling ?
Ans: Exception Handling is a mechanism to handle runtime errors.It is mainly used to handle checked exceptions.
Que: What is difference between Checked Exception and Unchecked Exception ?
Ans:
Checked Exception:
The classes that extend Throwable class except RuntimeException and Error are known as checked
exceptions e.g.ServletException,IOException,SQLException etc. Checked exceptions are checked at compile-time.
Unchecked Exceptions:
The classes that extend RuntimeException are known as unchecked exceptions e.g.ArrayIndexOutOfBoundExceptionArithmeticException,NumberFormatException,
NullPointerException etc. Unchecked exceptions are not checked at compile-time.
Que: What is the base class(parent class) for Error and Exception ?
Ans: Throwable class.
Que: Is it necessary that each try block must be followed by a catch block ?
Ans: It is not necessary that each try block must be followed by a catch block. It should be followed by either a catch block OR a finally block.
Que: What is finally block ?
Ans: finally block is a block that is always executed either exception occured or not.
Que: Can finally block be used without catch ?
Ans: Yes, by try block. finally must be followed by either try or catch block.
Que: Is there any case when finally will not be executed ?
Ans: finally block will not be executed if program exits(either by calling System.exit() method or by causing a fatal error that causes the process to abort).
Que: What is difference between throw and throws ?
Ans: Throw Throws
1)throw is used to explicitly throw an exception. hrows is used to declare an exception.
2)checked exceptions can not be propagated with checked exception can be propagated throw only. with throws.
3)throw is followed by an instance. throws is followed by class.
4)throw is used within the method. throws is used with the method signature. 5)You cannot throw multiple exception. You can declare multiple exception e.g.
public void method()throws IOException,SQLException.
Que: Can an exception be rethrown ?
Ans: Yes.
Que: Can subclass overriding method declare an exception if parent class method doesn't
throw an exception ?
Ans: Yes but only unchecked exception not checked.
Que: What is exception propagation ?
Ans: Forwarding the exception object to the invoking method is known as exception propagation.
Que: What is nested class ?
Ans: A class which is declared inside another class is known as nested class. There are 4 types of nested class member inner class, local inner class, annonymous inner class and static nested class.
Que: Is there any difference between nested classes and inner classes ?
Ans: Yes ofcourse inner classes are non-static nested classes i.e. inner classes are the part of nested classes. to see more click on Nested Classes
Que: Can we access the non-final local variable, inside the local inner class ?
Ans: No, local variable must be constant if you want to access it in local inner class.
Que: What is nested interface ?
Ans: Any interface i.e. declared inside the interface or class, is known as nested interface. It is static by default.
Que: Can a class have an interface ?
Ans: Yes, it is known as nested interface.
Que: Can an Interface have a class ?
Ans: Yes, they are static implicitely.
Que: What is multithreading ?
Ans: Multithreading is a process of executing multiple threads simultaneously.Its main advantage is:
Threads share the same address space.
Thread is lightweight.
Cost of communication between process is low.
Que: What is thread ?
Ans: A thread is a lightweight subprocess.It is a separate path of execution.It is called separate path of execution because each thread runs in a separate stack frame.
Que: What is the difference between preemptive scheduling and time slicing ?
Ans: Under preemptive scheduling, the highest priority task executes until it enters the waiting or dead states or a higher priority task comes into existence. Under time slicing, a task executes for a predefined slice of time and then reenters the pool of ready tasks. The scheduler then determines which task should execute next, based on priority and other factors.
Que: What does join() method ?
Ans: The join() method waits for a thread to die. In other words, it causes the currently running threads to stop executing until the thread it joins with completes its task.
Que: Is it possible to start a thread twice ?
Ans: No, there is no possibility to start a thread twice. If we does,it throws an exception as IllegalThreadStateException.
Que: Can we call the run() method instead of start() ?
Ans: yes, but it will not work as a thread rather it will work as a normal object so there will not be contextswitching between the threads.
Que: What about the daemon threads ?
Ans: The daemon threads are basically the low priority threads that provides the background support to the user threads. It provides services to the user threads.
Que: Can we make the user thread as daemon thread if thread is started ?
Ans: No, if you do so, it will throw IllegalThreadStateException.
Que: What is difference between wait() and sleep() method ?
Ans: wiat() sleep()
1)The wait() method is defined in Object class. The sleep() method is defined in Thread class.
2) wait() method releases the lock. The sleep() method doesn't releases the lock.
Que: What is shutdown hook ?
Ans: The shutdown hook is basically a thread i.e. invoked implicitely before JVM shuts down. So we can use it perform clean up resource.
Que: When should we interrupt a thread ?
Ans: We should interrupt a thread if we want to break out the sleep or wait state of a thread.
Que: What is the difference between notify() and notifyAll() ?
Ans: notify() is used to unblock one waiting thread whereas notifyAll() method is used to unblock all the threads in waiting state.
Que: What is deadlock ?
Ans: Deadlock is a situation when two threads are waiting on each other to release a resource. Each thread waiting for a resource which is held by the other waiting thread.
Que: What is synchronization ?
Ans: Synchronization is the capabilility of control the access of multiple threads to any shared resource.It is used:
1. To prevent thread interference.
2. To prevent consistency problem.
Que: What is the purpose of Synchnorized block ?
Ans: Synchronized block is used to lock an object for any shared resource. Scope of synchronized block is smaller than the method.
Que: Can Java object be locked down for exclusive use by a given thread ?
Ans: Yes. You can lock an object by putting it in a "synchronized" block. The locked object is inaccessible to any thread other than the one that explicitly claimed it.
Que: What is static synchronization ?
Ans: If you make any static method as synchronized, the lock will be on the class not on object.
No comments:
Post a Comment