Menu
Topics Index
...
`

Methods Declaration


If you need explanation Read this topic

If you need Answer Take test on this topic

Methods Declaration
What will be the output of the following program?
public class Array {
    int a = 2;
    int b = 4;
    public void setData(int a, int b) {
        a = a;
        b = b;
    }
    public void showData() {
        System.out.println(a + " " + b);
    }
    public static void main(String args[]) {
        Array obj = new Array();
        obj.setData(2, 3);
        obj.setData(1, 3);
        obj.showData();
    }
}

A. 1 3
B. 2 3
C. 2 4
D. Compilation Error
E. Runtime Error
Topic: Java Multiple Methods In One Class

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

d

Posted by Prudhvi Raj    2014-03-07 12:42:04


A

Posted by Sai Abhishek    2014-03-07 13:05:59


Answer is C
As Local Variables are being assigned to Local Variables...
To access class variable it should be this.a

Posted by Jayachandra Gupta    2014-03-07 13:12:26


Ans is C since a and b values are 2 and 4 and passed as parameters in setData function and we created obj to class in main and and objects is shown by obj.showData() and printed as 2 space 4

Posted by Bharath Yelchuri    2014-03-07 14:28:26


Ans b because first execute give value initially and then passed set funtion call

Posted by Prudviraj Regumani    2014-03-07 16:16:08


Congratulations Jayachandra Gupta. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-03-10 05:27:51


© meritcampus 2019

All Rights Reserved.

Open In App