1. Please answer the following questions. (20 points) We define the following 2-
ID: 3569658 • Letter: 1
Question
1. Please answer the following questions. (20 points) We define the following 2-D array; int[][][]x=new in [2][3][4]; Answer questions 1.1 -13. 1.1 What is the memory size of x (in terms of bytes)? (2 points) 1.2 What is the value of x[0][2]. length? 1.3 What is the value ofx[1][1][1]? (2 points) Give the following sequence of integers: 10,7,9,-5,33,18,0,7,5,1. Answer questions 1.4 -1.6. 1.4 How many comparisons do you need if you use sequential search algorithm to look up 0 and -1- respectively? 1.5 Suppose we first sort the sequence in an ascending order. Then how many comparisons do you need if you use the binary searching algorithm to look up 0 and -1 respectively? (4 points) 1.6 What is the difference between overriding and overloading in Java? (4 points) We have the following statements: String[] x = (Chicago, ?New Tory, Seattle, Los Angeles, Washington}; x[0]=x[0]+x[2] String y=x[0]; Answer questions 1.7-1.8 1.7 What is the value of y[7]? 1.8 What is the value of x[x. length ? 5]?Explanation / Answer
1)
size of int in java is 32 bits or 4 bytes
size of array is 2 x 3 x 4 x size of int = 96 bytes
2)
x[0][2].length is 4 x size of int = 16 bytes
3)
it's value is 0. since the elements are not assigned. default value stored will be 0.
4) using sequential search algorithm, you find a perticular element in an array by checking against all elements from first to last.
take it takes to find 0 is 7 (0 is 7th element in the array)
time it takes for -1 is 10 (since -1 is not present in the array. we know that only by searching through all the elements)
5)
10 7 9 -5 33 18 0 7 5 1 is the original array
-5 0 1 5 7 7 9 10 18 33 is the sorted array
using binary search algorithm
finding 0 takes 3 steps
finding -1 takes 4 steps ( Not found )
6)
7)
x[0] = x[0] + x[2]
it first concatinates Chicago and Seattle (Chicago + Seattle = ChicagoSeattle) and stores the result in x[0]
string y = x[0] stores ChicagoSeattle in y
y[7] = character of string y indexed at 7 i.e, character at 8th position = S
y[7] = S
8)
x.length = 5 (size of the string array)
x[x.length - 5] = x[5 - 5] = x[0] = ChicagoSeattle
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.