Topics Index

Fire Alarm

If you need explanation Read this topic If you need explanation Read this topic

If you need Answer Take test on this topic

Fire Alarm
What will be the output of the following program?
public class FireAlarm {
    public static void main(String[] args) {
        double d[] = {2.3,3.4,4.5};
        modify(d); p(d);
        modify2(d); p(d);
        modify(d); p(d);
    private static void p(double[] d) {
        for (double d1 : d) {
            System.out.print(d1 + ",");
    private static void modify(double[] d) {
        d[0] = d[1];
        d[1] = d[2];
        d[2] = d[0];
    private static void modify2(double[] d) {
        d = new double[3];
        d[0] = d[1] = d[2] = 1.0;

A. {2.3,3.4,4.5,},{3.4,4.5,3.4,},{3.4,4.5,3.4,},{4.5,3.4,4.5,},
B. {2.3,3.4,4.5,},{3.4,4.5,3.4,},{1.0,1.0,1.0,},{1.0,1.0,1.0,},
C. {2.3,3.4,4.5,},{3.4,4.5,2.3,},{3.4,4.5,2.3,},{4.5,2.3,3.4,},
D. Some other output or Compilation Error
Topic: Java Multiple Methods In One Class Static Keyword In Java

If you need explanation Read this topic 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 B,,, new creation of variable D is not affected bcoz it is in seperate method,,

Posted by Raju VarshnEy    2014-10-27 08:44:33

ans b.
array is declared initially using type double.

by calling method p(d)
it prints array value using for loop.

after modify(d) called.
hre shifting is done
0 th index replaced with 1 , 1 replace with 2 ,2 replaced with 0.

thn p (d) prints shifted values.

modify2(d) called hre all array values initialized with 1.0
thn p( d) prints newly initialized value.

motify(d) shifts 1to0,2to1,0 to 2 thn p(d) method prints shifted value .

Posted by Maheshwari Natarajan    2014-10-27 11:42:18

Answer is the first step of main method,we are initializing a double array then we are calling method p which actually prints the elements in the array....modify(d) is called then d[0] = 3.4 d[1] = 4.5 d[2] = 3.4 and p(d) will print these modify2(d) is called,since we are manipulating the values on the passed reference those values are affected.Next modify(d) is called ,still d[0] = 1 d[1] = 1 d[2] = 1...p(d) will print these values.

Posted by Shashanka Mogaliraju    2014-10-27 11:44:44

Ans is we create an double array with initial elements as 2.3,3.4,4.5

here we are calling p() which is java we can cal static withou using object..from one static method to another static method

in p() we are printing the elements of array using the for each loop which is safe for Exceptions..

next we are calling modify() by passing array..passing an array is always cal by reference in java...

in modify() d is pointing the array...
than it wil Bcm Lik
modification made here wil effects in the actual array...(Call by reference)

next we are calling P() there these modified elements wil get printed..

next we are calling modify2() by passin array which is call by reference..

here in this method initiall d is pointing the arry which is there in main() this method we create an array with size 3..and initilized with this d is pointing newly created array not old array..
next we are calling p() here it wil print sam elements bcz Modification didnt Occur here..

next calling modify()--sam thing happens as explained above

Posted by Uday Kumar    2014-10-27 15:37:49

This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Shubham Bansal. Please login into Merit Campus using facebook, to claim your recharge. Go to to raise the recharge.

Posted by Merit Campus    2014-10-28 03:41:17

© meritcampus 2019

All Rights Reserved.

Open In App