

















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


















Which of the following is the correct extension of the Python file? - Answers - .py Does python code need to be compiled or interpreted? - Answers - python code is both compiled and interpreted A syntax error is a _____________________. - Answers - The program contains invalid code that cannot be understood. It is found before the program is ever run by the interpreter. Which of the following is used to define a block of code in Python language? - Answers
" " " - Answers - All of them Which of the following is a legal Python identifier? - Answers - _oneForAll Which of the following is an example of an invalid assignment or declaration statement?
print("There " + "Everywhere") print("But not " + "in Texas" - Answers - "There Everywhere" on the same line as "Here" Which statement reads value as integer type? - Answers - x =int( input( 'Enter an integer' ) ) To find distance between point (x1, y1) and point (x2, y2), which expression is correct? - Answers - distance = math.sqrt( (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) ) Assume age = 10 pet = "dog" pet_name = "Gerald" To generate the output "Gerald is 10 years old dog", which print statement is NOT correct? - Answers - print( f"pet_name, ' is ', age, ' years old ', pet " ) Which of the following is not a core data type in Python programming? - Answers - Clsss What will be the output of the following Python code? str = 'abcd' print(str.upper()) print(str[1].upper()) - Answers - ABCD B Choose the container that best fits the described data.
to delete the entry for "john" what command do we use? - Answers - del d1["John"] Suppose dictionary d1 = {"Susan":42, "John":40, "Peter":45} To get Susan's age, what command do we use? - Answers - d1.get("Susan") What is the output of print str[2:5] if str = 'Python Programming'? - Answers - tho What is the output of print str[2: ] if str = 'Python Programming'? - Answers - thon Programming What is the output of print str[ : : 3 ] if str = 'Python Programming'? - Answers - Ph oai What will be the output of the following Python code? - Answers - ABCD What is the output of print str.title() if str = 'abcd'? - Answers - Abcd Given str="Hello, labor day!" what is the output of str.count( 'l' )? - Answers - 3 Given str="Hello, labor day!" what is the output of str.find( 'a' )? - Answers - 8 What will be the output of the following Python code? >>> url ='https://ung.edu/' >>> url.startswith('http') - Answers - True What will be the output of the following Python code? >>> url ='https://ung.edu/' >>> url[ : :-1].startswith('/') - Answers - None What is "Hello World".replace( "l", "e")? - Answers - Heeeo Wored What is the output of print "yxyxyxyxyxy".count('yxy')? - Answers - 3 What will be the output of the following Python code? "abcdef".find("cd") == "cd" in "abcdef" - Answers - return False What is the output of print 'abcdefcdghcd'.split('cd')? - Answers - ['ab', 'ef', 'gh', ''] What is the output of print 'abcdefcdghcd'.split('cd', 1)? - Answers - ['ab', 'efcdghcd'] What will be the output of the following Python code? colors = ('red', 'green', 'blue') rgb = '-'.join(colors) print(rgb) - Answers - red-green-blue What will be the output of the following Python code?
student= ( 'Jack', 19, 3.6) result= ", ".join( student ) print( student ) - Answers - Error What will be the output of the following Python code? student= ( 'Jack', 19, 3.6) result= ", ".join( str(d) for d in student ) print( student ) - Answers - Jack, 19, 3. What are the outputs of print the following statements? 'xyyabcdxyy'.strip('xyy') 'xyyabcdxyy'.lstrip('xyy') 'xyyabcdxyy'.rstrip('xyy') - Answers - abcd abcdxyy xyyabcd Assume str = "abcdefg", to check whether string str contains "cd", use ________ - Answers - "cd" in str What are the outputs of the following Python script? str = "abcd1234" print( str.isalnum( ) ) print( str.isalpha( ) ) print( str.isdigit( ) ) print( str.islower( ) ) print( str.capitalize( ) ) - Answers - True False False True Abcd Every if statement requires an associated else statement, but not every else statement requires an associated if statement. - Answers - False Which of the following best describes this code snippet? - Answers - If the variable count is not equal to 400, "Hello World" will be printed. Which of the following expressions best represents the condition "if the grade is between 75 and 100"? - Answers - if 75 < grade and grade < 100 : In Python, a block statement is - Answers - a set of statements that are all indented to the same column. In Python, the symbol "=" and the symbol "==" are used synonymously (interchangeably). - Answers - False
if num1 < num2 : print( 'red', end=' ') if (num1 +5) < num2 : print( 'white', end=' ' )else : print( 'blue', end=' ') print( 'yellow') - Answers - red blue yellow What output is produced by the following code fragment? limit =100; num1 = 15; num2 = 40 if limit <= 100 : if ( num1 == num2) : print( "lemon") print("orange") print( "grape") - Answers - orange grape What is wrong, logically, with the following code? if x > 10: print("Large") elif x > 6 and x <= 10: print("Medium") elif x > 3 and x <= 6: print("Small") else : print("Very small") - Answers - There is no logical error, but there is no need to have (x <= 10) in the second conditional or (x <= 6) in the third conditional For the following code, how to rewrite it by short hand if...else statement? if x > y: print( x + 1 ) else : print( y - 1 ) - Answers - print(x + 1) if x > y else print(y-1) For the following code, how to rewrite it by short hand if...else statement? if x > y: print( x + 1 ) else : print( y - 1 ) - Answers - print(x + 1) if x > y else print(y-1) Which expression checks whether the list my_list contains the value 15? - Answers - 15 in my_list Which of the following for loop will cause the body of the loop to be executed 100 times? - Answers - for x in range (0, 100) What output is produced by the following code fragment? x = 0 for i in range(1, 5): x += i
print(x) - Answers - 10 Given that str is a String, what does the following loop do? str = 'abcdef' for x in str[ : - 1 ] : print( x, end=' ') - Answers - it prints str out forwards: a b c d e f Given that str is a String, what does the following loop do? str = 'abcdef' for x in str[ : : - 1 ] : print( x, end=' ') - Answers - it prints s out backwards: f e d c b a What output is produced by the following code fragment? for value in range (20, - 1, - 1): if value % 4 != 0: print( value, end=', ' ) - Answers - print even number from 2 to 18: 2, 4, 6, 8, 10, 12, 14, 16, 18 What output is produced by the following code fragment? x = 0 while x < 100: x *= 2 print(x) - Answers - Error. Go to infinite loop What output is produced by the following code fragment? x = 1 while x < 100: x *= 2 print(x) - Answers - 128 What output is produced by the following code fragment? x = 1 while x < 100: x *= 2 if x == 64: break print(x, end=' ') - Answers - 2 4 8 16 32 What output is produced by the following code fragment? x = 1 while x < 100: x *= 2 if x == 64: continue
i += 1 - Answers - A B C 1 2 3 D E F 4 5 6 5 0 For str = "abc123_def456_$5.0", which code can print letters only? The output should be: a b c d e f - Answers - str = "abc123_def456_$5.0" i = 0 while i< len(str) - 1 : if str[i].isalpha(): print( str[i], end=' ') elif str[i].isdigit(): pass i += 1 Which keyword is used for function? - Answers - def Which are the advantages of functions in python? - Answers - All of above What are the two main types of functions in Python? - Answers - Built-in function & User defined function Where is function defined in Python? - Answers - All of the mentioned (Module, Class, Another function) The function must return a value - Answers - False Which correctly defines two parameters x and y for a function definition: def calc_val( ): - Answers - calc_val (x, y) : Given a function definition:def calc_val(a, b, c):and given variables i, j, and k, which are valid arguments in the call calc_val( )? - Answers - calc_val( k, i+ j, 99 ) A local variable is defined inside a function, while a global variable is defined outside any function. - Answers - True A function definition must be evaluated by the interpreter before the function can be called. - Answers - True What will be the output of the following Python code? def printMax(a, b):if a > b:print(a, 'is maximum')elif a == b:print(a, 'is equal to', b)else:print(b, 'is maximum') x = 3 y = 4 printMax( x+1, y) - Answers - 4 is equal to 4 What will be the output of the following Python code? x = 50 def func(x):
print('x is', x) x = 2 print('Changed local x to', x) func(x) print('x is now', x) - Answers - x is 50 Changed local x to 2 x is now 50 What will be the output of the following Python code? x = 50 def func( ): global x print('x is', x) x = 2 print('Changed global x to', x) func( ) print('x is now', x) - Answers - x is 50 Changed global x to 2 Value of x is 2 What will be the output of the following Python code? def say(message, times = 1): print(message * times) say('Hello') say('World', 5) - Answers - Hello WorldWorldWorldWorldWorld What will be the output of the following Python code? def func(a, b=5, c=10): print('a is', a, 'and b is', b, 'and c is', c) func(3, 7) func(25, c = 24) func(c = 50, a = 100) - Answers - a is 3 and b is 7 and c is 10 a is 25 and b is 5 and c is 24 a is 100 and b is 5 and c is 50 What will be the output of the following Python code? def change(a, b): a = a + 1 b = b + 1 print(a, b)
print( foo(1, 2, 3) ) print( foo(1, 2, 3, 4, 5) ) - Answers - 6 15 What will be the output of the following Python code? def foo( arg1, *args ): result = 0 for i in args: result += i return result print( foo(1, 2, 3) ) print( foo(1, 2, 3, 4, 5) ) - Answers - 5 14 What will be the output of the following Python code? def foo( i, x=[ ]): x.append(i) return x if name == "main": for i in range( 3 ): print( foo( i ) ) - Answers - [0] [0, 1] [0, 1, 2] A program can modify the elements of an existing list - Answers - True All elements of a list must have the same type. - Answers - False What will be the output of the following Python code? a= [1, 2, 3, 4, 5] for i in range(1, 5): a[ i-1 ] = a[ i ] for number in a: print( number, end = " ") - Answers - 2 3 4 5 5 What will be the output of the following Python code? my_list= ["Apple","Ball","Cobra","Don","Accomplish"] my_list.sort( key=len )
print( my_list ) - Answers - ['Don', 'Ball', 'Apple', 'Cobra', 'Accomplish'] What will be the output of the following Python code? def change(var, lst): var = 1 lst[0] = 44 x = 10 my_list = [1, 2, 3] change(x, my_list) print(x) print(my_list) - Answers - 10 [44, 2, 3] What will be the output of the following Python code? my_list = ['One', 'Two', 'Three'] for i, x in enumerate( my_list): print('{}: {}'.format(i, x),end=" ") - Answers - 0: One 1: Two 2: Three What is the output of the following python code fragment? values = [x**2 for x in range(0, 11)] print(values) - Answers - [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100] What will be the value of 'result' in following Python program? list1 = [1,2,3,4] list2 = [2,4,5,6] list3 = [2,6,7,8] result = list() result.extend( i for i in list1 if i not in (list2+list3) and i not in result ) result.extend( i for i in list2 if i not in (list1+list3) and i not in result ) result.extend( i for i in list3 if i not in (list1+list2) and i not in result ) print(result) - Answers - [1, 3, 5, 7, 8] What output in following Python program? list1 = [1,2,3,4] list2 = [2,4,5,6] list3 = [2,6,7,8] result1 = list1 + list result2 = list2 + list
insert('Apple') insert('Banana') insert('Apple') insert('Orange') insert('Apple') insert('Orange') print( total ) - Answers - {'Apple': 3, 'Banana': 1, 'Orange': 2} What will be the output of the following Python code? numbers = {} letters = {} comb = {} numbers['Josh'] = 89 numbers['Alex'] = 95 letters[1] = 'B' letters[2] = 'A' comb['Numbers'] = numbers comb['Letters'] = letters print( comb ) print( comb.get( 'Numbers') ) print( comb.get( 'Letters').get(2) ) - Answers - {'Numbers': {'Josh': 89, 'Alex': 95}, 'Letters': {1: 'B', 2: 'A'}} {'Josh': 89, 'Alex': 95} A What is the length of sys.argv? - Answers - number of arguments + 1 What is the value stored in sys.argv[0]? - Answers - The program's name What is the list comprehension equivalent for? {x : x is a whole number less than 20, x is even} (including zero) - Answers - [x for x in range(0, 20) if (x%2 ==0)] What will be the output of the following Python code? myList=["good", "oh!", "excellent!", "#450"] print( [item for item in myList if item.isalpha() or item.isdigit()] ) - Answers - ['good'] What is Instantiation in terms of OOP terminology? - Answers - Creating an instance of class
A class can be used to group related variables together. - Answers - True _____ is used to create an object? - Answers - constructor Find the correct matching explanation for each term - Answers - 3, A group of related variables and functions. 1, A name following a "." symbol. 5, An instantiation of a class. 2, A method parameter that refers to the class instance. 4, A constructor method that initializes a class instance. Find the correct matching explanation for each class or object term - Answers - 2, A variable that exists in a single instance. 5, Represents a single instance of a class. 3, A variable shared with all instances of a class. 1, Functions that are also class attributes. 4, A factory for creating new class instances. What will be the output of the following Python code? class test: def init(self, a="Hello World"): self.a = a def display(self): print(self.a) obj=test() obj.display() - Answers - "Hello World" is displayed What will be the output of the following Python code? class test: def init( self, a ): self.a = a def display(self): print(self.a) obj=test() obj.display() - Answers - Error as one argument is required while creating the object What will be the output of the following Python code? class test: def init(self):
print("Count.test=", Count.test) print("k =", k) main( ) - Answers - Count.test= k= To fix the error in the following Python code, which answer is correct? class Person: def init(self, the_last, the_first): last = the_last first = the_first def str(self): return( f"{last} {first} " ) p1 = Person( ) print( p1 ) - Answers - class Person: def init(self, the_last, the_first): self.last = the_last self.first = the_first def str(self): return(f"{self.last} {self.first} " ) p1 = Person("AB","CD") print(p1) Which code can output "we will go to mountain"? - Answers - class Foo: a = "mountain" def init(self, a): self.a = a def someday(self): return "we will go to " + Foo.a f1 = Foo("Hi, Jack") print(f1.someday()) Which of the following is not an exception handling keyword in Python? - Answers - accept Execution jumps to an except block only if an error occurs in the preceding try block. - Answers - True In the try-except construct, how many except statements can have? - Answers - more than 0 When will the else part of try-except-else be executed? - Answers - When no exception occurs
When is the finally block executed? - Answers - Always Find the correct term matching: - Answers - 5, Causes a ValueError exception to occur 1, Describes a block of code that uses exception-handling 2, An exception handler for NameError exceptions 3, An exception handler for ValueError and NameError exceptions 4, A catch-all exception handler What will be the output of the following Python code? def foo(): try: return 1 finally: return 2 k = foo() print(k) - Answers - 2 What will be the output of the following Python code? def foo(): try: print(1) finally: print(2) foo() - Answers - 1 2 What will be the output of the following Python code? try: if '1' != 1: raise "someError" else: print("someError has not occurred") except "someError": print ("someError has occurred") - Answers - invalid code What will be the output of the following Python code? def foo(): try: print( x + 4) finally: print('print finally block')