Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Consider the following method: public int[][] makeCounts(int n) { int[][] counts = new int[3][n]; counts[0][0] = 0; counts[1][0] = 0; counts[2][0] = 1; for (int

  1. Consider the following method:

    public int[][] makeCounts(int n) { int[][] counts = new int[3][n]; counts[0][0] = 0; counts[1][0] = 0; counts[2][0] = 1; for (int k = 1; k < n; k++) { counts[0][k] = counts[0][k-1] + counts[1][k-1]; counts[1][k] = counts[1][k-1] + counts[0][k-1] + counts[2][k-1]; counts[2][k] = counts[2][k-1] + counts[1][k-1]; } return counts; }

    What values are in the array returned by makeCounts(5)?

    1. 0 0 1 1 1 0 1 1 1 1 1 1 1 1 1

    2. 0 0 1 2 3 0 1 2 3 4 1 2 3 4 5

    3. 0 0 1 3 6 0 1 2 5 8 1 1 4 7 9

    4. 0 0 1 3 8 0 1 2 5 12 1 1 2 4 9

    5. 0 1 3 9 27 0 1 3 9 27 1 1 3 9 27

  2. What is the output from the following code segment?

    List cities = new ArrayList(); cities.add("Atlanta"); cities.add("Boston"); cities.add("Chicago"); for (String city : cities) city = city.substring(1); System.out.println(cities);

    1. [A, B, C]

    2. [C, B, A]

    3. [a, n, o]

    4. [tlanta, oston, hicago]

    5. [Atlanta, Boston, Chicago]

  3. Consider the following two recursive versions of the method choose(n, k).

    Version 1

    public static int choose(int n, int k) { if (k == 0) return 1; else return choose(n, k-1) * (n-k+1)/k; }

    Version 2

    public static int choose(int n, int k) { if (k < 0 || k > n) return 0; else if (n == 0) return 1; else return choose(n-1, k-1) + choose(n-1, k); }

    When choose(4, 2) is called, how many times total, including the original call, will choose be called in each version?

    Version 1 Version 2
    2 7
    2 19
    3 7
    3 19
    3 27

  4. Given two arrays of double values, sorted in ascending order, one with 100 elements, the other with 10 elements, how many comparisons will it take in an optimal algorithm to merge these arrays into one sorted array, in the best case and in the worst case?

    Best case Worst case
    10 109
    50 110
    100 110
    109 999
    100 1000

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Current Trends In Database Technology Edbt 2004 Workshops Edbt 2004 Workshops Phd Datax Pim P2panddb And Clustweb Heraklion Crete Greece March 2004 Revised Selected Papers Lncs 3268

Authors: Wolfgang Lindner ,Marco Mesiti ,Can Turker ,Yannis Tzitzikas ,Athena Vakali

2005th Edition

3540233059, 978-3540233053

More Books

Students also viewed these Databases questions