ADVERTISEMENT
ADVERTISEMENT

Java find output programs (Parameter Passing) | set 1

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.
Submitted by Nidhi, on February 02, 2021

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

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.

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

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.

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

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.

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

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.

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

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.

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

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.



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


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.