Programming Techniques: Arrays and Variables
38 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

Which syntax correctly defines an array with the integers 1, 2, and 3?

  • array = [1, 2, 3] (correct)
  • array {1, 2, 3}
  • array = <1, 2, 3>
  • array: (1, 2, 3)
  • In most programming languages, the index of the first element in an array is one.

    False

    What is the purpose of the pop() function in relation to arrays?

    To remove elements from an array.

    An array where the index starts at one is known as a ______ array.

    <p>one-indexed</p> Signup and view all the answers

    Match the following array definitions with their characteristics:

    <p>Zero-indexed array = Index starts at zero One-indexed array = Index starts at one Pop function = Removes the last element from the array Empty array = Contains no elements</p> Signup and view all the answers

    What will happen when the line 'highscores = 8' is executed in the program 'highscores = [125,63,35,12]'?

    <p>It will produce an error message.</p> Signup and view all the answers

    The append() function is used to add multiple elements to an array.

    <p>False</p> Signup and view all the answers

    What is the purpose of the extend() function in arrays?

    <p>To add multiple elements to the end of an array.</p> Signup and view all the answers

    The function used to remove the last item from an array is called ______.

    <p>pop</p> Signup and view all the answers

    Match the following array functions with their purposes:

    <p>Append() = Add a single element to the end of the array Extend() = Add multiple elements to the end of the array Remove() = Delete a specific element from the array Clear() = Remove all elements from the array</p> Signup and view all the answers

    What would be the output of printing the array 'highscores' using the print function directly?

    <p>[125, 63, 35, 12]</p> Signup and view all the answers

    Using an asterisk (*) before the array name in the print function will display the elements in a list format.

    <p>False</p> Signup and view all the answers

    How can the elements of an array be printed each on a new line?

    <p>By using print(*array_name, sep='\n')</p> Signup and view all the answers

    To print the elements of an array without brackets and commas, you can use an asterisk () before the name of the array, like this: print(______).

    <p>array_name</p> Signup and view all the answers

    Match the printing method with its description:

    <p>print(highscores) = Prints elements with brackets and commas print(*highscores) = Prints elements without brackets and commas print(*highscores, sep='\n') = Prints each element on a new line for element in highscores: print(element) = Prints each element in a loop</p> Signup and view all the answers

    Which programming construct allows for repeating a block of code based on a condition?

    <p>Iteration</p> Signup and view all the answers

    An array can only hold a single type of data.

    <p>False</p> Signup and view all the answers

    What function in programming can be used to count the number of elements in an array?

    <p>len()</p> Signup and view all the answers

    To create an array, the identifier is followed by the equals symbol and then open and closed ______.

    <p>square brackets</p> Signup and view all the answers

    Match the programming constructs with their descriptions:

    <p>Sequence = Executes instructions in the order they are written Selection = Chooses between different paths based on conditions Iteration = Repeats a block of code multiple times based on criteria</p> Signup and view all the answers

    What is the purpose of slicing an array?

    <p>To access a subset of elements from an array</p> Signup and view all the answers

    In an array, each value is referred to as an element.

    <p>True</p> Signup and view all the answers

    What is the difference between a variable and an array?

    <p>A variable stores one value, while an array stores multiple values under a single name.</p> Signup and view all the answers

    The len() function can be used to remove elements from an array.

    <p>False</p> Signup and view all the answers

    What is the syntax for using the slice function?

    <p>array_name[slice(start, stop, step)]</p> Signup and view all the answers

    The function _______ sorts the elements of an array into ascending order.

    <p>sort()</p> Signup and view all the answers

    Match the following array functions with their descriptions:

    <p>reverse() = Reverses the elements in an array sort() = Sorts an array into ascending order count() = Returns the number of occurrences of a specific element clear() = Removes all elements from an array</p> Signup and view all the answers

    What will the following code output: print(len([10, 20, 30, 40]))?

    <p>4</p> Signup and view all the answers

    The slice() function modifies the original array.

    <p>False</p> Signup and view all the answers

    How many elements are removed from the array using the clear() function?

    <p>All elements</p> Signup and view all the answers

    What does the function max() do?

    <p>Returns the maximum value in the array.</p> Signup and view all the answers

    User input cannot be used in array functions like append or insert.

    <p>False</p> Signup and view all the answers

    What does the remove() function do when used with an array?

    <p>It removes the specified element from the array.</p> Signup and view all the answers

    The function min() is used to return the ______ value in the array.

    <p>minimum</p> Signup and view all the answers

    Match the following functions to their descriptions:

    <p>min() = Returns the minimum value in the array. max() = Returns the maximum value in the array. remove() = Removes a specified element from the array. append() = Adds an element to the end of the array.</p> Signup and view all the answers

    In the provided example, what input is expected from the user when removing a highscore?

    <p>The score to be removed.</p> Signup and view all the answers

    The method ages.append(5) always appends the value 5 to the ages array regardless of user input.

    <p>True</p> Signup and view all the answers

    To append a user inputted value to an array, you would use the syntax ______(choice).

    <p>append()</p> Signup and view all the answers

    Study Notes

    Programming Techniques

    • Arrays are used to store multiple values under a single name.
    • Each value within an array is called an element.
    • Variables store a single value.

    Creating Variables and Arrays

    • Variables store a single value.
    • Arrays store multiple values using a single name.

    Creating Arrays

    • Create an array using identifier = [element1, element2, ...].
    • Elements can be integers or strings.
    • An empty array can also be created where elements are added later.

    Accessing Values in Arrays

    • Each element in an array has an index.
    • The index refers to its position.
    • array_name[index] accesses a specific element.
    • The index is used for printing, updating and removing elements

    Indexing

    • Most programming languages (like Python, C, Java) use zero-based indexing (index starts at 0).
    • Some use one-based indexing (index starts at 1).
    • Example for accessing elements: ages = [125,63,35,19,56], ages[0] = 125 accesses element 1.

    Adding and Removing Elements

    • Append(): Adds an element to the end of an array.
    • Extend(): Adds multiple elements to the end of an array.
    • Insert(): Inserts an element at a specified index.
    • Remove(): Removes the first occurrence of a specific element.
    • Pop(): Removes and returns the element at a specified index.
    • Clear(): Removes all elements from an array.

    Counting the Length of Arrays

    • len(array_name) counts the number of elements in an array.

    Slicing Arrays

    • array_name[slice(start, stop, step)] returns a specific range of elements.
    • Does not modify the original array.
    • Uses the same parameters as slicing strings

    Other Array Functions

    • reverse(): Reverses the elements in an array.
    • sort(): Sorts an array in ascending order.
    • count(value): Counts the occurrences of a specific element.
    • min(): Returns the minimum value (alphabetical if strings).
    • max(): Returns the maximum value (alphabetical if strings).

    Using User Input in Array Functions

    • User input can be used in array functions.
    • You can input an element you want to remove, add or modify in the array.

    Comparing Appending Methods

    • Normal method: array.append(value) appends immediately.
    • User input method: Takes user input to add to array using append function.

    Printing Array Elements

    • print(array_name) prints the array with commas and square brackets.
    • print(*array_name) removes these to print elements individually.
    • Using a separator using sep parameter: print(*array_name, sep=' ').

    Studying That Suits You

    Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

    Quiz Team

    Related Documents

    Arrays - Programming Techniques

    Description

    This quiz focuses on programming techniques related to arrays and variables. You'll explore how arrays are used to store multiple values, access specific elements, and understand indexing methods in various programming languages. Test your knowledge on creating and manipulating arrays effectively.

    Use Quizgecko on...
    Browser
    Browser