Quizzes Flashcards

1
Q

What is a dictionary in Python?

A

An unordered collection of key-value pairs

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the primary requirement for keys in a Python dictionary?

A

They must be unique and immutable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How can you create an empty dictionary in Python?

A

{}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does the built-in function ‘len; return for a dictionary?

A

Number of key-value pairs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How can you obtain a list of a dictionary’s keys in sorted order?

A

sorted(dict.keys())

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does the ‘==’ operator check for when used with dictionaries?

A

identical contents of key-value pairs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What do dictionary comprehensions provide a convenient notation for?

A

Quickly generating dictionaries.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a set in Python?

A

An unordered collection of key-value pairs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a significant characteristic of sets during iteration?

A

They are unordered

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How can you create an empty set in Python?

A

set()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the recommended way to import the NumPy library?

A

import numpy as np

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

what does the NumPy array function do?

A

Copies its argument’s contents into the array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What attribute of an array provides information about its number of dimensions?

A

ndim

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does the ‘linspace’ function in NumPy do?

A

Produces evenly spaced floating-point ranges

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is broadcasting in NumPy

A

Performing element-wise calculations with a scalar and an array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does element-wise arithmetic mean in the context of NumPy arrays?

A

Performing calculations on each element of an array.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How can you select an element in a two-dimensional array in NumPy?

A

Providing a tuple containing row and column indices

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is the difference between ‘reshape’ and ‘resize’ in NumPy?

A

‘resize’ modifies the original array; ‘reshape’ returns a view

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

How can you transpose an array in NumPy?

A

Using the ‘transpose’ method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What does ‘vstack’ do in NumPy?

A

Combines two arrays by adding rows.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

In creating custom functions, which of the following is not used when defining functions?

A

arguments list

22
Q

This can only be used inside the function and exist only while the function is executing.

A

Local variables

23
Q

A function with multiple parameters specifies them in a(n)?

A

Comma-separated list

24
Q

This function generates an integer from the first argument value up to, but not including, the second argument value.

A

randrange function

25
Q

This operator tests whether its right operand’s iterable contains its left operand’s value.

A

in operator

26
Q

When an argument with a default parameter value is omitted in a function call, the interpreter automatically passes the default parameter value in the call.

A

True, because the functions has a default parameter value.

27
Q

Parameters with default parameter values must be the leftmost arguments in a function’s parameter list.

A

False, parameters with default parameter values must appear to the right of parameters that do not have defaults.

28
Q

Keyword arguments must be passed in the same order as their corresponding parameters in the function definition’s parameter list.

A

False, the order of keyword arguments does not matter.

29
Q

To define a function with an arbitrary argument list, specify a parameter of the form ____.

A

*args (The name args is used by convention, but is not required.)

30
Q

A method is simply a function that you call on an object using which of the following form?

A

object_name.method_name(arguments)

31
Q

These are prepackaged data structures consisting of related data items.

A

Collection

32
Q

Which of the following is true?
a. Lists typically store homogeneous data, that is, values of the same data type.
b. Lists may also store heterogeneous data, that is, data of many different types.
c. Both A and B.
d. None of these.

A

c. Both A and B

33
Q

Which of the following is not part of the way to access a list element?

A

the index value always starts by 0

34
Q

Python’s string and tuple sequences are immutable, which means?

A

Changing string and tuple values is not allowed.

35
Q

Which of the following is not true about concatenation?

a. You can concatenate two lists, two tuples or two strings.
b. You can concatenate using the + operator.
c. The result is a new sequence of the same type containing the right operand’s elements followed by the left operand’s elements.
d. The original sequences are unchanged.

A

c. The result is a new sequence of the same type containing the right operand’s elements followed by the left operand’s elements.

36
Q

Which of the following process in tuples is not allowed?
a. Adding Items to a String or Tuple
b. Appending Tuples to Lists
c. Tuples May Contain Mutable Objects
d. None of the above.

A

d. None of the above.

37
Q

Which of the following is true about unpacking sequences?

a. You can unpack any sequence’s elements by assigning the sequence to a comma-separated list of variables.
b. A ValueError occurs if the number of variables to the left of the assignment symbol is not identical to the number of elements in the sequence on the right.
c. Both A and B.
d. None of these.

A

c. Both A and B.

38
Q

The following expression causes an error:

’-‘ * 10

a. True, using the multiplication operator of nonnumeric value of ‘-‘ is not allowed.
b. True, 10 cannot be multiplied with ‘-‘.
c. False, in this context, the multiplication operator (*) repeats the string (‘-‘) 10 times.
d. False, the result of this expression is: ———

A

c. False, in this context, the multiplication operator (*) repeats the string (‘-‘) 10 times.

39
Q

Which of the following is not true about sequence slicing?

a. You can slice sequences to create new sequences of the same type containing subsets of the original elements.
b. Slice operations can modify mutable sequences.
c. Those that do not modify a sequence work identically for lists, tuples and strings.
d. None of these.

A

d. None of these.

40
Q

What happens when both the start and end indices are omitted in sequence slicing?

A

Copies the entire sequence

41
Q

Which of the following is not allowed in del statements?

a. Deleting the Element at a Specific List Index
b. Deleting a Slice from a List
c. Deleting a Slice Representing the Entire List
d. None of these.

A

d. None of these.

42
Q

Which of the following is not true about sorting lists?

a. To use the sort() method, the object name should be placed inside its parentheses.
b. To sort a list in descending order, call list method sort with the optional keyword argument reverse set to True.
c. Built-in function sorted returns a new list containing the sorted elements of its argument sequence.
d. List method sort modifies a list to arrange its elements in ascending order by default.

A

a. To use the sort() method, the object name should be placed inside its parentheses.

43
Q

Which of the following is not true about searching sequences?

a. Searching is the process of locating a key.
b. List method index takes as an argument a search key—the value to locate in the list—then searches through the list from index 0 and returns the index of the last element that matches the search key.
c. Using method index’s optional arguments, you can search a subset of a list’s elements.
d. Operator in tests whether its right operand’s iterable contains the left operand’s value.

A

b. List method index takes as an argument a search key—the value to locate in the list—then searches through the list from index 0 and returns the index of the last element that matches the search key.

44
Q

Which of the following is true about Simulating Stacks with Lists?

a. Python does have a built-in stack type, but you can think of a stack as a constrained list.
b. You push using list method append, which adds a new element to the start of the list.
c. You pop using list method pop with some arguments, which removes and returns the item at the end of the list.
d. Items are retrieved from stacks in first-in, last-out (FILO) order.

A

d. Items are retrieved from stacks in first-in, last-out (FILO) order.

45
Q

Which is not true about generator expressions?

a. A generator expression is similar to a list comprehension, but creates an iterable generator object that produces values on demand.
b. Generator expressions have the same capabilities as list comprehensions, but are defined in parentheses instead of square brackets.
c. Generator expression creates a list of its generated values.
d. Generator expressions use lazy evaluation.

A

c. Generator expression creates a list of its generated values.

45
Q

Which of the following is allowed in list comprehensions?

a. Using a List Comprehension to Create a List of Integers
b. Mapping is Performing Operations in a List Comprehension’s Expression
c. Filtering is List Comprehensions with if Clauses
d. All of these.

A

d. All of these.

45
Q

Which is not true when using lambda expressions?
a. Lambda expression is used to define the function inline where it’s needed.
b. A lambda expression is an anonymous function—that is, a function without a name.
c. A lambda begins with the lambda keyword followed by a comma-separated parameter list, a semi-colon (;) and an expression.
d. A lambda implicitly returns its expression’s value.

A

c. A lambda begins with the lambda keyword followed by a comma-separated parameter list, a semi-colon (;) and an expression.

46
Q

Lists that require two indices to identify an element are called?

a. two-dimensional lists
b. double-indexed lists
c. double-subscripted lists
d. All of these.

A

d. All of these.

47
Q

In the given 3-by-3 list grade_list, what is the value of the element in grade_list[1][2]?

A

90

48
Q

In a two-dimensional list, the first index by convention identifies the of an element and the second index identifies the of an element.

A

row, column