Programming in Python
Which of the following best defines a problem?
Answer : A situation that makes achieving a goal more difficult.
What is an algorithmic approach?
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?
Answer : They are interpreted and often used for automating tasks.
What is the first step towards effective problem-solving?
Answers: Understanding and describing the problem correctly.
What does a program comprise?
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?
Answers: Creating a well-defined problem statement.
What are codes, also referred to as the source code?
Answers: Instructions written in a specific programming language.
What does the compilation process do?
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?
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?
Answers: To provide textual information that helps human readers understand, modify, or debug the program.
Which characteristic is NOT essential for an algorithm?
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?
Answers: To illustrate the flow of algorithms
Which of the following flowchart symbols is used to represent a decision or branching point?
Answers: Diamond
What is a decision table primarily used for?
Answers: Specifying which actions to perform depending on given conditions
Why are decision tables considered an effective tool in software 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?
Answers: A preferred approach to programming that a language supports, or its programming style
What is a key characteristic of the procedural programming approach?
Answers: Dividing the code into procedures, routines, subroutines, modules, methods, or functions
In procedural programming, how does a developer typically begin solving a problem?
Answers: By breaking the problem down into sub-problems (sub-procedures)
What is one of the main advantages of structured programming?
Answers: It makes it easier to understand the program and detect errors
What is the primary focus of object-oriented programming (OOP)?
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?
Answers: Download the Anaconda installer from the official Anaconda website.
What does syntax in computer programming refer to?
Answers: The rules that control the structure of symbols, punctuation, and words.
Which of the following is a valid Python identifier?
Answers: variable_name
What is a variable in programming?
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?
Answers: List
How are strings represented in Python?
Answers: Using single or double quotation marks
Which arithmetic operator in Python is used to perform floor division?
Answers: //
Which of the following identity operators in Python checks if two variables point to the same object?
Answers: is
Which bitwise operator in Python is used to perform a binary AND operation?
Answers: &
What does the bitwise operator << do in Python?
Answers: Left shift the bits of a number
Which of the following is the correct syntax for an if statement in Python?
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")
Answers: x is greater than 5
What is the purpose of the elif statement in Python?
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?
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)
Answers: 0 1 2
What will be the output of the following Python code?
for i in range(2, 5): print(i * i)
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)
Answers: 6
What does the continue statement do in a 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?
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')
Answers: 1 2 3 4 5 End
What is the index of the last character in the string "PYTHON"?
Answers: 5
Which operator is used to concatenate two strings in Python?
Answers: +
What will be the output of the following code?
first_str = "New" second_str = "Delhi" result = first_str + second_str print(result)
Answers: NewDelhi
What is the correct way to append one string to another in Python?
Answers: str1 += str2
What will be the output of the following code?
str = 'spyder' print(str[2:5])
Answers: yde
Which of the following string methods converts all characters in a string to uppercase?
Answers: upper()
What does the split() method return when called on a string?
Answers: A list
If you want to remove leading and trailing spaces from a string, which method would you use?
Answers: strip()
Which of the following is used to specify a string in multiple lines in Python?
Answers: Triple Quotes (''')
What does the format() function do in Python?
Answers: It allows text alignment and formatting of strings.
Which of the following statements about a list in Python is true?
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])
Answers: Kolkata
In Python, what is the index of the first element in a list?
Answers: 0
What does the following Python code return?
List_num = [100, 200, 114, 12, 34] print(len(List_num))
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])
Answers: ['C', 'D', 'E']
What does the following Python code print?
List = [1, 2, 'a', 4, 'b', 6, 'c'] print(List[-3])
Answers: b
Which method is used to remove an element from a list in Python?
Answers: remove()
What will be the output of the following code?
list1 = list("MORNING")
print(list1)
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)
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?
Answers: extend()
Which of the following is a characteristic of tuples in Python?
Answers: Tuples allow duplicate values
What is the correct way to create an empty tuple?
Answers: empty_tuple = ()
Which method returns the number of occurrences of a specified value in a tuple?
Answers: count()
How are tuples different from lists in Python?
Answers: Tuples are immutable, lists are mutable
Which operator is used to concatenate two tuples?
Answers: +
What is the output of the following code?
T1=("hello","everyone","welcome","to","Swayam","Courses")
print(T1[0],T1[-2])
Answers: hello Swayam
What will be the output of the following code?
x=(1,2,3,4,5,6,7) print( x [ : ] )
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 "])
Answers: 2
What is the output of the following code?
x=(7,8,9,10) print(x*2)
Answers: (7, 8, 9, 10, 7, 8, 9, 10)
Which keyword is used to check the particular element inside the tuples?
Answers: in
What is a dictionary in Python?
Answers: A collection of key-value pairs
Which of the following is the correct syntax for creating a dictionary in Python?
Answers: my_dict = {key1: value1, key2: value2}
Which statement is true about the keys in a Python 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'])
Answers: 2
How can you safely access the value of a key that might not exist in a dictionary?
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)
Answers: Replaces the value of key 'b' with 5
Which method is used to remove a key from a dictionary?
Answers: pop()
What is the output of the following code?
d = {'a': 1, 'b': 2, 'c': 3}
print(min(d, key=d.get))
Answers: 'a'
How can you iterate over all keys and values in a dictionary?
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)
Answers: {1: 10, 2: 20, 3: 30, 4: 40}
What is a function in Python?
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?
Answers: def function_name():
What will be the output of the following code?
def greet():
print("Hello, World!")
greet()
Answers: Hello, World!
Which of the following is used to call a function in Python?
Answers: function_name()
Which keyword is used to create a lambda function in Python?
Answers: lambda
What is the output of the following code?
add = lambda x, y: x + y print(add(3, 4))
Answers: 7
What happens if the number of arguments passed to a function does not match the function definition?
Answers: Python will raise a TypeError.
What is the scope of a local variable in Python?
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?
Answers: def my_func(x, y=10):
Which type of function does not have a name in Python?
Answers: Lambda function
What is the file extension for a Python module?
Answers: .py
What keyword is used to import a Python module into another program?
Answers: import
In which order does Python search for a module?
Answers: Current directory, PYTHONPATH, Python installation path
What exception is raised if a module is not found during import?
Answers: ImportError
How can you rename a module when importing it?
Answers: Use the as keyword
Which function can list all identifiers (like functions, classes, variables) in a module?
Answers: dir()
In Python, which namespace is searched first when an identifier is encountered?
Answers: Local namespace
Which symbol is used to create a private identifier in Python?
Answers: Two underscores (__) before the identifier
What is the purpose of the __init__.py file in a Python package?
Answers: It specifies that the directory is a package
Which statement correctly imports the sqrt function from the math module?
Answers: from math import sqrt
What is the default mode in which a file is opened in Python using the open() function?
Answers: r
Which file mode is used to open a file for both reading and writing in binary format?
Answers: rb+
What does the file_object.closed attribute return?
Answers: True if the file is closed, False if it is open
Which method is used to close a file in Python?
Accepted Answers: close()
Which method is used to write a string to a file in Python?
Answers: write()
What is the purpose of the writelines() method in Python?
Answers: Write a list of strings to a file
Which method is used to read all lines from a file?
Answers: readlines()
If you want to open a file for appending in binary format, which mode will you use?
Answers: ab
Which file mode allows both reading and appending in text format?
Accepted Answers: a+
What is the difference between the write() and append() methods in Python?
Answers: write() overwrites the content, append() adds data to the existing content
Which keyword is used to raise an exception in Python?
Answers: raise
When you try to convert a non-numeric string to an integer, which exception is raised?
Answers: ValueError
Which block is used to catch exceptions in Python?
Answers: except
What will be the output of the following code?
try:
print(y)
except:
print("An exception occurred")
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)
Answers: 3
Which block is executed when no exceptions are raised?
Answers: else
Which exception is raised when you attempt to divide a number by zero?
Answers: ZeroDivisionError
Which of the following blocks must be followed by a finally block in Python?
Answers: try
Which statement asserts a condition in Python and raises an exception if the condition is false?
Answers: assert
What exception is raised when trying to access an index that is out of range?
x = [1, 2, 3] print(x[5])
Answers: IndexError