Constructor Programs and Output Tracing in Java (ICSE Class 10)
Updated 17 October 2026 · Fact-checked
A constructor is a member that has the same name as its class and no return type; it runs automatically when an object is created with new and initialises the object. If a class has no constructor the compiler supplies a default one that sets default values. Constructors with different parameter lists are overloaded constructors, chosen by the arguments in new.
Java picks the constructor whose parameter list matches the arguments in new; all share the class name and have no return type.
Illustration drawn for QuestionBuddy.
Understand Constructor Programs and Output Tracing
Characteristics.
- The name is exactly the class name.
- It has no return type, not even
void. Addingvoidmakes it an ordinary method. - It is called automatically once for every
newand cannot be called later by name. - Its purpose is to initialise the instance variables when the object is created.
- It can have parameters and can be overloaded.
Types.
- Default (non-parameterized) constructor: no parameters, e.g.
Pen() { brand = "Unknown"; price = 10; }. If you write no constructor at all, the compiler supplies an empty one which leaves variables at their default values (0, 0.0, false, null). - Parameterized constructor: takes values, e.g.
Pen(String b, int p). Once you write any constructor, the compiler does not supply the no-argument one, sonew Pen()gives a compile-time error if only the parameterized version exists.
Constructor overloading. Several constructors in one class with different parameter lists; new Room(), new Room(3) and new Room(4, 5, 2) call different ones.
Parameter with the same name as the variable. In Acc(int bal) { bal = bal; } both names mean the parameter, so the instance variable is never set and stays 0. Use this.bal = bal; (this is the current object) or choose different names.
Constructor vs method. A constructor has the class name, no return type and is called by new once at creation; a method has any valid name, a return type (or void) and is called by an object any number of times.
Tracing steps. For each new: find the constructor whose parameter list matches the arguments, run its statements to set the variables, then run the method calls that follow.
Constructor Programs and Output Tracing: key facts
- Constructor rules
- Same name as the class
- No return type, not even void
- Called automatically by new
- Can be overloaded
- With void added it becomes a method.
- Compiler default constructor
- Supplied only if the class has no constructor
- Sets default values 0, 0.0, false, null
- Vanishes once any constructor is written
- Then new ClassName() needs a no-argument version of your own.
- Choosing the constructor
- Match the number and types of arguments in new
- Widening of types is allowed
- No match: compile-time error
- Same rules as method overloading.
- Initialising with same names
- this.variable = parameter; | The plain variable = variable; sets the parameter to itself
- The most common constructor bug.
How to answer Constructor Programs and Output Tracing questions
Use this routine for constructor output questions.
- 1List the constructors of the class with their parameter lists.
- 2For each new statement, match the arguments to one constructor.
- 3Run that constructor: write the values it assigns to the instance variables of the new object.
- 4Execute the method calls in order using the stored values.
- 5Check for traps: a void before the constructor name, a missing no-argument constructor, or a parameter that hides the variable.
Constructor Programs and Output Tracing in real life
The idea, met in an everyday situation. Answer the question in your own words, then compare with the model answer. The one-line hook is what to remember.
Constructor as setup at creation
Suppose a hostel gives every new student a room key and a locker number the moment they are admitted, with standard values when nothing special is requested.
Which constructor would assign the standard values and which would take special values?
Try it first, then show the solutionSolution
- A no-argument (default) constructor assigns the standard values.
- A parameterized constructor takes the special key and locker numbers.
Answer: Default constructor for standard values; parameterized constructor for special values.
Memory hook: Setup happens at creation.
Tricks to remember Constructor Programs and Output Tracing
Short memory aids. Say each one aloud once, then use it on a question the same day.
Same name, no type
A constructor wears the class name and carries no return type badge.
Use it when: Identification.
Write one, lose the free one
The moment you write any constructor, the free default one disappears.
Use it when: Compile-time error questions.
this = the one being built
this.bal = bal means: my bal gets the value from the parameter.
Use it when: Same-name parameters.
Constructor Programs and Output Tracing: solved examples
Ordered from board level to HOTS. Try each one before you open the solution.
Example 1: Characteristics · 2 marks · Short answerBoard level
State four characteristics of a constructor.
Try it first, then show the solutionSolution
- Its name is the same as the class name and it has no return type, not even void. (1 mark)
- It is called automatically when an object is created with new, and it initialises the instance variables; it can be overloaded. (1 mark)
Answer: Same name as class; no return type; automatic call at new; initialises the object; can be overloaded.
Example 2: Default and parameterized constructor · 2 marks · Short answerHarder board level
Write the output:
class Pen {
String brand;
int price;
Pen() { brand = "Unknown"; price = 10; }
Pen(String b, int p) { brand = b; price = p; }
void show() { System.out.println(brand + " " + price); }
}
public class Test {
public static void main(String[] args) {
Pen a = new Pen();
Pen b = new Pen("Gel", 25);
a.show();
b.show();
}
}Try it first, then show the solutionSolution
- new Pen() runs the no-argument constructor: brand = Unknown, price = 10. (1 mark)
- new Pen("Gel", 25) runs the second constructor: brand = Gel, price = 25. (1 mark)
Answer: Unknown 10
Gel 25
Example 3: Compiler default constructor and its loss · 3 marks · Short answerHarder board level
(a) Write the output:
class Q { int x; double y; }
// in main
Q q = new Q();
System.out.println(q.x + " " + q.y);(b) What happens for new Pen() if the class Pen has only the constructor Pen(String b, int p)? Give the reason.
Try it first, then show the solutionSolution
- (a) Q has no constructor, so the compiler supplies a default one; the variables keep their default values: 0 and 0.0. (1 mark)
- Output of (a): 0 0.0. (1 mark)
- (b) A compile-time error: once a class has any constructor, the compiler does not supply the no-argument one, so no constructor matches new Pen(). (1 mark)
Answer: (a) 0 0.0 (b) compile-time error: no matching no-argument constructor
Example 4: Case study: overloaded constructors in a Room class · 4 marks · Case-basedHOTS / competency
A class is written:
class Room {
int l, b, h;
Room() { l = 2; b = 2; h = 3; }
Room(int a) { l = a; b = a; h = a; }
Room(int x, int y, int z) { l = x; b = y; h = z; }
int volume() { return l * b * h; }
}
public class Test {
public static void main(String[] args) {
Room p = new Room();
Room q = new Room(3);
Room r = new Room(4, 5, 2);
System.out.println(p.volume() + " " + q.volume() + " " + r.volume());
}
}(i) Which concept is shown by the three constructors? (1)
(ii) Which constructor does each new statement call? (1)
(iii) Write the output. (1)
(iv) If void is written before Room(int a), what happens? (1)
Try it first, then show the solutionSolution
- (i) Constructor overloading (polymorphism).
- (ii) p: Room(); q: Room(int); r: Room(int, int, int).
- (iii) p: 2 * 2 * 3 = 12; q: 3 * 3 * 3 = 27; r: 4 * 5 * 2 = 40. Output 12 27 40.
- (iv) void Room(int a) is an ordinary method, not a constructor; then new Room(3) has no matching constructor and gives a compile-time error.
Answer: (i) constructor overloading (iii) 12 27 40 (iv) compile-time error for new Room(3)
Example 5: Parameter hides the variable · 3 marks · Short answerHarder board level
Write the output and correct the class:
class Acc {
int bal;
Acc(int bal) { bal = bal; }
}
// in main
Acc a = new Acc(500);
System.out.println(a.bal);Try it first, then show the solutionSolution
- Inside the constructor both names mean the parameter, so bal = bal assigns the parameter to itself. (1 mark)
- The instance variable keeps its default value: the output is 0. (1 mark)
- Correction: this.bal = bal; stores 500, so the output becomes 500. (1 mark)
Answer: 0 (corrected with this.bal = bal; the output is 500)
Where students lose marks in Constructor Programs and Output Tracing
The slips examiners see most often, and what to write instead.
Mistake: Writing void before a constructor
It becomes a method and the object is created with the default constructor instead.
Wrong working
void Box(int x) { ... }
Right working
Box(int x) { ... }
Do this: No return type at all.
Usually costs: 1 mark
Mistake: Calling new Pen() when only a parameterized constructor exists
The compiler stops supplying the default constructor once you write any constructor.
Wrong working
Pen p = new Pen(); // with only Pen(String, int)
Right working
Add Pen() { ... } or write new Pen("Gel", 25)
Do this: Add a no-argument constructor.
Usually costs: 1 mark
Mistake: Assigning bal = bal in the constructor
The parameter hides the instance variable.
Wrong working
Acc(int bal) { bal = bal; }
Right working
Acc(int bal) { this.bal = bal; }
Do this: Use this. or different names.
Usually costs: 1 to 2 marks
Exam tips
- Write the characteristics as separate short points; each is a mark.
- In output questions, write which constructor each new calls before calculating.
- Mention that a default constructor is supplied by the compiler only if no constructor is written.
- Differences between constructor and method: name, return type, when called, how often.
Practice questions on Constructor Programs and Output Tracing
Write each answer on paper first. Open the hint only if you are stuck, and the answer only when you have finished.
Q1 · 1 mark · MCQBoard level
A constructor has the same name as:
(a) the package (b) the class (c) the object (d) the main methodHint
Look at the first line of a constructor.
Answer
(b) the class
Q2 · 1 mark · Very short answerBoard level
What is the return type of a constructor?
Hint
Not even void.
Answer
A constructor has no return type.
Q3 · 2 marks · Short answerHarder board level
Write the output:
class C { int n; C() { n = 5; } C(int k) { n = k * 2; } } // in main System.out.println(new C().n + " " + new C(4).n);Hint
Two objects are created.
Answer
5 8
Q4 · 4 marks · Long answerHarder board level
Write a class Student with instance variables name (String) and marks (int), a parameterized constructor that sets them, and a method show() that prints "name: marks". Create an object for Asha with 92 and call show().
Hint
Use this to avoid hiding the variables.
Answer
class Student {
String name;
int marks;
Student(String name, int marks) {
this.name = name;
this.marks = marks;
}
void show() {
System.out.println(name + ": " + marks);
}
}
// in main
Student s = new Student("Asha", 92);
s.show();
Output: Asha: 92Q5 · 1 mark · Assertion-reasonHOTS / competency
Assertion (A): A constructor is invoked automatically when an object is created using new.
Reason (R): A constructor has the same name as its class and has no return type.
(a) Both A and R are true and R is the correct explanation of A. (b) Both A and R are true but R is not the correct explanation of A. (c) A is true but R is false. (d) A is false but R is true.Hint
Does the name rule explain the automatic call?
Answer
(b) Both are true, but R does not explain A: the automatic call is the language rule for new; the name rule only identifies the constructor.
Test your recall
Answer in your head or on paper first, then open the card. Pulling an answer out of memory makes it stick far better than re-reading.
1.When is a constructor called?Show answer
Automatically, when an object is created with new.
2.Can a constructor have a return type?Show answer
No.
3.What is constructor overloading?Show answer
Several constructors in one class with different parameter lists.
4.What does the compiler supply if no constructor is written?Show answer
A default constructor with no parameters.
5.What is this?Show answer
A reference to the current object.
6.What is a parameterized constructor?Show answer
A constructor that takes arguments to initialise the object.
Constructor Programs and Output Tracing: frequently asked questions
Can a constructor be called like a normal method?
No. It is called only through new when the object is created.
Why does my object have zeros after using a parameterized constructor?
The parameter name probably hides the instance variable. Use this.variable = parameter; to assign to the variable.
Is a constructor inherited?
Inheritance is outside this Class X syllabus.