Python Week 4 Quiz Answers

 Python Week 4 Quiz Answers 

Q:1. How do you declare a dictionary in python ?

·         dict [“key”:”value”]

·         dict = {“key”:”value”}

·         Dict = {“key” - “value”}

·         Dict = [“key”:”value”]

Answer: dict = {“key”:”value”}

Reason: Dictionary id listed in curly brackets inside these curly brackets, key and value are declared, each key is separated from its value by a colon (:), while commas separate each element.

Q:2. What will be the output of the following code:

        dict = { x:x for x in range(1,2) }

        print(dict)

·         {}

·         {x:x,x:x,x:x}

·         {1: 1, 2: 2}

·         {1: 1}

Answer: {1: 1}

Reason: As the for loop runs upto one hence {1:1} will print.

Q:3. In python 3.x, Which of the following statement is true for following code:

        age = input (“Please enter your age”)

·         Age will be integer type.

·         It removes trailing spaces.

·         age variable's type will be Whatever type is given to it

·         None of the above

Answer: None of the above

Reason: We are asking for the age in the age = input("Please enter your age"), hence none of the above statement is true hence answer is none of the above.

Q:4. What will be the output of the following program ?

        int =5

        while int <=-1:

            print (int)

            int = int -1

·         3

              2

·         blank(nothing will be printed)

·         5

              4

              3

·         5

              4

              3

              2

              1

Answer: blank(nothing will be printed)

Reason: As in the program while condition is not true hence it will not run, and out side the while loop nothing is printed therefore second option is correct.

Q:5. What will be the output of the following program ?

        int =3

        while int >=2:

            print (int)

            int = 1

·         3

·         2

·         3 2

·         Error in code

Answer: 3

Reason: while condition is satisfied hence it will print 3 as it declared above. But in end int = 1 it will not print as print statement is not written.

Post a Comment

0 Comments