Docsity

PYTHON SYNTAX UPDATED EXAM QUESTIONS AND CORRECT ANSWERS.pdf, Exams of Nursing

Toggle sidebar
bg1
Page 1 of 167
PYTHON SYNTAX UPDATED EXAM QUESTIONS AND
CORRECT ANSWERS
Question 1
What is the value of total_cost that gets printed
Total_cost = 5
Total_cost += 10
Print (total cost)
Correct Answer
15
Question 2
What is the output of the following code
Cool_number = 12 + 30
Cool_number *5
Print (cool_number)
Correct Answer
42
Question 3
What happens when running the following code
Message = what a cool message !
Print (message)
Correct Answer
Python throws a syntax error because the string is not surrounded by quotes
Question 4
What number is saved to exponented_variable in the following expression
2 ** 4
Correct Answer
16
Question 5
This produces a SyntaxError
Correct Answer
Answer= "is this an error'
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28

Partial preview of the text

Download PYTHON SYNTAX UPDATED EXAM QUESTIONS AND CORRECT ANSWERS.pdf and more Exams Nursing in PDF only on Docsity!

PYTHON SYNTAX UPDATED EXAM QUESTIONS AND

CORRECT ANSWERS

Question 1

What is the value of total_cost that gets printed

Total_cost = 5 Total_cost += 10 Print (total cost)

Correct Answer

15

Question 2

What is the output of the following code Cool_number = 12 + 30 Cool_number * Print (cool_number)

Correct Answer

42

Question 3

What happens when running the following code Message = what a cool message! Print (message)

Correct Answer

Python throws a syntax error because the string is not surrounded by quotes

Question 4

What number is saved to exponented_variable in the following expression 2 ** 4

Correct Answer

16

Question 5

This produces a SyntaxError

Correct Answer

Answer= "is this an error'

How does one define a multi-line string in python

Correct Answer

" " " Like this " " "

Question 7

What character begins a comment in python

Correct Answer

Question 8

What is python syntax for creating a variable and setting it equal to the number 10

Correct Answer

Variable_name = 10

Question 9

What function outputs text to the terminal

Correct Answer

Print ()

Question 10

How do you combine two strings

Correct Answer

String1 + string

Question 11

What is the difference between float and an int

Correct Answer

A float represents decimal quantities. An int represents whole numbers

Question 12

What is the value of modulo_variable in the following expression Modulo_variable = 14%

Correct Answer

2

Mismatched quotes will cause what error example: print "Mismatched quotes will cause a'

Correct Answer

SyntaxError: EOL while scanning a string literal. This mean that the string wasn't closed with the same quote- character that staryed it

Question 8

Without quotes will cause a

Correct Answer

NameError This means that Python found what it thinks is a command, but doesn't know what it means because it's not defined anywhere

Question 9

Python uses what to define things that are subject to change.

Correct Answer

Variables

Question 10

Create a variable called 'todays_date" and assign a value that will represent today's date to that variable.

Correct Answer

todays_date = "12/14/2021"

Question 11

Example of Python Arithmetic

Correct Answer

mirthful_addition = 12381 + 91817 amazing_subtraction = 981 - trippy_multiplication = 38 * 902 happy_division = 540 / 45 sassy_combinations = 129 * 1345 + 120 / 6 -

Question 12

Python also offers a companion to division called the modulo operator. The modulo operator is indicated by _____ and returns the remainder after division is performed.

Correct Answer

% percentage sign

Question 13

Multiply two numbers together and assign the results to a variable called product

Correct Answer

product = 2 * 2

What is the remainder when 1398 is divided by 11? Save the results in a variable called remainder.

Correct Answer

remainder = 1398 % 11

Question 15

Updating Variables Examples: we start with 50 fish in a local pond the 10 were fished out so we set the variable " fish_in_clarks_pond equal to 40

Correct Answer

fish_in_clarks_pond = 50

print "Catching fish"

number_of_fish_caught = 10 fish_in_clarks_pond = fish_in_clarks_pond - numer_of_fish_caught

Question 16

if you want to include a piece of information to explain a part of your code you can use this sign a text preceded by this sign is called a COMMENT

Correct Answer

sign

Question 17

example of comments in Python

Correct Answer

city_name = "St. Potatosburg

St. Potatosburg is know for its potatos

city_pop = 340000

Question 18

Variables can also hold numeric values. The simplest kind of number in Python is the integer, which is a whole number with no decimal point

Correct Answer

int1 = 1 int2 = 10 int3 = -

Question 19

A number with a decimal point is called a float. You can define floats with numbers after the decimal point or by just including a decimal point at the end

Correct Answer

float1 = 1. float2 = 10. float3 = -5.

to yield a float as the result instead, programmers often change either the numerator or the denominator ( or both) to be a float:

Correct Answer

quotient1 = 7./

the value of quotietnt1 is 3.

quotient2 = 7/2.

the value of quotient2 is 3.

quotient3 = 7./

the value of quotient3 is 3.

Question 28

An alternative way is to use the "Float()" method

Correct Answer

quotient1 = float (7)/

the value of quotient1 is 3.

Question 29

Create a variable cucumbers that holds 100 and num_people that holds 6

Correct Answer

cucumbers = 100 num_people =

Question 30

Create a varible called whole_cucumbers_per_person that is the integer result of fividing cucumbers by num_people Print whole_cucumbers_per_person to the console.

Correct Answer

whole_cucumbers_per_person = 100/

Question 31

Create a variable called float_cucumbers_per_person that holdfsd the float result of dividing cucumbers by num_people. Print float_cucumbers_per_person to the console

Correct Answer

float_cucmbers_per_person = float(100)/

Question 32

We have seen how to define a string with single quotes and with double quotes. if we want a string to span multiple lines, we can also use triple quotes :

Correct Answer

address_+string = """136 Whowho Rd Apt 7 Whosville, WZ 44494

This address spans multiple lines, and is still contained in one varible, address_string. Whenm a string like this is not assigned to a variable, it works aas a multi-line comment. This can be helpful as tour code gets more complex:

Correct Answer

""" The following piece of

Question 34

When a string like this is not assigned to a variable, it works as a multi-line comment. This can be helpful as your code gets more complex:

Correct Answer

"""The following piece of code does the following steps: takes in some input does An Important Calculation returns the modified input and a string that says "Success!" or "Failure..." """ ... a complicated piece of code here...

Question 35

Create a varible called haiku and store this as multi-line string: The old pond, A frog jumps in: Plop!

Correct Answer

haiku = """The old pond A frog jumps in: Plop!"""

print haiku

Question 36

Sometimes we have a need for variables that are either true or false. This datatype, which can only ever take one of two values, is called a _____. In Python, we define these using the keywords True and False

Correct Answer

They are called a boolean a = True b = Flase

Question 37

Python automatically assigns a variable the appropriate datatype based on the value it is given. A variable with the value 7 is an integer, 7. is a float,"7" is a string. sometimes we will want to convert variables to different datatypes using str()

Correct Answer

age = 13 print "I am " + str(age) + " years old!" This would print out to " I am 13 years old!"

Print a string to the console that says: "I got x points!" with the value of point_total where x is

Correct Answer

print("Igot"+str(point_total)+" points!")

Question 1

Access the x th value of a tuple tup

Correct Answer tup[x]

Question 2

Turn the string str into a list (where each character is an element of the list)

Correct Answer list(str)

Question 3 In Python, strings ________ be changed. Correct Answer cannot

Question 4 In Python, strings cannot be changed (they're immutable). If you want to change a string, you can

Correct Answer convert it into a list and then convert it back into a string

Question 5 In Python, ____ means Null Correct Answer None

Question 6 Are "true" and "false" in Python capitalized? Correct Answer Yes

Syntax to mean that item item1 is optional (either a value or None)

Correct Answer

Optional[item1]

Question 8

Syntax to convert the integer x to its binary string representation

Correct Answer

bin(x)

Question 9

Write code to make dict1 equal {'a': 1, 'b': 3, 'c': 4}

dict1 = {'a': 1, 'b': 2} dict2 = {'b': 3, 'c': 4}

Correct Answer

dict1.update(dict2)

(Note: If dict1 and dict2 share a key X, dict2's value will correspond to key X.)

Question 10

Lists ( can/cannot ) be keys of a dictionary.

Correct Answer

cannot

Question 11

Tuples ( can/cannot ) be keys of a dictionary.

Correct Answer

can

Question 12

Add key x and value y to dictionary dict

Correct Answer

dict1[x] = y

Question 13

Add x to list list

Correct Answer

list1.append(x)

Turn anything (a list, an integer, etc.) into a set

Correct Answer

set(anything)

Question 21

A set of anything contains _______________________________

Correct Answer

only the unique elements (no duplicates) from anything

Question 22

Create an empty set set

Correct Answer

set1 = set()

Question 23

Add the item x to the set set

Correct Answer

set1.add(x)

Question 24

Add list2 to the end of list1 , making one big list in the end

Correct Answer

list1.extend(list2)

Question 25

Turn a string str1 into an integer (given that str1 is a string of numbers)

Correct Answer

int(str1)

If you want to modify a linked list but keep the head of the linked list equal to head1 , then do ____________. Then modify _____ all you want. When you're done modifying _____, when you return _____ it will have all of _____'s modifications

Correct Answer

head2 = head head head head head

Question 27

_____ and _____ can be used to find the lexicographically smallest and largest elements within an iterable, such as a list of strings. For dictionaries, they can be combined with _____ or _____ to find the min or max value or key

Correct Answer

min() max() .values() .keys()

Question 28

The syntax ___ finds the remainder of division between two numbers (so ____ calculates the remainder of x divided by y)

Correct Answer

% x % y

Question 29

All classes have a function called ______ , which is always executed when a new object is being created as an instance of the class

Correct Answer

init

Question 30

Fill in the init function: def init(x, y, z): ...

Correct Answer

self.x = x self.y = y self.z = z

Syntax that returns the number of times value appears in seq (a list or tuple)

Correct Answer

seq.count(value)

Question 36

Syntax: the value of i goes from start to stop , and increases by step each time. It ( does / doesn't ) include step (it ends at _______).

Correct Answer

for i in range(start, stop, step) doesn't the value before step

Question 37

Syntax to update each element of a list list1 (in this case, add 1 to each element)

Correct Answer

for i in range(len(list1)): list1[i] = list1[i] + 1

(Note: DON'T do this: for elem in list1: elem = elem + 1 Because this only modifies the variable elem and NOT the element of list1)

Question 38

If you want to see if x is a whole number, do this: __________. If it equals ___, then it's a whole number. If it doesn't equal ___, then it's not a whole number.

Correct Answer

x % 1 0 0

Question 39

Syntax to return a new sorted list from thing (any iterable (list, string, tuple, etc.)), leaving thing unchanged. ____ and ____ are optional. If you don't include them, then thing is _______ and is in ________. _____ is ____________________. ______ is _______________.

Correct Answer

sorted(thing, key=, reverse=) sorted by default (alphabetically, numerically, etc.) non-reverse order (from smallest to largest) key a function that is applied to each element of thing before comparisons are made for sorting (it can be a simple or anonymous function), which is the basis for sorting. reverse either True or False; False means it's sorted from smallest to largest (the default), and True means it's sorted from largest to smallest.

Syntax to calculate the sum of numeric items in thing (which can be a list, tuple, set, etc.)

Correct Answer

sum(thing)

Question 41

(For Python 3.7+, which is what Leetcode has) Python dictionaries are sorted by ____________

Correct Answer

the order you insert keys

Question 42

Delete the key-value pair associated with the key key1 from the dictionary dict

Correct Answer

del dict1[key1]

Question 43

Creates a new instance ( inst ) of a class Class.

Correct Answer

inst = Class1( all of Class1's parameters ) ( all of Class1's parameters means if Class1's init has parameters, all these parameters are filled here)

Question 44

Syntax that takes an iterable (like a list, string, or tuple) iter and creates a dictionary whose keys are the elements of iter and whose values are the number of times that key occurs in iter

Correct Answer

Counter(iter)

Question 45

Use ________ to create a queue in Python

Correct Answer

a deque

Question 46

Create a new empty deque called dq

Correct Answer

dq1 = deque()

Remove and return the leftmost element from the deque dq

Correct Answer

dq1.popleft()

Question 55

Add all elements from the iterable iter1 to the right end of the deque dq

Correct Answer

dq1.extend(iter1)

Question 56

Add all elements from the iterable iter1 to the left end of the deque dq1 , in reverse order

Correct Answer

dq1.extendleft(iter1)

Question 57

See what type x is. This will return a boolean: True if x ’s type is type_name , and False if x ’s type is not type_name.

Correct Answer

type(x) == type_name

Question 58

Syntax that returns True if the string string1 is made up of all digits, and False if the string string1 is not made up of all digits. Note: if string1 is a string of a negative number (for example, “-45”), then this will return False (because the negative sign is technically not a digit)

Correct Answer

string1.isdigit()

Question 59

Python only checks the while loop condition at the _________ of each iteration. This means that before each execution: Python evaluates the while loop's condition. If it's True, the code block inside the while loop is executed. If it's False, the loop's code block is never executed, and the program proceeds to the code after the loop. So, if the while loop's condition becomes false midway through the body of the loop, the current iteration will still finish. This is because Python only checks the while loop condition at the beginning of each iteration.

Correct Answer

beginning

Finish this syntax for HTTP GET. You're given a URL, and you want to put all the information in the dictionary called data.

Define the URL to send the GET request to

url = 'https://api.example.com/data'

Correct Answer

(# Send the GET request) response = requests.get(url)

(# Parse the JSON response into a Python dictionary) data = response.json()

Question 61

Syntax that creates an array arr1 that has value in it size number of times

Correct Answer

arr1 = [value] * size

Question 62

The syntax "______" is used within for loops and while loops. If you see "______", skip the remaining code within the current iteration of the loop and immediately proceed to the next iteration of the loop (aka go back to the start of the loop)

Correct Answer

continue continue

Question 63

Syntax that maps. It takes in a lambda function (lambda x: func) and a list list1 as an argument. It then goes through every element in the list list1 and applies the lambda function to it. Then, it the result back into a list (you must do this). In the end, you return a list where every element in it is equal to applying the lambda function to the original element.

Correct Answer

list(map(lambda x: func, list1))

(You must add the list() function. This is because the map() function just returns a map object, which is just a place in memory (and you can't use it))

Question 64

Lambda function

Correct Answer

lambda x: your_output