How would you remove duplicate values from a list in Python?
- Use a set to store unique values
- Use a for loop with conditions
- Use the list.remove() method
- Use the list.sort() method
To remove duplicate values from a list in Python, you can convert the list to a set. Sets automatically store only unique values, so when you convert a list to a set and back to a list, duplicates are removed. The other options do not directly address the removal of duplicates.
How can you handle an exception for an error caused by dividing by zero in Python?
- Use a try-except block
- Use a for loop
- Use a while loop
- Use an if statement
To handle an exception for an error caused by dividing by zero in Python, you should use a try-except block. Inside the try block, you should place the code that might raise the exception, and in the except block, you can define how to handle the exception gracefully. The other options are not suitable for handling this specific exception.
Which of the following data types in PHP is not scalar?
- int
- string
- array
- boolean
To check the data type of a variable in PHP, which function do you use?
is_type()
gettype()
datatype()
typeof()
gettype()
function. This function returns a string indicating the data type of the variable, such as "integer," "string," or "array."Which of the following is not a magic constant in PHP?
__FILE__
__DIR__
__FUNCTION__
__VARIABLE__
In PHP, which function is used to get the length of a string?
strlen()
count()
sizeof()
strlength()
strlen()
function is used to determine the length (number of characters) of a string. It's particularly useful for validating input or working with text data.In PHP, which keyword is used to define a constant?
- constant
- define
- const
- let
PHP is loosely typed, meaning:
- Data types are strictly enforced
- Data types are dynamically determined
- Data types are not used in PHP
- Data types are implicitly cast
How would you remove duplicate values from a list in Python?
- Use a set to store unique values
- Use a for loop with conditions
- Use the list.remove() method
- Use the list.sort() method
To remove duplicate values from a list in Python, you can convert the list to a set. Sets automatically store only unique values, so when you convert a list to a set and back to a list, duplicates are removed. Here’s an example:
my_list = [1, 2, 2, 3, 4, 4]
unique_list = list(set(my_list))
The other options do not directly address the removal of duplicates.
How can you handle an exception for an error caused by dividing by zero in Python?
- Use a for loop
- Use a while loop
- Use an if statement
- Use a try-except block
To handle an exception for an error caused by dividing by zero in Python, you should use a try-except block. Inside the try block, you should place the code that might raise the exception, and in the except block, you can define how to handle the exception gracefully. The other options are not suitable for handling this specific exception.
How would you access the last element of a list stored in a variable named my_list?
- my_list[-1]
- my_list[0]
- my_list[1]
- my_list[len(my_list)]
In Python, my_list[-1]
is used to access the last element of a list. my_list[0]
would access the first element. my_list[1]
would access the second element. my_list[len(my_list)]
would cause an IndexError as the index would be out of range.
What would be the output of the following Python code? print(type([]))
- <class 'str'>
- <class 'list'>
- <class 'int'>
- <class 'tuple'>
The output of type([])
would be <class 'list'>
because the empty square brackets []
represent an empty list, so the type() function returns <class 'list'>.