Computer Applications
Assertion (A): The default constructor initializes object fields to zero or null depending on their data type.
Reason (R): Java constructors are inherently tied to the initialization of an object and therefore do not specify a return type.
Answer
Both Assertion (A) and Reason (R) are true and Reason (R) is a correct explanation of Assertion (A)
Explanation
- Assertion (A): The default constructor initializes fields to their default values (e.g.,
0for numbers,nullfor objects,falsefor boolean). - Reason (R): Constructors are used for object initialization and do not have a return type.
- The Reason explains why the default constructor initializes fields automatically.
Related Questions
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()?
If the name of the class is "Yellow", what can be the possible name for its constructors?
- yellow
- YELLOW
- Yell
- Yellow