Sakala Code 10 months ago
sakalacode #python

Programming in Python

Programming in Python

Programming in Python

Which of the following best defines a problem?

  1. A task that can be easily accomplished without any obstacles.
  2. A situation that makes achieving a goal more difficult.
  3. An opportunity to achieve something easily.
  4. A condition where everything goes as planned.

Answer : A situation that makes achieving a goal more difficult.

What is an algorithmic approach?

  1. A random set of actions that might solve a problem.
  2. A creative method to explore different solutions.
  3. A defined set of step-by-step procedures that gives the correct answer to a particular problem.
  4. An unpredictable process with no specific outcome.

Answer : A defined set of step-by-step procedures that gives the correct answer to a particular problem.

Which of the following is a characteristic of scripting languages?

  1. They are compiled before execution.
  2. They are primarily used for system-level programming.
  3. They are interpreted and often used for automating tasks.
  4. They cannot be used for web development.

Answer : They are interpreted and often used for automating tasks.

What is the first step towards effective problem-solving? 

  1. Immediately implementing solutions. 
  2. Understanding and describing the problem correctly. 
  3. Gathering feedback from others. 
  4. Testing various solutions simultaneously. 

Answers: Understanding and describing the problem correctly. 

What does a program comprise? 

  1. A random collection of data. 
  2. A set of instructions provided to the computer to solve a particular problem. 
  3. Hardware components of a computer. 
  4. An algorithm that is executed without user input. 

Answers: A set of instructions provided to the computer to solve a particular problem. 

What is the first step towards solving a problem with a computer? 

  1. Writing the code immediately. 
  2. Designing the user interface. 
  3. Creating a well-defined problem statement. 
  4. Purchasing the necessary hardware. 

Answers: Creating a well-defined problem statement. 

What are codes, also referred to as the source code? 

  1. Instructions written in binary form. 
  2. Machine-readable instructions. 
  3. Instructions written in a specific programming language. 
  4. Random strings of text. 

Answers: Instructions written in a specific programming language. 

What does the compilation process do? 

  1. Converts machine-readable code to source code. 
  2. Converts source code written in a programming language to a machine-readable or executable format. 
  3. Translates code from one programming language to another. 
  4. Optimizes the source code for better readability. 

Answers: Converts source code written in a programming language to a machine-readable or executable format. 

Which of the following is true about compiler errors? 

  1. Compiler errors are independent and do not accumulate. 
  2. Reading the error messages issued by the compiler is unnecessary. 
  3. Compiler error messages are often not very helpful, but reading them can provide hints to help find and fix the errors. 
  4. Compiler errors automatically fix themselves after some time.

Answers: Compiler error messages are often not very helpful, but reading them can provide hints to help find and fix the errors. 

What is the primary purpose of documentation within a program? 

  1. To optimize the program's performance. 
  2. To provide textual information that helps human readers understand, modify, or debug the program. 
  3. To convert the program into machine code. 
  4.  To automatically correct any errors in the program. 

Answers: To provide textual information that helps human readers understand, modify, or debug the program. 

Which characteristic is NOT essential for an algorithm? 

  1. Definiteness (clear and unambiguous steps) 
  2. Finiteness (having a finite number of steps) 
  3. Efficiency (minimizing resource use) 
  4. Input and Output (having zero or more inputs and one or more outputs) 

Answers: Efficiency (minimizing resource use) 

A flowchart is a graphical representation of steps that shows steps in sequential order. Which of the following is a common use of flowcharts? 

  1.  To represent mathematical equations 
  2.  To illustrate the flow of algorithms 
  3.  To create complex databases 
  4.  To design user interfaces 

Answers: To illustrate the flow of algorithms 

Which of the following flowchart symbols is used to represent a decision or branching point? 

  1.  Oval 
  2.  Rectangle 
  3.  Diamond 
  4.  Parallelogram 

Answers: Diamond 

What is a decision table primarily used for? 

  1. Creating detailed database schemas
  2. Specifying which actions to perform depending on given conditions
  3. Designing the layout of user interfaces
  4. Visualizing the flow of control in a program

Answers: Specifying which actions to perform depending on given conditions 

Why are decision tables considered an effective tool in software testing? 

  1. They help in creating user-friendly interfaces. 
  2. They simplify the process of designing complex algorithms. 
  3. They provide a simple representation, help check all possible combinations of conditions, and allow testers to identify missed conditions easily. 
  4. They are primarily used for performance testing. 

Answers: They provide a simple representation, help check all possible combinations of conditions, and allow testers to identify missed conditions easily. 

What is a programming paradigm? 

  1. A type of software development tool 
  2. A preferred approach to programming that a language supports, or its programming style 
  3. A graphical notation for designing algorithms 
  4. A set of syntax rules for a programming language 

Answers: A preferred approach to programming that a language supports, or its programming style 

What is a key characteristic of the procedural programming approach? 

  1. Dividing the code into objects 
  2.  Dividing the code into procedures, routines, subroutines, modules, methods, or functions 
  3.  Using a graphical user interface to design programs 
  4.  Emphasizing event-driven programming

Answers: Dividing the code into procedures, routines, subroutines, modules, methods, or functions 

In procedural programming, how does a developer typically begin solving a problem? 

  1. By creating a hierarchy of objects and classes 
  2. By writing the entire program as a single monolithic block of code 
  3. By breaking the problem down into sub-problems (sub-procedures) 
  4. By defining the data structures first 

Answers: By breaking the problem down into sub-problems (sub-procedures) 

What is one of the main advantages of structured programming? 

  1. It uses graphical tools to design programs 
  2. It allows for the integration of multiple programming languages in one program 
  3.  It makes it easier to understand the program and detect errors 
  4.  It automatically generates code without human intervention 

Answers: It makes it easier to understand the program and detect errors 

What is the primary focus of object-oriented programming (OOP)? 

  1.  Creating a sequence of instructions for problem-solving 
  2.  Utilizing objects to include benefits of modularity and reusability 
  3. Designing programs with flowcharts and diagrams 
  4.  Managing data through relational databases 

Answers: Utilizing objects to include benefits of modularity and reusability 

Which of the following is the correct first step to install Anaconda on a Windows operating system? 

  1. Open Command Prompt and type pip install anaconda. 
  2.  Download the Anaconda installer from the official Anaconda website. 
  3.  Search for Anaconda in the Microsoft Store. 
  4.  Clone the Anaconda repository from GitHub and run install.py. 

Answers: Download the Anaconda installer from the official Anaconda website. 

What does syntax in computer programming refer to? 

  1.  The logical flow of a program. 
  2.  The rules that control the structure of symbols, punctuation, and words. 
  3.  The execution speed of a program. 
  4.  The graphical user interface of a program. 

Answers: The rules that control the structure of symbols, punctuation, and words. 

Which of the following is a valid Python identifier? 

  1. 2variable 
  2.  variable-name 
  3.  variable_name 
  4.  variable.name 

Answers: variable_name 

What is a variable in programming? 

  1. A constant value that never changes 
  2.  A reserved memory location associated with a name to store a value that can change. 
  3.  A type of function used to perform calculations. 
  4.  A special character used to separate statements. 

Answers: A reserved memory location associated with a name to store a value that can change. 

Which of the following is a mutable data type in Python? 

  1.  Tuple 
  2.  List 
  3.  String 
  4.  Integer 

Answers: List 

How are strings represented in Python? 

  1.  Using brackets [ ] 
  2.  Using parentheses ( ) 
  3. Using single or double quotation marks 
  4.  Using curly braces { } 

Answers: Using single or double quotation marks 

Which arithmetic operator in Python is used to perform floor division? 

  1. /
  2. %
  3. //
  4. **

Answers: //

Which of the following identity operators in Python checks if two variables point to the same object? 

  1. ==
  2. is not
  3. is
  4. !=

Answers: is 

Which bitwise operator in Python is used to perform a binary AND operation? 

  1.  |
  2.  ^
  3.  &
  4.  ~

Answers: & 

What does the bitwise operator << do in Python? 

  1. Perform a binary OR operation 
  2. Perform a binary XOR operation 
  3. Left shift the bits of a number 
  4. Right shift the bits of a number 

Answers: Left shift the bits of a number 

Which of the following is the correct syntax for an if statement in Python? 

  1. if condition {
  2. if (condition) then
  3. if condition:
  4. if condition;

Answers: if condition:

What will be the output of the following Python code? 

x = 10 
if x > 5: 
  print("x is greater than 5") 
else: 
  print("x is less than or equal to 5")
  1. x is greater than 5 
  2. x is less than or equal to 5 
  3. x is greater than 5 and x is less than or equal to 5 
  4. Error 

Answers: x is greater than 5 

What is the purpose of the elif statement in Python? 

  1. To define a loop
  2. To handle exceptions
  3. To check multiple conditions after an initial if statement
  4. To import modules

Answers: To check multiple conditions after an initial if statement 

Which of the following statements correctly describes the purpose of a while loop in Python? 

  1. It iterates over a sequence of elements.
  2. It executes a block of code as long as a specified condition is true.
  3. It defines a function that can be called multiple times.
  4. It handles exceptions that occur during program execution.

Answers: It executes a block of code as long as a specified condition is true. 

What will be the output of the following Python code? 

for i in range(3):
  print(i)
  1. 0 1 2 3 
  2. 0 1 2 
  3. 1 2 3 
  4. 0 1 2 3 4 

Answers: 0 1 2 

What will be the output of the following Python code? 

for i in range(2, 5): 
  print(i * i) 
  1. 4 9 16 25
  2. 4 9 16
  3. 2 4 6
  4. 4 8 12

Answers: 4 9 16 

What is the output of the following code snippet? 

total = 0 
for i in range(5): 
  total += i 
  if i == 3: 
    break 
print(total) 
  1. 10
  2. 6
  3. 4
  4. 5

Answers: 6 

What does the continue statement do in a loop? 

  1. It exits the loop completely. 
  2. It skips the remaining code inside the current iteration of the loop and proceeds to the next iteration. 
  3. It terminates the program. 
  4. It starts a new loop. 

Answers: It skips the remaining code inside the current iteration of the loop and proceeds to the next iteration. 

What is the effect of the pass statement in Python? 

  1. It breaks out of the current loop. 
  2. It continues to the next iteration of the loop. 
  3. It does nothing and serves as a placeholder. 
  4. It raises an exception. 

Answers: It does nothing and serves as a placeholder. 

What will be the output of the following code? 

count = 0 
while count < 5: 
  count += 1 
  if count == 3: 
    pass 
  print(count, end=' ') 
else: 
  print('End') 
  1. 1 2 3 4 5 End
  2. 1 2 4 5 End
  3. 1 2 3 4 End
  4. 1 2 4 5

Answers: 1 2 3 4 5 End 

What is the index of the last character in the string "PYTHON"? 

  1. 5
  2. 6
  3. 7
  4. 8

Answers: 5 

Which operator is used to concatenate two strings in Python? 

  1. +=
  2. *
  3. +
  4. =

Answers: + 

What will be the output of the following code? 

first_str = "New" 
second_str = "Delhi" 
result = first_str + second_str 
print(result) 
  1. New-Delhi
  2. New Delhi
  3. NewDelhi
  4. New+Delhi

Answers: NewDelhi

What is the correct way to append one string to another in Python? 

  1. str1 + str2
  2. str1.append(str2)
  3. str1 += str2
  4. str1 = str2 + str1

Answers: str1 += str2 

What will be the output of the following code? 

str = 'spyder'
print(str[2:5])
  1. spy
  2. pyd
  3. yde
  4. der

Answers: yde 

Which of the following string methods converts all characters in a string to uppercase? 

  1. lower() 
  2. upper() 
  3. isupper() 
  4. capitalize() 

Answers: upper() 

What does the split() method return when called on a string? 

  1. A string
  2. A list
  3. An integer
  4. A dictionary

Answers: A list 

If you want to remove leading and trailing spaces from a string, which method would you use? 

  1. strip()
  2. find()
  3. replace()
  4. split()

Answers: strip() 

Which of the following is used to specify a string in multiple lines in Python? 

  1. Single Quotes (')
  2. Double Quotes (")
  3. Triple Quotes (''')
  4. Both Single and Double Quotes

Answers: Triple Quotes (''')

What does the format() function do in Python? 

  1. It converts strings to uppercase. 
  2. It allows text alignment and formatting of strings. 
  3. It finds a substring in a string. 
  4. It splits a string into a list. 

Answers: It allows text alignment and formatting of strings. 

Which of the following statements about a list in Python is true? 

  1. A list can only store elements of the same data type. 
  2. A list can store elements of different data types. 
  3. A list cannot store multiple items in a single variable. 
  4. Elements in a list are separated by curly braces. 

Answers: A list can store elements of different data types. 

What will be the output of the following code? 

List_city = ["Delhi", "Mumbai", "Kolkata"] 
print(List_city[2]) 
  1. Delhi
  2. Mumbai
  3. Kolkata
  4. IndexError

Answers: Kolkata 

In Python, what is the index of the first element in a list? 

  1.  1
  2.  -1
  3.  0
  4.  N

Answers: 0 

What does the following Python code return? 

List_num = [100, 200, 114, 12, 34]
print(len(List_num))
  1.  4 
  2.  5 
  3.  6 
  4.  7 

Answers: 5 

What will be the output of the following Python code? 

List_A = ['A','B','C','D','E','F','G'] 
print(List_A[2:5]) 
  1. ['A', 'B', 'C']
  2. ['C', 'D', 'E']
  3. ['D', 'E', 'F']
  4. ['E', 'F', 'G']

Answers: ['C', 'D', 'E'] 

What does the following Python code print? 

List = [1, 2, 'a', 4, 'b', 6, 'c'] 
print(List[-3]) 
  1.  4
  2.  a
  3.  b
  4.  c

Answers: b

Which method is used to remove an element from a list in Python? 

  1. delete() 
  2. remove() 
  3. discard() 
  4. pop() 

Answers: remove() 

What will be the output of the following code? 

list1 = list("MORNING") 
print(list1) 
  1. ['M', 'O', 'R', 'N', 'I', 'N', 'G'] 
  2. ['MORNING'] 
  3. ['M', 'O', 'R', 'N'] 
  4. 'MORNING' 

Answers: ['M', 'O', 'R', 'N', 'I', 'N', 'G'] 

What will be the output of the following Python code? 

ListA = [2, 4, 2, 6, 7, 2]
ListA.reverse()
print(ListA)
  1. [2, 4, 2, 6, 7, 2] 
  2. [2, 7, 6, 2, 4, 2] 
  3. [7, 6, 4, 2, 2, 2] 
  4. [2, 2, 2, 6, 4, 7]

Answers: [2, 7, 6, 2, 4, 2] 

Which of the following methods is used to add elements from one list to the end of another list? 

  1. append()
  2. extend()
  3. insert()
  4. concatenate()

Answers: extend() 

Which of the following is a characteristic of tuples in Python? 

  1. Tuples are mutable
  2. Tuples can be changed after creation
  3. Tuples allow duplicate values
  4. Tuples are enclosed in square brackets 

Answers: Tuples allow duplicate values 

What is the correct way to create an empty tuple? 

  1. empty_tuple = {}
  2. empty_tuple = []
  3. empty_tuple = ()
  4. empty_tuple = ''

Answers: empty_tuple = () 

Which method returns the number of occurrences of a specified value in a tuple? 

  1. find()
  2. index()
  3. count()
  4. search()

Answers: count()

How are tuples different from lists in Python? 

  1. Tuples are mutable, and lists are immutable
  2. Tuples are used for homogeneous data types, lists for heterogeneous
  3. Tuples are immutable, lists are mutable
  4. Tuples do not allow duplicate values, lists do

Answers: Tuples are immutable, lists are mutable

Which operator is used to concatenate two tuples? 

  1. +
  2. *
  3. -
  4. /

Answers: + 

What is the output of the following code? 

T1=("hello","everyone","welcome","to","Swayam","Courses") 
print(T1[0],T1[-2]) 
  1. hello everyone
  2. hello Swayam
  3. hello Courses
  4. Swayam Courses

Answers: hello Swayam 

What will be the output of the following code? 

x=(1,2,3,4,5,6,7) 
print( x [ : ] )  
  1. (1,2,3,4,5,6,7)
  2. (7,6,5,4,3,2,1)
  3. ( )
  4. Error

Answers: (1,2,3,4,5,6,7)

What is the size of the following tuple? 

x=(["Swayam",1,2,3], ["Hi"," welcome"," to "," Python "," Program "])
  1.  1
  2.  2
  3.  3
  4.  4

Answers: 2

What is the output of the following code? 

x=(7,8,9,10)
print(x*2)
  1. (7, 8, 9, 10)
  2. (14, 16, 18, 20)
  3. (7, 8, 9, 10, 7, 8, 9, 10)
  4. Error

Answers: (7, 8, 9, 10, 7, 8, 9, 10)

Which keyword is used to check the particular element inside the tuples? 

  1. is
  2. in
  3. is_there
  4. in_there

Answers: in 

What is a dictionary in Python? 

  1. A collection of ordered data
  2. A collection of key-value pairs
  3. A collection of unique elements
  4. A collection of mutable elements

Answers: A collection of key-value pairs 

Which of the following is the correct syntax for creating a dictionary in Python? 

  1. my_dict = (key1: value1, key2: value2)
  2. my_dict = [key1: value1, key2: value2]
  3. my_dict = [key1: value1, key2: value2]
  4. my_dict = {key1: value1, key2: value2}

Answers: my_dict = {key1: value1, key2: value2} 

Which statement is true about the keys in a Python dictionary? 

  1. Keys can be duplicated.
  2. Keys must be of a mutable data type.
  3. Keys must be unique and of an immutable data type.
  4. Keys can be a list or a dictionary.

Answers: Keys must be unique and of an immutable data type. 

What will be the output of the following code? 

my_dict = {'a': 1, 'b': 2, 'c': 3} 
print(my_dict['b']) 
  1. {'b': 2}
  2.  2
  3.  b
  4.  KeyError

Answers: 2

How can you safely access the value of a key that might not exist in a dictionary?

  1. dict[key]
  2. dict.key
  3. dict.get(key)
  4. dict.find(key)

Answers: dict.get(key)

What does the following code do? 

my_dict = {'a': 1, 'b': 2, 'c': 3} 
my_dict['b'] = 5 
print(my_dict) 
  1. Adds a new key 'b' with value 5
  2. Replaces the value of key 'b' with 5
  3. Raises a KeyError
  4. Removes key 'b' from the dictionary

Answers: Replaces the value of key 'b' with 5

Which method is used to remove a key from a dictionary?

  1. remove()
  2. delete()
  3. pop()
  4. discard()

Answers: pop() 

What is the output of the following code? 

d = {'a': 1, 'b': 2, 'c': 3} 
print(min(d, key=d.get)) 
  1. 'a'
  2. 'c'
  3. 1
  4. 'b'

Answers: 'a' 

How can you iterate over all keys and values in a dictionary? 

  1. for k, v in dict
  2. for k, v in dict.items()
  3. for k in dict.keys()
  4. for v in dict.values()

Answers: for k, v in dict.items() 

What will be the output of the following code? 

dict1 = {1: 10, 2: 20}
dict2 = {3: 30, 4: 40}
dict1.update(dict2)
print(dict1)
  1. {1: 10, 2: 20, 3: 30, 4: 40}
  2. {3: 30, 4: 40}
  3. {1: 10, 2: 20}
  4. {1: 10, 2: 20, '3': 30, '4': 40}

Answers: {1: 10, 2: 20, 3: 30, 4: 40} 

What is a function in Python? 

  1. A piece of code that can be reused to perform a specific task
  2. A variable used to store values
  3. A method to take user input
  4. A built-in Python data structure

Answers: A piece of code that can be reused to perform a specific task

What is the correct syntax for defining a function in Python? 

  1. function_name():
  2. def function_name():
  3. define function_name:
  4. def function_name[]:

Answers: def function_name(): 

What will be the output of the following code? 

def greet():
  print("Hello, World!")
 
greet()
  1.  Hello, Python 
  2.  Hello, World! 
  3.  Syntax Error 
  4.  No output 

Answers: Hello, World!

Which of the following is used to call a function in Python? 

  1. call function_name()
  2. function_name[]
  3. function_name()
  4. function_name;

Answers: function_name() 

Which keyword is used to create a lambda function in Python? 

  1. lambda
  2. def
  3. func
  4. return

Answers: lambda 

What is the output of the following code? 

add = lambda x, y: x + y
print(add(3, 4))
  1. 7
  2. 12
  3. 1
  4. Error

Answers: 7

What happens if the number of arguments passed to a function does not match the function definition? 

  1. The function will still execute but return None.
  2. Python will raise a TypeError.
  3. The extra arguments will be ignored.
  4. The function will raise a ValueError.

Answers: Python will raise a TypeError. 

What is the scope of a local variable in Python? 

  1. It can be accessed globally throughout the program.
  2. It can be accessed only within the function where it is defined.
  3. It can be accessed within the class where it is defined.
  4. It can be accessed within the module.

Answers: It can be accessed only within the function where it is defined. 

Which of the following is a valid example of a default argument in Python? 

  1. def my_func(x, y=10):
  2. def my_func(x=10, y):
  3. def my_func(x, y=default):
  4. def my_func(x, y=10, z=):

Answers: def my_func(x, y=10): 

Which type of function does not have a name in Python?

  1. Built-in function
  2. Lambda function
  3. User-defined function
  4. Complex function

Answers: Lambda function

What is the file extension for a Python module? 

  1. .mod
  2. .py
  3. .pymod
  4. .ext

Answers: .py

What keyword is used to import a Python module into another program? 

  1. include
  2. require
  3. import
  4. use

Answers: import

In which order does Python search for a module?

  1. PYTHONPATH, current directory, Python installation path
  2. Current directory, PYTHONPATH, Python installation path
  3. Python installation path, PYTHONPATH, current directory
  4. Python installation path, d:

Answers: Current directory, PYTHONPATH, Python installation path 

What exception is raised if a module is not found during import? 

  1. ModuleError
  2. FileNotFoundError
  3. ImportError
  4. IOError

Answers: ImportError 

How can you rename a module when importing it?

  1. Use the rename keyword
  2. Use the as keyword
  3. Use the alias keyword
  4. Use the import keyword again

Answers: Use the as keyword 

Which function can list all identifiers (like functions, classes, variables) in a module?

  1. list()
  2. getIdentifiers()
  3. dir()
  4. ids()

Answers: dir() 

In Python, which namespace is searched first when an identifier is encountered? 

  1. Global namespace
  2. Local namespace
  3. Built-in namespace
  4. No Namespace

Answers: Local namespace 

Which symbol is used to create a private identifier in Python? 

  1. One underscore (_) before the identifier
  2. Two underscores (__) before the identifier
  3. Double hash (##) before the identifier
  4. There is no private identifier in Python

Answers: Two underscores (__) before the identifier

What is the purpose of the __init__.py file in a Python package?

  1. It initializes the module
  2. It specifies that the directory is a package
  3. It lists the functions to be imported
  4. It is not required in Python 3.0 and later

Answers: It specifies that the directory is a package 

Which statement correctly imports the sqrt function from the math module? 

  1. import math.sqrt
  2. from math import sqrt
  3. import sqrt from math
  4. include math.sqrt

Answers: from math import sqrt 

What is the default mode in which a file is opened in Python using the open() function? 

  1.  w 
  2.  a 
  3.  r 
  4.  r+ 

Answers: r 

Which file mode is used to open a file for both reading and writing in binary format? 

  1.  r 
  2.  rb 
  3.  r+ 
  4.  rb+ 

Answers: rb+ 

What does the file_object.closed attribute return? 

  1. The name of the file
  2. The file size
  3. True if the file is closed, False if it is open
  4. The file's access mode

Answers: True if the file is closed, False if it is open 

Which method is used to close a file in Python? 

  1. stop()
  2. quit()
  3. close()
  4. end()

Accepted Answers: close()

Which method is used to write a string to a file in Python? 

  1.  writelines() 
  2.  append() 
  3.  write() 
  4.  add() 

Answers: write() 

What is the purpose of the writelines() method in Python? 

  1. Write a single line to a file 
  2. Write a list of strings to a file 
  3. Read all the lines from a file 
  4. Append a single line to a file 

Answers: Write a list of strings to a file 

Which method is used to read all lines from a file? 

  1.  readline() 
  2.  read() 
  3.  readlines() 
  4.  write() 

Answers: readlines()

If you want to open a file for appending in binary format, which mode will you use? 

  1.  ab+ 
  2.  a 
  3.  ab 
  4.  rb+ 

Answers: ab 

Which file mode allows both reading and appending in text format? 

  1.  a 
  2.  a+ 
  3.  w+ 
  4.  r+ 

Accepted Answers: a+ 

What is the difference between the write() and append() methods in Python? 

  1. write() adds data to an existing file, append() overwrites the file 
  2. write() creates a new file, append() deletes the file 
  3. write() overwrites the content, append() adds data to the existing content 
  4. write() reads data, append() writes data 

Answers: write() overwrites the content, append() adds data to the existing content

Which keyword is used to raise an exception in Python? 

  1. throw 
  2. generate 
  3. raise 
  4. error 

Answers: raise 

When you try to convert a non-numeric string to an integer, which exception is raised? 

  1.  TypeError 
  2.  ValueError 
  3.  KeyError 
  4.  SyntaxError 

Answers: ValueError 

Which block is used to catch exceptions in Python? 

  1.  try 
  2.  except 
  3.  catch 
  4.  finally 

Answers: except 

What will be the output of the following code? 

try: 
  print(y) 
except: 
  print("An exception occurred") 
  1.  y 
  2.  An exception occurred 
  3.  An exception occurred 
  4.  no output 

Answers: An exception occurred

What will be the output of the following code?

def example():
  try: 
    return 1
  finally:
    return 3
result = example()
print(result)
  1.  1
  2.  3
  3.  example
  4.  result

Answers: 3

Which block is executed when no exceptions are raised?

  1.  try
  2.  except
  3.  else
  4.  finally

Answers: else 

Which exception is raised when you attempt to divide a number by zero? 

  1. ValueError 
  2. ZeroDivisionError 
  3. TypeError 
  4. NameError 

Answers: ZeroDivisionError 

Which of the following blocks must be followed by a finally block in Python? 

  1. else 
  2. except 
  3. try 
  4. assert 

Answers: try  

Which statement asserts a condition in Python and raises an exception if the condition is false? 

  1. assert 
  2. raise 
  3. throw 
  4. except 

Answers: assert 

What exception is raised when trying to access an index that is out of range? 

x = [1, 2, 3]
print(x[5])
  1. IndexError 
  2. ValueError 
  3. TypeError 
  4. KeyError 

Answers: IndexError

0
381
Php Magic Constants Examples

Php Magic Constants Examples

1709870648.png
Sakala Code
1 year ago
HTML Main Tag

HTML Main Tag

1709870648.png
Sakala Code
1 year ago
HTML hr Tag

HTML hr Tag

1709870648.png
Sakala Code
1 year ago
HTML Events for Drag and Drop API feature

HTML Events for Drag and Drop API feature

1709870648.png
Sakala Code
1 year ago
Php Superglobals Cookie

Php Superglobals Cookie

1709870648.png
Sakala Code
1 year ago