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.
Java Constructors
1 Like
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.,
0
for numbers,null
for objects,false
for 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.
Answered By
3 Likes
Related Questions
Explain the following terms:
Copy constructor
Fill in the blanks:
The _________ refers to the current object.
The basic salary of employees is undergoing a revision. Define a class called Grade_Revision with the following specifications:
Data Members Purpose String name to store name of the employee int bas to store basic salary int expn to consider the length of service as an experience double inc to store increment double nbas to store new basic salary (basic + increment) Member Methods Purpose Grade_Revision() constructor to initialize all data members void accept() to input name, basic and experience void increment() to calculate increment based on experience as per the table given below void display() to print all the details of an employee Experience Increment Up to 3 years ₹1,000 + 10% of basic 3 years or more and up to 5 years ₹3,000 + 12% of basic 5 years or more and up to 10 years ₹5,000 + 15% of basic 10 years or more ₹8,000 + 20% of basic Write the main method to create an object of the class and call all the member methods.
Consider the following Java class and answer the questions given below:
class Rectangle { int length, breadth; Rectangle() { length = 5; breadth = 3; } Rectangle(int l, int b) { length = l; breadth = b; } void area() { System.out.println("Area: " + (length * breadth)); } public static void main(String args[]) { Rectangle r1 = new Rectangle(); Rectangle r2 = new Rectangle(8, 4); r1.area(); r2.area(); } }
(a) Name the type of constructors used in the above class.
(b) What is the output of the method main()?