Python is a popular and versatile programming language known for its simplicity and readability. It's widely used in web development, data analysis, AI, and automation. Python is favored for its clean syntax and extensive library ecosystem. It's employed by companies like Google, Dropbox, Instagram, and NASA
Last updated on:- 5th June 2023
1.
Which of the following is not a primitive data type in Python ?
int
str
list
bool
In Python, the primitive data types include int, str, and bool. However, list is not a primitive data type.
2.
Which of the following is the correct way to check if a variable is an instance of a specific class in Python ?
type(variable) == ClassName
isinstance(variable, ClassName)
variable instanceof ClassName
variable.type() == ClassName
The 'isinstance()' function in Python is used to check if a variable is an instance of a specific class. It returns 'True' if the variable is an instance of the specified class, and 'False' otherwise.
3.
Which of the following is used to declare a function in Python ?
func
def
fun
define
In Python, the 'def' keyword is used to declare a function.
4.
What is the correct way to comment multiple lines of code in Python ?
// This is a comment
/* This is a comment */
# This is a comment
// This is a comment //
In Python, the '#' symbol is used to indicate a comment. It can be placed at the beginning of a line or after code.
5.
Which of the following operators is used for exponentiation in Python ?
**
^
//
^^
The '**' operator is used for exponentiation in Python. For example, 2 ** 3 evaluates to 8.
6.
What is the output of the following code ?
x = [1, 2, 3]
print(x[1])
1
2
3
Error
In Python, lists are zero-indexed, so x[1] accesses the element at index 1, which is 2.
7.
Which of the following is used to check the length of a string in Python ?
length()
count()
size()
len()
The 'len()' function is used to obtain the length of a string or any iterable object in Python.
8.
What does the 'range()' function return in Python ?
A list of numbers
A tuple of numbers
A generator object
A dictionary
The 'range()' function in Python returns a generator object that generates a sequence of numbers.
9.
What is the correct way to check if a key exists in a dictionary in Python ?
key in dict
dict.contains(key)
dict[key] != None
key.exists(dict)
To check if a key exists in a dictionary, you can use the 'in' operator. For example, 'key in dict' returns True if the key exists.
10.
Which of the following is not a valid way to create a set in Python ?
{1, 2, 3}
set([1, 2, 3])
set(1, 2, 3)
set()
To create a set in Python, you can use curly braces ({}) or the 'set()' function. However, 'set(1, 2, 3)' is not a valid way to create a set.
11.
What is the output of the following code ?
x = [1, 2, 3]
y = x
y.append(4)
print(x)
[1, 2, 3]
[1, 2, 3, 4]
[4]
[1, 2, 3, [4]]
In Python, lists are mutable objects. When y is assigned to x, it creates a reference to the same list object. Therefore, when we modify y by appending 4, it also modifies x.
12.
Which of the following is used to iterate over a dictionary in Python ?
for key in dict:
for item in dict.items():
for value in dict.values():
All of the above
Python provides multiple ways to iterate over a dictionary, including iterating over keys, items (key-value pairs), and values. You can use any of the mentioned options depending on your requirements.
13.
Which of the following statements is true about Python's pass statement ?
It does nothing and acts as a placeholder
It raises an exception
It terminates the program
It removes the need for indentation
The pass statement in Python is used as a placeholder when a statement is syntactically required but no action is needed. It does nothing and helps in avoiding syntax errors.
14.
What is the result of the expression '3' + 2 in Python ?
5
32
Error
None of the above
The expression '3' + 2 will raise a TypeError because it's trying to concatenate a string ('3') with an integer (2). To perform this operation, the types of operands must match.
15.
What is the purpose of the 'break' statement in Python ?
To exit the current loop completely
To skip the current iteration and continue to the next
To define the end of a function
To raise an exception
The 'break' statement is used to exit the current loop immediately. When encountered, it terminates the loop execution and continues with the next statement after the loop.
16.
Which of the following is used to read input from the user in Python ?
read()
input()
scan()
get()
The 'input()' function is used to read input from the user in Python. It allows the user to enter data from the keyboard, which can be stored in variables or used for further processing.
17.
What is the output of the following code?
x = (1, 2, 3)
x[0] = 4
print(x)
(1, 2, 3)
(4, 2, 3)
[1, 2, 3]
Error
Tuples in Python are immutable, meaning their elements cannot be changed once defined. Therefore, assigning a new value to an element in a tuple will raise a TypeError.
18.
What does the 'join()' method do in Python ?
Concatenates elements of a list into a single string
Splits a string into a list of substrings
Reverses the order of elements in a list
Removes whitespace characters from the beginning and end of a string
The 'join()' method is used to concatenate elements of a list into a single string. It takes a separator string and returns a new string by concatenating all the elements of the list separated by the specified separator.
19.
Which of the following is a correct way to open a file in Python ?
open(file_path, 'w')
open(file_path, 'write')
open(file_path, 'r+')
All of the above
To open a file in Python, you can use the 'open()' function. The second argument specifies the mode in which the file is opened. 'r+' is a valid mode that allows both reading and writing.
20.
What is the result of the expression 'Hello' * 3 in Python ?
HelloHelloHello
Hello3
Error
None of the above
The expression 'Hello' * 3 repeats the string 'Hello' three times, resulting in 'HelloHelloHello'. The '*' operator performs string repetition in Python.
21.
What is the output of the following code ?
x = 5
y = 2
print(x / y)
2.5
2
2.0
Error
In Python, division of two integers returns a float value. Therefore, the output of 5 / 2 is 2.5.
22.
What does the 'os' module provide in Python ?
File input/output operations
Operating system-related functionality
String manipulation functions
Mathematical operations
The 'os' module in Python provides a way to use operating system-dependent functionality. It includes functions for interacting with the file system, managing processes, and more.
23.
Which of the following is not a standard Python library ?
math
numpy
random
stringutil
The 'stringutil' library is not a standard Python library. However, the 'math', 'numpy', and 'random' libraries are part of the standard library and provide various functionalities.
24.
What is the purpose of the 'super()' function in Python ?
To invoke the parent class's methods
To create a new instance of a class
To define a subclass
To access private variables
The 'super()' function is used to invoke the methods of a parent class from a subclass. It allows the subclass to extend or override the behavior of the parent class while still retaining its functionality.
25.
Which of the following is a correct way to handle exceptions in Python ?
try/finally
try/else
try/except
All of the above
Python provides multiple ways to handle exceptions. The 'try/except' block is used to catch and handle exceptions, the 'try/finally' block ensures that certain actions are always performed, and the 'try/else' block is executed if no exception occurs.
26.
What is the output of the following code?
x = [1, 2, 3]
print(x[10:])
[1, 2, 3]
[]
Error
None
When accessing a list with an index that is out of range, Python doesn't raise an error but returns an empty list ([]). Therefore, x[10:] will result in an empty list.
27.
What is the purpose of the 'lambda' keyword in Python ?
To define anonymous functions
To import modules
To perform type casting
To raise an exception
The 'lambda' keyword in Python is used to create anonymous functions, also known as lambda functions. These functions are defined without a name and are typically used when a small function is required for a short period.
28.
What is the result of the expression 'Hello, {}!'.format('John') in Python ?
'Hello, {}!'
'Hello, {John}!'
'Hello, John!'
'Hello, {John}!'
The 'format()' method is used to format strings in Python. When the '{}' placeholder is used, it can be replaced by the corresponding argument passed to the 'format()' method. Therefore, 'Hello, {}!'.format('John') results in 'Hello, John!'
29.
Which of the following is true about Python's global variables ?
They can be accessed from any function or module
They can only be accessed within the module they are defined
They can only be accessed within the function they are defined
They can only be accessed within a class
Global variables in Python can be accessed from any part of the program, including functions or modules. However, it is considered a good practice to limit their usage and prefer passing variables as arguments.
30.
What is the result of the expression '10 // 3' in Python?
3.3333
3.0
3
Error
The '//' operator in Python performs floor division, which returns the quotient as an integer. Therefore, 10 // 3 evaluates to 3.