Skip to main content

Short Q&A about constructors

 

Q1: What is a constructor in Java, and when is it called?

A1: A constructor in Java is a special method used to initialize objects. It is called when an instance of a class is created, allocating memory for the object in RAM.


Q2: What happens if a class in Java does not have any constructor defined?

A2: If a class in Java doesn't have any constructor defined, the Java compiler provides a default constructor by default.


Q3: What are the two types of constructors in Java?

A3: There are two types of constructors in Java: 

1. No-arg constructor (Default constructor)

2. Parameterized constructor


Q4: What are the rules for creating a Java constructor?

A4: The rules for creating a Java constructor are as follows:

- Constructor name must be the same as its class name.

- A constructor must have no explicit return type.

- A Java constructor cannot be abstract, static, final, or synchronized.


Q5: What are the different access modifiers that can be used while declaring a constructor in Java?

A5: You can use private, protected, public, or default (no access modifier) while declaring a constructor in Java.


Q6: Can you provide the syntax for a constructor in Java?

A6: The syntax for a constructor in Java is as follows:

```java

class ClassName {

   ClassName() {

      // Constructor code here

   }

}

```


Q7: What is the purpose of a no-argument constructor in Java?

A7: A no-argument constructor is used to initialize objects with default values when no specific initialization values are provided.


Q8: What is the purpose of a parameterized constructor in Java?

A8: A parameterized constructor is used to initialize objects with specific values provided as arguments when creating the object.


Q9: In the given example, what is the value of the "num" variable for objects "t1" and "t2" after creating them?

A9: The value of the "num" variable for both objects "t1" and "t2" is 100.


Q10: How do you call a constructor to initialize objects in Java?

A10: You call a constructor by using the `new` keyword followed by the class name and parentheses. For example:

```java

MyClass t1 = new MyClass();

```


Q11: In the example provided for the parameterized constructor, what values are assigned to the "model" and "name" variables when creating the "ob" object?

A11: The values assigned to the "model" and "name" variables when creating the "ob" object are "234" and "helllooooo," respectively.


Q12: Can you use a constructor to call methods in Java?

A12: No, constructors are specifically used for initializing object attributes, and they cannot be used to call methods directly. However, you can call methods on objects created within a constructor or in the main method, as shown in the example provided.

Comments

Popular posts from this blog

DAE CIt 244 Electronics 2ndyear Past Paper 2019

 

Semantic Data Model Chapter No 3 RDBMS

  Chapter No 3 ⦁ Semantic Data Model ⦁ Relational Model ⦁ Database Models and Internet Semantic Data Model: "A semantic data model is a method of organizing data in a logical and meaningful way. " It provides a conceptual representation of data and the relationships between them, adding a layer of semantic information that gives data a basic meaning. Key Elements: 1.    - Entities: Represent objects or concepts (e.g., Person, Product). 2.    - Attributes: Characteristics or properties of entities. 3.    - Relationships: Connections between entities, defining associations through the foreign key. A semantic data model describes data about its real-world interpretation and usage.  For example, the object "Person" can be generalized to include "Employee," "Applicant," and "Customer," and is related to "Project" and "Task." A person can own multiple projects, and a specific task can be associated with differe...

Chapter No 2 Database System DAE CIT 2nd year

Chapter No. 2  Database System Legacy DB Systems    -  File Processing Systems    - Hierarchical Model    - Network Model Legacy Database Systems Legacy database systems played a crucial role in the evolution of data management, paving the way for more advanced relational database systems. Legacy Database Systems, including File Processing Systems Hierarchical Model Network Model,. 1. File Processing Systems Definition: File Processing Systems are traditional data management systems that use flat files to store and manage data. In this approach, each department or user group maintains its own set of files, leading to data redundancy. Characteristics: Data stored in flat files. No centralized control or structure. Limited data sharing and integration. Advantages: Simplicity: Easy to implement and understand. Independence: Each department has control over its data. Cost: Relatively low implementation and maintenance costs. Performance: Can be effic...