ADVERTISEMENT
ADVERTISEMENT

Java find output programs (Class and Objects) | set 3

Find the output of Java programs | Class and Objects | Set 3: Enhance the knowledge of Java Class and Objects concepts by solving and finding the output of some Java programs.
Submitted by Nidhi, on February 02, 2021

Question 1:

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

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

  public void printInfo() {
    System.out.println(sid + "," + name + "," + fees);
  }
}

public class ClassEx {
  public static void main(String[] args) {
    Student S = new Student();

    Student * ptr;

    ptr = &S;

    ptr ->setInfo(101, "Amit", 10000);
    ptr ->printInfo();
  }
}

Output:

/ClassEx.java:21: error: not a statement
    Student * ptr;
            ^
/ClassEx.java:23: error: illegal start of expression
    ptr = &S;
          ^
/ClassEx.java:25: error: not a statement
    ptr ->setInfo(101, "Amit", 10000);
    ^
/ClassEx.java:26: error: not a statement
    ptr ->printInfo();
    ^
4 errors

Explanation:

The above program will generate compile-time errors because in the above program we created pointer ptr in the main() method, but Java does not support pointers.

Question 2:

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

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

  public void printInfo() {
    System.out.println(sid + "," + name + "," + fees);
  }
}

public class ClassEx {
  public static void main(String[] args) {
    Student S = new Student();

    System.out.println(sizeof(S));
  }
}

Output:

/ClassEx.java:21: error: cannot find symbol
    System.out.println(sizeof(S));
                       ^
  symbol:   method sizeof(Student)
  location: class ClassEx
1 error

Explanation:

The above program will generate a compile-time error because of the below statement,

System.out.println(sizeof(S));

In the above statement, we used sizeof() to get the size of object S. The sizeof() is not built-in operator in Java.

Question 3:

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

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

  public void printInfo() {
    System.out.println(sid + "," + name + "," + fees);
  }
}

public class ClassEx {
  public static void main(String[] args) {
    Student S = new Student();

    System.out.println(S);
  }
}

Output:

[email protected]

Explanation:

In the above program, we created a Student class that contains data member id, name, and fees and we also created two methods setInfo() and printInfo().

The setInfo() is used to set the data members of the Student class and the printInfo() method is used to print the information of the Student class.

public static void main(String []args)
{
    Student S=new Student();
        
    System.out.println(S);
}

In the main() method, we created the object of the Student class. Then we print object S using println() method than "[email protected]" will be printed on the console screen.

Question 4:

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

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

  public void printInfo() {
    System.out.println(sid + "," + name + "," + fees);
  }

  public int getSize() {
    return (Integer.SIZE / 8) + name.length() + (Integer.SIZE / 8);
  }
}

public class ClassEx {
  public static void main(String[] args) {
    Student S = new Student();

    S.setInfo(101, "Anand", 2000);
    System.out.println(S.getSize());

  }
}

Output:

13

Explanation:

In the above program, we created a Student class that contains data member id, name, and fees and we also created two methods setInfo() and printInfo().

The setInfo() is used to set the data members of the Student class and the printInfo() method is used to print the information of the Student class.

In the main() method we created object S and set values of data members using the setInfo() method and then print the size of the object using the getSize() method on the console screen.

Question 5:

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

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

  public void printInfo() {
    System.out.println(sid + "," + name + "," + fees);
  }

  public int getSize() {
    return (Integer.SIZE / 8) + name.length() + (Integer.SIZE / 8);
  }
}

public class ClassEx {
  public static void main(String[] args) {
    Student S1 = new Student();
    Student S2 = new Student();

    S1.setInfo(101, "Anand", 2000);
    S2.setInfo(102, "Amit", 4000);

    System.out.println(S1.getSize());
    System.out.println(S2.getSize());

  }
}

Output:

13
12

Explanation:

In the above program, we created a Student class that contains data member id, name, and fees and we also created two methods setInfo() and printInfo().

The setInfo() is used to set the data members of the Student class and the printInfo() method is used to print the information of the Student class.

In the main() method we created two object S1 and S2 and then set values of data members using the setInfo() method and then print the size of objects S1 and S2 using the getSize() method on the console screen.



ADVERTISEMENT




ADVERTISEMENT



Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

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.