Python - O - Pedia

Approved & Edited by ProProfs Editorial Team
The editorial team at ProProfs Quizzes consists of a select group of subject experts, trivia writers, and quiz masters who have authored over 10,000 quizzes taken by more than 100 million users. This team includes our in-house seasoned quiz moderators and subject matter experts. Our editorial experts, spread across the world, are rigorously trained using our comprehensive guidelines to ensure that you receive the highest quality quizzes.
Learn about Our Editorial Process
| By HardCoderr
H
HardCoderr
Community Contributor
Quizzes Created: 2 | Total Attempts: 328
Questions: 60 | Attempts: 120

SettingsSettingsSettings
Programming Quizzes & Trivia

.


Questions and Answers
  • 1. 

     Which of the following is not a keyword?

    • A.

      Eval

    • B.

      Assert

    • C.

      Nonlocal

    • D.

      Pass

    Correct Answer
    A. Eval
    Explanation
    The keyword "eval" is used in Python to evaluate a string as a Python expression. It is not a keyword because it can be used as a variable name or a function name without any syntax error. Keywords in Python have predefined meanings and cannot be used as identifiers. On the other hand, "assert", "nonlocal", and "pass" are all keywords in Python. "assert" is used for debugging and testing purposes, "nonlocal" is used to declare a nonlocal variable, and "pass" is used as a placeholder for empty blocks of code.

    Rate this question:

  • 2. 

    All keywords in Python are in

    • A.

      Lowercase

    • B.

      UPPER CASE

    • C.

      Capitalized

    • D.

      None of the mentioned

    Correct Answer
    D. None of the mentioned
    Explanation
    In Python, keywords are reserved words that have predefined meanings and cannot be used as variable names. The keywords in Python are written in lowercase, not in uppercase or capitalized. Therefore, the correct answer is "None of the mentioned".

    Rate this question:

  • 3. 

    Which one of these is floor division?

    • A.

      /

    • B.

      //

    • C.

      %

    • D.

      None of the mentioned

    Correct Answer
    B. //
    Explanation
    The double forward slash operator (//) is the floor division operator. Floor division returns the largest integer that is less than or equal to the division of two numbers. It is used to divide two numbers and discard the decimal part, giving the result as an integer. For example, 9 // 2 would return 4, as it discards the decimal part of 4.5.

    Rate this question:

  • 4. 

    Operators with the same precedence are evaluated in which manner:

    • A.

      Left to Right

    • B.

      Right to Left

    • C.

      Can’t say

    • D.

      None of the mentioned.

    Correct Answer
    A. Left to Right
    Explanation
    Operators with the same precedence are evaluated from left to right. This means that when there are multiple operators with the same precedence in an expression, the one on the left will be evaluated first, followed by the one on the right.

    Rate this question:

  • 5. 

    Which of these is not a core data type?

    • A.

      Lists

    • B.

      Dictionary

    • C.

      Tuples

    • D.

      Class

    Correct Answer
    D. Class
    Explanation
    The correct answer is "class". In programming, a class is not considered a core data type. Core data types are fundamental data types that are built into the programming language and are used to represent basic values. Examples of core data types include lists, dictionaries, and tuples. However, a class is a user-defined data type that allows the programmer to create their own data structures and define their own methods and attributes.

    Rate this question:

  • 6. 

    What will be the output of the following Python Code?     1.>>>str=”hello”     2.>>>str[:2]     3.>>>

    • A.

      He

    • B.

      Lo

    • C.

      Olleh

    • D.

      Hello

    Correct Answer
    A. He
    Explanation
    The code is creating a variable named "str" and assigning it the value "hello". Then, it is using slicing to extract the characters from index 0 to index 1 (excluding index 2) from the string. This will result in the output "he".

    Rate this question:

  • 7. 

    What is the return type of function id?

    • A.

      Int

    • B.

      Float

    • C.

      Bool

    • D.

      Dict

    Correct Answer
    A. Int
    Explanation
    The return type of the function "id" is int.

    Rate this question:

  • 8. 

    What error occurs when you execute the following Python code snippet? apple=mango

    • A.

      SyntaxError

    • B.

      NameError

    • C.

      ValueError

    • D.

      TypeError

    Correct Answer
    B. NameError
    Explanation
    The error that occurs when executing the given Python code snippet is a NameError. This error is thrown when a variable is used before it is defined or assigned a value. In this case, the variable "mango" is not defined or assigned a value, so trying to assign it to the variable "apple" results in a NameError.

    Rate this question:

  • 9. 

    What datatype is the object below? L=[1, 23, “Hello” , 2]

    • A.

      List

    • B.

      Dictionary

    • C.

      Array

    • D.

      Tuple

    Correct Answer
    A. List
    Explanation
    The object "L" is a list because it is enclosed in square brackets and contains a sequence of different data types, including integers, strings, and possibly other objects. Lists are a type of data structure in Python that can hold multiple values and allow for indexing and manipulation of its elements.

    Rate this question:

  • 10. 

    Which data type use key-value pair?

    • A.

      List

    • B.

      Dictionary

    • C.

      Array

    • D.

      Tuple

    Correct Answer
    B. Dictionary
    Explanation
    A dictionary data type uses key-value pairs. In a dictionary, each value is associated with a unique key, allowing for efficient retrieval and manipulation of data. Unlike lists, arrays, and tuples, dictionaries provide a way to store and access data based on a specific identifier or key, rather than by position or index. This makes dictionaries useful for tasks such as storing and retrieving data in a structured and organized manner.

    Rate this question:

  • 11. 

    Which of the following is not a complex number?

    • A.

      K=2+3j

    • B.

      K=complex(2,3)

    • C.

       k=2+3I

    • D.

      K=2+3J

    Correct Answer
    C.  k=2+3I
    Explanation
    The given expression k=2+3I represents a complex number. The imaginary unit i is typically used to represent the square root of -1 in mathematics, but in some contexts, the letter I or J is used instead. Therefore, k=2+3I is a valid complex number.

    Rate this question:

  • 12. 

    What does ~4 evaluate to?

    • A.

      -5

    • B.

      -4

    • C.

      -3

    • D.

      +3

    Correct Answer
    A. -5
    Explanation
    The "~" operator in many programming languages is a bitwise negation operator. When applied to a number, it flips all the bits in its binary representation. In this case, "~4" would flip all the bits in the binary representation of 4, resulting in a binary number with all bits set to 1 except for the sign bit. In two's complement representation, this binary number represents -5. Therefore, "~4" evaluates to -5.

    Rate this question:

  • 13. 

    Which of the following operators has its associativity from right to left?

    • A.

      +

    • B.

      //

    • C.

      %

    • D.

      **

    Correct Answer
    D. **
    Explanation
    The operator that has its associativity from right to left is the exponentiation operator (**). This means that when there are multiple occurrences of this operator in an expression, the evaluation starts from the rightmost occurrence and moves towards the left. For example, in the expression 2 ** 3 ** 2, the rightmost exponentiation operation (3 ** 2) is evaluated first, resulting in 9. Then, the expression becomes 2 ** 9, which equals 512.

    Rate this question:

  • 14. 

    The value of the expressions 4/(3*(2-1)) and 4/3*(2-1) is the same.

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    Explanation
    The value of the expressions 4/(3*(2-1)) and 4/3*(2-1) is the same because both expressions follow the order of operations. In both cases, the parentheses are evaluated first, resulting in (2-1) equaling 1. Then, the division operation is performed, resulting in 4/3 equaling 1.3333. Finally, the multiplication operation is performed, resulting in 1.3333*(2-1) equaling 1.3333. Therefore, the value of both expressions is the same, which makes the answer True.

    Rate this question:

  • 15. 

    What is the value of the following expression? float (22//3+3/3)

    • A.

      8

    • B.

      8.0

    • C.

      8.3

    • D.

      8.33

    Correct Answer
    B. 8.0
    Explanation
    The expression evaluates to 8.0 because the division operator (/) performs floating-point division, and the result of 3/3 is 1.0. The floor division operator (//) performs integer division, and the result of 22//3 is 7. Adding these two results together gives 8.0.

    Rate this question:

  • 16. 

    Which of the following expressions is an example of type conversion?

    • A.

      4.0+float(3)

    • B.

      5.3+6.3

    • C.

      5.0+3

    • D.

      3+7

    Correct Answer
    A. 4.0+float(3)
    Explanation
    The expression "4.0+float(3)" is an example of type conversion because it involves converting the integer value 3 into a float value using the float() function. The float() function is used to convert a value into a floating-point number, and in this case, it converts the integer 3 into the float 3.0. The addition operation then takes place between the float value 4.0 and the float value 3.0.

    Rate this question:

  • 17. 

    What will be the output of the following code snippet? bool(‘False’) bool()

    • A.

      True True

    • B.

      . False   True

    • C.

      False False

    • D.

      True False

    Correct Answer
    D. True False
    Explanation
    The code snippet is checking the boolean value of the string 'False' and an empty boolean value. In Python, any non-empty string is considered as True, so 'False' will be evaluated as True. On the other hand, an empty boolean value is considered as False. Therefore, the output of the code snippet will be True for the first statement and False for the second statement.

    Rate this question:

  • 18. 

    What will be the output of the following Python code? class Truth:     pass x=Truth() bool(x)

    • A.

      Pass

    • B.

      True

    • C.

      False

    • D.

      Error

    Correct Answer
    B. True
    Explanation
    The code defines a class called Truth and creates an instance of it called x. The bool() function is then used to check the truthiness of x. In Python, any object that is not explicitly defined as False is considered True. Since x is an instance of the Truth class, it is not explicitly defined as False, so the bool(x) expression evaluates to True.

    Rate this question:

  • 19. 

    What will be the output of the following Python code? if (9 < 0) and (0 < -9):     print("hello") elif (9 > 0) or False:     print("good") else:     print("bad")

    • A.

      Error

    • B.

      Hello

    • C.

      Good

    • D.

      Bad

    Correct Answer
    C. Good
    Explanation
    The output of the code will be "good". This is because the condition in the elif statement, (9 > 0) or False, evaluates to True. Therefore, the code inside the elif block will be executed, which prints "good".

    Rate this question:

  • 20. 

    What will be the output of the following Python code? i = 1 while True:     if i%2 == 0:         break     print(i)     i += 2

    • A.

      1

    • B.

      1 2

    • C.

      1 2 3 4 5 6

    • D.

      1 3 5 7 9 11

    Correct Answer
    D. 1 3 5 7 9 11
    Explanation
    The code starts with initializing the variable i to 1. It then enters a while loop that runs indefinitely (due to the condition True). Inside the loop, it checks if i is divisible by 2. If it is, the break statement is executed, causing the loop to terminate. If i is not divisible by 2, it prints the value of i and then increments i by 2. This process continues until the break statement is executed. Therefore, the output of the code will be a sequence of odd numbers: 1 3 5 7 9 11.

    Rate this question:

  • 21. 

    What will be the output of the following Python code? x = ['ab', 'cd'] for i in x:     i.upper() print(x)

    • A.

      [‘ab’, ‘cd’]

    • B.

      [‘AB’, ‘CD’]

    • C.

      [None, None]

    • D.

      None of the mentioned

    Correct Answer
    A. [‘ab’, ‘cd’]
    Explanation
    The output of the given Python code will be ['ab', 'cd']. This is because the code iterates over each element in the list x, but does not modify the elements in any way. The method i.upper() returns a new string with all the characters in i converted to uppercase, but it does not change the original string. Therefore, when the print statement is executed, it will display the original list x without any modifications.

    Rate this question:

  • 22. 

    What will be the output of the following Python code? x = "abcdef" while i in x:     print(i, end=" ")

    • A.

      A b c d e f

    • B.

      Abcdef

    • C.

      Iiiiii…

    • D.

      Error

    Correct Answer
    D. Error
    Explanation
    The code will throw an error because the variable "i" is not defined before using it in the while loop.

    Rate this question:

  • 23. 

    What will be the output of the following Python code? x = "abcdef" i = "a" while i in x[:-1]:     print(i, end = " ")

    • A.

      A a a a a

    • B.

      A a a a a a

    • C.

      A a a a a a …

    • D.

      A

    Correct Answer
    C. A a a a a a …
    Explanation
    The code initializes a variable "x" with the string "abcdef" and another variable "i" with the string "a". It then enters a while loop that checks if "i" is in the substring of "x" excluding the last character. Since "i" is always present in the substring "abcde" in each iteration, the loop continues to execute. Inside the loop, it prints the value of "i" followed by a space. This process continues until "i" is no longer in the substring. Therefore, the output will be a series of "a" separated by spaces.

    Rate this question:

  • 24. 

    What will be the output of the following Python code? d = {0: 'a', 1: 'b', 2: 'c'} for x in d.values():     print(d[x])

    • A.

      0 1 2

    • B.

       a b c

    • C.

      0 a 1 b 2 c

    • D.

      None of the mentioned 

    Correct Answer
    D. None of the mentioned 
  • 25. 

    What will be the output of the following Python code? ⦁    class father: ⦁        def __init__(self, param): ⦁            self.o1 = param ⦁      ⦁    class child(father): ⦁        def __init__(self, param): ⦁            self.o2 = param ⦁      ⦁    >>>obj = child(22) ⦁    >>>print "%d %d" % (obj.o1, obj.o2)

    • A.

      None None

    • B.

      None 22

    • C.

      22 None

    • D.

      Error is generated

    Correct Answer
    D. Error is generated
    Explanation
    The output of the code will be "error is generated" because the child class does not have an __init__ method, so when the child object is created and the print statement tries to access obj.o1 and obj.o2, it will result in an AttributeError.

    Rate this question:

  • 26. 

     What will be the output of the following Python code? ⦁    >>>example = "snow world" ⦁    >>>print("%s" % example[4:7])

    • A.

      Wo

    • B.

      World

    • C.

      Sn

    • D.

      Rl

    Correct Answer
    A. Wo
    Explanation
    The code is using string slicing to extract a portion of the "example" string. The slicing is done from index 4 to 7 (exclusive), so it will include the characters at index 4, 5, and 6. Therefore, the output will be "wo".

    Rate this question:

  • 27. 

    What will be the output of the following Python code snippet? print('a@ 1,'.islower())

    • A.

      True

    • B.

      False

    • C.

      None

    • D.

      Error

    Correct Answer
    A. True
    Explanation
    The given Python code snippet is checking whether the string "a@ 1," is in lowercase or not. The islower() function returns True if all characters in the string are lowercase, and False otherwise. In this case, the string contains the lowercase letter 'a', so the output will be True.

    Rate this question:

  • 28. 

    What will be the output of the following Python code snippet? print('HelloWorld'.istitle())

    • A.

      True

    • B.

      False

    • C.

      None

    • D.

      Error

    Correct Answer
    B. False
    Explanation
    The output of the given code snippet will be False because the string "HelloWorld" is not in title case. The istitle() method checks if each word in the string starts with an uppercase letter and the remaining letters are lowercase. In this case, "HelloWorld" does not meet this condition, so the method returns False.

    Rate this question:

  • 29. 

    Suppose list1 is [2445,133,12454,123], what is max(list1)?

    • A.

      2445

    • B.

      133

    • C.

      12454

    • D.

      123

    Correct Answer
    C. 12454
    Explanation
    The correct answer is 12454 because it is the largest number in the given list.

    Rate this question:

  • 30. 

    Suppose list1 is [4, 2, 2, 4, 5, 2, 1, 0], Which of the following is correct syntax for slicing operation?

    • A.

      Print(list1[0])

    • B.

      Print(list1[:2])

    • C.

      Print(list1[:-2])

    • D.

      All of the mentioned

    Correct Answer
    D. All of the mentioned
    Explanation
    The correct syntax for slicing operation in Python is to use the colon (:) symbol to specify the start and end indices of the slice. The first option, `print(list1[0])`, selects only the element at index 0 of the list. The second option, `print(list1[:2])`, selects the elements from index 0 to index 1. The third option, `print(list1[:-2])`, selects all elements except the last two elements. Therefore, all of the mentioned options correctly demonstrate the syntax for slicing operation.

    Rate this question:

  • 31. 

     Suppose listExample is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after listExample.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]

    Correct Answer
    C. [3, 5, 20, 5, 25, 1, 3]
    Explanation
    After executing the code listExample.pop(1), the element at index 1 in listExample is removed. Therefore, the resulting list list1 will be [3, 5, 20, 5, 25, 1, 3].

    Rate this question:

  • 32. 

    What will be the output of the following Python code? ⦁    >>>list1 = [1, 3] ⦁    >>>list2 = list1 ⦁    >>>list1[0] = 4 ⦁    >>>print(list2)

    • A.

      [1, 3]

    • B.

      [4, 3]

    • C.

      [1, 4]

    • D.

      [1, 3, 4]

    Correct Answer
    B. [4, 3]
    Explanation
    The output of the code will be [4, 3]. This is because when we assign `list2` to `list1`, it creates a reference to the same list object. Therefore, any changes made to `list1` will also be reflected in `list2`. In this case, we are changing the first element of `list1` to 4, so when we print `list2`, it will show the updated value [4, 3].

    Rate this question:

  • 33. 

     What will be the output of the following Python code? a=[1,2,3] b=a.append(4) print(a) print(b)

    • A.

      [1,2,3,4] [1,2,3,4]

    • B.

       [1, 2, 3, 4] None

    • C.

      Syntax error

    • D.

      [1,2,3] [1,2,3,4]

    Correct Answer
    B.  [1, 2, 3, 4] None
    Explanation
    The output of the code will be [1, 2, 3, 4] and None. The append() method modifies the original list by adding the element to the end of it. So, when we print(a), it will display [1, 2, 3, 4]. However, the append() method does not return anything, so when we print(b), it will display None.

    Rate this question:

  • 34. 

    What will be the output of the following Python code? s=["pune", "mumbai", "delhi"] [(w.upper(), len(w)) for w in s]

    • A.

      Error

    • B.

      [‘PUNE’, 4, ‘MUMBAI’, 6, ‘DELHI’, 5]

    • C.

      [PUNE, 4, MUMBAI, 6, DELHI, 5]

    • D.

      [(‘PUNE’, 4), (‘MUMBAI’, 6), (‘DELHI’, 5)]

    Correct Answer
    D. [(‘PUNE’, 4), (‘MUMBAI’, 6), (‘DELHI’, 5)]
    Explanation
    The code snippet is using a list comprehension to iterate over the elements in the list 's'. For each element, it is converting the element to uppercase using the 'upper()' method and finding the length of the element using the 'len()' function. The output is a list of tuples, where each tuple contains the uppercase version of the element and its length. Therefore, the correct answer is [(‘PUNE’, 4), (‘MUMBAI’, 6), (‘DELHI’, 5)].

    Rate this question:

  • 35. 

    Which of the following is a Python tuple?

    • A.

      [1, 2, 3]

    • B.

      (1, 2, 3)

    • C.

      {1, 2, 3}

    • D.

      {}

    Correct Answer
    B. (1, 2, 3)
    Explanation
    The correct answer is (1, 2, 3) because it is enclosed in parentheses, which is the syntax for creating a tuple in Python. A tuple is an ordered collection of elements and is immutable, meaning its values cannot be changed once it is created. The other options are not tuples: [1, 2, 3] is a list, {1, 2, 3} is a set, and {} is an empty dictionary.

    Rate this question:

  • 36. 

    Suppose d = {“john”:40, “peter”:45}, to delete the entry for “john” what command do we use?

    • A.

      D.delete(“john”:40)

    • B.

      D.delete(“john”)

    • C.

      Del d[“john”]

    • D.

      Del d(“john”:40)

    Correct Answer
    C. Del d[“john”]
    Explanation
    The correct command to delete the entry for "john" in the dictionary is "del d['john']". This command uses the "del" keyword followed by the name of the dictionary "d" and the key "john" enclosed in square brackets. By using this command, the entry for "john" will be removed from the dictionary "d".

    Rate this question:

  • 37. 

    Suppose d = {“john”:40, “peter”:45}. To obtain the number of entries in dictionary which command do we use?

    • A.

      D.size()

    • B.

      Len(d)

    • C.

      Size(d)

    • D.

      D.len()

    Correct Answer
    B. Len(d)
    Explanation
    The correct answer is len(d). In Python, the len() function is used to obtain the number of entries in a dictionary. By passing the dictionary name as an argument to len(), it returns the length of the dictionary, which represents the number of key-value pairs present in it. Therefore, len(d) is the appropriate command to use in order to obtain the number of entries in the dictionary d.

    Rate this question:

  • 38. 

    What will be the output of the following Python code snippet? 1. d={“john”:40 , “peter”:45} 2. print(list(d.keys()))

    • A.

      [“john”, “peter”]

    • B.

      [“john”:40, “peter”:45]

    • C.

      (“john”, “peter”)

    • D.

      (“john”:40, “peter”:45)

    Correct Answer
    A. [“john”, “peter”]
    Explanation
    The code snippet creates a dictionary 'd' with two key-value pairs. The 'keys()' method is then used to retrieve a list of all the keys in the dictionary. The 'print' statement outputs the list of keys, which is ['john', 'peter'].

    Rate this question:

  • 39. 

    What will be the output of the following Python code? >>> a={'B':5,'A':9,'C':7} >>> sorted(a)

    • A.

      [‘A’,’B’,’C’]

    • B.

      [‘B’,’C’,’A’]

    • C.

      [5,7,9]

    • D.

      [9,5,7]

    Correct Answer
    A. [‘A’,’B’,’C’]
    Explanation
    The output of the code will be ['A', 'B', 'C']. The sorted() function in Python returns a new list containing all items from the original list in ascending order. In this case, the original list is the keys of the dictionary 'a', which are 'B', 'A', and 'C'. When sorted, they will be arranged in alphabetical order, resulting in ['A', 'B', 'C'].

    Rate this question:

  • 40. 

     What will be the output of the following Python code? >>> a={"a":1,"b":2,"c":3} >>> b=dict(zip(a.values(),a.keys())) >>> b

    • A.

      {‘a’: 1, ‘b’: 2, ‘c’: 3}

    • B.

      An exception is thrown

    • C.

      {‘a’: ‘b’: ‘c’: }

    • D.

      {1: ‘a’, 2: ‘b’, 3: ‘c’}

    Correct Answer
    D. {1: ‘a’, 2: ‘b’, 3: ‘c’}
    Explanation
    The code first creates a dictionary 'a' with keys 'a', 'b', and 'c' and their corresponding values 1, 2, and 3. Then, it uses the zip() function to swap the keys and values of dictionary 'a' and creates a new dictionary 'b'. Finally, it prints the dictionary 'b', which will have the keys and values swapped from dictionary 'a'. So, the output will be {1: 'a', 2: 'b', 3: 'c'}.

    Rate this question:

  • 41. 

    What will be the output of the following Python expression? round(4.5676,2)?

    • A.

      4.5

    • B.

      4.6

    • C.

      4.57

    • D.

      4.56

    Correct Answer
    C. 4.57
    Explanation
    The round() function in Python is used to round a number to a specified number of decimal places. In this case, the expression round(4.5676,2) will round the number 4.5676 to 2 decimal places. Since the third decimal place is 7, which is greater than or equal to 5, the number will be rounded up to 4.57. Therefore, the output of the expression will be 4.57.

    Rate this question:

  • 42. 

    Which keyword is used for function?

    • A.

      Fun

    • B.

      Define

    • C.

       Def

    • D.

      Function

    Correct Answer
    C.  Def
    Explanation
    The keyword "def" is used for defining a function in Python. It is followed by the function name and a pair of parentheses, which can include parameters if any. The "def" keyword is essential for creating reusable blocks of code that can be called and executed multiple times throughout a program.

    Rate this question:

  • 43. 

    What will be the output of the following Python code? ⦁    def printMax(a, b): ⦁        if a > b: ⦁            print(a, 'is maximum') ⦁        elif a == b: ⦁            print(a, 'is equal to', b) ⦁        else: ⦁            print(b, 'is maximum') ⦁    printMax(3, 4)

    • A.

      3

    • B.

      4

    • C.

      4 is maximum

    • D.

      None of the mentioned

    Correct Answer
    C. 4 is maximum
    Explanation
    The output of the code will be "4 is maximum" because the function printMax is called with arguments 3 and 4. Since 4 is greater than 3, the if condition "a > b" is true and the code inside that block is executed, which prints "4 is maximum".

    Rate this question:

  • 44. 

    What will be the output of the following Python code? ⦁    x = 50 ⦁    def func(): ⦁        global x ⦁        print('x is', x) ⦁        x = 2 ⦁        print('Changed global x to', x) ⦁    func() ⦁    print('Value of x is', x)

    • A.

      X is 50 Changed global x to 2 Value of x is 50

    • B.

      X is 50 Changed global x to 2 Value of x is 2

    • C.

      X is 50 Changed global x to 50 Value of x is 50

    • D.

      None of the mentioned

    Correct Answer
    B. X is 50 Changed global x to 2 Value of x is 2
    Explanation
    The code first assigns the value 50 to the variable x. Then, the function func is defined and the global keyword is used to indicate that the variable x inside the function is the same as the global variable x. Inside the function, the value of x is printed, which is 50. Then, the value of x is changed to 2 and printed again. Finally, outside the function, the value of x is printed again, which is 2. Therefore, the output of the code will be "x is 50", "Changed global x to 2", and "Value of x is 2".

    Rate this question:

  • 45. 

    Python supports the creation of anonymous functions at runtime, using a construct called __________

    • A.

      Lambda

    • B.

      Pi

    • C.

      Anonymous

    • D.

      None of the mentioned

    Correct Answer
    A. Lambda
    Explanation
    Python supports the creation of anonymous functions at runtime using a construct called "lambda". The lambda keyword allows us to create small, one-line functions without a name. These functions are commonly used in situations where we need a simple function for a short period of time and don't want to define a separate function using the "def" keyword. The lambda functions are often used in combination with built-in functions like map(), filter(), and reduce().

    Rate this question:

  • 46. 

     What will be the output of the following Python code? a=10 b=20 def change():     global b     a=45     b=56 change() print(a) print(b)

    • A.

      10 56

    • B.

      45 56

    • C.

      10 20

    • D.

      Syntax Error

    Correct Answer
    A. 10 56
    Explanation
    The code first assigns the values 10 and 20 to variables a and b, respectively. Then, the function change is defined, which modifies the global variable b to have a value of 56. Inside the function, a local variable a is also defined and assigned a value of 45, but since it is not declared as global, it does not affect the value of the global variable a. After calling the change function, the values of a and b are printed. The value of a remains 10 because the local variable a inside the function does not affect the global variable a. The value of b is 56 because it was modified inside the function using the global keyword.

    Rate this question:

  • 47. 

    What will be the output of the following Python code? def foo(i, x=[]):     x.append(i)     return x for i in range(3):     print(foo(i))

    • A.

      [0] [1] [2]

    • B.

      [0] [0, 1] [0, 1, 2]

    • C.

      [1] [2] [3] 

    • D.

      [1] [1, 2] [1, 2, 3]

    Correct Answer
    B. [0] [0, 1] [0, 1, 2]
    Explanation
    The code defines a function called "foo" that takes two arguments, "i" and "x". The default value for "x" is an empty list. Inside the function, the value of "i" is appended to the list "x". The function then returns the updated list.

    In the main code, a loop is used to iterate through the range of numbers from 0 to 2. Each iteration, the "foo" function is called with the current number as the argument. The returned list is then printed.

    The output of the code will be:
    [0]
    [0, 1]
    [0, 1, 2]

    This is because the default value of "x" is a mutable object (a list), and it is created only once when the function is defined. Therefore, each time the function is called without explicitly passing a value for "x", the same list is used and the value of "i" is appended to it.

    Rate this question:

  • 48. 

    Which of these definitions correctly describes a module?

    • A.

      Denoted by triple quotes for providing the specification of certain program elements

    • B.

      Design and implementation of specific functionality to be incorporated into a program

    • C.

      Defines the specification of how it is to be used

    • D.

      Any program that reuses code

    Correct Answer
    B. Design and implementation of specific functionality to be incorporated into a program
    Explanation
    A module is a defined and implemented functionality that is designed to be incorporated into a program. It is a self-contained unit that can be reused in different programs. It is not just any program that reuses code, but rather a specific functionality that is designed and implemented to be used as a part of a larger program. The other options mentioned, such as denoting by triple quotes or defining the specification of how it is to be used, do not accurately describe the concept of a module.

    Rate this question:

  • 49. 

    What will be the output of the following Python code? #mod1 def change(a):     b=[x*2 for x in a]     print(b) #mod2 def change(a):     b=[x*x for x in a]     print(b) from mod1 import change from mod2 import change #main s=[1,2,3] change(s)

    • A.

      [2,4,6]

    • B.

      [1,4,9]

    • C.

      [2,4,6] [1,4,9]

    • D.

      There is a name clash

    Correct Answer
    D. There is a name clash
    Explanation
    The given code imports two functions named "change" from two different modules, mod1 and mod2. Both functions have the same name, but they perform different operations on the input list. When the code calls the "change" function with the list [1,2,3], it will first execute the "change" function from mod1, which doubles each element in the list and prints [2,4,6]. Then, it will try to execute the "change" function from mod2, which squares each element in the list and prints [1,4,9]. However, there is a name clash because both functions have the same name. Therefore, the code will raise an error and the output will be "There is a name clash".

    Rate this question:

  • 50. 

    _____ represents an entity in the real world with its identity and behaviour.

    • A.

      A method

    • B.

      An object

    • C.

      A class

    • D.

      An operator

    Correct Answer
    B. An object
    Explanation
    An object represents an entity in the real world with its identity and behavior. Objects are instances of a class, which defines their properties and methods. Each object has its own unique identity and can perform actions or exhibit behavior based on its defined methods. Objects encapsulate data and functionality, allowing them to interact with other objects and manipulate their own state. Therefore, an object is the most appropriate choice to represent an entity in the real world with its identity and behavior.

    Rate this question:

Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.