click below
click below
Normal Size Small Size show me how
IPT MOD 3&4
| Question | Answer |
|---|---|
| Given the code below: a = ‘the quick brown fox’ print (a[:10]) What will be the output? | the quick a[:10] means take the first 10 characters (from index 0 to 9). That includes the space after quick, so you’ll see a space at the end. |
| The replace() method replaces a string with another string. | True |
| Given the code below: a = “Study, Program” print (a[2:]) What will be the output? | udy a[2:] starts at index 2 and goes to the end of the string. |
| Square brackets can be used to access elements of the string. | True Square brackets [] are used to access elements (characters or slices) of a string in Python. |
| The lower() method removes any whitespace from the beginning or the end. | False |
| Parenthesis can be used to access elements of the string. | False You use square brackets [], not parentheses (), to access elements of a string in Python. |
| Given the code below: b = “Learning, Python!” print (b.lower()) What will be the output? | learning, python! |
| The strip() method splits the string into substrings if it finds instances of the separator. | False strip() does NOT split a string. It only removes whitespace (or specified characters) from the beginning and end of the string. |
| Given the code below: a = “Learning, Python!” print a.split (“ ,”)) What will be the output? | [‘Learning’, ‘Python!’ |
| String literals in python are surrounded by either single quotation marks, or double quotation marks. | True In Python, string literals can be written using single quotes ' ' or double quotes " ". |
| Binary files are not human readable. Group of answer choices True False | True |
| What does the <readlines> method returns? | list of lines The readlines() method reads all the lines from a file and returns them as a list, where each element is a line (including the newline character \n at the end, if present). |
| writelines() method reads the characters starting from the current reading position up to a newline character. | False The writelines() method does not read anything. It is used to write a list of strings to a file. |
| You need to read a file object that will be mapped to an image file called abc.txt. Which of the following is the correct syntax to use. | open('abc.txt','wt') |
| To open a file in binary format, add "b" to the mode parameter. | True |
| The buffer size argument is an optional parameter which decides the purpose of opening a file, e.g. read, write, append, etc. | False The buffer size argument in open() controls how much data is read/written at a time (buffering), not the purpose of opening the file. |
| The "w" mode will always treat the file as a new file. | True Opening a file in 'w' (write) mode will: Create a new file if it doesn’t exist. Truncate (erase) the file if it already exists, treating it as a new empty file. |
| "rb" mode opens the file in binary format for reading. | True |
| "r" mode allows reading only and not writing. | True |
| To open a file in binary format, add "a" to the mode parameter. | False Explanation: To open a file in binary format, you add b to the mode parameter (e.g., 'rb' for reading binary, 'wb' for writing binary). |
| Given the code below: a = “Study” b = “Program” c = a + b print (c) What will be the output? | StudyProgram |
| When slicing a String the return value is a range of characters. | True |
| Given the code below: a = “Learning, Python!” print (a[7]) What will be the output? | G |
| Given the code below: a = ‘the quick brown fox’ print (len(a)) What will be the output? | 19 |
| Given the code below: b = “Learning, Python!” print (len(b)) What will be the output? | 17 |
| Given the code below: a = “Learning, Python!” print (a[2:4]) What will be the output? | Ar |
| Given the code below: b = “Learning, Python!” print (b.upper()) What will be the output? | PYTHON! |
| An example of an illegal character is a double quote inside a string that is surrounded by double quotes. | True |
| Which of the following is used to open a file for both reading and writing? Group of answer choices w rb+ r+ w+ | r+ exp: 'r+' → Open a file for both reading and writing, without truncating the file. 'w+' → Open for reading and writing, but it truncates (erases) the file first. 'w' → Write only (truncates file). 'rb+' → Read and write in binary mode. |
| Which of the following is incorrect file handling mode in Python. | v |
| In the open() method, the first parameter is the name of a file including its path. | True |
| The readline() method used to save the contents of a list object in a file. | False |
| Which of the following is used to open a file for both writing and reading? | w+ |
| Which of the following is used to open a file for both appending and reading? | a+ |
| Which of the following is used to open a file for appending? | a |
| Which of the following is used to open a file for writing only? | w |
| Given the code below: a = ‘the quick brown fox’ print (a[4:]) What will be the output? | quick brown fox a[4:] means start at index 4 and go to the end of the string. |
| An escape character is a slash / followed by the character you want to insert. | False |
| When slicing a String the return value is a range of data types. | True |
| The lower() method replaces a string with another string. | False |
| Given the code below: a = ‘the quick brown fox’ print (a.upper ()) What will be the output? | THE QUICK BROWN FOX |
| Given the code below: a = “Learning” b = “Python” c = a + “ “ + b print (c) What will be the output? | Learning Python |
| To concatenate, or combine, two strings you can use the * operator. | False |
| Which of the following is incorrect file handling mode in Python. | xr |
| Which of the following is used to open a file for reading only? | r |
| The file object has an inbuilt iterator. | True |
| The built-in function bytearray() returns a byte representation of the object. | True The bytearray() function returns a mutable sequence of bytes representing the given object. |
| The access mode parameter is an optional parameter which decides the purpose of opening a file, e.g. read, write, append, etc. | True |
| You develop a Python application for your school. You need to read and write data to a text file. If the file does not exist it must be created. If the file has content the content must be removed. | open(“local_data”, “w+”) |
| The built-in function __________returns a byte representation of the object. | bytearray() |
| Which of the following is incorrect file handling mode in Python. | t+ |
| Given the code below: a = “Learning” b = “Python” c = a + b print (c) What will be the output? | LearningPython |
| Python does not have a character data type, a single character is simply a string with a length of 1. | True |
| Given the code below: b = “Python” print (len(b)) What will be the output? | 6 |
| Given the code below: a = “Learning, Python!” print (a[2:6]) What will be the output? | arni |
| Given the code below: a = “Learning, Python!” print (a[-5:-2]) What will be the output? | Tho |
| To concatenate, or combine, two strings you can use the + operator. | True |
| writelines() To get the length of a string, use the len() function. | False |
| Which of the following is incorrect file handling mode in Python. | rz |
| In the access mode, the first parameter is the name of a file including its path. | False |
| Which of the following is used to open a file for reading only binary formats? Group of answer choices ab+ w rb r+ | rb --- 'rb' → Open a file for reading only in binary mode. 'ab+' → Append and read in binary mode. 'w' → Write only (text mode). 'r+' → Read and write (text mode). |
| “____" mode allows reading only and not writing. | r |
| Strings in Python are arrays of bytes representing unicode characters. | True |
| Given the code below: x = “Success” print (len(x)) What will be the output? | 7 |
| Given the code below: a = ‘the quick brown fox’ print (‘fox’ in a) What will be the output? | True |
| Which of the following is used to open a file for both appending and reading in binary format? | ab+ |
| read(chars) method reads the specified number of characters starting from the current position. | True |
| To determine if a specified item is present in a list use the in keyword. | True |
| To change the value of a specific item, refer to the index item. | False |
| Tuple is a collection which is ordered and unchangeable. | True |
| Given the code below: aTuple = (10, 20, 30, 40, 50) print(aTuple[2:5]) What will be the output? | (30, 40, 50) |
| In Python tuples are written with square brackets | True |
| Tuples are mutable. | False |
| Once a tuple is created, you can change its values. | False |
| A tuple is a collection which is ordered and unchangeable. | True |
| Given the code below: aTuple = (100, 200, 300, 400, 500) print(aTuple[-3:-1]) What will be the output? | (300, 400) |
| Which of the following is a Python tuple? | (10, 20, 30) |
| What method used to return a list of all the values in the dictionary? | values() |
| To add more than one item to a set use the update() method. | True |
| In Python sets are written with square brackets. | False |
| What method used to determine how many items a dictionary has? | len() |
| Which of these about a set is not true? | Immutable data type |
| When using the pop() method in set, the item that gets removed is known. | False |
| Suppose d = {“john”:40, “peter”:45}, to delete the entry for “john” what command do we use? | del d[“john”] |
| To add one item to a set use the update() method. | False To add a single item to a set in Python, you use the add() method, not update() |
| The remove keyword will delete the set completely. | False |
| In Python sets are written with curly brackets. | True |
| Set is a collection which is unordered, changeable and indexed. | False |
| You cannot convert the tuple into a list. | False |
| Tuple is a collection which is ordered and changeable. | False |
| In Python lists are written with square brackets. | True |
| Set is a collection which is unordered and unindexed. | True |
| List is a collection which is unordered and changeable | False A list is a collection that is ordered and changeable (mutable) |
| List is a collection which is ordered and changeable | True |
| Given the code below: d1 = {“john”:40, “peter”:45} d2 = {“john”:466, “peter”:45} d1 == d2 What will be the output? | False |
| Adding an item to the dictionary is done by using a new index key and assigning a value to it. | True |
| What method used to return a copy of the dictionary? | copy() |
| Given the code below: >>> s={5,6} >>> s*3 | Error as unsupported operand type for set data type |
| What method is used to return a new set containing all items from both sets? | union() |
| The clear() keyword empties the dictionary. | True |
| A set is a collection which is unordered and unindexed. | True |
| To add more than one item to a set use the add() method. | True |
| Given the code below: aTuple = (10, 20, 30, 40, 50) print(aTuple[3:]) What will be the output? | (40, 50) |
| Given the code below: x = list (('abc',23, (78,16),3.14,[1,2,3,4])) print(x[-1]) What will be the output? | [1,2,3,4] |
| Given the code below: aTuple = (10, 20, 30, 40, 50) print(aTuple[:4]) What will be the output? | (10, 20, 30, 40) The slicing operator [:4] extracts elements from the start of the tuple up to, but not including, index 4, which corresponds to the first four items. |
| Given the code below: n = [4,2,8,5,7] n.append(3) print(n) What will be the output? | [4,2,8,5,7,3] |
| When specifying a range, the return value will be a new list with the specified items. | True |
| Dictionary is a collection which is unordered, changeable and indexed. | False |
| Once a tuple is created, you cannot change its values. | True |
| List is a collection which is ordered and changeable. | True |
| The copy() method returns a copy of the dictionary. | True |
| You can loop through a dictionary by using while loop. | True |
| Make a copy of a dictionary with the dict() method. | True |
| Given the code below: d = {“john”:40, “peter”:45} “d[“john”] What will be the output? | 40 In Python, a dictionary is a collection of key-value pairs. You access a specific value by providing its corresponding key inside square brackets. |
| The update() method is used to return a new set containing all items from both sets. | False |
| Given the code below: nums = set([1,1,2, 3,3,3,4,4]) print(len(nums)) What will be the output? | 4 A set in Python automatically removes duplicate values. |
| A dictionary is a collection which is unordered, changeable and indexed. | True |
| The len() method to determine how many items (key-value pairs) a dictionary has. | True The len() method returns the number of key-value pairs in a dictionary. |
| Given the code below: x = list (('abc',23, (78,16),3.14,[1,2,3,4])) print(x[:3]) | [‘abc’,23,(78,16)] x is a list with 5 elements. x[:3] means “get elements from index 0 up to (but not including) index 3”. That includes the first three elements of the list. |
| You can convert the tuple into a list. | True |
| Given the code below: aTuple = (10, 20, 30, 40, 50) print(aTuple[2:4]) What will be the output? | (30, 40) |
| Given the code below: x = list (('abc',23, (78,16),3.14,[1,2,3,4])) print(x[-1][-2]) What will be the output? | 3 |
| Given the code below: >>>t=(1,2,4,3) >>>t[1:3] What will be the output? | (2,4) |
| Given the code below: >>>my_tuple = (1, 2, 3, 4) >>> my_tuple.append = (5, 6, 7) >>>print len(my_tuple) | Error Tuples are immutable in Python, meaning you cannot add or change elements. |
| Which of the following statements is used to create an empty set? | set() |
| To add one item to a set use the add() method. | True |
| What method used to remove an item in a set? | pop() |
| You can loop through a dictionary by using a for loop. | True |
| Given the code below: aTuple = “Yellow”, 20, “Red” a, b, c = aTuple print (c) What will be the output? | Red |
| Suppose t = (1, 2, 4, 3), which of the following is incorrect? | t[3] = 45 |
| In Python lists are written with parenthesis. | False |
| Which of the following is a Python tuple? | (1, 2, 3) |
| What method used to remove all the elements from the set? | clear() |
| A list is a collection which is unordered and unindexed. | False |
| The union() method is used to return a new set containing all items from both sets. | True |
| What method used to add more than one item to a set? | update() |
| Given the code below: d1 = {“john”:40, “peter”:45} d2 = {“john”:466, “peter”:45} d1 > d2 What will be the output? Group of answer choices True False None Error | Error |
| The keys() method returns the value of the specified key. | False |
| To change the value of a specific item, refer to the index number. | False To change the value of a specific item in a dictionary, you refer to the key, not an index number. |
| Dictionary is a collection which is unordered and unindexed. | True |
| The pop() method can be used to remove an item in a set. | True |
| What method used to return the value of the specified key? | KEY() (NONE OF THE ABOVE TLAGA I2) |
| Given the code below: a = {5,4} b={1,2,4,5} a<b What will be the output? | True |
| Given the code below: x = list (('abc',23, (78,16),3.14,[1,2,3,4])) print(x[0][1:]) What will be the output? Group of answer choices ac ab abc bc | bc |
| Is a collection which is ordered and changeable. Allows duplicate members. | List |
| Tuples are immutable. | True |
| Given the code below: aTuple = “Yellow”, 20, “Red” a, b, c = aTuple print (a) Group of answer choices Yellow None of the given Red 20 | Yellow |
| When using the pop() method in set, the item that gets removed is unknown. | True |
| Given the code below: >>>t =(1,2,4,3) >>>t[1:1] What will be the output? Group of answer choices (1,2,4) (2,4,3) (1, 2) (2, 4) | (2, 4) (NONE OF THE ABOVE DAPAT) |
| Which of the following collection is unordered, changeable and indexed. | dictionary |
| The del keyword will delete the set completely. | True |
| A set is a collection which is unordered, changeable and indexed. | False |
| Given the code below: d = {“john”:40, “peter”:45} “john” in d What will be the output? | True |