Pre-Test (Python Basic)

Description

Quiz on Pre-Test (Python Basic), created by Mohammed Arif Mazumder on 02/07/2021.
Mohammed Arif Mazumder
Quiz by Mohammed Arif Mazumder, updated more than 1 year ago
Mohammed Arif Mazumder
Created by Mohammed Arif Mazumder over 3 years ago
1775
0

Resource summary

Question 1

Question
What is the output of the following code snippet: (You can see the picture too) if 'bar' in {'foo': 1, 'bar': 2, 'baz': 3}: print(1) print(2) if 'a' in 'qux': print(3) print(4)
Answer
  • 1 2 3 4
  • 1 2 4
  • It doesn’t generate any output.
  • 4

Question 2

Question
The following if/elif/else statement will raise a KeyError exception: d = {'a': 0, 'b': 1, 'c': 0} if d['a'] > 0: print('ok') elif d['b'] > 0: print('ok') elif d['c'] > 0: print('ok') elif d['d'] > 0: print('ok') else: print('not ok')
Answer
  • True
  • False

Question 3

Question
Shown below is a diagram of a simple dictionary: Which of the following is not a valid way to define this dictionary in Python:
Answer
  • d = {'foo': 100, 'bar': 200, 'baz': 300}
  • d = { ('foo', 100), ('bar', 200), ('baz', 300) }
  • d = {} d['foo'] = 100 d['bar'] = 200 d['baz'] = 300
  • d = dict(foo=100, bar=200, baz=300)
  • d = dict([ ('foo', 100), ('bar', 200), ('baz', 300) ])

Question 4

Question
List a is defined as follows: a = [1, 2, 3, 4, 5] Select all of the following statements that remove the middle element 3 from a so that it equals [1, 2, 4, 5]:
Answer
  • a[2] = []
  • del a[2]
  • a.add(3)
  • a[0:3] = []
  • a[2:2] = []

Question 5

Question
Which of the following would separate a string input_string on the first 2 occurences of the letter “e”?
Answer
  • input_string.split('e', 2)
  • 'e'.split(input_string, maxsplit=2)
  • 'e'.split(input_string, 2)

Question 6

Question
What is the output of the following code snippet: d = {'foo': 1, 'bar': 2, 'baz': 3} while d: print(d.popitem()) print('Done.')
Answer
  • Done.
  • foo bar baz
  • The snippet doesn’t generate any output.
  • ('baz', 3) ('bar', 2) ('foo', 1) Done.

Question 7

Question
The function sqrt() from the math module computes the square root of a number. Will the highlighted line of code raise an exception? x = -100 from math import sqrt x > 0 and sqrt(x)
Answer
  • True
  • False

Question 8

Question
Which of the following mathematical operators can be used to concatenate strings:
Answer
  • +
  • *
  • /
  • -

Question 9

Question
Suppose you have the following tuple definition: t = ('foo', 'bar', 'baz') Which of the following statements replaces the second element ('bar') with the string 'qux':
Answer
  • It’s a trick question—tuples can’t be modified.
  • t(1) = 'qux'
  • t[1:1] = 'qux'
  • t[1] = 'qux'

Question 10

Question
What is value of this expression: 'a' + 'x' if '123'.isdigit() else 'y' + 'b'
Answer
  • 'ab'
  • 'axb'
  • 'ax'
  • 'axyb'
Show full summary Hide full summary

Similar

English Literary Devices
miss.bakare
Biology Unit 1
hannahsanderson1
Biology Unit 1
anna.mat1997
Memory - AQA Psychology Unit 1 GCSE - created from Mind Map
joshua6729
P4: Explaining motion
thegeekymushroom
Key word flashcards
I M Wilson
History GCSE AQA B: Modern World History - International Relations: Conflict and Peace in the 20th Century - Topic 2: Peacemaking 1918-19 and the League of Nations
mariannakeating
Cells And Cell Techniques - Flashcards (AQA AS-Level Biology)
Henry Kitchen
Denary, Binary and Hexadecimal
Samuel Leonard
Crude Oils and others quiz
Dale George
Passing Dark Colored Urine
Batool Aldaher