Python Course Quiz 1

 Python Course Quiz 1


Ques 1: What is the best way to see which version of python3 is installed without running it?

·   (a) python -version


·   (b) python3


·   (c) python -v


·   (d) python3 --version

 Answer: Option(Dpython3 --version


Ques 2: When should you verify what the VS Code python.pythonPath setting is set to?

·  (a) when you have multiple installations of Python on your computer

·  (b) when the desired Python debugging extension fails to install

·  (c) when the debugger does not default to your script's directory

·  (d) when VS Code fails to load your Python script

 Answer: Option(A): when you have multiple installations of Python on your computer


Ques 3: What code should come instead of the ??? placeholders to have a function that takes the amount of local currency and a varying number of exchange rates, and prints the value of the currency at each provided exchange rate?

def Calc(???):

     for i in rates:

         print(???)


·  (a)  currency,*rates; currency*i


·  (b) *currency,rates; currency*i


·  (c) currency,rates; i*currency


·  (d) *rates,currency; currency+i

    

    Answer: Option(A)  currency,*ratescurrency*i


    

    Ques 4: What code will you need to place instead of the ??? placeholder for the script to print '3'?

a=1

b=2 

def func(): 

            ??? 

func() 

print(b)


·  (a) global a 

               b=3


·  (b) b=b+1


·  (c) global b 

              b=a+b


·  (d) b=a+a+a


    Answer: Option(C) global b

                                     b=a+b

    

    

    Ques 5: You have an existing class Simple() that returns the sum of two numbers using its Add(x,y) method. How can you leverage it to build another class calculates the inverse of the sum of two numbers?


·  (a) class Advanced(Simple): 

            def Inverse(x,y): 

                  sum=Simple.Add(x,y)

                   return (1/sum)


·  (b) class Advanced(Simple): 

            def Inverse(self,x,y): 

                return (1/Simple.Add(self,x,y))


·  (c) class Advanced(): 

            def Inverse(self,x,y):

                 return (1/Simple.Add(x,y))

    

·  (d) class Advanced(): 

            def Inverse(x,y): 

                return (1/(x+y))

B

    Answer: Option(B) class Advanced(Simple): 

                                        def Inverse(self,x,y): 

                                            return (1/Simple.Add(self,x,y))


    

    Ques 6: This code has three critical issues. Which is not actually an issue?

def main:

         print(hello!)


·   (a) The function must be defined with the keyword 'func', not 'def'.

·  (b) The string to print is missing quotes before and after it.

·  (c) The print statement needs to be indented using spaces or a tab.

·  (d) The function definition is missing parentheses that define the argument list.

    

    Answer: Option(A) The function must be defined with the keyword 'func', not 'def'.

    

    

    Ques 7: In Python, what is the correct way to develop a class called Person that has parameters in the initialize function called name, age, and sex?


·   (a) class initialize: 

                def __Person__(self, name, age, sex):

                self.name = name 

                self.age = age 

                self.sex = sex


·   (b) class Person:

             def __initialize__(self, name, age, sex): 

                   self.name = name 

                   self.age = age 

                   self.sex = sex


·   (c) class def Person:

              (self, name, age, sex): 

                    self.name = name 

                    self.age = age 

                    self.sex = sex


·   (d) class Person{ 

               def __initialize__(self, name, age, sex):

                    self.name = name 

                    self.age = age

                    self.sex = sex

     } 

    

    Answer: Option(B): class Person:

                                         def __initialize__(self, name, age, sex): 

                                              self.name = name 

                                              self.age = age 

                                              self.sex = sex


    

    Ques 8: What will the following script print?

def inc(a,b=1): 

          return(a+b)

 a=inc(1)

 a=inc(a,a)

 print(a)

(a) The script will error out.

(b) 4

(c) 3

(d) 2

Answer: Option(B): 4


Ques 9: You need to set the annual payment in one function and print the respective monthly payment in a separate function. How would you fix the suggested code to work properly?


def SetAnnual():

     annual=10000 

def PrintMonthly(): 

    print("Your monthly payment is "+annual/12+" USD.") 

SetAnnual() 

PrintMonthly()


(a) annual=0 

def SetAnnual(): 

    annual=10000 

def PrintMonthly():

     print("Your monthly payment is "+annual/12+" USD.") 

SetAnnual()

PrintMonthly()


(b) annual=0 

    def SetAnnual(): 

        annual=10000 

   def PrintMonthly(): 

        print("Your monthly payment is "+str(annual/12)+" USD.") 

    SetAnnual() 

    PrintMonthly()


(c) annual=0 

    def SetAnnual(): 

        global annual=10000

 def PrintMonthly():

     print("Your monthly payment is "+annual/12+" USD.") 

SetAnnual()    

PrintMonthly()


(d) def SetAnnual():

         global annual 

         annual=10000 

    def PrintMonthly(): 

        print("Your monthly payment is "+str(annual/12)+" USD.") 

    SetAnnual() 

    PrintMonthly()

 Answer: Option(D) 

def SetAnnual():

         global annual 

         annual=10000 

    def PrintMonthly(): 

        print("Your monthly payment is "+str(annual/12)+" USD.") 

    SetAnnual() 

    PrintMonthly()

 

Ques 10: Which code snippet can you use to print the number of digits in the number variable? You can assume this number is always positive and always less than 10,000.

(a) if (number>=0):

         print(1) 

    elif (number>=10):

         print(2) 

    elif (number>=100): 

            print(3) 

    else: 

            print(4) 

(b) if (number<=10):

          print(1) 

    elif (number<=100): 

            print(2) 

    elif (number<=1000): 

            print(3) 

     else:

              print(4)

(c) if (number>=1000): 

          print(4) 

    elif (number>=100): 

            print(3) 

    elif (number>=10):

            print(2) 

    else: 

              print(1)

(d) if (number<10000): 

          print(4) 

      elif (number<1000): 

            print(3) 

      elif (number<10): 

             print(2) 

        else: 

                print(1)

 Answer: Option(C): 

 if (number>=1000): 

          print(4) 

    elif (number>=100): 

            print(3) 

    elif (number>=10):

            print(2) 

    else: 

              print(1)


Ques 11: Which alternative code is logically equivalent to the code below?

max=x if (x>y) else y


(a) if (x>y): 

         max=y 

    elif (x==y):

           max=y 

     else: 

             max=x


(b) if (x>=y): 

         max=x 

     elif: 

            max=y


(c) max=y 

    if (x>y):

         max=x


(d) if (y>x):

             y 

     else:

             x

Answer: Option(D): 

 if (y>x):

             y 

     else:

             x



Ques 12: Why is the code below often added to a Python program file?

if __name__ == "__main__": 

                                                    main()


·   (a)  It executes the main() function only if this file is executed as the main program.

·   (b) It assures that the program will run correctly when executed from either an IDE or command line.

·   (c) It confirms that it is an actual Python program before starting the interpreter.

·   (d) It allows us to skip calling the main() function by defining an environment variable.

    

    Answer: Option(A): It executes the main() function only if this file is executed as the main program.

 

Ques 13:  Which function will return the sum of the first item in the data list and every tenth item after?

(a) def Sum10th(data): 

            sum=0 

            for i,d in enumerate(data): 

                    if (i % 10 != 0): sum=sum+d 

            return sum

(b) def Sum10th(data): 

            sum=0 

            for i,d in enumerate(data):

                 if (i % 10 == 0): sum=sum+d 

            return sum

(c) def Sum10th(data): 

            sum=0 

            for i,d in enumerate(data):

                  if (i % 10 == 0): continue 

                   sum=sum+d 

            return sum

(d) def Sum10th(data):

             sum=0 

             for i,d in enumerate(data): 

              if (i % 10 == 0): return sum 

              sum=sum+d

Answer: Option(B):  

def Sum10th(data): 

            sum=0 

            for i,d in enumerate(data):

                 if (i % 10 == 0): sum=sum+d 

            return sum


Ques 14: Which real-world scenario is best described as a standard while loop?

(a) Read a book starting at a certain page, and finish at a certain page.

(b) Read a book by skipping every second page.

(c) Start reading a book, and stop after reading a certain number of pages.

(d) Read a book in its entirety, from the end to the beginning.

Answer: Option(c) Start reading a book, and stop after reading a certain number of pages.

 

 

 

Comments

Popular posts from this blog

Python Quiz 2

Supervised Learning Quiz