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.,
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.
Related Questions
What is meant by a constructor?
State whether the following statement is True or False :
Every class must have all types of constructors.
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()?
Define a class named FruitJuice with the following description:
Data Members Purpose int product_code stores the product code number String flavour stores the flavour of the juice (e.g., orange, apple, etc.) String pack_type stores the type of packaging (e.g., tera-pack, PET bottle, etc.) int pack_size stores package size (e.g., 200 mL, 400 mL, etc.) int product_price stores the price of the product Member Methods Purpose FruitJuice() constructor to initialize integer data members to 0 and string data members to "" void input() to input and store the product code, flavour, pack type, pack size and product price void discount() to reduce the product price by 10 void display() to display the product code, flavour, pack type, pack size and product price