Menu
Question Index
...

What will be the output of the following program?

import java.lang.reflect.Array;
public class DailyDoseC {
    public static void main(String args[]) {
        int[] array = (int[]) Array.newInstance(int.class, 3);
        for (int i = 0; i < array.length; i++) {
            array[i] = i;
        }
        int[] arrayDoubled = (int[]) getArray(array);
        for (int i : arrayDoubled) {
            System.out.print(i + " ");
        }
    }
    static Object getArray(Object original) {
        Object returnValue = null;
        Class type = original.getClass();
        if (type.isArray()) {
            int length = Array.getLength(original);
            Class elementType = type.getComponentType();
            returnValue = Array.newInstance(elementType, length * 2);
            System.arraycopy(original, 0, returnValue, 0, length);
        }
        return returnValue;
    }
}


0, 1, 2, 0, 0, 0,
0, 0, 0, 0, 0, 0,
1, 2, 0, 0,0, 0,
6, 6, 6, 6, 6, 6,
Compilation Error or Runtime Error

Doubts

Problems

Topic: Advanced Java Programming Concepts

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App