Q:

Java find output programs (Parameter Passing) | set 1

belongs to collection: Java find output programs

0

Find the output of Java programs | Parameter Passing | Set 1: Enhance the knowledge of Java Parameter Passing concepts by solving and finding the output of some Java programs.

Question 1:

public class ClsParameter {
  public static void swapValues(int & val1, int & val2) {
    int temp = 0;

    temp = val1;
    val1 = val2;
    val2 = val1;
  }

  public static void main(String[] args) {
    int X = 10;
    int Y = 20;

    swapValues(X, Y);

    System.out.println(X);
    System.out.println(Y);
  }
}

Question 2:

public class ClsParameter {
  public static void swapValues(int * val1, int * val2) {
    int temp = 0;

    temp = *val1; * val1 = *val2; * val2 = *val1;
  }

  public static void main(String[] args) {
    int X = 10;
    int Y = 20;

    swapValues( & X, &Y);

    System.out.println(X);
    System.out.println(Y);
  }
}

Question 3:

public class ClsParameter {
  private int val1 = 10;
  private int val2 = 20;

  public static void swapValues(ClsParameter C) {
    int temp = 0;

    temp = C.val1;
    C.val1 = C.val2;
    C.val2 = temp;
  }

  public static void main(String[] args) {
    ClsParameter C = new ClsParameter();

    swapValues(C);

    System.out.println(C.val1);
    System.out.println(C.val2);
  }
}

Question 4:

class Sample {
  private int val1 = 10;
  private int val2 = 20;

  public static void swapValues(Sample C) {
    int temp = 0;

    temp = C.val1;
    C.val1 = C.val2;
    C.val2 = temp;
  }
}

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

    Sample.swapValues(S);

    System.out.println(S.val1);
    System.out.println(S.val2);
  }
}

Question 5:

public class Sample {
  int val1 = 10;
  int val2 = 20;

  public static void swapValues(Sample C) {
    int temp = 0;

    temp = C.val1;
    C.val1 = C.val2;
    C.val2 = temp;
  }
}

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

    Sample.swapValues(S);

    System.out.println(S.val1);
    System.out.println(S.val2);
  }
}

Question 6:

class Sample {
  int val1 = 10;
  int val2 = 20;

  public static void swapValues(Sample C) {
    int temp = 0;

    temp = C.val1;
    C.val1 = C.val2;
    C.val2 = temp;
  }
}

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

    Sample.swapValues(S);

    System.out.println(S.val1);
    System.out.println(S.val2);
  }
}

All Answers

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

Answer Question 1:

Output:

/ClsParameter.java:2: error: <identifier> expected
  public static void swapValues(int & val1, int & val2) {
                                   ^
/ClsParameter.java:2: error: <identifier> expected
  public static void swapValues(int & val1, int & val2) {
                                               ^
/ClsParameter.java:2: error: <identifier> expected
  public static void swapValues(int & val1, int & val2) {
                                                      ^
3 errors

Explanation:

In the above program, we defined a static method swapValues() to swap two integer values, but we used '&' operator with arguments, we cannot use '&' operator with arguments in Java.

Answer Question 2:

Output:

/ClsParameter.java:2: error: <identifier> expected
  public static void swapValues(int * val1, int * val2) {
                                   ^
/ClsParameter.java:2: error: <identifier> expected
  public static void swapValues(int * val1, int * val2) {
                                               ^
/ClsParameter.java:2: error: <identifier> expected
  public static void swapValues(int * val1, int * val2) {
                                                      ^
/ClsParameter.java:5: error: illegal start of expression
    temp = *val1; * val1 = *val2; * val2 = *val1;
           ^
/ClsParameter.java:5: error: illegal start of expression
    temp = *val1; * val1 = *val2; * val2 = *val1;
                  ^
/ClsParameter.java:5: error: illegal start of expression
    temp = *val1; * val1 = *val2; * val2 = *val1;
                           ^
/ClsParameter.java:5: error: illegal start of expression
    temp = *val1; * val1 = *val2; * val2 = *val1;
                                  ^
/ClsParameter.java:5: error: illegal start of expression
    temp = *val1; * val1 = *val2; * val2 = *val1;
                                           ^
/ClsParameter.java:12: error: illegal start of expression
    swapValues( & X, &Y);
                ^
/ClsParameter.java:12: error: illegal start of expression
    swapValues( & X, &Y);
                     ^
10 errors

Explanation:

The above program will generate syntax errors because we use pointers in the swapValues() method to swap argument values, but java does not support pointers.

Answer Question 3:

Output:

20
10

Explanation:

In the above program, we created a class ClsParameter that contains two data members val1 and val2 initialized with 10 and 20 respectively. Here, we also created a static method swapValues() that takes the object of ClsParameter as an argument. Here, we swapped the values of val1 and val2 with each other using the temporary variable temp.

Now look to the main() method, here we created the object C of ClsParameter class and pass object C into static method swapValues and then print swapped values val1 and val2 on the console screen.

Answer Question 4:

Output:

/ClsParameter.java:20: error: val1 has private access in Sample
    System.out.println(S.val1);
                        ^
/ClsParameter.java:21: error: val2 has private access in Sample
    System.out.println(S.val2);
                        ^
2 errors

Explanation:

The above program will generate syntax errors because data members val1 and val2 are private members of Sample class but we accessed them from ClsParameter. But private members cannot be accessed outside the class.

Answer Question 5:

Output:

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

Explanation:

The above program will generate syntax error because we cannot create two public classes in a java program.

Answer Question 6:

Output:

20
10

Explanation:

In the above program, we created two classes Sample and ClsParameter. The Sample class contains two data members val1 and val2 and a static method swapValues() with the object of Sample class as an argument two swap values of data members val1 and val2 with each other.

Now look to the ClsParameter class. The ClsParameter class contains a main() method. In the main() method we created an object S of Sample class. Then we swapped values of val1 and val2 using static method swapValues() and then print swapped values 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 (Class and Objects) | se... >>
<< Java find output programs (static Keyword) | set 2...