friend function :
we know that the private members of a class cannot be accessed by a non-member function of a class. Friend function is a special mechanism of letting non-members functions of class to access / modify the private members of class. A normal function, which is not a member of any class, but declared as friend within class, is bale to access the private members of a class and this function is said to be friend function to this class. Placing keyword friend before the declaration of function within class does this.
e.g. :
class complex;
int fsum(complex); // normal funnction declaration
class complex
{
private :
int real,img;
public :
void getdata(int a,int b)
{
real=a;
img=b;
}
void dispdata( )
{
cout<
A thread is a smallest unit of code in a program which is dispatched by the scheduler. A thread is similar to the program having the basic code. It is also known as light weight and the program is a heavy weight process. A thread is a similar to a program that has a single flow of control. It has a beginning, a body, and, an end and executes commands sequentially. Every program will have at least one thread.
Advantages of thread
1. They can be created easily.
2. Requires fewer overheads.
3. Inter process communication is fater.
4. Context switching is faster
5. Maximum use of CPU time
Life Cycle of thread
During the life time of a thread, there are may states it can enter:
1. New born state
2. Run able state
3. Running state
4. Blocked state
1. New born state
when we create a thread object, the thread is born and is said to be a new born state. The thread is not yet scheduled for running. At this stage it can do only one of the following:
- schedule it for running using start() method
- kill it using stop() methods.
2. Run able state
Run able state means that the thread is ready for execution and is waiting for the availability of the processor. That is the thread has joined the queue of threads that are waiting for execution. If all threads have equal priority, then they are given time slots for execution in round robin fashion. That means first-come-first-serve manner.
3. Running state
Running means that the processor has given its time to the thread for its execution .the thread rums until it relinquishes control on its own or it is pre-empted by a higher priority thread. A running thread may relinquish its control in one of the following situations:
- it has been suspended using suspend() methods
- it has been made to sleep. We can put a thread to sleep for a specific time period using sleep(time) method.
- It has been told to wait until some event occurs. This is done by wait() method.
4. Blocked state:
A thread is said to be blocked when it is prevented for entering into run able state and subsequently the running state. This happens when the thread is suspended, sleeping or waiting in order to satisfying some requirements. A blocked thread is considered to be “not run able “, but not dead, therefore fully qualified to run again.
5. Dead State
Every thread has a life cycle. A running thread ends its life when it has completed executing run() method. It is a natural death. However, we can kill it by sending the stop massage to it at any state thus causing premature death to it. A thread can be killed as soon as it is born or while it is running, or even when it is blocked state.
What is Exceptions and Exception Handling?
An exception is a condition that can cause run-time error in a program. When the Java interpreter encounters an error, it creates an exception object and throws it (i.e. inform us that an error has occurred). If error (exception object) is not handled properly, then error massage is displayed and no further execution is done.
If we want the program to continue the execution of remaining code then we must catch the error (exception object) and then display appropriate massage or write appropriate statements that handle the error occurred and hence take corrective actions. This is known as Exception handling.
The purpose of exception handling mechanism is to provide a means to detect and report Exception circumstance so that appropriate action can be taken.
Exception handling method follows the following steps:
1. find the problem (hit the exception)
2. inform that error has occurred (throws exception)
3. receive the error information (catch the exception)
4. take corrective steps (handle the exception)
Types of Exception
- Arithmetic Exception Caused by math error such as divide by ZERO
- Arraystore Exception Caused when a programmer tries to store the wrong type of
data in array.
- File not found Exception Caused by an attempt to access a non-existed file
Define error and explain its types
A program rarely executes successfully at the first attempt. It is common to make mistakes while developing as well as typing a program. Mistake might lead to an error causing the program to produce unexpected results. Errors are the wrongs that make a program go wrong.
An error may produce an incorrect output or may cause the system to crash. It is therefore important to detect and manage properly all the possible error conditions in the program so that the program will not crash during the execution.
Types of errors
1. compile-time errors
2. run-time errors
1. Compile time errors :
All the syntax errors which are detected and displayed by the Java compiler are known as compile-time errors.
Most of the compile time errors are due to typing mistakes. Typographic errors are hard to find. We have to check word by word or even character by character.
The most common compile-time errors are :
(i) missing semicolons
(ii) missing brackets in classes or methods
(iii) Misspelled identifiers or keywords.
(iv) Use of undeclared variables.
(v) Use of = in place or = = operator
2. Run-time errors
Some programs may compile successfully creating the .class file but may not run properly. Such programs may produce wrong results due to wrong logic. So the errors created by logical mistakes are known as run-time errors.
Most commonly used run-time errors are:
(i) dividing an integer by 0
(ii) trying to store a value into an array of incompatible type or class
(iii) Attempting to use negative size of array
(iv) Trying to illegally change the state of thread.
(v) Converting invalid string to number.