Docsity

CAPP-001 Certified Associate Python Programmer Exam, Exams of Technology

Toggle sidebar
bg1
CAPP-001 Certified Associate Python Programmer Practice Exam
Question 1: What is the primary role of the Python interpreter?
A. To compile code into machine language
B. To execute Python code line by line
C. To convert Python code into Java code
D. To manage hardware resources directly
Answer: B
Explanation: The Python interpreter reads and executes code line by line, converting it into actions
immediately.
Question 2: Which built-in function is used to display output to the console in Python?
A. input()
B. type()
C. print()
D. eval()
Answer: C
Explanation: The print() function outputs text or other data types to the console.
Question 3: What is the correct file extension for a Python script?
A. .py
B. .txt
C. .java
D. .pyt
Answer: A
Explanation: Python scripts are saved with the .py extension.
Question 4: Which function returns the data type of a variable in Python?
A. id()
B. type()
C. print()
D. str()
Answer: B
Explanation: The type() function shows the data type of a given variable.
Question 5: How do you declare a single-line comment in Python?
A. // comment
B. /* comment */
C. # comment
D. <!-- comment -->
Answer: C
Explanation: In Python, the hash symbol (#) is used to mark single-line comments.
Question 6: Which of the following is a valid Python identifier?
A. 1variable
B. variable-name
C. variable_name
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e

Partial preview of the text

Download CAPP-001 Certified Associate Python Programmer Exam and more Exams Technology in PDF only on Docsity!

CAPP-001 Certified Associate Python Programmer Practice Exam

Question 1: What is the primary role of the Python interpreter? A. To compile code into machine language B. To execute Python code line by line C. To convert Python code into Java code D. To manage hardware resources directly Answer: B Explanation: The Python interpreter reads and executes code line by line, converting it into actions immediately. Question 2: Which built-in function is used to display output to the console in Python? A. input() B. type() C. print() D. eval() Answer: C Explanation: The print() function outputs text or other data types to the console. Question 3: What is the correct file extension for a Python script? A. .py B. .txt C. .java D. .pyt Answer: A Explanation: Python scripts are saved with the .py extension. Question 4: Which function returns the data type of a variable in Python? A. id() B. type() C. print() D. str() Answer: B Explanation: The type() function shows the data type of a given variable. Question 5: How do you declare a single-line comment in Python? A. // comment B. /* comment */ C. # comment D. Answer: C Explanation: In Python, the hash symbol (#) is used to mark single-line comments. Question 6: Which of the following is a valid Python identifier? A. 1variable B. variable-name C. variable_name

D. variable name Answer: C Explanation: Identifiers must start with a letter or underscore and cannot contain hyphens or spaces. Question 7: Which operator in Python is used for exponentiation? A. ^ B. ** C. % D. // Answer: B Explanation: The ** operator performs exponentiation in Python. Question 8: What is the purpose of the len() function in Python? A. To calculate the length of an object B. To convert a value to an integer C. To list all functions in a module D. To check variable types Answer: A Explanation: The len() function returns the number of items in an object like a string, list, or tuple. Question 9: Which data type does the value True represent in Python? A. Integer B. String C. Boolean D. Float Answer: C Explanation: True is a Boolean value representing truth in Python. Question 10: Which of these is a mutable data type in Python? A. Tuple B. String C. List D. Integer Answer: C Explanation: Lists can be modified after creation, making them mutable. Question 11: How do you start Python’s interactive shell? A. By running python.exe in a web browser B. By typing python in a terminal or command prompt C. By opening a .py file directly D. By compiling the code first Answer: B Explanation: Typing “python” in the terminal launches the interactive Python shell. Question 12: What is the function of input() in Python? A. To output data B. To receive user input as a string

B. var_ C. var- 2 D. var 2 Answer: B Explanation: Variable names must not start with a digit and cannot contain hyphens or spaces. Question 19: What is the result of evaluating 3 + 2.5 in Python? A. 5 B. 5. C. 6 D. Error Answer: B Explanation: Adding an integer and a float yields a float result (5.5). Question 20: Which function converts a string into an integer in Python? A. str() B. int() C. float() D. bool() Answer: B Explanation: The int() function converts a string representing an integer into an actual integer. Question 21: Which statement is used to execute code conditionally in Python? A. for B. if C. while D. def Answer: B Explanation: The if statement allows conditional execution based on a boolean expression. Question 22: Which keyword provides an alternative condition in an if block? A. else if B. elseif C. elif D. otherwise Answer: C Explanation: In Python, “elif” is used as a shorthand for “else if.” Question 23: How do you start a while loop in Python? A. while condition: B. while (condition) { } C. loop while condition: D. do while condition: Answer: A Explanation: The syntax for a while loop in Python is “while condition:” followed by the indented block.

Question 24: What is the purpose of the break statement inside loops? A. To skip the current iteration B. To exit the loop immediately C. To restart the loop D. To pause execution Answer: B Explanation: The break statement stops loop execution and exits the loop immediately. Question 25: What does the continue statement do within a loop? A. Exits the loop completely B. Skips the rest of the current iteration and continues with the next C. Repeats the current iteration D. Terminates the program Answer: B Explanation: The continue statement skips to the next iteration of the loop without executing further code in the current iteration. Question 26: Which statement in Python allows you to have an empty code block? A. null B. empty C. pass D. skip Answer: C Explanation: The pass statement is a no-operation placeholder used when no action is needed. Question 27: Which structure is best suited for checking multiple conditions? A. Single if statement B. if-elif-else ladder C. for loop D. try-except block Answer: B Explanation: An if-elif-else ladder allows you to test multiple conditions in a clear and organized way. Question 28: What does an if statement evaluate in Python? A. A numeric value B. A boolean expression C. A string D. A function Answer: B Explanation: The if statement evaluates a boolean expression to determine which block of code to execute. Question 29: Which loop repeats code until a condition becomes false? A. for loop B. if loop C. while loop D. switch loop

C. *

D. //

Answer: B Explanation: The “and” operator combines boolean expressions, returning True only if both operands are True. Question 36: What happens when an if statement’s condition evaluates to False? A. The code inside the if block executes B. The code inside the if block is skipped C. An error is raised D. The condition is re-evaluated Answer: B Explanation: If the condition is False, the code within the if block does not execute. Question 37: Which keyword is not used for controlling loops in Python? A. break B. continue C. pass D. stop Answer: D Explanation: “Stop” is not a Python keyword; break, continue, and pass are used within loops. Question 38: Which loop structure guarantees at least one execution of its block regardless of the condition? A. while loop B. for loop C. do-while loop (simulated) D. if loop Answer: C Explanation: Although Python does not have a built-in do-while loop, it can be simulated to ensure at least one execution. Question 39: How does the for loop typically iterate over a list in Python? A. By using indices only B. By directly accessing each element in order C. By sorting the list first D. By converting it to a tuple Answer: B Explanation: Python’s for loop automatically iterates over each element of a list in order. Question 40: Which keyword is sometimes used in loops to create a placeholder when no action is required? A. continue B. pass C. break D. skip Answer: B

Explanation: The pass statement is used as a placeholder when no action is needed inside a loop or block. Question 41: Which data structure in Python is ordered and mutable? A. Tuple B. List C. Set D. Frozenset Answer: B Explanation: Lists maintain order and can be modified after creation. Question 42: Which data structure is defined using parentheses in Python? A. List B. Tuple C. Set D. Dictionary Answer: B Explanation: Tuples are created using parentheses and are immutable. Question 43: Which data structure does not allow duplicate elements? A. List B. Tuple C. Set D. Dictionary keys Answer: C Explanation: Sets automatically remove duplicate elements, ensuring each element is unique. Question 44: What is a dictionary in Python? A. An ordered list of numbers B. A collection of key-value pairs C. A mutable sequence of characters D. An immutable sequence of items Answer: B Explanation: Dictionaries store data in key-value pairs, allowing fast lookups. Question 45: How do you access an element at a specific index in a list named my_list? A. my_list{index} B. my_list[index] C. my_list(index) D. my_list.index Answer: B Explanation: Square brackets are used to access elements in a list by index. Question 46: What does list slicing allow you to do? A. Delete a list B. Reverse a list C. Extract a subset of a list

C. Both A and B D. Only A works Answer: C Explanation: Iterating directly over a dictionary or using the keys() method both return its keys. Question 53: What does the zip() function do in Python? A. Compresses files B. Combines multiple iterables into a single iterator of tuples C. Splits strings D. Sorts lists Answer: B Explanation: The zip() function pairs items from multiple iterables into tuples. Question 54: How do you reverse a list called items in Python? A. items.reverse() B. reverse(items) C. items[::-1] D. Both A and C Answer: D Explanation: You can reverse a list in-place with items.reverse() or create a reversed copy using slicing. Question 55: What is a list comprehension? A. A concise way to generate lists B. A method to delete lists C. A function to combine lists D. A built-in module for list operations Answer: A Explanation: List comprehensions provide a compact syntax for creating new lists by applying an expression to each item. Question 56: Which data type in Python is defined using curly braces? A. List B. Tuple C. Set and Dictionary D. String Answer: C Explanation: Both sets and dictionaries use curly braces, though dictionaries contain key-value pairs. Question 57: How do you check if an element exists in a set named my_set? A. if element exists in my_set: B. if element in my_set: C. if my_set.contains(element): D. if element is my_set: Answer: B Explanation: The “in” keyword is used to test membership in sets.

Question 58: What is the main difference between lists and tuples? A. Lists are immutable while tuples are mutable B. Lists are mutable while tuples are immutable C. Both are immutable D. Both are mutable Answer: B Explanation: Lists can be changed after creation, whereas tuples cannot. Question 59: How do you sort a list named numbers in ascending order? A. numbers.sorted() B. sorted(numbers) C. numbers.sort() D. Both B and C Answer: D Explanation: You can either use numbers.sort() to sort in place or sorted(numbers) to return a new sorted list. Question 60: Which method converts a list of key-value pair tuples into a dictionary? A. dict() B. list() C. tuple() D. set() Answer: A Explanation: The dict() constructor can create a dictionary from an iterable of key-value pairs. Question 61: Which keyword is used to define a function in Python? A. function B. def C. fun D. define Answer: B Explanation: The def keyword is used to declare functions in Python. Question 62: In a function, what is a parameter? A. A value returned by the function B. A variable used to pass data into a function C. A command to exit the function D. A type of error Answer: B Explanation: Parameters are variables in function definitions that receive values when the function is called. Question 63: Which type of argument allows a function to accept an unspecified number of positional parameters? A. Keyword arguments B. Default arguments C. Variable-length arguments using *args

Question 69: What is recursion in the context of functions? A. A function calling itself B. A function that returns multiple values C. A function that executes in reverse D. A function with no parameters Answer: A Explanation: Recursion is a programming technique where a function calls itself to solve smaller instances of a problem. Question 70: What does the def keyword do in Python? A. Declares a new class B. Defines a new function C. Imports a module D. Initializes a variable Answer: B Explanation: The def keyword is used to define a new function in Python. Question 71: How can you assign default values to function parameters? A. By using the equals sign in the parameter list B. By declaring them outside the function C. By using the default keyword D. By initializing variables after the function call Answer: A Explanation: Default parameters are assigned using the equals sign in the function definition. Question 72: What is the output of a function that does not include a return statement? A. None B. 0 C. An empty string D. It raises an error Answer: A Explanation: Functions without a return statement automatically return None in Python. Question 73: Which keyword is used to import a module with an alias in Python? A. as B. alias C. importas D. using Answer: A Explanation: The “as” keyword is used to assign an alias when importing a module. Question 74: What is a nested function in Python? A. A function defined inside another function B. A function with multiple return statements C. A function that calls itself D. A function imported from a module Answer: A

Explanation: Nested functions are defined within another function’s body and have access to the enclosing function’s variables. Question 75: What is the purpose of variable-length arguments in Python functions? A. To limit the number of arguments B. To allow passing an arbitrary number of arguments C. To enforce type checking on arguments D. To create multiple functions at once Answer: B Explanation: Variable-length arguments allow a function to accept any number of positional or keyword arguments. Question 76: How do you call a function defined in another file? A. Use import to bring the module into the current namespace and then call the function B. Copy the function code directly C. Use the exec() function D. It is not possible Answer: A Explanation: Importing the module makes its functions accessible, allowing you to call them with the module’s name. Question 77: What is one advantage of using lambda functions? A. They allow for multi-line definitions B. They are always faster than regular functions C. They provide a concise way to write small functions D. They can only accept keyword arguments Answer: C Explanation: Lambda functions are ideal for short, throwaway functions that are defined in one line. Question 78: What does the map() function do in Python? A. Applies a function to all items in an iterable and returns an iterator of results B. Creates a new list from two lists C. Filters out items from a list D. Sorts a list in ascending order Answer: A Explanation: The map() function applies the specified function to each item in the given iterable. Question 79: How do you filter a list using a function in Python? A. Using the filter() function B. Using list slicing C. Using the reduce() function D. Using the sort() function Answer: A Explanation: The filter() function returns an iterator containing only those items for which the function returns True.

Answer: B Explanation: Inheritance allows a class (child) to inherit attributes and methods from another class (parent). Question 86: What does the super() function do in a class method? A. Calls a method from a parent class B. Creates a new subclass C. Deletes an object D. Initializes the class attributes Answer: A Explanation: The super() function gives access to methods of a parent class, facilitating method overriding and code reuse. Question 87: Which method is used to represent an object as a string for end-users? A. str B. repr C. init D. del Answer: A Explanation: The str method is used to define a human-readable string representation of an object. Question 88: What does polymorphism in Python allow? A. Objects to have multiple identities B. The use of a single interface for different data types C. The restriction of method access D. The creation of duplicate classes Answer: B Explanation: Polymorphism lets the same operation be performed on objects of different classes. Question 89: How do you define a method within a Python class? A. Using the function keyword B. By writing a function inside a class with the def keyword and including self as the first parameter C. By declaring it outside the class D. Using the method keyword Answer: B Explanation: Methods are defined within a class using def, and the first parameter is typically self to refer to the instance. Question 90: What is a class attribute in Python? A. A variable defined inside a method B. A variable shared by all instances of a class C. A function that modifies an object D. A temporary variable inside a loop Answer: B Explanation: Class attributes are shared among all instances of the class rather than belonging to one specific instance.

Question 91: What is the difference between instance variables and class variables? A. Instance variables are immutable, class variables are mutable B. Instance variables are unique to each object, class variables are shared across all objects C. Instance variables are only used in functions, class variables are used in methods D. There is no difference Answer: B Explanation: Instance variables are defined for each object, while class variables are common to all instances of the class. Question 92: How do you denote a private attribute in a Python class? A. By prefixing the attribute name with a single underscore (_) B. By prefixing the attribute name with a dollar sign ($) C. By using the private keyword D. By suffixing the attribute name with _private Answer: A Explanation: A single underscore is the convention for indicating that an attribute is intended to be private. Question 93: What is method overriding? A. Defining two methods with the same name in one class B. Redefining a method in a subclass that already exists in the parent class C. Removing a method from a class D. Calling a method twice Answer: B Explanation: Method overriding occurs when a subclass provides its own implementation for a method that is already defined in its parent class. Question 94: What is a static method in Python? A. A method that can modify class state B. A method that is bound to the class rather than the object and does not access instance data C. A method that only works with immutable data D. A method used to initialize objects Answer: B Explanation: Static methods do not receive the self or cls parameter and are defined using the @staticmethod decorator. Question 95: How do you define a class method in Python? A. Using the @classmethod decorator and a parameter named cls B. By placing the method outside the class C. Using the def keyword without decorators D. By using the static keyword Answer: A Explanation: Class methods are defined with the @classmethod decorator and take cls as the first parameter, referring to the class itself. Question 96: What is the purpose of the repr method? A. To provide an official string representation of the object for developers

D. To import modules Answer: B Explanation: Try-except blocks are used to catch and manage errors without crashing the program. Question 102: Which keyword is used to catch exceptions in Python? A. catch B. handle C. except D. error Answer: C Explanation: The except keyword is used in conjunction with try to catch exceptions. Question 103: What does the finally clause do in a try-except block? A. It executes only if an exception occurs B. It executes only if no exception occurs C. It always executes regardless of exceptions D. It stops exception handling Answer: C Explanation: The finally clause is guaranteed to run after the try and except blocks, regardless of whether an exception was raised. Question 104: How do you manually trigger an exception in Python? A. raise B. throw C. trigger D. call Answer: A Explanation: The raise keyword is used to trigger an exception manually. Question 105: What is a custom exception in Python? A. An exception defined by the Python interpreter B. An exception that can be created by the programmer by subclassing Exception C. A built-in error D. An error that cannot be caught Answer: B Explanation: Custom exceptions are defined by inheriting from the Exception class, allowing tailored error messages and handling. Question 106: What is the purpose of assertions in Python code? A. To log errors to a file B. To check that conditions are true during development C. To convert data types D. To compile code Answer: B Explanation: Assertions are used to verify that a condition is true; if it’s false, an AssertionError is raised.

Question 107: Which keyword is used for assertions in Python? A. assert B. check C. verify D. test Answer: A Explanation: The assert keyword is used to test if a condition in the code is True during development. Question 108: How can you handle multiple exceptions in a single try block? A. By writing several try blocks B. By separating exception types with commas in one except clause C. By using nested try-except blocks D. By listing exceptions after the finally clause Answer: B Explanation: Multiple exceptions can be handled in one except clause by enclosing them in a tuple. Question 109: What type of error is raised when dividing by zero in Python? A. ValueError B. ZeroDivisionError C. TypeError D. IndexError Answer: B Explanation: Dividing by zero raises a ZeroDivisionError in Python. Question 110: Which exception is raised when a file is not found? A. FileNotFoundError B. IOError C. IndexError D. KeyError Answer: A Explanation: Python raises FileNotFoundError when it cannot locate the specified file. Question 111: What is exception chaining in Python? A. Handling exceptions one after the other B. Linking one exception to another to provide context C. Raising exceptions repeatedly D. Ignoring exceptions Answer: B Explanation: Exception chaining allows one exception to reference another, providing additional context for errors. Question 112: How can you debug exceptions in Python? A. By using print statements only B. By ignoring them C. By examining the traceback and using debugging tools D. By recompiling the code Answer: C