ICSE Board Computer Application Syllabus (2012-2013)
[1] Elementary Concept of Objects and Classes, [2] Values and types, [3] Conditionals and non-nested loops, [4] Class as the Basis of all Computation, [5] Constructors, [6] Functions, [7] Class as a User Defined Type, [8] Iterations, [9] Using Library Classes, [10] Encapsulation, [11] Arrays, [12] Input/Output.

Sunday, October 9, 2011

Constructors

Q. What is constructor?
Ans: A constructor is a Member function that automatically called, when the object is created of that class. It has the same name as that of the class name and its primary job is to initialise the object to a legal value for the class.
Q. Why do we need a constructor as a class member?
Ans: Constructor is used create an instance of of a class, This can be also called creating an object.0
Q. Why does a constructor should be define as public?
Ans: A constructor should be define in public section of a class, so that its objects can be created in any function.
Q. Explain default constructor?
Ans: The constructor that accepts no parameter is called the default constructor. If we do not explicitly define a constructor for a class., then java creates a default constructor for the class. The default constructor is often sufficient for simple class but not for sophisticated classes.Example:  class ant  {    int i;    public static void main()    ant nc=new ant();  }the line new ant() creates an object and calls the default constructor, without it we have no method to call to build our objects. once you create a constructor with argument the default constructor becomes hidden.
Q. Explain the Parameterised constructor?
Ans: If we want to initialise objects with our desired value, we can use parameters with constructor and initialise the data members based on the arguments passed to it . Constructor that can take arguments are called Parameterised constructor.Example:  public class result  {    int per;    int tot;    public result (int percentage)    {      per=percentage;      tot=0;    }  }
Q. Give a syntax/example of constructor overloading. Define a class, which accept roll number and marks of a student. Write constructor for the class, which accepts parameter to initialise the data member. Also take care of the case where the student has not appeared for the test where just the roll number is passed as argument.
Ans: class student  {    int roll;    float marks;    student(int r, float m)                 // constructor with two argument.    {      roll=r;      marks=m;    }    student(int r)                            // constructor with one argument    {      roll=r;      marks=0;    }    student()                                  // default constructor    {      roll=0;      marks=0;    }  }
Q. Mention some characteristics of constructors.
Ans: The special characteristics of constructors are:(i) Constructors should be declared in the public section of the class. (ii) They are invoked automatically when an object of the class is created. (iii) They do not have any return type and cannot return any values. (iv) Like any other function, they can accept arguments. (v) A class can have more than one constructor. (vi) Default constructor do not accept parameters. (vii) If no constructor is present in the class the compiler provides a default constructor.
Q. State the difference between Constructor and Method.  [2005]
Ans: The function has a return type like int. but the constructor has no return type. The function must be called in programs where as constructor automatically called when the object of that class is created.
Q. Enter any two variables through constructor parameters and write a program to swap and print the values.   [2005]
class swap{  int a,b;  swap(int x,int y)  {    a=x;    b=y;  }  public void main(String args[])  {    int t=a;    a=b;    b=t;    System.out.println("the value of a and b after swaping : "+a+" "+b);  }}
 Q. What are the types of Constructors used in a class?
Ans: The different types of constructors are as follows:i. Default Constructors.ii. Parameterized Constructor.iii. Copy Constructors.
Q. Define Copy constructors.
Ans: A copy constructors initializes the instant variables of an object by copying the initial value of the instant variables from another objects. e.g.class xyz{  int a,b;  xyz(int x,int z)  {    a=x;    b=y;  }  xyz(xyz p)  {    a=p.x;    b=p.y;  }}

No comments:

Post a Comment