Constructors

ICSE Class 10 Computer Applications · Sumita Arora Computer Applications · 26 solved questions

Exercise
1Objective Type Questions

A member function having the same name as that of its class is called constructor function.

Solution not available in the source paper.

2Objective Type Questions

A constructor function has no return type.

Solution not available in the source paper.

3Objective Type Questions

A private constructor allows object creation only inside member functions.

Solution not available in the source paper.

4Objective Type Questions

A non-parameterized constructor takes no arguments.

Solution not available in the source paper.

5Objective Type Questions

A parameterized constructor creates objects through values passed to it.

Solution not available in the source paper.

6Objective Type Questions

The keyword this refers to current object.
Or
Name the Java keyword that stores the address of the currently-calling object.

Show SolutionHide Solution↓

'this' keyword stores the address of the currently-calling object.

7Objective Type Questions

Many constructor definitions in the same class is known as constructor overloading.

Solution not available in the source paper.

1Assignment Questions

What is a constructor ? What does it do ?

Show SolutionHide Solution↓

A constructor is a member function with the same name as that of its class but no return type.

A constructor is used to initialize the objects of that class type with legal initial values.

2Assignment Questions

What is that class called which does not have a public constructor ?

Show SolutionHide Solution↓

A class which does not have a public constructor is called a private class as only the member functions may declare objects of the class.

3Assignment Questions

When is a constructor automatically invoked ?

Show SolutionHide Solution↓

A constructor is automatically invoked when the object of a class is created.

4Assignment Questions

Write two characteristics of a constructor.

Show SolutionHide Solution↓

Two characteristics of a constructor are:

  1. It has the same name as that of its class.
  2. It has no return type, not even void.
5Assignment Questions

Write a class specifier (along with its constructor) that creates a class student having two private data members : rollno and grade and two public functions init( ) and display( ).
(Do not write full definitions of member functions except for constructor).

Show SolutionHide Solution↓
class student
{
    private int rollno;
    private char grade;

    public student()    {
        rollno = 0;
        grade = '\0';
    }

    public student(int roll, char g)
    {
        rollno = roll;
        grade = g;
    }

    public void init()  {}
    
    public void display()   {}
}
6Assignment Questions

Can you think of the benefits of a private class if any ? What are they ?

Show SolutionHide Solution↓

The benefits of a private class (i.e., a class with a private constructor) are:

  1. Controlled object creation — By making the constructor private, we can control the number of objects of a class that can be created. We can also enforce certain rules or constraints on how the object is created and used.
  2. Better memory management — By limiting the number of objects of a class that can be created, we can reduce memory usage and improve performance.
7Assignment Questions

Here is a skeleton definition of a class :

class Sample
{
    int i;
    char c;
    float f;
    :
    //public members
    :
}

Implement the constructor.

Show SolutionHide Solution↓

The constructor of the class is given below:

public Sample(int a, char b, float c)
{
    i = a;
    c = b;
    f = c;
}
8Assignment Questions

Define a constructor function for a Date class that initializes the Date objects with given initial values. In case initial values are not provided, it should initialize the object with default values.

Show SolutionHide Solution↓
public Date()
{
    day = 1;
    month = 1;
    year = 1970;
}
public Date(int d, int m, int y)
{
    day = d;
    month = m;
    year = y;
}
9Assignment Questions

What condition(s) a function must specify in order to create objects of a class ?

Show SolutionHide Solution↓

Every time an object is created, the constructor is automatically invoked. If the function creating the object, does not have access privilege for the constructor, it cannot be invoked for that function. Thus, the object cannot be created by such function.

Therefore, it is essential for a function to have access privilege for the constructor in order to create objects of a class.

10Assignment Questions

Constructor functions obey the usual access rules. What does this statement mean ?

Show SolutionHide Solution↓

Constructor functions obey the usual access rules. It means that a private or protected constructor is not available to the non-member functions.

With a private or protected constructor, one cannot create an object of the same class in a non-member function. Only the member functions of that class can create an object of the same class and invoke the constructor.

11Assignment Questions

How are parameterized constructors different from non-parameterized constructors?

Show SolutionHide Solution↓
Parameterised constructorNon-parameterised constructor
Parameterised constructor receives parameters and uses them to initialise the object's member variables.Non-parameterised constructor does not accept parameters and initialises the object's member variables with default values.
Parameterised constructors need to be explicitly defined for the class. They are never created automatically by the compiler.Non-parameterised constructors are considered default constructors. If no constructor is explicitly defined, then the compiler automatically creates a non-parameterized constructor.
For example,
Test(int x, int y)
{
a = x;
b = y;
}
For example,
Test()
{
a = 10;
b = 5;
}
12Assignment Questions

What are benefits/drawbacks of temporary instances ?

Show SolutionHide Solution↓

The benefit of temporary instances is that they live in the memory as long as they are being used or referenced in an expression and after this, they are deleted by the compiler. The memory is freed as soon as they are deleted rather than staying in the memory till the program completes execution.

The drawback of temporary instances is that they are anonymous i.e., they do not bear a name. Thus, they cannot be reused in a program.

13Assignment Questions

How do we invoke a constructor ?

Show SolutionHide Solution↓

A constructor is invoked automatically when an object of a class is created. The constructor is called using the new keyword followed by the name of the class and a set of parentheses. If the constructor requires arguments, the arguments are passed inside the parentheses.

For example, suppose we have a class named Person with a parameterized constructor that takes two arguments name and age. To create an object of this class and invoke the constructor, we would use the following code:

Person personObj = new Person("John", 30);
14Assignment Questions

How can objects be initialized with desired values at the time of object creation ?

Show SolutionHide Solution↓

Objects can be initialized with desired values at the time of object creation by using parameterized constructor and passing the desired values as arguments at the time of creation of object.

15Assignment Questions

When a compiler can automatically generate a constructor if it is not defined then why is it considered that writing constructors for a class is a good practice ?

Show SolutionHide Solution↓

Writing constructors for a class is considered a good practice for the following reasons:

  1. Control over object creation — By writing our own constructors, we can control how objects of our class are created and initialised. It ensures initialising member variables to specific values and executing certain initialisation logic before the object is used.
  2. Parameter validation — We can prevent runtime errors and bugs by validating input parameters and ensuring only valid objects are created.
  3. Flexibility — Multiple constructors allows users of the class to create objects in different ways, depending on their needs. This makes the class more flexible and easier to use.
16Assignment Questions

'Accessibility of a constructor greatly affects the scope and visibility of their class.' Elaborate this statement.

Show SolutionHide Solution↓

Every time an object is created, it is automatically initialised by the constructor of the class. Therefore, it is very much necessary for the constructor of a class to be accessible by the function in which the object is created. If the constructor of a class is declared 'private' or 'protected', then the scope and visibility of the constructor (and the class) is defined by the rules of the access specifier.

In such a case, a function not having access to constructor of a class cannot declare and use objects of that class. Hence, accessibility of a constructor greatly affects the scope and visibility of their class.

17Assignment Questions

List some of the special properties of the constructor functions.

Show SolutionHide Solution↓

Some of the special properties of the constructor functions are as follows:

  1. It has the same name as that of its class.
  2. It doesn't have any return type, not even void.
  3. It adheres to the rules of access specifiers.
  4. It can be parameterized or non-parameterized.
  5. It is used to create and initialize objects from a class.
  6. It can be overloaded to provide multiple ways of creating objects with different initialisation parameters
  7. It is always called implicitly when an object is created using the new keyword.
18Assignment Questions

What is a parameterized constructor ? How is it useful ?

Show SolutionHide Solution↓

Parameterized constructors are the constructors that receive parameters and initialize objects with received values. For example:

class XYZ
{
    private int i; 
    private float j;

    XYZ(int a, float b)     //parameterized constructor
    {	
        i = a ;
        j = b;
    }
    :
    //other  members
    :
}

Parameterized constructors are useful in the following ways:

  1. Control over object creation — By writing our own constructors, we can control how objects of our class are created and initialised. It ensures initialising member variables to specific values and executing certain initialisation logic before the object is used.
  2. Parameter validation — We can prevent runtime errors and bugs by validating input parameters and ensuring only valid objects are created.
19Assignment Questions

Design a class to represent a bank account. Include the following members:

Data members

  1. Name of the depositor
  2. Account number
  3. Type of account
  4. Balance amount in the account

Methods

Show SolutionHide Solution↓
class BankAccount
{
    private String name; 
    private long accno;
    private char type;
    private double bal;

    BankAccount()   {
        name = "";
        accno = 0;
        type = '\0';
        bal = 0.0d;
    }
    BankAccount(String n, long accnumber, char t, double b)   {
        name = n;
        accno = accnumber;
        type = t;
        bal = b;
    }

    public void deposit(double amt)   {
        bal = bal + amt;
        System.out.println("Amount deposited: Rs. " + amt);
        System.out.println("Balance: Rs. " + bal);
    }

    public void withdraw(double amt)    {
        if(amt <= bal)  {
            bal = bal - amt;
            System.out.println("Amount withdrawn: Rs. " + amt);
            System.out.println("Balance: Rs. " + bal);
        }
        else {
            System.out.println("Insufficient Balance");
        }
    }
    
    public void display()
    {
        System.out.println("Name: " + name);
        System.out.println("Balance: Rs. " + bal);
    }
}

Press ↓ to reveal a solution and move to the next question. Back to Sumita Arora Computer Applications.

Free sample bank