Chapter 5: Arrays

Subdecks (1)

Cards (88)

  • Given array scorePerQuiz has 10 elements. Which assigns element 7 with the value 8?
    Question options:
    scorePerQuiz = 8;
    scorePerQuiz[0] = 8;
    scorePerQuiz[7] = 8;
    scorePerQuiz[8] = 7;
    scorePerQuiz[7] = 8;
  • What is the index of the last element?int[] numList = new int[50];
    Question options:
    0
    49
    50
    Unknown, because the array has not been initialized.
    49
  • What is the ending value of the element at index 0?
    int[] numbers = new int[10];
    numbers[0] = 35;
    numbers[1] = 37;
    numbers[1] = numbers[0] + 4;
    Question options:
    0
    35
    37
    39
    35
  • What is the ending value of the element at index 1?
    int[] numbers = new int[10];
    numbers[0] = 35;
    numbers[1] = 37;
    numbers[1] = numbers[0] + 4;
    Question options:
    0
    35
    37
    39
    39
  • How many elements does the array declaration create?
    int[] scores = new int[10];
    scores[0] = 25;
    scores[1] = 22;
    scores[2] = 18;
    scores[3] = 28;
    Question options:
    0
    4
    9
    10
    10
  • Which is an invalid access for the array?
    int[] numsList = new int[5];
    int x = 3;
    Question options:
    numsList[x-3]
    numsList[0]
    numsList[x+2]
    numsList[(2*x) - x]
    numsList[x+2]
  • Which XXX / YYY declare an array having MAX_SIZE elements and initializes all elements with -1?
    final int MAX_SIZE = 4;
    int[] myNumbers = new int[XXX];
    int i;
    for (i = 0; i < YYY; ++i) {
    myNumbers[i] = -1;
    }
    Question options:
    MAX_SIZE / MAX_SIZE
    MAX_SIZE / MAX_SIZE - 1
    MAX_SIZE - 1 / MAX_SIZE
    MAX_SIZE - 1 / MAX_SIZE - 1
    MAX_SIZE / MAX_SIZE
  • Which assigns the array's first element with 99?int[] myVector = new int[4];
    Question options:
    myVector[] = 99;
    myVector[-1] = 99;
    myVector[0] = 99;
    myVector[1] = 99;
    myVector[0] = 99;
  • Which assigns the array's last element with 99?int[] myVector = new int[15];
    Question options:
    myVector[0] = 99;
    myVector[14] = 99;
    myVector[15] = 99;
    myVector[16] = 99;
    myVector[14] = 99;
  • What are the ending contents of the array? Choices show elements in index order 0, 1, 2.
    int[] yearsList = new int[3];
    yearsList[0] = 5;
    yearsList[1] = yearsList[0];
    yearsList[0] = 10;
    yearsList[2] = yearsList[1];
    Question options:
    5, 5, 5
    5, 10, 10
    10, 10, 10
    10, 5, 5
    10, 5, 5
  • Which XXX and YYY correctly output the smallest values? Array userVals contains 100 elements that are integers (which may be positive or negative). Choices are in the form XXX / YYY.
    // Determine smallest (min) value
    int minVal;
    XXX
    for (i = 0; i < 100; ++i) {
    if (YYY) {
    minVal = userVals[i];
    }
    }
    System.out.println("Min: " + minVal);
    Question options:
    minVal = 0; / userVal > minVal
    minVal = 0; / userVal < minVal
    minVal = userVals[0]; / userVals[i] > minVal
    minVal = userVals[0]; / userVals[i] < minVal
    minVal = userVals[0]; / userVals[i] < minVal
  • The following program generates an error. Why?final int NUM_ELEMENTS = 5;
    int[] userVals = new int[NUM_ELEMENTS];
    int i;
    userVals[0] = 1;
    userVals[1] = 7;
    userVals[2] = 4;
    for (i = 0; i <= userVals.length; ++i) {
    System.out.println(userVals[i]);
    }
    Question options:
    Variable i is never initialized.
    The array userVals has 5 elements, but only 3 have values assigned.
    The integer NUM_ELEMENTS is declared as a constant
    The for loop tries to access an index that is out of the array's valid range.
    The for loop tries to access an index that is out of the array's valid range.
  • The numNegatives variable counts the number of negative values in the array userVals. What should numNegatives be initialized to?
    int[] userVals = new int[20];
    int i;
    numNegatives = XXX;
    for (i = 0; i < 20; ++i) {
    if (userValues[i] < 0) {
    numNegatives = numNegatives + 1;
    }
    }
    Question options:
    No initialization needed
    0
    -1
    userVals[0]
    0
  • Which best describes what is output? Assume v is a large array of ints.
    int i;
    int s;
    s = v[0];
    for (i = 0; i < N_SIZE; ++i) {
    if (s > v[i]) {
    s = v[i];
    }
    }
    System.out.println(s);
    Question options:
    first value in v
    max value in v
    min value in v
    last value in v
    min value in v
  • Given two arrays, studentNames that maintains a list of student names, and studentScores that has a list of the scores for each student, which XXX and YYY prints out only the student names whose score is above 80? Choices are in the form XXX / YYY.
    String[] studentNames = new String[NUM_STUDENTS];
    int[] studentScores = new int[NUM_STUDENTS];
    int i;
    for (i = 0; i < NUM_STUDENTS; ++i) {
    if (XXX > 80) {
    System.out.print(YYY + " ");
    }
    }
    Question
    options:studentNames[i] / studentNames[i]
    studentNames[i] / studentScores[i]
    studentScores[i] / studentNames[i]
    studentScores[i] / studentScores[i]
    studentScores[i] / studentNames[i]
  • Two arrays, itemsNames and itemsPrices, are used to store a list of item names and their corresponding prices. Which is true?
    Question options:
    Using two arrays saves memory versus using one array.
    Both arrays should be of the same data type.
    Both arrays should be declared to have the same number of elements.
    The item names should appear in both arrays.
    Both arrays should be declared to have the same number of elements.
  • Given two arrays, which code will output all the arrays' elements, in the order key, item followed by a newline?
    int[] keysList = new int[SIZE_LIST];
    int[] itemsList = new int[SIZE_LIST];
    Question options:
    System.out.println(keysList[SIZE_LIST - 1] + ", " + itemsList[SIZE_LIST - 1]);
    System.out.println(keysList[SIZE_LIST] + ", " + itemsList[SIZE_LIST]);
    for (i = 0; i < SIZE_LIST - 1; ++i) { System.out.println(keysList[i] + ", " + itemsList[i]);
    for (i = 0; i < SIZE_LIST; ++i) { System.out.println(keysList[i] + ", " + itemsList[i]);}
    for (i = 0; i < SIZE_LIST; ++i) { System.out.println(keysList[i] + ", " + itemsList[i]);
  • Given x = 4 and y = 8, what are the ending values of x and y?
    x = y;
    y = x;
    x = y;
    Question options:
    x = 4, y = 4
    x = 4, y = 8
    x = 8, y = 4
    x = 8, y = 8
    x = 8, y = 8
  • What code for XXX, YYY correctly swaps a and b? Choices are written as XXX / YYY.
    XXX;
    a = b;
    YYY;
    b = tmp;
    Question options:
    tmp = a / (nothing)
    tmp = a / tmp = b
    tmp = b / (nothing)
    (nothing) / tmp = a

    tmp = a / (nothing)
  • Given that integer array x has elements 4, 7, 3, 0, 8, what are the elements after the loop?
    int i;
    for (i = 0; i < 4; ++i) {
    x[i] = x[i + 1];
    }
    Question options:
    4, 4, 7, 3, 0
    7, 3, 0, 8, 8
    7, 3, 0, 8, 4
    Error: Invalid access
    7, 3, 0, 8, 8
  • Given that integer array x has elements 5, 10, 15, 20, what is the output?
    int i;
    for (i = 0; i < 4; ++i) {
    System.out.print(x[i] + x[i + 1]);
    }
    Question options:
    10, 15, 20, 5
    15, 25, 35, 25
    15, 25, 35, 20
    Error: Invalid access
    Error: Invalid access
  • Given two integer arrays prevValues and newValues. Which code copies the array prevValues into newValues? The size of both arrays is NUM_ELEMENTS.
    Question options:
    newValues = prevValues;
    newValues[] = prevValues[];
    newValues[NUM_ELEMENTS] = prevValues[NUM_ELEMENTS];
    for (i = 0; i < NUM_ELEMENTS; ++i) { newValues[i] = prevValues[i];}

    for (i = 0; i < NUM_ELEMENTS; ++i) { newValues[i] = prevValues[i];}
  • Given two integer arrays, largerArray with 4 elements, and smallerArray with 3 elements. What is the result after this code executes?
    for (i = 0; i < 4; ++i) {
    largerArray[i] = smallerArray[i];
    }
    Question options:
    All the elements of largerArray will get copied to smallerArray.
    Some of the elements of largerArray will get copied to smallerArray.
    All of the elements of smallerArray will get copied to largerArray.
    Error: The two arrays are not the same size.
    Error: The two arrays are not the same size.
  • Which XXX and YYY correctly complete the code to find the maximum score? Choices are in the form XXX / YYY.
    int[] scores = {43, 24, 58, 92, 60, 72};
    int maxScore;
    maxScore = scores[0];
    for (XXX) {
    if (num > maxScore) {
    YYY;
    }
    }
    Question options:
    int scores: num / maxScore = num
    scores != 0 / num = maxScore
    int num: scores / maxScore = num
    num < scores / num = maxScore
    int num: scores / maxScore = num
  • Which regular loop corresponds to this enhanced for loop?
    char[] vowels = {'a', 'e', 'i', 'o', 'u'};
    for (char item: vowels) {
    System.out.println(item);
    }
    Question options:
    for (int i = 0; i < item; ++i) { System.out.println(vowels[i]);}
    for (int i = 0; i < item.length; ++i) { System.out.println(vowels[i]);}
    for (int i = 0; i < vowels.length; ++i) { System.out.println(vowels[i]);}
    for (int i = 0; i < vowels.length; ++i) { System.out.println(item[i]);}
    for (int i = 0; i < vowels.length; ++i) { System.out.println(vowels[i]);}
  • Which XXX / YYY declare an array having MAX_SIZE elements and initializes all elements with -1?

    final int MAX_SIZE = 4; int[] myNumbers = new int[XXX]; int i; for (i = 0; i < YYY; ++i) { myNumbers[i] = -1; }

    MAX_SIZE / MAX_SIZE
    MAX_SIZE / MAX_SIZE - 1
    MAX_SIZE - 1 / MAX_SIZE - 1
    MAX_SIZE - 1 / MAX_SIZE
    MAX_SIZE / MAX_SIZE
  • What code for XXX, YYY correctly swaps a and b? Choices are written as XXX / YYY.

    XXX; a = b; YYY; b = tmp;

    tmp = a / tmp = b
    tmp = a / (nothing)
    (nothing) / tmp = a
    tmp = b / (nothing)

    tmp = a / (nothing)
  • Which expression for XXX causes the code to output the strings in alphabetical order? (Assume the strings are lowercase)

    if (XXX) { System.out.println(firstStr + " " + secondStr); } else { System.out.println(secondStr + " " + firstStr); }

    firstStr.compareTo(secondStr) < 0
    !firstStr.equals(secondStr)
    firstStr.equals(secondStr)
    firstStr.compareTo(secondStr) > 0
    firstStr.compareTo(secondStr) < 0
  • Which if branch executes when an account lacks funds and has not been used recently? hasFunds and recentlyUsed are booleans and have their intuitive meanings.
    if (!hasFunds && !recentlyUsed)
    if (!hasFunds && recentlyUsed)
    if (hasFunds && recentlyUsed)
    if (hasFunds && !recentlyUsed)

    if (!hasFunds && !recentlyUsed)
  • What values for x cause Branch 1 to execute?
    If x > 100 : Branch 1
    Else If x > 200: Branch 2
    100 to 200
    101 or larger
    100 or larger
    101 to 200
    101 or larger
  • Which is an example of a process?
    x + y
    int
    y, 10
    15
    x + y
  • Which XXX will output only values up to 3? i is an int.

    for (i = 1; i < 10; i++) { System.out.print(i + " "); XXX { break; } }

    if (i != 3)
    if (i != 4)
    if (i == 4)
    if (i == 3)
    if (i == 3)
  • What is the output if count is 4?

    for (i = count; i > 0; --i) { // Output i }

    4
    43210
    4321
    321
    4321
  • Which for loop will iterate 100 times?
    for (i = 0; i < 100; i++)
    for (i = 1; i < 99; i++)
    for (i = 1; i < 100; i++)
    for (i = 0; i < 99; i++)
    for (i = 0; i < 100; i++)
  • The program should output even values between -10 and 10 (inclusive), so -10 -8 ... 8 10. What should XXX be?

    for (XXX) { System.out.print(i + " "); }

    i = -10; (i * 2) <= 10, ++i
    i = -10; (i * 2) < 10; ++i
    i = -10; i <= 10; i = i + 2
    i = -10; i < 10; i = i + 2

    i = -10; i <= 10; i = i + 2
  • What is not a key advantage of using an enum as below have versus using a string for myCar and string literals like "red", "green", and "black"?

    public enum CarColor {RED, GREEN, BLACK} CarColor myCar; myCar = CarColor.GREEN; if (myCar == CarColor.GREEN) { System.out.print("Green is our most popular color"); }

    The possible colors are made explicit
    The compiler will yield an error for a misspelled color like GREN
    Capital letters can be used to make the colors clearer
    The compiler will yield an error for an unsupported color like BLUE

    Capital letters can be used to make the colors clearer
  • Which statement is true?
    Javadoc tags should precede methods and class definitions
    Javadoc comments are intended for the initial developer rather than future programmers who will interface with the code
    Javadoc comments are placed in separate files from source code
    The @loop tag should be used when appropriate
    Javadoc tags should precede methods and class definitions
  • Which is not a good use of a perfect size array?
    university student IDs
    names of the week
    names of the months
    names of marker colors in a box of 8
    university student IDs
  • The world population is over 7 billion. Which declaration uses the fewest bits while guaranteeing that worldPopulation can be assigned the value 7 billion without error?
    int worldPopulation;
    byte worldPopulation;
    short worldPopulation;
    long worldPopulation;
    long worldPopulation;
  • Which XXX completes the following code?

    import java.util.Scanner; public class GuessMyAge { public static void myGuess(int low, int high, Scanner scnr) { int mid; char userAnswer; mid = (low + high) / 2; System.out.print("Is it " + mid + "? (/=): "); userAnswer = scnr.next().charAt(0); if ((userAnswer != '<') && (userAnswer != '>')) { System.out.println("Great!"); } else { if (userAnswer == '>') { XXX } else { myGuess(low, mid, scnr); } } } public static void main(String[] args) { Scanner scnr = new Scanner(System.in); myGuess(0, 100, scnr); } }

    myGuess(mid + 1, high, scnr);
    myGuess (mid, high, scnr);
    myGuess (low, high, scnr );
    myGuess(low, mid, scnr);
    myGuess(mid + 1, high, scnr);