For the coding questions please explain your answers.
1)
We have the following 2 Python statements:
str1 = "Testing"
print( str1 )
What are the 3 different ways we can run the above statements in a Unix system where Python is installed.
2)
What does the following print ?
x1 = 5
x2 = 2
x3 = x1 ** x2
x3 += 1
print( x3 )
print( len( str( x3) ) )
3)
What does the following print ?
str1 = "Testing"
str2 = str1[1:-1]
print( str2 )
str2 = str1[1:-1] + str1[-1]
str2
str2 = str2 + "a"*2
print( str2 )
print( len(str2 ) )
4)
What does the following print ?
list1 = [12]
list1 = list1 + [ 2,3,4 ]
print ( list1 )
list1.pop( 2 )
list1.append( 5 )
list1.sort()
print( list1 )
5)
list1 = [ [1,2] , [3,4] , [5,6] ]
list2 = [ x1 for x1 in list1 ]
print( list2 )
list2 = [ x1 for x1 in list1 if sum(x1) % 2 == 0 ]
print( list2 )
list2 = [ sum(x1) for x1 in list1 ]
print( list2 )
list3 = [ list2[0] , list2[1] ]
print( list3 )
6)
set1 = { 1, 4 ,6 , 7 }
set2 = { 1 ,2, 3, 6 }
print( set1 & set2 )
print( set1 | set2 )
print( set1 - set2 )
print( set2 - set1 )
print( set1 - set2 | set2 - set1 )
7)
What does the following print ?
dict1 = { "key1" : { "key2": {"key3" : [ "value1", "value2" , "value3" ] } } }
print( dict1[ "key1" ][ "key2" ][ "key3" ][-1] )
dict1.pop( "key1" )
dict1["name"] = "Joe"
dict1[ "age" ] = 20
dict1[ "age" ] = dict1.pop( "age" ) + 1
if not "age" in dict1:
print( "Step1" )
else:
print( "Step2" )
if len(dict1) == 2 :
print( "Step3" )
else:
print( "Step4" )
print( "Step5" )
print( "Step6" )
8)
What does the following print ?
for i1 in range( 3 ) :
for x1 in range( 2 , 10 ):
if ( x1 > 4 ):
break
print( x1 , end = " " )
continue
print( "End of for loop")
print ( i1 )
9)
What does the following print ?
try:
x2 = "Testing"
print ( x2 )
print(x1)
except:
print("An exception occurred")
finally:
print ( x2 )
10)
What does the following program print ?
for x1 in range(2,8,2) :
for y1 in range( x1, x1+2) :
print( x1 + y1 )
1)
The "Counter" class is a custom iterable object. Explain how it works and what does the "for" loop print ?
class Counter:
def __init__(self, low, high):
self.current = low - 1
self.high = high
def __iter__(self):
return self
def __next__(self) :
if self.current > self.high:
raise StopIteration
else:
self.current += 2
return self.current - 1
for c1 in Counter(3, 10):
print( c1 )
2)
What does the following print ?
var1 = "Testing"
def foo():
global var1
var1 = "the foo()."
print("var1 inside :", var1)
def fooOne():
var1 = "the fooOne."
print("var1 inside :", var1)
foo()
fooOne()
print("var1 outside:", var1)
3)
What does the below print ?
def printEmployee( id=1 , name="John", pay=100 ) :
print( "Employee Id:" , id , " Name:" , name, " pay" , pay )
printEmployee( 2 )
printEmployee( 3, "Louis", 300 )
printEmployee( name= "Doug", pay = 200 )
4)
What does the below print ?
class Base:
x1 = 200
def __init__( self, param1 ) :
self.x1 = 200
def getBaseVariable(self) :
return self.x1
class Derived( Base ) :
x1 = 100
def __init__( self, param1 ) :
super().__init__( param1 )
self.x1 = param1
d1 = Derived(10)
#Print derived class d1's x1 should print 100
#Print d1's x1 should print 10
print( d1.x1 )
print( Derived.x1 )
#print Base's x1
print( Base.x1 )
#print Base object's x1
print( d1.getBaseVariable() )
5)
What does the below print ?
class Point:
def __init__(self, x1, y1) :
self.x1 = x1
self.y1 = y1
def __str__(self) :
return( "x cordinate is:" + str(self.x1) +
" y cordinate is:" + str(self.y1) )
def __add__(self, point1) :
return( Point( self.x1 + point1.x1, self.y1 + point1.y1 ) )
def __sub__(self, adjust) :
x2 = self.x1 - adjust
y2 = self.y1 - adjust
return( Point( x2, y2 ) )
p1 = Point( 1,1 )
p2 = Point( 2,2 )
p3 = p1 + p2
p3 = p3 - 1
print( p1 )
print( p2 )
print( p3 )
6)
What does the following print ?
def function1( x1 ) :
try:
x2 = 2
assert x1 == x2
except:
print("caught exception")
else:
print("No exception raised")
finally:
print("finally")
function1( 2 )
function1( 3 )
7)
What does the following print ?
def function_square( x1):
return ( x1 * x1 )
items1 = [ 1 ,3 , 5, 7 ]
list1 = list( map(function_square, items1 ) )
print( list1 )
8)
What does the following print ?
class Super:
def __init__(self) :
print( "Super Constructor." )
def method(self):
print('in Super.method')
def delegate(self):
self.action()
def action( self ) :
assert False, 'action must be defined!'
class ProviderBase( Super ): # Fill in a required method
def action(self):
print('in Provider Base.action')
class Provider( ProviderBase ): # Fill in a required method
def __init__(self) :
super().__init__()
print( "Provider Constructor." )
p1 = Provider()
p1.delegate()
9)
What does the following print ?
class tire :
def __init__( self, noOfTires, brandName ) :
self.noOfTires = noOfTires
self.brandName = brandName
def __str__( self ) :
return( "Tire info: noOfTires: " + str(self.noOfTires) + " brandName:" +
str(self.brandName) )
class engine :
def __init__( self, noOfCC, gasOrDiesel, manFacturerName ) :
self.noOfCC = noOfCC
self.gasOrDiesel = gasOrDiesel
self.manFacturerName = manFacturerName
def __str__( self ) :
return( "Engine info: noOfCC: " + str(self.noOfCC) + " gasOrDiesel:" +
str(self.gasOrDiesel) + " manFacturerName:" +
str(self.manFacturerName) )
class vehicle :
def __init__( self, tireObj, engineObj ) :
self.tireObj = tireObj ;
self.engineObj = engineObj ;
def __str__( self ) :
return( "Vechicle info: " + str(self.tireObj) +
" " + str(self.engineObj) )
tireObj1 = tire( 4 , "FireBird" )
engineObj1 = engine( 1600 , "gas" , "Dodge" )
vehicleObj1 = vehicle( tireObj1, engineObj1 )
print( vehicleObj1 )
10)
What does the below print ?
def simple_generator_function():
print( "Step1" )
yield 1
print( "Step2" )
yield 2
print( "Step3" )
yield 3
print( "Step4" )
gen1 = simple_generator_function( )
next( gen1 )
print( "Next1" )
next( gen1 )
print( "Next2" )
next( gen1 )
print( "Next3" )