Java_I_Part_II

                         Click Here for Java Unit-I Part-I

5.Explain Different loops in java?

Ans:

While: The while is an entry-controlled loop statement. The test condition is evaluated and if the condition is true, then the body of the loop is executed. After execution of the body, the test condition is once again evaluated and if it true, the body is executed once again. This process of repeated execution of the body continues until the test condition finally becomes false and the control is transferred out of the loop. On exit, the program continues with the statement immediately after the body of the loop.

The body of the loop may have one or more statements. The braces are needed only if the body contains two or more statements. However, it is a good practice to use if the body has only statement.

while(condition)

{

Body of the loop;

}


do…while: The while makes a test condition before the loop is executed. Therefore, the body of the loop may not be executed at all if the condition is not satisfied at the very first attempt. On some occasions it might be necessary to execute the body of the loop before the test is performed. Such situations can be handled with the help of the do…while statement.

do

{

Body of the loop;

}while(condition);

On reaching the do statement, the program proceeds to evaluate the body of the loop first. At the end of the loop, the test condition in the while statement is evaluated. If the condition is true, the program continues to evaluate the body of the loop once again. This process continues as long as the condition is true. When the condition becomes false, the loop will be terminated and the control goes to the statement that appears immediately after the while statement. Since the test condition is evaluated at the bottom of the loop, the do…while construct provides an exit-controlled loop and therefore the body of the loop is always executed atleast once.

For loop:

The for loop is entry-controlled loop that provides a more concise loop control structure. The general form of for loop is:

for(initialization; test condition;increment/decrement)

{

Body of the loop;

}

The execution of for statement is as follows:

1. Initialization of the control variables is done first, using assignment statements such as i=1 and count=0. These variables are known as loop-control variables.

2. The value of the control variable is tested using the test condition. The test condition is a relational expression such as i<10 that determines when the loop will exit. If the condition is true, the body of the loop is executed; otherwise theloop is terminated and the execution continues with the statement that immediately follows the loop.

3. When the body of the loop is executed, the control is transferred back to the for statement after evaluating the last statement in the loop. Now, the control variable is incremented/decremented using an assignment statement such as i=i+1 and the new value of the control variable is again tested to see whether it satisfies the loop condition. If the condition is satisfied, the body of the loop is again executed. This process continues till the value of the control variable fails to satisfy the test condition.

6. Differentiate between break and continue

break: an early exit from a loop can be accomplished by using the break statement. When the break statement is encountered inside a loop, the loop is immediately exited and the program continues with the statement immediately following the loop. When the loops are nested, the break would only exit from the loop containing it. That is, the break will exit only a single loop.

class PrimeNumber

{

public static void main (String args[])

{

int n=10,i;

i=2;

while(i<n)

{

if(n%i= =0)

{

system.out.println(“Number is not prime”);

break;

}

i++;

}

if(n==i)

System.out.println(“Given no is prime”);

}

}

continue: Java supports similar statement called the continue statement. However, unlike the break which causes the loop to be terminated, the continue as the name implies causes the loop to be continued with the next iteration after skipping any statements in between.

class Demo

{

public static void main(String args[])

{

int i;

for(i=1;i<=10;i++)

{

if(i<5)

continue;

System.out.println(i);

}

}

}

In the above program we are redirecting the flow of execution back to the next iteration of the loop till i<5. When i value changes from 1 to 4(i=1,2,3,4) continue statement will be executed and System.out.println is not executed. Whenever i value will become 5 i values will be displayed.


7. Explain class and object?

Ans:

Class: A class is a user-defined data type with a template that serves to define its properties. Once the class type has been defined, we can create variables of that type using declarations that are similar to the basic type declaration. These variables are termed as instance of classes.

The basic form a class definition is:

class classname [extends superclass]

{

[fields declaration]

[methods declaration]

}

The keyword extends indicates that the properties of the superclass are extended to the subclass.

Object: An instance of a class is called Object.

Objects in Java are created using new operator. The new operator creates an object of the specified class and returns a reference to that object.

Rectangle r;

R=new Rectangle();

Both statements can be combined into one as below:

Rectangle r=new Rectangle();

8. How is a method defined?

Ans:

A class with is a Java function. The general form of method is:

type methodname(parameter list)

{

Body of the method

}

Method declaration have four basic parts:

 The name of the method

 The type of the value the method returns

 A list of parameters

 The body of the method.

The type specifies the type of the value the method would return. This could be a simple data type such as int, float,char and so on. It could be even void, if the method doesn’t return any value. The parameter list is always enclosed in parenthesis. This list contains variable names and types of all values which are given as input. In case where no input data is required, the declaration must retain empty parenthesis. The body actually describes the operations to be performed.

class Rectangle

{

int length;

int width;

void getdata(int x,int y)

{

length=x;

width=y;

}

}

9. What is a Constructor? Explain types of constructors and constructor overloading?

Ans:

Java supports a special type of method called Constructor that enables an object to initialize itself when it is created.

class Rectangle

{

int length;

int width;

Rectangle(int x,int y)

{

length=x;

width=y;

}

}

Special Characters:

 Constructors have the same name as the class itself.

 They don’t have any return type, not even void. This is because they return the instance of the class.

Copy Constructor

Copy Constructor is a constructor that takes the object of same class as argument. If all the properties of an object which has to be assigned to another object, this is advisable.

Box b1=new Box(5);

Box b2=new Box(b1);



Default constructor

A constructor that does not take any parameters is known as default constructor.

Parameterized Constructors

Just like methods, arguments can be passed to the constructors, in order to initialize the instance variables of an object. Hence we need to create a parameterized constructor that accepts arguments to initialize the instance variables with the arguments.



Java states that if you provide a constructor for your class, the (automatically created) default constructor will not be provided to your class.

Constructor Overloading

·         Just like methods, constructors can also be overloaded.

·         Constructors are declared just as we declare methods, except that the constructors don’t have any return type.

·         Constructors for a class have the same name as that of the class, but they can have different signatures, i.e., different types of arguments or different number of arguments.

·         Such constructors can be termed as overloaded constructors.

·         Constructors are differentiated on the basis of arguments passed to them.

·         In the example below, we have used two overloaded constructors, each having a different number of arguments, so that the JVM can differentiate between the various constructors. 


10.Explain Method Overloading ?
Ans:
Method overloading:-
Methods are created with the same name but different parameter list and different definitions. This is called method overloading.
 Method overloading is used when objects are required to perform similar task but using different. Input parameters.
 When we call a method in an object, java matches up the method name first and then the number of parameters and then type of parameters to decide which one of the definitions to execute. This process is known as polymorphism.
 In the below example, constructor overloading is used.
class Room
{
float length;
float breadth;
Room(float x,float y)
{
length=x;
breadth=y;
}
Room(float x)
{
length=breadth=x;
}
Int area()
{
Return length*breadth;
}
                            Visit https://degreecsa.blogspot.com

No comments:

Post a Comment