Request More Information

Email:

koenig-logo

Introduction to Python Quiz Questions and Answers

Answer :
  • 5

Explanation :

Option A is CORRECT because in the Perl, Python and Unix shell scripting, "~" is a bitwise NOT operator. When multiple "~"s are placed before a number, each "~" in turn inverts all bits of the number. Eventually, this can lead to the number getting back to its original value. Since there are 6 "~", an even number, so ~~~~~~5 becomes 5.<br/><br/>Option B is INCORRECT because the "~" operator is not performing any mathematical operation that would result in -11 from 5.<br/><br/>Option C is INCORRECT because the "~" operator is not performing any mathematical operation that would result in 11 from 5.<br/><br/>Option D is INCORRECT because though one "~" operator before a number would invert all the bits of the number in binary form converting positive numbers to negative, the even numbers of "~"s (six in this case) eventually leaves the number in its original form. So ~~~~~~5 remains 5 not -5.
Answer :
  • [1, 4, 8]

Explanation :

Option A is INCORRECT because the list comprehension is taking every 2nd (even-indexed) element from the tuple 't'. The elements at indexes 2, 3 and 9 in the tuple are 4, 3 and 9, which does not match the output of [2, 3, 9].<br/><br/>Option B is INCORRECT because even though all the elements are present in the tuple 't', the list comprehension does not output all the elements. It skips every odd-indexed item and only outputs the even-indexed items.<br/><br/>Option C is CORRECT because the list comprehension is taking every 2nd (even-indexed) element from the tuple 't'. The elements at indexes 0, 2 and 4 in the tuple are 1, 4 and 8, which match the output of [1, 4, 8].<br/><br/>Option D is INCORRECT because although the values match the correct output, the data type is wrong. The list comprehension in Python outputs a list, not a tuple.
Answer :
  • (1, 2, 1, 2)

Explanation :

Option A is CORRECT because in Python, multiplying a tuple by an integer n creates a new tuple that repeats the original tuple n times. Therefore, repeating the tuple (1,2) twice results in (1,2,1,2).<br/><br/>Option B is INCORRECT because the output will be a tuple not a list. In Python, tuples are denoted by parentheses '()', while lists are denoted by square brackets '[]'.<br/><br/>Option C is INCORRECT because multiplying a tuple by an integer repeats the entire tuple, not each element of the tuple separately. Therefore the output will be (1,2,1,2) not (1,1,2,2).<br/><br/>Option D is INCORRECT for the same reasons as Option B and Option C. The operation yields a tuple, not a list, and the entire tuple is being repeated, not the individual elements.
Answer :
  • { "john ":40, "peter ":45}

Explanation :

Option A is INCORRECT because:<br/>- The output is not a list of separate elements; it's a printed representation of a dictionary that includes its keys and values.<br/><br/>Option B is CORRECT because:<br/>- The print statement outputs the dictionary as it is represented in Python, including curly braces and with keys and values paired using colons.<br/><br/>Option C is INCORRECT because:<br/>- Although 40 and 45 are the values in the dictionary, the print function does not isolate them; it prints the entire dictionary.<br/><br/>Option D is INCORRECT because:<br/>- This implies a tuple syntax with invalid syntax for a dictionary. Dictionary elements are defined with curly braces, not parentheses, and each key-value pair is represented with a colon.
Answer :
  • 6

Explanation :

Option A is CORRECT because:<br/>- In Python's `datetime` module, the `date.weekday()` function returns an integer where Monday is 0 and Sunday is 6. Since the date in question was a Sunday, the correct answer is 6.<br/><br/>Option B is INCORRECT because:<br/>- The `date.weekday()` function does not return 1 for Sunday; it returns 1 for Tuesday.<br/><br/>Option C is INCORRECT because:<br/>- The `date.weekday()` function returns 0 for Monday, not Sunday.<br/><br/>Option D is INCORRECT because:<br/>- The `date.weekday()` function returns integers in the range 0-6, corresponding to the days of the week from Monday to Sunday respectively. There is no return value of 7 in this range.
Answer :
  • [‘DATA’, ‘STATEMENT’]

Explanation :

Option A is INCORRECT because although the upper() function converts each string to uppercase, it doesn't change the original list as the uppercase strings are not being reassigned back into the list.<br/><br/>Option B is CORRECT because the original list remains unchanged due to the reason explained for Option A.<br/><br/>Option C is INCORRECT because the upper function doesn't return None; it returns an uppercase version of each string.<br/><br/>Option D is INCORRECT because the code is syntactically correct and will run without any unexpected errors.
Answer :
  • python myscript.py

Explanation :

The correct command to run a Python script from the command line is 'python myscript.py', assuming 'python' points to your installed version of Python and is recognized in your PATH. 'run' and 'execute' are not valid Python commands, and 'python3 run myscript.py' would attempt to run a file named 'run myscript.py' instead of 'myscript.py'.
Answer :
  • An isolated Python environment with its own interpreter and libraries, separate from the system's Python.

Explanation :

A Python virtual environment is an isolated environment that includes its own Python interpreter and libraries, separate from the system's Python. This allows you to install and manage project-specific dependencies without interfering with the system-wide Python installation or other projects.
Answer :
  • To provide recommendations for Python code style and formatting.

Explanation :

PEP8 is the official Python style guide that offers recommendations for coding style, such as indentation, line length, imports, and more. It helps developers write code that is readable and consistent. It does not dictate execution speed, define data types, or set language-independent rules.
Answer :
  • re

Explanation :

The built-in Python module for regular expressions is 're'. It provides the key functions such as search(), match(), findall(), and sub() for working with regex. The 'string' module does not support regular expressions, 'regex' is an external library, and 'pattern' is not a module.