MCQ IN COMPUTER SCIENCE & ENGINEERING

COMPUTER SCIENCE AND ENGINEERING

DATA STRUCTURES

Question [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER]
When run, which of the following yields the integer 5?
A
int[] newArray = {5, 5, 5, 5, 5, 5};System.out.println(newArray.length);
B
String pets[] = new String[5];System.out.println(pets.length());
C
int[] anotherArray = {5, 10, 15, 20, 25};System.out.println(anotherArray[(anotherArray.length-2)]-15);
D
String[] favoriteNumbers = {"5", “7", “12", “13", “5"};System.out.println(favoriteNumbers[favoriteNumbers[0]]);
E
int[] greatArray = {5, 4, 3, 2, 1, 0};System.out.println(greatArray.get(0));
Explanation: 

Detailed explanation-1: -When we declare int A[5], that means size of array A[] is 5, which can store values in memory locations from A to A+4.

Detailed explanation-2: -Which one of the following will declare an array and initialize it with five numbers? Explanation: Option B is the legal way to declare and initialize an array with five elements. Option A is wrong because it shows an example of instantiating a class named Array, passing the integer value 5 to the object’s constructor.

Detailed explanation-3: -For example, creating an int array of size 10: int array[] = new int[10]; By default, you will already have 10 arrays (all populated with default 0). You will always have these 10 elements at your disposal.

Detailed explanation-4: -array size 10^7 to 10^8 declared globally(i.e. on heap) is possible…if u declare nething in the main or in ne fxn…it goes into the stack which has a smaller size hence ur 10^7 array did not work out… try declaring it globally and it should work…

There is 1 question to complete.