Skip to content

Latest commit

 

History

History
81 lines (59 loc) · 2.72 KB

09 - CD Basic.md

File metadata and controls

81 lines (59 loc) · 2.72 KB

✨ CD Basic ✨

Consider the following array creation statement and answer the questions that follow.

int[] taxi = {10, 70, 20, 90};
  1. Draw the memory diagram that represents the reference and the instance. Clearly label each as reference and instance.
  2. What is the value of taxi.length?
  3. What is the index of the last item?
  4. What do you get when you display taxi[3]?
  5. What do you get when you display taxi[0]?
  6. What do you get when you display taxi[-1]?
  7. What do you get when you display taxi[4]?

For an arbitrary array cab that contains 1 or more items,

  1. What is the index of the last item?
  2. What is the variable that gives you the value of the last item (for example, cab[0] gives you the first item)?
  3. Write a statement that doubles the last item. For example, if the last item is 10, it should become 20.

For an arbitrary array cab that contains 0 or more items (but is definitely not null),

  1. Write a statement that displays the last item, if any (do nothing if array is empty).
  2. Write a statement that doubles the last item, if any (do nothing if array is empty).

Create an array,

  1. that holds 200 integer values.
  2. that holds 5000 salaries (think about the data type).
  3. that holds, for a binary image, values of 240000 pixels (think about the data type).
  4. that holds the values 10, 70, 20, 90, 30, 80, 40, 70.
  5. that holds the values 10, 70, 20, 90, 30, 80.6, 40, 70.

For an arbitrary array cab that contains 0 or more integer values (but is definitely not null), write a piece of code that,

  1. displays items of the array, each on a different line
  2. stores the sum of all items in a variable total
  3. stores the number of positive items in a variable countPositives
  4. stores the sum of all odd items in a variable sumOdds

What is the value of result after the following code is executed:

int[] a = {10, 70, 20, 90};
int[] b = {50, 30, 80};
int[] c = a;
a = b;
b = c;
int result = a[0]+b1-c[2];

Write a loop to calculate the sum of any given one-dimensional array named arr.

For example, for the following arr, the sum will be 135:

int arr[] = {10, 30, 50, 45};

What is the value of foo[3] + bar[2] * bar[1]?

int[] foo = {20, 2, 5, 1, 10, 3, 9};
int[] bar = {0, 9, 32, 59, 2, 5, 8};