Menu
Topics Index
...
`

Class with static method


If you need explanation Read this topic

If you need Answer Take test on this topic

Class with static method
What will be the output of the following program?
public class NValueIs {
    public static void main(String[] args) {
        int num1 = 1;
        int num2 = 2;
        System.out.println("Before swap method, num1 is " + num1 + " and num2 is " + num2);
        swap(num1, num2);
        System.out.println("After swap method, num1 is " + num1 + " and num2 is " + num2);
    }
    public static void swap(int n1, int n2) {
        int temp = n1;
        n1 = n2;
        n2 = temp;
    }
}

A. Before swap method, num1 is 1 and num2 is 2
After swap method, num1 is 2 and num2 is 1
B. Before swap method, num1 is 1 and num2 is 2
After swap method, num1 is 1 and num2 is 2
C. Some other output
D. Compilation Error or Runtime Error
Topic: Static Keyword In Java

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.

Ans is A..Initially the values of num1 and num2 are 1 and 2 ..After swap method is called the values in num1 and num2 are interchanged then num1=2 and num2=1

Posted by Badam Swathi    2014-06-09 08:56:58


Ans:B First it displays "1,2 " next swapping will be done. but it does not reflect to num1,num2. Hence it displays "1-2 1-2 "

Posted by Indrasena Reddy    2014-06-09 09:25:29


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

Posted by Merit Campus    2014-06-10 11:39:07


© meritcampus 2019

All Rights Reserved.

Open In App