DifferenceModerate2 marks
Differentiate between constructor and function.
Topic: Constructor and function
Previous-year question practice database
Answer
Exam answer
The difference is shown in the table.
Explanation
Three differences are available and any two earn the marks, but the return-type point is the one examiners look for — write "not even void" in full. The automatic-invocation point is the second strongest: you never write ob.ClassName(); the constructor runs by itself at the moment of creation.
Comparison
| Constructor | Function (method) |
|---|---|
| Has the same name as the class. | May have any valid name. |
| Has no return type at all — not even void. | Must have a return type, which may be void. |
| Invoked automatically when an object is created with new. | Invoked explicitly through the object or class name. |
More from Constructors
In constructor overloading all constructors should have the same name as of the class but with a different set of __________.2026Write the Java statement for creating an object named 'sifra' of the class 'Robot', which takes three double parameters.2025Consider 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();
}
}2025If the name of the class is "Yellow", what can be the possible name for its constructors?2024Consider 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()?2024Name the following:
(a) What is an instance of the class called?
(b) The method which has the same name as that of the class name.2023