Q:

Java find output programs (Constructor & Destructor) | set 2

belongs to collection: Java find output programs

0

Find the output of Java programs | Constructor & Destructor | Set 2: Enhance the knowledge of Java Constructor & Destructor concepts by solving and finding the output of some Java programs.

Question 1:

class Student {
  private int sid;
  private String name;
  private int fees;

  Student() {
    sid = 101;
    name = "virat";
    fees = 8000;
  }

  Student(int sid, String name, int fees) {
    sid = sid;
    name = name;
    fees = fees;
  }

  void printInfo() {
    System.out.println("S-ID: " + sid);
    System.out.println("Name: " + name);
    System.out.println("Fees: " + fees);
  }
}

public class CtorEx {
  public static void main(String[] args) {
    Student S1 = new Student();
    Student S2 = new Student(102, "Rohit", 5000);

    S1.printInfo();
    S2.printInfo();
  }
}

Question 2:

class Student {
  private int sid;
  private String name;
  private int fees;

  Student() {
    sid = 101;
    name = "virat";
    fees = 8000;
  }

  Student(int id, String na, int fee) {
    sid = id;
    name = na;
    fees = fee;
  }

  public setInfo(int id, String na, int fee) {
    sid = id;
    name = na;
    fees = fee;
  }

  public void printInfo() {
    System.out.println("S-ID: " + sid);
    System.out.println("Name: " + name);
    System.out.println("Fees: " + fees);
  }
}

public class CtorEx {
  public static void main(String[] args) {
    Student S1 = new Student();
    Student S2 = new Student(102, "Rohit", 5000);
    Student S3 = new Student();

    S3.setInfo(103, "Rahul", 6000);

    S1.printInfo();
    S2.printInfo();
    S3.printInfo();
  }
}

Question 3:

class Student {
  private int sid;
  private String name;
  private int fees;

  Student() {
    sid = 101;
    name = "virat";
    fees = 8000;
  }

  Student(int id, String na, int fee) {
    sid = id;
    name = na;
    fees = fee;
  }

  public void setInfo(int id, String na, int fee) {
    sid = id;
    name = na;
    fees = fee;
  }

  public void printInfo() {
    System.out.println("S-ID: " + sid);
    System.out.println("Name: " + name);
    System.out.println("Fees: " + fees);
  }
}

public class CtorEx {
  public static void main(String[] args) {
    Student S1 = new Student();
    Student S2 = new Student(102, "Rohit", 5000);
    Student S3 = new Student();

    S3.setInfo(103, "Rahul", 6000);

    S1.printInfo();
    S2.printInfo();
    S3.printInfo();
  }
}

Question 4:

class Student {
  private int sid;
  private String name;
  private int fees;

  Student() {
    sid = 101;
    name = "virat";
    fees = 8000;
  }

  Student(int id, String na, int fee) {
    sid = id;
    name = na;
    fees = fee;
  }

  public void setInfo(int id, String na, int fee) {
    sid = id;
    name = na;
    fees = fee;
  }

  public void printInfo() {
    System.out.println("S-ID: " + sid);
    System.out.println("Name: " + name);
    System.out.println("Fees: " + fees + "\n");
  }
}

public class CtorEx {
  public static void main(String[] args) {
    Student S1 = new Student();
    Student S2 = new Student(102, "Rohit", 5000);
    Student S3 = S1;

    S3.setInfo(103, "Rahul", 6000);

    S1.printInfo();
    S2.printInfo();
    S3.printInfo();
  }
}

Question 5:

class Student {
  private int sid;
  private String name;
  private int fees;

  Student() {
    sid = 101;
    name = "virat";
    fees = 8000;
  }

  Student(int id, String na, int fee) {
    sid = id;
    name = na;
    fees = fee;
  }

  public void setInfo(int id, String na, int fee) {
    sid = id;
    name = na;
    fees = fee;
  }

  public void printInfo() {
    System.out.println("S-ID: " + sid);
    System.out.println("Name: " + name);
    System.out.println("Fees: " + fees + "\n");
  }
}

public class CtorEx {
  public static void main(String[] args) {
    new Student().printInfo();
    new Student(102, "Rohit", 5000).printInfo();
  }
}

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer Question 1:

Output:

S-ID: 101
Name: virat
Fees: 8000
S-ID: 0
Name: null
Fees: 0

Explanation:

In the above program, we created a class Student that contains data member sid, name, and fees. Here, we defined a default constructor and a parameterized constructor to initialize the data members with specified values, and we also defined a printInfo() method to print the values of data members.

The default constructor initialized data member with 101, "virat", and 8000 respectively.

Student(int sid, String name, int fees)
{
    sid  = sid;
    name = name;
    fees = fees;
}

In the parameterized constructor we tried initialized data members but we used local parameters with the same name that's why here data members were not initialized because sid, name, and fees are local variables not data members inside the parameterized constructor.

Answer Question 2:

Output:

/CtorEx.java:18: error: invalid method declaration; return type required
  public setInfo(int id, String na, int fee) {
         ^
1 error

Explanation:

The above program will generate syntax error because the setInfo() method of the Student class does not have a return type.

Answer Question 3:

Output:

S-ID: 101
Name: virat
Fees: 8000
S-ID: 102
Name: Rohit
Fees: 5000
S-ID: 103
Name: Rahul
Fees: 6000

Explanation:

In the above program, we created a class Student that contains data member sid, name, and fees. Here, we defined default and parameterized constructor to initialize the data members, and we also defined setInfo() and printInfo() method to set and print the values of data members.

Now look to the CtorEx class, the CtorEx class contains the main() method, which is the entry program of the program. Here, we created objects S1 and S2. Object S1 initialized using the default constructor and S2 initialized using the parameterized constructor.

Student S3 = new Student();

We set values of S3 object using the setInfo() method and then print the student details using the printInfo() method on the console screen.

Answer Question 4:

Output:

S-ID: 103
Name: Rahul
Fees: 6000

S-ID: 102
Name: Rohit
Fees: 5000

S-ID: 103
Name: Rahul
Fees: 6000

Explanation:

In the above program, we created a class Student that contains data member sid, name, and fees. Here, we defined default and parameterized constructor to initialize the data members, and we also defined setInfo() and printInfo() method to set and print the values of data members.

Now look to the CtorEx class, the CtorEx class contains the main() method, which is the entry program of the program. Here, we created objects S1 and S2. Object S1 initialized using the default constructor and S2 initialized using the parameterized constructor.

Here, we assigned the reference of an S1 object to the S3, then S1 and S3 will point the same object. Then we set the values of S3 object using the setInfo() method, these new values will be reflected in both S1 and S3.

Then print the student detail for all objects using the printInfo() method on the console screen.

Answer Question 5:

Output:

S-ID: 101
Name: virat
Fees: 8000

S-ID: 102
Name: Rohit
Fees: 5000

Explanation:

In the above program, we created a class Student that contains data member sid, name, and fees. Here, we defined default and parameterized constructor to initialize the data members, and we also defined setInfo() and printInfo() method to set and print the values of data members.

Now look to the CtorEx class, the CtorEx class contains the main() method, which is the entry program of the program. Here, we created two anonymous objects and call the printInfo() method that will print student details on the console screen.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java find output programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java find output programs (Constructor & Destructo... >>
<< Java find output programs (Constructor & Destructo...