Computer Programming - MHS
What is used to concatenate two strings in Python?
b. +operator
What is the recursive function?
b. A function that calls itself
What is the output of the following program? names = "{1}, {2} and {0}" .format('John', 'Bill', 'Sean') print(names)
b. Bill, Sean, John
For the following code, which of the following statements is true? defprintHello(): print("Hello") a = printHello()
b. Both printHello() and a refer to the same object
What will happen if you try to open a file that doesn't exist?
c. An exception is raised
What is the output of the following code? class Foo: def printLine(self, line='Python'): print(line) o1 = Foo() 01.printLine('Java')
c. Java
11/20 Check the correct box
an object is an instance of a class
If return statement is not used inside the function, the function will return:
b. None object
What is the output of the following code? while 4 == 4: print('4')
c. 4 is printed infinitely until the program is closed
What does the following code do? os.listdir()
c. Prints all the directories and files inside the given directory
What is used to take input from the user in Python?
c. input()
10/20 Should you use recursion for factorial?
no
Does python have support for random numbers?
Yes, using random library
18/20 How can you include a module in Python?
import module
2/20 How do we get a variable from the command line?
input("Enter message")
Which is not a valid Python loop?
iter loop
14/20 If User is a class, how do we make a new User object?
x = User()
20/20 True or False, a function can be defined inside a function?
false
6/20 Which operator is used to compare two numbers for equality?
==
13/20 True or false, a dictionary is always in order
False
Who originally created Python?
Guido van Rossum
16/20 When would you use a while loop?
If the number of iterations is uncertain
12/20 What is the inheritance used for?
To give a class the methods and variables of an interface
What is a nested loop?
a loop inside a loop
17/20 What is a nested loop
a loop within a loop
What is the output of the following code? for i in [1, 0]: print(i+1)
a. 2 1
Which of the following is correct?
a. An exception is an error that occurs at the runtime
what is the output of the following code? for char in 'PYTHON STRING': if char == ' ': break print(char, end=' ') if char == '0': continue
a. PYTHON
The if...elif...else executes only one block of code among several blocks.
a. True
What is the output of the following program? print((1, 2) + (3, 4))
b. (1, 2, 3, 4)
What is the output of the following code? numbers = [1, 3, 6] newNumbers = tuple(map(lambda x: x, numbers)) print(newNumbers)
b. (1, 3, 6)
What is the output of the following code? class Point: def __init__(self, x = 0, y = 0): self.x = x+1 self.y = y+1 p1 = Point() print(p1.x, p1.y)
b. 1 1
What is the output of the following code? i = sum = 0 while i<= 4: sum += i i = i + 1 print(sum)
b. 10
What is the output of the following code? number = 5.0 try: r = 10/number print(r) except: print("Oops! Error occurred.")
b. 2.0
What is the output of the following code? class Point: def __init__(selfm x = 0, y = 0): self.x = x self.y = y def __sub__(self, other): x = self.x + other.x y = self.y + other.y return Point(x, y) p1 = Point(3, 4) p2 = Point(1, 2) result = p1-p2 print(result.x, result.y)
b. 4 6
What does the following code do? f = open("test.txt")
b. opens test.txt file for reading only
Suppose a list with name test, contains 10 elements. You can get the 5th element from the test list using:
b. test[4]
Is it better to use for loop instead of while if you are iterating through a sequence (like: list)?
b. yes, for loop is more pythonic choice
What is the output of the following program? result = lambda x: x * x print(result(5))
c. 25
In python, for and while loop can have optional else statement?
c. Both loops can have optional else statement
What is used to define a block of code (body of loop, function, etc.) in Python?
c. Indentation
How to access list elements?
d
Which of the following statements is true?
d. All of the above
Suppose a tuple test contains 5 elements. How can you set the 3rd element of the tuple to 'Python'?
d. Elements of tuple cannot be changed
For the following code, which of the following statements is true? f = open('test.txt', 'r', encoding = 'utf-8') f.read()
d. all of the above
Which of the following is correct?
d. all of the above
Which of the following statements is true?
d. all of the above
Which of the following codes closes file automatically if exception occurs?
d. both of the above
7/20 How can we define function in Python?
def add(a, b)
19/20 How to find your Python version?
python --version
How can we add two strings together?
s = s + s2
Does python support object oriented programming?
yes
How can we create a newline in a print function?
/n
8/ 20 Which is the correct output for: for i in range(1, 10): print(i)
1, 2, 3, 4, 5, 6, 7, 8, 9
When was Python first created?
1991
15/20 An object can contain:
Variables and methods
Which operator is used in Python to import modules form packages?
a. .operator
If you a class is derived from two different classes, it's called __________
b. Multiple Inheritance
9/20 What is the binary number 00001010 in decimal?
10
What is the output of the following program def Foo(x): if (x==1): return 1 else: return x+Foo(x-1) print(Foo(4))
a. 10
What is the output of the following program? squares = {1:1, 2:4, 3:9, 4:16, 5:25} print(squares.pop(4)) print(squares)
a. 16 {1: 1, 2: 4, 3: 9, 5: 25}
Which of the following is correct?
a. Variable name can start with an underscore
Which of the following statements is true?
a. a class is blueprint for the object
The statement using and operator results true if __________
a. both operands are true
What is the output of the following code? def greetPerson(*name): print('Hello', name) greetPerson('Frodo', 'Sauron')
b. Hello('Frodo', 'Sauron')
What is the output of the following code? def printLine(text): print(textm 'is awesome.') printLine('Python')
b. Python is awesome
What does the __init__() function do in python?
b. This function is called when a new object is instantiated.
What is the output of the following code? print(3 >= 3)
b. True
What is the output of the following program? language = ['P', 'y', 't', 'h', 'o', 'n',] print(language[:-4])
b. ['P', 'y']
What is the output of the following program? n = [x*x for x in range(4)] print(n)
b. [0, 1, 4, 9]
In the following code, n is a/an _______? n = '5'
b. string
Which of the following statements is true?
b. you can create a user-defined exception by deriving a class from Exception class
Which of these is not a Python datatype?
c
What is the output of the following code? print(1, 2, 3, 4, sep='*')
c. 1*2*3*4
Which of the following statements is true about the pass statement?
c. It is used as a placeholder for the future implementation of functions, loops, etc.
What is the output of the following code? if None: print("Hello")
c. Nothing will be printed
What does the following code do? try: # code that can raise error pass except (TypeError, ZeroDivisionError): print("Two")
c. Prints Two if TypeError or ZeroDivisionError exception occurs
What is the output of the following code? numbers = [2, 3, 4] print(numbers)
c. [2, 3, 4]
What is the output of the following program? def outerFunction(): global a a = 20 def innerFunction(): global a a = 30 print('a =', a) a = 10 outerFunction() print('a =', a)
c. a = 20
Which of the following code uses the inheritance feature in Python?
c. class Hoo(Foo): pass
Suppose you need to print pi constant defined in math module. Which of the following code can do this task?
c. from math import pi print(pi)
How can you change num = {'one': 1, 'two': 3} to num = {'one': 1, 'two': 2}
c. num['two']=2
Opening a file in 'a' mode
c. opens a file for appending at the end of the file
5/20 How do we remove an element from a list?
list.remove()
3/20 Which of these prints the length of a string s?
print len(s)
4/20 If we have a list x [1, 2, 3, 4], how do we print the first element?
print x[0]
1/20 Which function displays a message on the screen in Python?
print()