Stony Brook University**We aren't endorsed by this school
Course
CSE 337
Subject
Computer Science
Date
Dec 16, 2024
Pages
14
Uploaded by BarristerFieldMoose45
CSE/ISE 337 - Scripting LanguagesMidTerm 1 Spring 20242/14/20241. A language that converts a value from one data type to another to make an operation work is:A.Statically TypedB.Dynamically TypedC.Weakly TypedD.Strangely Typed2. Which of the following is not an advantage of interpreted languages, like Python:A.No waiting for compilation before executionB.Flexible modification because source code is alwaysavailableC.Platform independenceD.Efficiency of execution3. Which of the following is an appropriate use of a scripting language:A.Creating a Graphical User Interface (GUI) for an applicationB.Implementing an application where execution speed is criticalC.Connecting preexisting componentsD.(a) and (c)Page 2 of 16
CSE/ISE 337 - Scripting LanguagesMidTerm 1 Spring 20242/14/20244. What will be the output of the following Python code?print(type(type(int)))A.class ’int’B.class ’type’C.type ’int’D.05. Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after list1.pop(1)?A.[3, 4, 5, 20, 5, 25, 1, 3]B.[1, 3, 3, 4, 5, 5, 20, 25]C.[3, 5, 20, 5, 25, 1, 3]D.[1, 3, 4, 5, 20, 5, 25]6. Given a string s = “Welcome”, which of the following Python code is incorrect?A.print(s[0])B.print(s.lower())C.s[0] =’r’D.print(s.strip())7. What is the output of the following Python code?str1 = ’{2}, {1} and {0}’.format(’a’, ’b’, ’c’)str2 = ’{0}{1}{0}’.format(’abra’, ’cad’)print(str1, str2)A.c, b and a abracad0B.a, b and c abracadabraC.a, b and c abracadcadD.c, b and a abracadabraPage 3 of 16
CSE/ISE 337 - Scripting LanguagesMidTerm 1 Spring 20242/14/20248. Consider the following class definition:class Markula:__bat_exp = 999def __init__(self, lxp):self.landlord_exp = lxpIt is possible for different instances of the Markula class to have different values for the__bat_exp variable.A.TrueB.FalseC.BothD.None9. Consider the following class definition:class Markula:__bat_exp = 999def __init__(self, lxp):self.landlord_exp = lxpIt is possible for different instances of the Markula class to have different values for the self.landlord_exp variable.A.TrueB.FalseC.BothD.None10. Consider the recursive function defined below:def weird_fun(n):if n == 1:return 1else:return n*weird_fun(n-1)What is the largest integer input you can pass to this function that will lead to infinite recursion?A.0B.10C.100D.1000Page 4 of 16
CSE/ISE 337 - Scripting LanguagesMidTerm 1 Spring 20242/14/202411. What is the output of the following Python code?class Test:def __init__(self):self.x = 0class Derived_Test(Test):def __init__(self):self.y = 1def main():b = Derived_Test()print(b.x,b.y)main()A.0 1B.0 0C.AttributeError: ’Derived_Test’ object has no attribute ’x’D.Error an argument must be passed like Derived_Test(1)12. What type of inheritance is illustrated in the following Python code?class A():passclass B():passclass C(A,B):passA.Multi-level inheritanceB.Multiple inheritanceC.Hierarchical inheritanceD.Single-level inheritancePage 5 of 16
CSE/ISE 337 - Scripting LanguagesMidTerm 1 Spring 20242/14/202413. What will be the output of the following Python code?class A:def test1(self):print(" test of A called ")class B(A):def test(self):print(" test of B called ")class C(A):def test(self):print(" test of C called ")class D(B,C):def test2(self):print(" test of D called ")obj=D()obj.test()A.test of B calledtest of C calledB.test of C calledtest of B calledC.test of B calledD.Error, both the classes from which D derives has same method test()Page 6 of 16
CSE/ISE 337 - Scripting LanguagesMidTerm 1 Spring 20242/14/202414. What is the output of the following Python code?d1 = [10, 20, 30, 40, 50]d2 = [1, 2, 3, 4, 5]print(d2 - d1)A.0B.ErrorC.9D.4515. What is the output of the following Python code?a={}a[2]=1a[1]=[2,3,4]print(a[1][1])A.3B.[2,3,4]C.ExceptionD.216. What is the output of the following Python code?data = [x for x in range(5)]temp = [x for x in range(7) if x in data and x%2==0]print(temp)A.[0, 2, 4, 6]B.[0, 2, 4]C.[0, 1, 2, 3, 4, 5]D.Runtime errorPage 7 of 16
CSE/ISE 337 - Scripting LanguagesMidTerm 1 Spring 20242/14/202417. What is the output of the following Python code?D = dict()for i in range (3):for j in range(2):D[i] = jprint(D)A.{0: 0, 1: 0, 2: 0}B.{0: 1, 1: 1, 2: 1}C.{0: 0, 1: 0, 2: 0, 0: 1, 1: 1, 2: 1}D.TypeError: Immutable object18. What is the output of the following Python code?i = 1while True:if i % 3 == 0:breakprint(i, end="")i+= 1A.123B.12C.Syntax ErrorD.None of the above19. What is the output of the following Python code?tuple = {}tuple[(1,2,4)] = 8tuple[(4,2,1)] = 10tuple[(1,2)] = 12_sum = 0for k in tuple:_sum += tuple[k]print(len(tuple) + _sum)A.34B.12C.0D.33Page 8 of 16
CSE/ISE 337 - Scripting LanguagesMidTerm 1 Spring 20242/14/202420. What is the output of the following Python code?i = 0while i < 3:print(i, end="")i += 1if False:passelse:print(0)A.01230B.0120C.0D.Error21. Which of the following calls to the open() function will create the file if it does not already exist?A.open(’aFile.txt’)B.open(’aFile.txt’, ’a’)C.open(’aFile.txt’, ’r’)D.(a) and (b)22. Which of the following functions will take in the entire contents of a file as a single string?A.f.readline()B.f.readlines()C.f.read()D.(a) and (b)Page 9 of 16
CSE/ISE 337 - Scripting LanguagesMidTerm 1 Spring 20242/14/2024Part 2:Page 10 of 16
CSE/ISE 337 - Scripting LanguagesMidTerm 1 Spring 20242/14/202423. Short Questions [ 12 points/ 2 each]1. What is a Lambda Function? Give one example to explain the syntax of a lambda function in Python.2. How a Generator is different from a normal Python function?3. What is the use of an Iterator in Python?4. Given two variables, a1 and b1, write a single line of Python code to swap their values.Page 11 of 16
CSE/ISE 337 - Scripting LanguagesMidTerm 1 Spring 20242/14/20245. Given a tuple (18, ’19’, 20.0) assigned to the variablet, write a single line of code to assign the firstelement to a variable namedn, the second to a variable nameds, and the third to a variable namedf.6. What are Exceptions in Python and why they are used?Page 12 of 16
CSE/ISE 337 - Scripting LanguagesMidTerm 1 Spring 20242/14/202424. Write a Python function calledrel_freq()that takes a string as argument. The function then uses a dictio-nary to count how many times each character appears in the string. The function then creates a dictionary thatrecords the relative frequency of each character in the string (number of appearances / length of string). Thefunction should return the dictionary that contains the frequencies. Treat whitespace as a character (don’t try toremove it). [ 10 points]Example:>>> input_str = "hello hello">>> result = rel_freq(input_str)>>> print(result)>>>{’h’: 0.18, ’e’: 0.18, ’l’: 0.36, ’o’: 0.18, ’ ’: 0.09}Page 13 of 16
CSE/ISE 337 - Scripting LanguagesMidTerm 1 Spring 20242/14/202425. Given the following Python function:def f(n):result = Truefor i in range(2, (n // 2) + 1):if n % i == 0:result = Falsebreakreturn result[ 10 points for this question]• Explain what this function does.• For n = 27, give the result, and state how many times the loop will iterate and explain why.• For n = 19, give the result, and state how many times the loop will iterate and explain why.Page 14 of 16
CSE/ISE 337 - Scripting LanguagesMidTerm 1 Spring 20242/14/202426. Define a Python class called Circle. This class has one instance variable of type float,r. Define an__init__()method that takes one float argument to initialize the instance variable,r. Then define the methods that allow usto use Python’s comparison operators, == and !=, to determine whether two circle objects are or are not equal toeach other based on their respective radii. [ 10 points]Page 15 of 16