Menu
Topics Index
...
`

HaiTwice


If you need explanation Read this topic

If you need Answer Take test on this topic

HaiTwice
What will be the output of the following program?
public class HaiTwice {
    static int num;
    public static void main(String[] args) {
        HaiTwice p = new HaiTwice();
        p.start();
        System.out.println(num);
    }
    void start() {
        int var = 7;
        twice(var);
        System.out.print(var + " ");
    }
    void twice(int var) {
        var = var * 2;
        num = var;
    }
}

A. 7 7
B. 7 14
C. 14 0
D. 14 14
E. Compilation Error or Runtime Error
Topic: Static and Non Static Variables - Static and Non Static Methods

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.

14 14

Posted by Arul Palanisamy    2014-07-28 03:46:51


option B 7 14
as in start method var value will remain 7 only,
But in twice(int) function num is assigned to local variable var of twice function

Posted by Sasikanth Sharma    2014-07-28 03:54:23


b. 7 14
in start() method var assigned as 7 but in twice() method it doesn't change because of call by value..
after while num will assigned 14 because of static variable..

Posted by Shubham Bansal    2014-07-28 04:27:05


b

Posted by Ria Gupta    2014-07-28 10:15:20


b

Posted by Din Kurth    2014-07-29 01:06:53


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

Posted by Merit Campus    2014-07-29 04:39:41


© meritcampus 2019

All Rights Reserved.

Open In App