QnA - Python Revision

Python Fundamentals

  • Which of the following is valid arithmetic operator in Python:
           (i) //         (ii) ?         (iii) <         (iv) and
  • Identify the valid arithmetic operator in Python from the following.
a) ?         b) <         c) **         d) and

  • Find the invalid identifier from the following
            1. (a) MyName         (b) True           (c) 2ndName           (d) My_Name
            2.  (a) f%2                 (b) 20ans         (c) ans                     (d) $ans
            3. (a) and                   (b) for_is         (c) simple-interest   (d) 4you
  • Which of the following expressions is an example of type casting?

a) 4.0+float(6)                  b) 5.3+6.3              c) 5.0+3             d) None of these

  • Which of the following is an invalid datatypes in Python?

(a) Set                     (b) None               (c) Integer               (d) Real 

  • To use randint(a,b) which of the following module should be imported ?

                a)math           b)random        c)CSV        d)randinteger() 

        • Name the built-in mathematical function / method that is used to return an absolute value of a number.
        • Write the type of tokens from the following:

                             (i) if     (ii) roll_no

        • Name the Python Library modules which need to be imported to invoke the following functions:

                            (i) sin()         (ii) randint ()

        • State weather True or False:

        “ The expression 2**2**3 is evaluated as (2**2)**3.
        • Evaluate the following expressions:
        (1) 5 // 10 * 9 % 3 ** 8 + 8 – 4
        (2) 65 > 55 or not 8 < 5 and 0 != 55
        (3) 10*1*2**4-4//4 
        (4) 65 > 55 or not 8 < 5 and 0 != 55 

         

        • How many times the following loop will execute: 

        for L in range(7,29,4):
            if L%5==0: 
                break 
            else: 
                print(L) 

        • Find the Output:

        Msg1="WeLcOME"
        Msg2="GUeSTs"
        Msg3=""
        for I in range(0,len(Msg2)+1):
            if Msg1[I]>="A" and Msg1[I]<="M":
                Msg3=Msg3+Msg1[I]
            elif Msg1[I]>="N" and Msg1[I]<="Z":
                Msg3=Msg3+Msg2[I]
            else:
                Msg3=Msg3+"*"
                print(Msg3)

          •  What will be the output of following program:
            s="welcome2kv"
            n = len(s) 
            m=""
        for i in range(0, n):
            if (s[i] >= 'a' and s[i] <= 'm'):
                m = m +s[i].upper()
            elif (s[i] >= 'n' and s[i] <= 'z'):
                m = m +s[i-1]
            elif (s[i].isupper()):
                m = m + s[i].lower()
            else:
                m = m +'#' 
                print(m)
          •  Find the Output:

        x = 27
        y = 9
        while x < 30 and y < 15:
        x = x + 1
        y = y + 1
        print(x,y) 
          • Rewrite the following code in python after removing all syntax error(s). Underline each correction done in the code.

                (1) 

        30=To
        for K in range(0,To)
        IF k%4==0:
        print (K*4)
        Else:
        print (K+3)

                (2)

        num=int(input("Enter any integer number"))
        fact=1
        for x of range(num,1,-1)
            if num=1 or num=0
                print ("Fact=1")
                break
            else
                fact=fact*x
        print(fact)

                (3)

        for mn in loop(4, 20, 2):
            if mn%4=0:
                    print("Number is divided by 4")
              else
                    print("Number is not divided by 4)

        • What possible outputs(s) are expected to be displayed on screen at the time of execution of the program from the following code? Also specify the maximum values that can be assigned to each of the variables FROM and TO.

        import random
        AR=[20,30,40,50,60,70];
        FROM=random.randint(1,3)
        TO=random.randint(2,4)
        for K in range(FROM,TO+1):
            print (AR[K],end="#")
                        (i) 10#40#70#          (ii) 30#40#50#          (iii) 50#60#70#         (iv) 40#50#70#
        • Look at the following python code and find the possible output(s) from the options (i) to (iv) following it.
          import random
          for i in range(4):
          VALUE = random.randint (4,11) + 7
          print(VALUE, "#", end=" ")

          (i) 10#13#17#12#       (ii) 11#16#17#17#      (iii) 18#12#15#17#       (iv) 14#9#16#12# 

        • Look at the following python code and find the possible output(s) from the options (i) to (iv) following it. Also, write the highest and lowest values that can be pointed by label LABEL. import random
          for b in range(5,9):
              LABEL = random.randint (20,26) + 7
              print(LABEL, "@", end=" ")

          (i) 28@30@31@29@                          (ii) 29@28@27@29
          (iii) 27@33@29@30@                        (iv) 31@32@33@27@

        STRING 

        • Select the correct output of the code:
        Str="Computer"
        Str=Str[-4:]
        print(Str*2)
                          a. uter               b. uterretu              c. uteruter               d. None of these
              • Which of the following statement(s) would give an error after executing the following code?
              str= “Python Programming” #statement 1
              x= ‘2’ #statement 2
              print(str*2) #statement 3
              print(str*x) #statement 4 
              a) statement 1      b) statement 2       c) statement 3    d) statement 4
              • Select the correct output of the code:
              Str = "NOTHING IS IMPOSSIBLE IN THE WORLD"
              Str = Str.split()
              NewStr = Str[0] + "#" + Str[1] + "#" + Str[4]
              print (NewStr)
              a) NOTHING#IMPOSSIBLE#IN
              b) NOTHING#IS#IMPOSSIBLE
              c) NOTHING#IS#THE
              d) None of these
              • What is the output of the following?
                print('KV Sangathan'.split('an'))
                (a) ['KV S', 'gath', ' ']
                (b) [‘KV Sangathan’,’an’]
                (c) [‘KV’,’Sang’,’athan’]
                (d) All of them
                      • State True or False

                      “The characters of string will have two-way indexing.”

                      •  A string is declared as: 
                        S = “HELLO PROGRAMMER” 
                      What will be the output of 
                      S.find("GRAM", 2, 15)
                      • Find and write the output of the following python code:

                      x = "abcdef"
                      i = "a"
                      while i in x:
                          print(i, end = " ")

                      • Consider the following code:

                      str=input("Enter a string: ")

                      for j in range(1, 5):
                          if str[-1]=='k':
                              str=str[0:2]+ 'p'
                          elif 'j' in str:
                              str=str[1:]+ 'r'
                          else:
                              str= 'T' +str[1:]+ 'z' print(str)
                      What will be the output produced if input is: (i) Joy7k (ii) jump5 (iii) 2020HY 

                      • Find the output of the following:
                        >>>Name = “Python Examination”
                        >>>print (Name [ : 8 : -1]) 

                      LIST

                      • What will be the output of the following statements?
                      a = [0, 1, 2, 3]
                      del a[:]
                      print(a)

                            (a) None             (b) [ ]         ( c) [0, 1, 2, 3]          (d) NameError

                            • Find the output:
                            list= [10, 20, 30, 40, 50, 60, 70, 80]
                              print(list[ : : 2])
                              • Given the list L=[25,8,61,80,54,17,11,99] 
                                Write the output of print(L[-2 : -7 : -3])  
                              • Select the correct output of the following code :
                              s=“I#N#F#O#R#M#A#T#I#C#S”
                              L=list(s.split(‘#’))
                              print(L)
                                    a) [I#N#F#O#R#M#A#T#I#C#S]
                                    b) [‘I’, ‘N’, ‘F’, ‘O’, ‘R’, ‘M’, ‘A’, ‘T’, ‘I’, ‘C’, ‘S’]
                                    c) [‘I N F O R M A T I C S’]
                                    d) [‘INFORMATICS’]
                                          • Write output for the following python code:
                                            >>>K=[5,7,8, [10,4], 11,2]
                                            >>>print(len(K))
                                            >>>print(K[3])
                                          • Given a list:
                                            List1=[10,[20,30,40],50,60,70,80,90]
                                            What will be the output of
                                            print(List1[1:3:2])?
                                            • Suppose that L is a list as given below:
                                              ["Book", "is""the", ["best""friend"], "of""a""human""being"]
                                              What does each of the following expressions evaluate to ?
                                              (i) L[3:4][0]
                                              (ii) L[3:4][0][1]
                                              (iii) L[3:4][0][1][3]
                                              (iv) "modern" in L
                                            • Predict the output of the Python code given below:
                                            T1 = (12, 22, 33, 55 ,66)
                                            list1 =list(T1)
                                            new_list = []
                                            for i in list1:
                                               if i%3==0:
                                                  new_list.append(i)
                                            new_T = tuple(new_list)
                                            print(new_T)
                                            • Write the output of following code:
                                            lst1=[10,15,20,25,30]
                                            lst1.insert(3,4)
                                            lst1.insert(2,3)
                                            print(lis[-5])
                                            • State True or False
                                              “ There is no conceptual limit to the size of a list.” 

                                            TUPLE
                                            • Find the output:
                                            mylist = [2,14,54,22,17]
                                            tup = tuple(mylist)
                                            for i in tup:
                                            print(i%3, end=",")
                                            • Write the output of following code:
                                            Tup1=(10,15,20,25,30)
                                            print(Tup1[-1:0:-2])

                                            • Given a Tuple 
                                              tup1=(10,20,30,40,50,60,70,80,90)
                                            What will be the output of print(tup1[3:7:2])? 

                                            •  Suppose a tuple T is declare as: 
                                              T=(10,12,43,39)
                                            Which of the following is in correct?
                                            (a) print(t[1])                (b) T[2]=-29             (c) print(max(T)           (d) print(len(T))

                                            •  Which of the following options will not result in an error when performed on types in python where tp = (5,2,7,0,3) ?
                                            (a) tp[1] = 22                        (b) tp.append(23)      
                                            (c) tp1 = tp+tp*2              (d) tp.sum()

                                            •  Write the output of the code given below:

                                            a,b,c,d = (1,2,3,4)
                                            mytuple = (a,b,c,d)*2+(5**2,)
                                            print(len(mytuple)+2)


                                            DICTIONARY
                                            • What will be the output of the following code?
                                            D1={1: “One”,2: “Two”, 3: “C”}
                                            D2={4: “Four”,5: “Five”}
                                            D1.update(D2)
                                            print (D1)
                                            a) {4:’Four’,5: ‘Five’}
                                            b) Method update() doesn’t exist for dictionary
                                            c) {1: “One”,2: “Two”, 3: “C”}
                                            d) {1: “One”,2: “Two”, 3: “C”,4: ‘Four’,5: ‘Five’}

                                            • Given the following dictionary

                                            D1={"Exam":"ICSCE", "Year":2023, "Total":500}
                                            Which statement will add a new value pair (Grade: “A++”) in dictionary D1?
                                            a. D1.update(“REMARK” : “EXCELLENT”)
                                            b. D1 + {“REMARK” : “EXCELLENT”}
                                            c. D1[“REMARK”] = “EXCELLENT”
                                            d. D1.merge({“REMARK” : “EXCELLENT”})

                                            • Which is the correct form of declaration of dictionary?

                                            a) Day={1:’Monday’,2:’Tuesday’,3:’Wednesday’}
                                            b) Day={‘1:Monday’,’2:Tuesday’,’3:Wednesday’}
                                            c) Day=(1:’Monday’,2:’Tuesday’,3:’Wednesday’)
                                            d) Day=[1:’Monday’,2:’Tuesday’,3:’Wednesday’] 
                                            • Consider a dictionary given below:
                                            Marks ={‘Physics’ : 67, ’Chemistry’ : 78, ‘CS’ : 75}
                                            Write the python statements for the following:
                                            (i) Add ‘Maths’ : 71 to the given dictionary.
                                            (ii) Update the marks of ‘CS’ 75 to 81

                                            • Write the output of the code given below:

                                            dict = {"Item": "Laptop", "Make": "LG" }
                                            dict["Price"] = 57000
                                            dict["Make"] = "DELL"
                                            for k in dict:
                                                print(k, ‘@’, dict[k]) 

                                            • Predict the output of the Python Code given below:

                                            d={"Rohan":67,"Ahasham":78,"naman":89,"pranav":79}
                                            print(d)
                                            sum=0
                                            for i in d:
                                               sum+=d[i]
                                               print(sum)
                                            print("sum of values of dictionaries",sum) 

                                              •  Write the output of the following code given below:

                                            Marks = {‘Sidhi’:65,’Prakul’:62,’Suchitra’:64,’Prashant’:50}
                                            newMarks = {‘Suchitra’:66,’Arun’:55,’Prashant’:49}
                                            Marks.update(newMarks)
                                            for key,value in Marks.items():
                                                print(key,’scored’,value,’marks in Pre Board’,end= ‘ ‘)
                                                if(value<55):
                                                    print(‘and needs imporovement’end=’.’)
                                                print()

                                            No comments:

                                            Post a Comment