Python Objective 2

Question: Consider the following code:

Code Editor:
old_list = [“John”, “Amy”, “Jules”]
new_list = old_list
new_list.append(“Emily”)

What will be the value stored in old_list after these lines of code are executed?

[“Emily”, “John”, “Amy”, “Jules”]

[“John”, “Amy”, “Jules”]

[“John”, “Amy”, “Emily”]

[“John”, “Amy”, “Jules”, “Emily”]

Ans:- [“John”, “Amy”, “Jules”, “Emily”]

Which of the following function definitions allows the function to accept variable length arguments?

some_fn(…args)

some_fn(*)

some_fn(…)

some_fn(*args)

Ans:- some_fn(*args)

Which of the following are valid types of arguments in Python?

Ordered arguments

Keyword arguments

Variable length arguments

Default arguments

Ans:-

Keyword arguments

Variable length arguments

Default arguments

Question: Which TWO of the following statements about for loops in Python are TRUE?

They can iterate over the digits of an int type

They can only be used with sequences containing the same data type

They can iterate over the elements in tuples, lists, and dictionaries

They may have an associated else block

Ans:-

They can iterate over the elements in tuples, lists, and dictionaries

They may have an associated else block

Which of the following statement(s) about variable length arguments in Python is/are true?

Variable length keyword arguments are passed into the function as a dictionary

Variable length keyword arguments are passed into the function as a tuple

Variable length positional arguments are passed into the function as a tuple

Variable length positional arguments are passed into the function as a dictionary

Ans:-

Variable length keyword arguments are passed into the function as a dictionary

Variable length positional arguments are passed into the function as a tuple

What is the correct value of x given the assignment shown?

x = list(range(-17, -7, 2))

[-16, -14, -12, -10, -8]

[7, 9, 11, 13, 15, 17]

[-17, -15, -13, -11, -9]

[-17, -15, -11, -9, -7]

Ans:- [-17, -15, -13, -11, -9]

Question: How is the body of an if-statement block syntactically represented in Python?

Using curly braces {}

Using the end keyword

Using additional right indentation relative to lines just before and after the block

Using additional indentation from the left relative to lines just before and after the block

Ans:- Using additional indentation from the left relative to lines just before and after the block

Question: Which of the following statement(s) about global and local variables is/are true?

Local variables with the same name hide global variables

Local variables are defined outside a function

Global variables are defined inside a function

Global variables with the same name hide local variables

Ans:- Local variables with the same name hide global variables

Question: Which of the following statement(s) about passing arguments by value is/are true?

Changes made to string variables inside a function are reflected outside a function

Changes made to numeric variables inside a function are NOT reflected outside a function

Changes made to string variables inside a function are NOT reflected outside a function

Changes made to numeric variables inside a function are reflected outside a function

Ans:-

Changes made to numeric variables inside a function are NOT reflected outside a function

Changes made to string variables inside a function are NOT reflected outside a function

Question: Which of the following types of arguments are passed by reference to Python functions?

Lists

Integers

Strings

Booleans

Ans:- Lists

Which of the following function calls will generate the list below?
[10, 7, 4, 1, -2]

list(range(10, -3, 3))

list(range(-2, 10, -3))

list(range(10, -3, -3))

list(range(-2, 11, -3))

Ans:- list(range(10, -3, -3))

Question: How do you access a new module’s functions from within Python?

Download the module first using the “download” statement

It is not possible to access functions from another module

Install the module first using the “install” statement

Import the module first using the “import” statement

Ans:- Import the module first using the “import” statement

Question: Which of the following are modules which are available as a part of the Anaconda distribution of Python?

random

text

datetime

os

Ans:-

random

datetime

os

What is the maximum value in the sequence x?

x = range(2, 14)

13

14

2

0

Ans:- 13

Question: Which of the following is true about functions as first class citizens in Python?

Functions can have any name

Every function in your program should be unique

Functions cannot return other functions

Functions can be passed in as input arguments to other functions

Ans:- Functions can be passed in as input arguments to other functions

Question: Which statement is false about functions as first class citizens in Python?

Functions can have any name

Functions can be passed in as input arguments to other functions

Functions can be return values from another function

Every function name in your program should be unique

Ans:- Functions can have any name

What is the output for this code?

if ‘bin’ in {‘float’: 1.2, ‘bin’: 0b010}:
print(‘a’)
print(‘b’)
print(‘c’)

a b c

a b

Error

Value Error

Ans:- a b c

Question: Which of the following is possible using other data types but not functions?

Updating function code programmatically

Passing functions in as input arguments to another function

Using functions as values in a dictionary

Returning functions from functions

Ans:- Updating function code programmatically

Question: What are lambdas in Python?

Functions that cannot be invoked

Functions with no name

Functions that can be assigned to variables

Functions with enhanced features

Ans:- Functions with no name

Given a variable my_dict which is a dictionary, consider you use it in a for loop in this manner:

for x in my_dict:
print (x)

What are the contents printed out?

The keys in the dictionary

An error as we have not specified whether to iterate over keys or values

Tuples containing the key and value pairs

The values in the dictionary

Ans:- The keys in the dictionary

Which of these Python data types can NOT be iterated through using for loops?

int

list

string

tuple

Ans:- int

Question: Consider a tuple as shown below:

Code Editor:
tuple_a = (“John”, “Amy”, “Jules”, [“Jim”, “Ana”])

Question: Which of the following is true of lambdas in Python?

Lambdas can be defined and invoked right away in one statement

Lambdas cannot be invoked unless stored in a variable

Lambdas are basically functions with no input arguments

The body of a lambda can only have expressions

Ans:-

Lambdas can be defined and invoked right away in one statement

The body of a lambda can only have expressions

Question: Which of the following are valid use cases for lambdas in Python?

Use and throw away functions which may not be reused

A function that will be reused over and over again

Complex functions for reuse

To use with the built-in filter() function in Python

Defining a function on the fly

Ans:-

Use and throw away functions which may not be reused

To use with the built-in filter() function in Python

Defining a function on the fly

What code would you run to create a deep copy of this tuple?

A deep copy cannot be created

deep_copy = tuple_a

deep_copy = copy.copy(tuple_a)

deep_copy = copy.deepcopy(tuple_a)

Ans:- deep_copy = copy.deepcopy(tuple_a)

Given the following code, what is the type of x which is printed out in each iteration?

my_list = [[‘tiger’, ‘lion’, ‘leopard’], [‘camel’, ‘llama’, ‘alpaca’], [‘zebra’, ‘donkey’, ‘wildebeest’]]

for x in my_list:
print(x)

A tuple

A string

A list of strings

A list of lists

Ans:- A list of strings

Question: What is recursion?

A function invoking a built-in Python function

A function invoking itself within its function body

A function invoking another custom function in Python

A function returning another function

Ans:- A function invoking itself within its function body

Question: Your recursive function call can go on forever till Python terminates it. To fix this you need to:

Specify a clear terminating condition for the recursive call

Specify input arguments to your recursive function

Specify a clear return value from your recursive function

Specify a clear name for your recursive function

Ans:- Specify a clear terminating condition for the recursive call

What is the output for this code?

if None:
print(‘Hi’)

Nothing is printed – no output

None

False

Hi

Ans:- Nothing is printed – no output

Question: Which of the following about the break statement in for loops is TRUE?

It ends the current iteration of the for loop but continues to the next iteration

It has no effect on code execution

It terminates the current iteration immediately and the for loop as well

It allows the current iteration to complete but ends the for loop after that

Ans:- It terminates the current iteration immediately and the for loop as well

Question: Which of the following functions can be written recursively?

Finding the minimum of two elements

Printing numbers in ascending order

Finding the maximum of 2 elements

Find the Fibonacci series

Ans:-

Printing numbers in ascending order

Find the Fibonacci series

Question: Which of the following is true about a generator function?

It has a yield statement rather than a return statement

It is defined and executed in one command

It is executed as soon as it is invoked

It returns a generator object

Ans:-

It has a yield statement rather than a return statement

It returns a generator object

Question: Which of these keywords has no effect on code execution when used in for loops?

pass

continue

break

print

Ans:- pass

Question: Why would you prefer generator functions over regular lists for generating infinite sequences?

Generators automatically generate infinite sequences

Generators are meant only for infinite sequences

Generators are more memory efficient while generating infinite sequences

Infinite sequences cannot fit in a list because it won’t fit in Python memory

Ans:-

Generators are more memory efficient while generating infinite sequences

Infinite sequences cannot fit in a list because it won’t fit in Python memory

Question: What are closures?

Nested functions which carry with them local state

Functions which are passed in as arguments to other functions

Outer functions which have nested functions within them

Functions used to decorate other functions

Ans:- Nested functions which carry with them local state

What is the output of the following line of code?

[x * 3 for x in [1, 4, 5]]

30

[1, 4, 5]

[30]

[3, 12, 15]

Ans:- [3, 12, 15]

Evaluate the expression provided. What does the following expression evaluate to?

‘1’ + ‘2’ if ‘123’.isdigit() else ‘2’ + ‘3’

‘12’

‘123’

‘1’

‘23’

Ans:- ‘12’

Question: Which of the following is true about local state and closures?

The local state information associated with a closure can be updated

The local state information associated with a closure can be updated

The local state information associated with a closure is lost after a certain time period

The local state information associated with a closure remains even if the outer functions is no longer in Python memory

Ans:- The local state information associated with a closure remains even if the outer functions is no longer in Python memory

Question: What is the biggest advantage of the decorator in Python?

Can be used to decorate variables as well as functions

Decorators are not available in other languages

Adds functionality to existing code without modifying the code itself

All decorators work with all functions

Ans:- Adds functionality to existing code without modifying the code itself

What is the output of the following line of code?

[x * 3 if x <5 else x * 4 for x in [1, 4, 5]]

[3, 12]

[4, 16, 15]

[3, 12, 20]

[3, 12, 15]

Ans:- [3, 12, 20]

Question: Which of the following statement(s) is/are true about Python decorators?

Can be used to decorate variables and classes as well

Can be used to remove functionality from existing code

Can be constructed in such a way that they can be used with functions which have any number of arguments

Are built using closure under the hood

Ans:-

Can be constructed in such a way that they can be used with functions which have any number of arguments

Are built using closure under the hood

Consider the code below:

@decorator_1
@decorator_2
def some_function():
print(“Hello”)

Which of the following statement(s) is/are true?

decorator_1 will be applied to the function first

decorator_2 will be applied to the function first

This is referred to as chaining decorators and is valid

This is an error, multiple decorators cannot be used for the same function

Ans:-

decorator_2 will be applied to the function first

This is referred to as chaining decorators and is valid

Which TWO of the following about break statements within Python for loops are TRUE?

When invoked in the if block, the code in the else block of the for loop is not executed

If there is an else block associated with the for loop, the break statement has no effect

If invoked, the current iteration and the for loop in general terminate

It stops the for loop and then executes the code in the else block of the for loop

Ans:-

When invoked in the if block, the code in the else block of the for loop is not executed

If invoked, the current iteration and the for loop in general terminate

Question: Consider the following code:

Code Editor:
old_list = [“John”, “Amy”, “Jules”]
new_list = old_list.copy()
new_list.append(“Emily”)

What will be the value stored in old_list after these lines of code are executed?

[“John”, “Amy”, “Jules”]

[“John”, “Amy”, “Jules”, “Emily”]

[“Emily”, “John”, “Amy”, “Jules”]

[“John”, “Amy”, “Emily”]

Ans:- [“John”, “Amy”, “Jules”]

Which TWO of these points about continue and break statements are accurate?

continue and break are interchangeable

continue and break can be used within the same for loop

continue ends the for loop, break ends the current iteration

continue ends the current iteration, break ends the for loop

Ans:-

continue and break can be used within the same for loop

continue ends the current iteration, break ends the for loop

Which of these statements about continue statements is TRUE?

continue terminates the for loop

continue has no effect on code execution

continue has no effect on the current iteration

continue terminates the current iteration of the for loop

Ans:- continue terminates the current iteration of the for loop

Question: Which of these is NOT a consideration when picking a data structure to represent your data?

It should make the most efficient use of space

It should make common operations on the data very quick

It should not require the programmer to understand the data

It should make difficult operations possible

Ans:- It should not require the programmer to understand the data

Question: Which of these is NOT a factor for how complexity is measured?

Network bandwidth

Space

Hardware cost

Time

Ans:- Hardware cost

Question: Consider the following bit of code:

Code Editor:
dict_a = {“John”: 35, “Jim”: 22, “Jill”: 44}
dict_copy = dict_a
dict_copy[‘John’] = 40
del dict_copy[‘John’]

After executing this code, what is the value associated with John in dict_a?

40

The key John does not exist in dict_a

35

None

Ans:- The key John does not exist in dict_a

Question: Which TWO of the following statements about while and for loops are TRUE?

for loops continue with iterations until a condition evaluates to True/False

while loops iterate over a sequence without the user needing to maintain an index to that sequence

for loops iterate over a sequence without the user needing to maintain an index to that sequence

while loops continue to loop while the condition is True and end when the condition evaluates to False

Ans:-

for loops iterate over a sequence without the user needing to maintain an index to that sequence

while loops continue to loop while the condition is True and end when the condition evaluates to False

Question: What is the correct ordering of time complexities from the slowest to the quickest?

O(1), O(n), O(n^2)

O(n), O(n^2), O(1)

O(n^2), O(n), O(1)

O(n), O(1), O(n^2)

Ans:- O(n^2), O(n), O(1)

Question: Which TWO of the following statements about linked lists are TRUE?

It comprises two lists – one to store the node’s data and another for the node’s connections

The head of the list is the entry point to the linked list

Each node points to the next node in the list, except if it is the last node

Each node in a linked list is connected to every other node

Ans:-

The head of the list is the entry point to the linked list

Each node points to the next node in the list, except if it is the last node

Question: What is the time complexity for inserting a node at the head of a linked list?

O(1)

O(n^2)

O(n)

O(logn)

Ans:- O(1)

What is the output of the code?

a = [1, ‘one’, {2: ‘two’}, 3]
b = len(a)

if b == 4:
print(‘Length of this list is 4’)
if b == 5:
print(‘Length of this list is 5’)
else:
print(b)

Length of this list is 4 Length of this list is 5

TypeError

Length of this list is 5 5

Length of this list is 4 4

Ans:- Length of this list is 4 4

What is the output of the following code?

my_list = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’]
vowels = [‘a’, ‘e’, ‘i’, ‘o’, ‘u’]
i = 0

while i < len(my_list):

if(my_list[i] in vowels):
    i += 1
    continue

print(my_list[i])
i += 2

b d f h

a b c d e f g h

a c e g

c g

Ans:- b d f h

Question: What step needs to be performed when removing a node from the middle or end of a linked list?

Modify the value of the “next” field in the node before the one being deleted

Modify the “data” field in the node after the one being deleted

Set the value of the “next” field in the node being deleted to null

Modify the “data” field in the node before the one being deleted

Ans:- Modify the value of the “next” field in the node before the one being deleted

Question: In the absence of a count variable to track the number of nodes in a linked list, what is the complexity of an operation to perform such a count?

O(n)

O(n^2)

O(1)

O(logn)

Ans:- O(n)

Question: What is returned by invoking the “peek” operation on a stack?

The total capacity of the stack

The number of elements in the stack

The value of the most recently inserted element

The value of the first element inserted

Ans:- The value of the most recently inserted element

What is the value of b in the snippet of python code?

a = “six”
b = (int(a), float(a))

Type Error

ValueError: invalid literal for int() with base 10: ‘six’

6 and 6.0

six

Ans:- ValueError: invalid literal for int() with base 10: ‘six’

Question: Which TWO of these stack operations will result in an error?

PUSH on a full stack

PUSH on an empty stack

POP on a full stack

POP on an empty stack

Ans:-

PUSH on a full stack

POP on an empty stack

Question: Which TWO of the following statements about queue data structures are TRUE?

Elements are removed from the front

Elements are inserted at the front

Elements are inserted at the end

Elements are removed from the end

Ans:-

Elements are removed from the front

Elements are inserted at the end

What is the output of this while loop?

x = 8
while x < 10: print(x) x += 1

Syntax error

8 9

No output as the condition always evaluates to false

8 9 10

Ans:- Syntax error

Question: Consider the following bit of code:

Code Editor:
dict_a = {“John”: 35, “Jim”: 22, “Jill”: 44}
dict_copy = dict_a
dict_copy[‘John’] = 40
del dict_copy[‘John’]

After executing this code, what is the value associated with John in dict_a?

40

The key John does not exist in dict_a

35

None

Ans:- 35

Question: Which of the following represents an operation whose complexity is O(1)?

Renaming each of the n documents

Signing half of the n documents

Signing n documents one at a time

Opening a folder containing n documents

Ans:- Opening a folder containing n documents

Question: Which TWO of the following operations has an O(n) time complexity?

Unloading half of the n toys in a box one at a time

Packaging a box containing n toys

Loading n toys into a box one at a time

Closing a box containing n toys

Ans:-

Unloading half of the n toys in a box one at a time

Loading n toys into a box one at a time

Question: Given an input of size n, below are the number of operations performed by 4 different algorithms. Which TWO of these has a complexity of O(n)?

4n

34

n + 34

4n^2 + 1

Ans:-

4n

n + 34

Which of the following statements about the use of break statements in a while loop are true?

The else block associated with the while loop is executed only if break is invoked

It is possible to terminate a while loop only if a break is invoked

If break is invoked, the else block associated with the while loop is not executed

The condition after the while keyword must eventually evaluate to False

Ans:- If break is invoked, the else block associated with the while loop is not executed

Question: Given an input of size n, below are the number of operations performed by 4 different algorithms. Which TWO of these has a complexity of O(n*n)?

n^2 + 3n

n^3 + 7n^2

3n + 97

2n^2 + 5

Ans:-

n^2 + 3n

2n^2 + 5

Question: What does the value returned by the qsize() function of a queue represent?

The number of elements presents in the queue

The number of empty spots in the queue

The maximum capacity of the queue

The number of elements that have passed through the queue

Ans:- The number of elements presents in the queue

Question: When using a Python list to function as a queue, which of these functions can be used to implement the enqueue operation?

pop

append

add

get

Ans:- append

Question: The following 4 elements are added to a Python list in the given order: A, B, C and D. What occurs when the pop() function is called without arguments?

The element A is returned but remains in the list

The element A is removed from the list and returned

An error is thrown as the list is full

The element D is removed from the list and returned

Ans:- The element D is removed from the list and returned

What is the number of iterations the following while loop will perform before terminating?

x = 4
while x < 10:
print(x+1)

5

Infinite

0

1

Ans:- Infinite

Question: What is a PEEK operation on a stack supposed to perform?

It returns the first element which was added to the stack

It returns the last element which was added to the stack

It empties the stack of all its elements

It removes the first element which was added to the stack

Ans:- It returns the last element which was added to the stack

Question: Which of the following linked list operations has a time complexity of O(1)?

Inserting a node at the head of the list

Inserting a node at the tail of the list

Searching for an element containing a specific value

Inserting a node at the halfway point in the list

Ans:- Inserting a node at the head of the list

Question: What is the time complexity of adding an element in a linked list after a node containing a specific value?

O(n^2)

O(n)

O(log(n))

O(1)

Ans:- O(n)

Consider the following snippet of Python code:

a = “40.6 ”
b = “60.4 ”
c = a + b

What does c evaluate to?

‘40.6 60.4’

101

101.0

ValueError

Ans:- ‘40.6 60.4’

What is the number of iterations the following while loop will perform before terminating?

x = 4
while x < 4+6:
print(x)
x += 1

6

0, since we cannot include arithmetic operations in while loop conditions

1

Infinity

Ans:- 6

Question: Which TWO of the following linked list operations have a time complexity of O(n)?

Adding a node at the tail

Adding a node at the head

Removing the node at the head

Removing a node containing a specific value

Ans:-

Adding a node at the tail

Removing a node containing a specific value

What is the effect of the pass statement within a nested while loop being invoked?

The inner loop terminates

The inner loop skips the current iteration

There is no effect on code execution

All while loops terminate

Ans:- There is no effect on code execution

What would the output of the following code snippet be?

num_one = 76
num_two = 23.4
print(“datatype of num_one:”, type(num_one))
print(“datatype of num_two:”, type(num_two))

datatype of num_one: datatype of num_two:

datatype of num_one: datatype of num_two:

datatype of num_one: datatype of num_two:

datatype of num_one: datatype of num_two:

Ans:- datatype of num_one: <class ‘int’> datatype of num_two: <class ‘float’>

Question: Which term is used to describe a sorting algorithm which performs better with lists that are nearly sorted?

complex

complete

adaptive

stable

Ans:- adaptive

Question: Which of the following are accurate statements about the Selection Sort algorithm?

It uses O(1) extra space

Its time complexity is O(n^2)

It is a stable sort

Its time complexity is O(n)

Ans:-

It uses O(1) extra space

Its time complexity is O(n^2)

Question: Which of the following statements about Bubble Sort are TRUE?

The number of swaps will vary depending on how much of the list is already sorted

On average, the number of swaps and comparisons reduces with each iteration

Bubble sort only works with small-sized arrays

Bubble sort cannot be performed in-place

Ans:-

The number of swaps will vary depending on how much of the list is already sorted

On average, the number of swaps and comparisons reduces with each iteration

Question: Which of the following statements about functions is true?

Function code is not executed when defined

A function is defined using the “def” keyword

Function code is executed when defined

A function is defined using the “func” keyword

Ans:-

Function code is not executed when defined

A function is defined using the “def” keyword

Question: Which of the following are valid function names in Python?

_function_name()

functionName()

123_Function_Name()

_123_Function_Name()

-functionName()

Ans:-

_function_name()

functionName()

_123_Function_Name()

Question: What is the worst-case time complexity of Bubble Sort?

O(n^2)

O(n)

O(1)

O(n^3)

Ans:- O(n^2)

Question: Which of the following statements about Insertion Sort is TRUE?

It requires O(n) extra space

It does not require any swaps

Its worst-case time complexity is O(n)

It is adaptive

Ans:-It is adaptive

Question: What is the time complexity of Shell Sort?

Always O(n2)

Always O(nlogn)

Between O(n) and O(n^2)

Always O(n)

Ans:- Between O(n) and O(n^2)

Question: Consider the following code:

Code Editor:
old_str = “Hello”
new_str = old_str
new_str = “World”

What will be the value stored in old_str after these lines of code are executed?

“Hello World”

“Hello”

None

“World”

Ans:- “Hello”

Question: Which of the following statements about functions is false?

Functions can contain any number of statements in the function body

Functions are invoked using parenthesis

Functions can have different names

Functions cannot access variables which are declared outside the function

Ans:- Functions cannot access variables which are declared outside the function

Consider a function definition which looks like this:

def some_function(a, b, c):
print(a, b, c)

Question: What is the time complexity of Merge Sort?

O(n)

O(1)

O(nlogn)

O(n^2)

Ans:- O(nlogn)

Question: Which of the following statements about Quicksort are TRUE?

At each iteration, the pivot element moves to its “rightful” place in the array

It is a divide and conquer algorithm

The pivot is always the right-most element of a partition

The choice of pivot depends on the size of the list

Ans:-

At each iteration, the pivot element moves to its “rightful” place in the array

It is a divide and conquer algorithm

Which of the following function invocations are correct?

some_function(2, 3, 4)

some_function(2, 3, “Hello”)

some_function( 3, “Hello”)

some_function()

Ans:-

some_function(2, 3, 4)

some_function(2, 3, “Hello”)

Question: What is the average case time complexity of Quicksort?

O(1)

O(n^2)

O(nlogn)

O(n)

Ans:- O(nlogn)

Data Structures & Algorithms in Python: Implementing Sorting Algorithms

Question: What is the time complexity of the Selection Sort algorithm?

O(log(n))

O(n)

O(n^2)

O(1)

Ans:- O(n^2)

Question: Which of the following statements is TRUE of the bubble sort algorithm?

Its worst-case time complexity is O(nlog(n))

It is an adaptive sort

It is not an adaptive sort

Its worst-case time complexity is O(n2)

Ans:-

It is an adaptive sort

Its worst-case time complexity is O(n2)

Question: What is the worst-case time complexity of the Insertion Sort algorithm?

O(n^2)

O(n)

O(log(n))

O(1)

Ans:- O(n^2)

What is the output of the code snippet below?

value = 4

a = str(value)
b = a + “^” + “2”
c = a + “^” + “3”

print(value, “+”, b, “+”, c)

4 + 4 ^ 2 + 4 ^ 3

17

None of the above

4 16 64

Ans:- 4 + 4 ^ 2 + 4 ^ 3

Consider the following bit of code. What will be the result of executing this bit of code?

x = 3
y = 4

def add(a, b):
result = x + y
print(result)

add(10, 20)

24

7

30

13

Ans:- 7

Question: Which basic sorting algorithm does shell sort apply on its sublists?

Bubble sort

Merge sort

Selection sort

Insertion sort

Ans:- Insertion sort

Question: Which of the following statements about shell sort are TRUE?

It is a divide and conquer algorithm

Its average case time complexity is O(n2)

It is an adaptive sort

The size of the sublists decreases with each iteration

Ans:-

It is a divide and conquer algorithm

It is an adaptive sort

Which of the following statement(s) about positional arguments to functions is/are true?

Positional arguments are specified by name

Positional arguments are optional while invoking the function

They can be of any data type – primitive or complex types

A function can accept any number of positional arguments

Ans:-

They can be of any data type – primitive or complex types

A function can accept any number of positional arguments

Question: At what point does the merge sort algorithm stop dividing the list and begin merging them together?

When the sublist contains just one element

When the sublist contains two elements

When the sublist is already sorted

When the sublist is half the size of the original list

Ans:- When the sublist contains just one element

Question: Which of these tasks is performed by the partition function of a Quicksort?

It recursively divides the list into interleaved sublists

It places the largest element in the list at the very end

It merges two sublists together and sorts their elements in the process

It picks a pivot element and places it in its correct position in the list

Ans:- It picks a pivot element and places it in its correct position in the list

Question: Which of the following statements is TRUE about the pivot element at each iteration of a Quicksort?

Elements to the left of the pivot are less than or equal to the pivot

The pivot is placed in its correct spot in the list

Elements to the right of the pivot are sorted

Elements to the left of the pivot are sorted

Ans:-

Elements to the left of the pivot are less than or equal to the pivot

The pivot is placed in its correct spot in the list

What do the values of d[0], d[1], d[2], d[3] evaluate to after the execution of the Python code below?

new_list = [“Red”, “Blue”, “White”, “Green”]
z = sorted(new_list)
d = list(z)
d[0], d[1], d[2], d[3] = d[3], d[2], d[1], d[0]

“White”, “Red”, “Green”, “Blue”

“Red”, “Blue”, “White”, “Red”

“Blue”, “Green”, “Red”, White”

“White, “Red”, “Blue”, Green”

Ans:- “White”, “Red”, “Green”, “Blue”

What is the default return value from a function when no return statement is specified?

0

None

-1

“”

Ans:- None

Question: What is the time complexity of a binary search?

O(1)

O(log n)

O(n^2)

O(n)

Ans:- O(log n)

Question: What term is used to denote the nodes of a tree which do not contain any children?

stem

trunk

root

leaf

Ans:- leaf

Question: Which of the following statements about binary search trees are TRUE?

The structure depends on the order in which nodes are inserted

The worst case time complexity of a lookup is O(n)

A node can be inserted anywhere in the tree as long as it is a leaf

It is always balanced

Ans:-

The structure depends on the order in which nodes are inserted

The worst case time complexity of a lookup is O(n)

Which of the following statement(s) about return values is/are false?

A function with input arguments cannot have a return statement

E. A function can return any kind of data, primitive as well as complex types

A function can have multiple return values

A function can have just one return statement

A function has to have a return statement

Ans:-

A function with input arguments cannot have a return statement

A function can have just one return statement

A function has to have a return statement

Question: For a set in Python what is the difference between copy.copy() and the copy.deepcopy() method?

No difference between the two because sets cannot hold mutable types like lists

One performs deep copies of nested elements other does not

Sets do not support deepcopy but they support copy

Sets do not support copy but they support deepcopy

Ans:- No difference between the two because sets cannot hold mutable types like lists

Question: Where in the binary search tree can its minimum value be found?

At the right-most node

At the root

At the left-most node

At the right child of the root

Ans:- At the left-most node

Question: Which of the following statements about breadth first traversal on a binary search tree are TRUE?

All nodes to the left of the root are visited before any node to its right

All nodes at one level are visited before any node lower in the tree

The root node is the first to be visited

The root node is the last to be visited

Ans:-

All nodes at one level are visited before any node lower in the tree

The root node is the first to be visited

What is the output of the program below?

var = “hi”
if(type(var) == int):
print(“Type of the variable is Integer”)
elif(type(var) == float):
print(“Type of the variable is Float”)
elif(type(var) == complex):
print(“Type of the variable is Complex”)
else:
print(“Type of the variable is Unknown”)

Unknown

Integer

Complex

Float

Ans:- Unknown

Question: Which of these binary tree traversals generates an ascending order of the values in its nodes?

Post-order

In-order

Breadth-first

Pre-order

Ans:- In-order

Question: What is the last node to be visited in a post-order traversal of a binary search tree?

The deepest leaf node of the tree

The root of the tree

The left-most node of the tree

The right-most node of the tree

Ans:- The root of the tree

Question: Which of the following statements about graphs are TRUE?

Each vertex must have a path to every other vertex

Two vertices connected by an edge represent a graph

The edges connecting vertices can be directed or undirected

They cannot have cycles

Ans:-

Two vertices connected by an edge represent a graph

The edges connecting vertices can be directed or undirected

Which of the following statements(s) about the data types of return values is/are false?

A function in Python can return a dictionary

A function in Python can return nothing

A function in Python may have no return statement

A function in Python can return a dictionary

A return statement is mandatory in functions

Ans:- A return statement is mandatory in functions

Which of the following are valid kinds of input arguments for Python functions?

Keyword arguments

Positional arguments

Complex arguments

Simple arguments

Ans:-

Keyword arguments

Positional arguments

Question: Given a graph with n vertices, what is the number of cells in its adjacency matrix?

n+1

n^2

n

2n

Ans:- n^2

Question: What is the complexity of the operation to look up whether an edge is present between two vertices in a graph when it is represented as an adjacency matrix?

O(log n)

O(n^2)

O(n)

O(1)

Ans:- O(1)

Question: Which options represent properties that a graph must fulfil to be eligible for a topological sort?

It should contain at least on cycle

It should be an undirected graph

It cannot contain any cycles

It should be a directed graph

Ans:-

It cannot contain any cycles

It should be a directed graph

What is the output of the program below?

total_classes = 100
attended_classes = 67

attendance = (attended_classes/total_classes)*100
if attendance >= 75:
print (“You are eligible to appear for the test.”)
else:
print (“Sorry, you are ineligible to appear for the test.”)

Sorry, you are ineligible to appear for the test.

Value Error

You are eligible to appear for the test.

Type Error

Ans:- Sorry, you are ineligible to appear for the test.

Which of the following are some of the advantages of using keyword arguments to invoke functions?

Keyword arguments have automatic error checking

Keyword arguments can be specified out of order

Easier to maintain code since the value of each argument is clearly seen during invocation

Keyword arguments need not be specified at function definition time

Ans:-

Keyword arguments can be specified out of order

Easier to maintain code since the value of each argument is clearly seen during invocation

What does this function definition indicate?

def some_fn(a, b, c=True):
print(a, b, c)

a and b are required arguments, c is optional

a and b are string arguments, c is boolean

a and b are optional arguments, c is required

a and b are numeric arguments, c is boolean

Ans:- a and b are required arguments, c is optional