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

Find the output of Java programs | Class and Objects | Set 1: 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:

class Sample {
  int var1;
  int var2;

  public void set(int v1, int v2) {
    var1 = v1;
    var2 = v2;
  }
  public void print() {
    System.out.println(var1 + "," + var2);
  }

}

public class ClassEx {
  public static void main(String[] args) {
    Sample S;

    S.set(10, 20);
    S.print();
  }
}

Output:

/ClassEx.java:19: error: variable S might not have been initialized
    S.set(10, 20);
    ^
1 error

Explanation:

The above program will generate syntax error because we did not create an object of the Sample class correctly.

The correct way to create an object of Sample class is given below:

Sample S = new Sample()

In Java, we need to use the new operator to create an object of the class.

Question 2:

class Sample {
  int var1;
  int var2;

  public void set(int v1, int v2) {
    var1 = v1;
    var2 = v2;
  }
}

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

    S.set(10, 20);
    System.out.println(S.var1 + "," + S.var2);
  }
}

Output:

10,20

Explanation:

In the above program, we created a class Sample that contains two data members var1 and var2 without any access modifier. If we did not specify the access modifier then it will be treated as default, it means these data members can be accessed within the same package.

In the Sample class, we defined a method set() to set the values of data members of the Sample class.

In the main() method we created object S of Sample class and then call set() method to set values to the data members and then print the values of data members on the console screen.

Question 3:

class Sample {
  int var1;
  int var2;

  public void set(int v1, int v2) {
    var1 = v1;
    var2 = v2;
  }
}S;

public class ClassEx {
  public static void main(String[] args) {
    S.set(10, 20);
    System.out.println(S.var1 + "," + S.var2);
  }
}

Output:

/ClassEx.java:9: error: class, interface, or enum expected
}S;
 ^
1 error

Explanation:

The above program will generate a compile-time error because we did not create an object of the Sample class correctly.

Here, we created object S in C++ fashion but we cannot create an object like this in Java.

The correct way to create an object is given below:

Sample S = new Sample();

Question 4:

public class Sample {
  int var1;
  int var2;

  public void set(int v1, int v2) {
    var1 = v1;
    var2 = v2;
  }
}

public class ClassEx {
  public static void main(String[] args) {
    Sample S = new Sample();
    S.set(10, 20);
    System.out.println(S.var1 + "," + S.var2);
  }
}

Output:

/Sample.java:11: error: class ClassEx is public, should be declared 
in a file named ClassEx.java
public class ClassEx {
       ^
1 error

Explanation:

The above program will generate a compile-time error because we can create only one public class that contains the main() method in Java.

Question 5:

class Sample {
  int var1 = 20;
  int var2 = 40;

  public void set(int v1, int v2) {
    var1 = v1;
    var2 = v2;
  }
}

public class ClassEx {
  public static void main(String[] args) {
    Sample S = new Sample();
    System.out.println(S.var1 + "," + S.var2);
  }
}

Output:

20,40

Explanation:

In the above program, we created a class Sample that contains two data members var1 and var2 initialized with 20 and 40 respectively.

In the main() method, we created object S of Sample class and then print the values of data members on the console screen.






Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.