(v) Consider the following Java program and determine its output:
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);
}
public static void main(String[] args)
{
report r = new report();
r.print();
report p = new report(4, 5);
p.print();
}
}
Views 4
Answer:
Let's analyze the given Java program and determine its output.
Code Analysis:
class report { int a, b; // Default constructor report() { a = 10; b = 15; } // Parameterized constructor report(int x, int y) { a = x; b = y; } // Print method void print() { System.out.println(a * b); } public static void main(String[] args) { report r = new report(); // Calls default constructor r.print(); // Output: 10 * 15 = 150 report p = new report(4, 5); // Calls parameterized constructor p.print(); // Output: 4 * 5 = 20 } }
Execution Steps:
-
report r = new report();
-
Calls the default constructor, setting
a = 10
andb = 15
. -
r.print();
→10 * 15 = 150
-
-
report p = new report(4, 5);
-
Calls the parameterized constructor, setting
a = 4
andb = 5
. -
p.print();
→4 * 5 = 20
-
Final Output:
150 20
Related Articles:
This section is dedicated exclusively to Questions & Answers. For an in-depth exploration of ICSE Computer Applications Class 10 – Previous Year Question Papers & Solutions, click the links and dive deeper into this subject.
Join Our telegram group to ask Questions
Click below button to join our groups.