ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

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

Find the output of Java programs | Constructor & Destructor | Set 1: Enhance the knowledge of Java Constructor & Destructor concepts by solving and finding the output of some Java programs.
Submitted by Nidhi, on February 03, 2021

Question 1:

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

  private Student() {
    sid = 101;
    name = "Test";
    fees = 1000;
  }

  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 S = new Student();
    S.printInfo();
  }
}

Output:

/CtorEx.java:21: error: Student() has private access in Student
    Student S = new Student();
                ^
1 error

Explanation:

The above program will generate a compile-time error because in the above program we defined a no-argument constructor with a private modifier that cannot be accessed outside the class but we created the object of Student class in the main() method of CtorEx class.

Question 2:

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

  Student() {
    sid = 101;
    name = "Test";
    fees = 1000;
  }

  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 S = new Student();
    S.printInfo();
  }
}

Output:

S-ID: 101
Name: Test
Fees: 1000

Explanation:

In the above program, we created two classes Student and CtorEx. The Student class contains three data members sid, name, and fees. we also created no-argument constructor and printInfo() method. The constructor is used to initialize the data members with some default values and printInfo() is used to print the values of data members on the console screen.

Now look to the CtorEx class: The CtorEx class contains the main() method, the main() method is the entry point of the program. Here we created the object S of Student class and then called the printInfo() method to print student information on the console screen.

Question 3:

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

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

  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(101, "Rohit", 5000);

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

Output:

/CtorEx.java:21: error: constructor Student in class Student 
cannot be applied to given types;
    Student S1 = new Student();
                 ^
  required: int,String,int
  found: no arguments
  reason: actual and formal argument lists differ in length
1 error

Explanation:

The above program will generate a syntax error because of the below statement,

Student S1 = new Student();

In the above statement, we call no argument constructor but we did not define a no-argument constructor in the Student class.

Question 4:

class Student {
  private int sid = 101;
  private String name = "Virat";
  private int fees = 8000;

  Student() {}

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

  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();
  }
}

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 initialized with 101, "Virat", and 8000 respectively. Here, we defined a default constructor with an empty body 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.

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 objects S1 and S2. The object S1 calls no-argument constructor and S2 calls parameterized constructor, and then call printInfo() method with both object to print student detail on the console screen.

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;
  }

  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();
  }
}

Output:

/CtorEx.java:6: error: ';' expected
  Student() : sid(101),
           ^
1 error

Explanation:

The above program will generate a compile-time error because of the definition of the no-argument constructor.

Student(): sid(101),name("Virat"),fees(8000)
{}

Here, we tried to initialize data members using member initializer list, C++ supports member initializer list to initialize data members but java does not support member initializer list.

ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.