LIWA

Constructors

ICSE Class 10 · Computer Applications · Chapter 4

What this chapter covers

Key points

Questions answered

Define a constructor.
A constructor is a special member of a class that has the same name as the class, has no return type, and is invoked automatically when an object is created, to initialise its instance variables.
Why does a constructor have no return type?
Its only job is to initialise the object that `new` has already created; it never returns a value to a caller. If a return type such as `void` is written, Java treats the block as an ordinary method.
State any four characteristics of a constructor.
It has the same name as the class; it has no return type; it is invoked automatically when an object is created; it runs once per object. (Others: it can be parameterised, it can be overloaded, it cannot be `static`, it is not inherited.)
Name the two types of constructors in the syllabus and state one difference between them.
The default constructor and the parameterised constructor. A default constructor takes no parameters, while a parameterised constructor receives values through its parameters to initialise the object.
What does the compiler-supplied default constructor store in an `int`, a `double`, a `boolean` and a `String` instance variable?
0, 0.0, `false` and `null` respectively.
A class has only the constructor `Car(String m)`. Will `Car c = new Car();` compile? Give a reason.
No. Because the class already has a constructor, the compiler does not supply a default constructor, and there is no constructor with an empty parameter list to match `new Car()`.
What is constructor overloading? State the three ways in which overloaded constructors may differ.
Constructor overloading is the definition of more than one constructor in the same class with different parameter lists. The lists may differ in the number of parameters, their data types, or the order of their data types.
Can `Shape(int a)` and `Shape(int b)` exist in the same class? Why?
No. Both have one `int` parameter, so their signatures are identical; changing only the parameter name does not make them different.

Past board exam questions

ICSE 2026 board paper · 1 mark

In constructor overloading all constructors should have the same name as of the class but with a different set of ____________.

  1. Access specifiers
  2. Classes
  3. Return type
  4. Parameters
Show answer
Parameters

ICSE 2025 board paper · 2 marks

Write the Java statement for creating an object named ‘sifra’ of the class ‘Robot’, which takes three double parameters.

Show answer
Robot sifra = new Robot(1.5, 2.5, 3.5); (any three double values may be passed to the parameterised constructor)

ICSE 2025 sample paper · 2 marks

Consider the following program segment and answer the questions below: ``` class calculate { int a; double b; calculate() { a=0; b=0.0; } calculate(int x, double y) { a=x; b=y; } void sum() { System.out.println(a*b); }} ``` Name the type of constructors used in the above program segment?

Show answer
calculate() — default (non-parameterised) constructor. calculate(int x, double y) — parameterised constructor. (Together they show constructor overloading.)

ICSE 2025 board paper · 2 marks

Consider and give the output of the following program: class report { int a,b; report() { a=10; b=15; } report(int x, int y) { a=x; b=y; } void print() { System.out.println(a*b); } static void main() { report r = new report(); r.print(); report p = new report(4, 5); p.print(); } }

Show answer
150 20 r is created by the default constructor (a=10, b=15), so r.print() displays 10*15 = 150; p is created by the parameterised constructor (a=4, b=5), so p.print() displays 4*5 = 20.

ICSE 2025 compartment paper · 2 marks

class perform { int m ; String name; perform( int x , String y) { m=x; name=y; } void print () { System.out.print( name+" "+m); } public static void main( ) { perform ob1=new perform( 95 , "Xavier"); ob1.print( ); } } (a) Give the output of the code given above. (b) Name the type of the constructor used.

Show answer
(a) Xavier 95 (b) Parameterised constructor

ICSE 2024 sample paper · 1 mark

Constructor overloading follows which principle of Object Oriented programming?

  1. Inheritance
  2. Polymorphism
  3. Abstraction
  4. Encapsulation
Show answer
Polymorphism

ICSE 2024 board paper · 1 mark

If the name of the class is “Yellow”, what can be the possible name for its constructors?

  1. yellow
  2. YELLOW
  3. Yell
  4. Yellow
Show answer
Yellow

ICSE 2024 board paper · 2 marks

Consider the given program and answer the questions given below: class temp { int a; temp() { a=10; } temp(int z) { a=z; } void print() { System.out.println(a); } void main() { temp t = new temp(); temp x = new temp(30); t.print(); x.print(); } } (a) What concept of OOPs is depicted in the above program with two constructors? (b) What is the output of the method main()?

Show answer
(a) Polymorphism – constructor overloading (two constructors with the same name and different parameter lists). (b) Output: 10 30