Q:

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

belongs to collection: Java find output programs

0

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.

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

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

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

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

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

All Answers

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

Answer Question 1:

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.

Answer Question 2:

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.

Answer Question 3:

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.

Answer Question 4:

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.

Answer Question 5:

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.

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 (Class and Objects) | se...