Docsity

Python Programming Exam limited edition, Exams of Programming Languages

Toggle sidebar
bg1
Python Programming Final
Examination
This exam evaluates core competency in Python programming, covering syntax,
data structures, functional paradigms, and error handling.
Question Set 1-10
1. What is the standard way to output the string "Hello, World" to the terminal
in Python?
A. console.log("Hello, World")
B. System.out.println("Hello, World")
C. print("Hello, World")
D. echo "Hello, World"
Correct Choice: C
Rationale: The print() function is the built-in utility for displaying output. Students
coming from JavaScript or Java often confuse this with console.log or println.
2. Which reserved keyword is utilized to initialize a function definition?
A. def
B. func
C. define
D. function
Correct Choice: A
Rationale: Python uses the def keyword for function definitions. Using the full word
function is a common syntax error for beginners coming from JavaScript.
3. How do you denote a single-line comment in a Python script?
A. // This is a comment
B. /* This is a comment */
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16

Partial preview of the text

Download Python Programming Exam limited edition and more Exams Programming Languages in PDF only on Docsity!

Python Programming Final

Examination

This exam evaluates core competency in Python programming, covering syntax, data structures, functional paradigms, and error handling.

Question Set 1-

  1. What is the standard way to output the string "Hello, World" to the terminal in Python? A. console.log("Hello, World") B. System.out.println("Hello, World") C. print("Hello, World") D. echo "Hello, World" Correct Choice: C Rationale: The print() function is the built-in utility for displaying output. Students coming from JavaScript or Java often confuse this with console.log or println.
  2. Which reserved keyword is utilized to initialize a function definition? A. def B. func C. define D. function Correct Choice: A Rationale: Python uses the def keyword for function definitions. Using the full word function is a common syntax error for beginners coming from JavaScript.
  3. How do you denote a single-line comment in a Python script? A. // This is a comment B. /* This is a comment */

C. -- This is a comment D. # This is a comment Correct Choice: D Rationale: The hash symbol (#) tells the interpreter to ignore everything following it on that line. C-style comments like // are not valid in Python.

  1. Which of the following collection types is considered immutable? A. List B. Tuple C. Dictionary D. Set Correct Choice: B Rationale: Tuples are immutable, meaning their elements cannot be changed once the tuple is created. Lists and Dictionaries are frequently modified, leading students to forget the fixed nature of tuples.
  2. What would be the result of the expression len(["red", "blue", "green"])? A. 12 B. 3 C. 4 D. 1 Correct Choice: B Rationale: The len() function counts the number of items in the list container, which is three strings. New learners occasionally count the characters in the words instead of the list elements.
  3. Which operator is used to calculate the value of a number raised to a power? A. ^ B. %

Correct Choice: D Rationale: range(5) starts at 0 and stops before 5, producing 0, 1, 2, 3, 4. It is a common mistake to think range(4) would include the number 4.

  1. What type of object is 3.14159 in Python? A. float B. int C. double D. decimal Correct Choice: A Rationale: Python stores all non-integer real numbers as the float type. Unlike C++ or Java, Python does not have a native double keyword for standard precision floats.

Question Set 11-

  1. Which method removes an item with a specific key from a dictionary and provides that item's value? A. remove() B. pop() C. delete() D. discard() Correct Choice: B Rationale: pop() is used to extract a value by its key while removing the entry. remove() is a list method, and discard() is specific to sets.
  2. What does the // operator do in a mathematical expression? A. Returns the remainder B. Performs float division C. Performs floor division D. Calculates the square root

Correct Choice: C Rationale: Floor division // divides two numbers and truncates the result to the nearest lower integer. Students often confuse this with the modulo % operator.

  1. To handle runtime errors without stopping the program, which block structure is used? A. try...except B. try...catch C. handle...error D. do...rescue Correct Choice: A Rationale: Python utilizes try and except for exception handling. The keyword catch is used in languages like Java, which is the most frequent mistake here.
  2. How do you represent an empty set in Python? A. {} B. () C. [] D. set() Correct Choice: D Rationale: set() is required for an empty set because {} defaults to creating an empty dictionary. This is a subtle nuance that catches many intermediate developers.
  3. What is the value of the expression 7 % 3? A. 2. B. 2 C. 1 D. 0 Correct Choice: C Rationale: The modulo operator % returns the remainder of the division. Since 3 goes into 7 twice with 1 left over, the result is 1.

C. is D. === Correct Choice: A Rationale: The == operator checks for value equality. The is operator checks for object identity (memory location), which is a common source of bugs.

  1. What is the primary role of the init method in a class? A. To destroy an instance B. To convert an object to a string C. To initialize instance attributes D. To define a private method Correct Choice: C Rationale: init is the constructor that runs automatically when a new object is instantiated. It is frequently mistaken for a generic initialization method that must be called manually.

Question Set 21-

  1. What happens when the break keyword is encountered inside a for loop? A. The loop skips the current step B. the program restarts C. the loop pauses D. the loop terminates immediately Correct Choice: D Rationale: break exits the loop entirely. Beginners often confuse this with continue, which only skips the current iteration.
  2. Which list method is used to add an item to the very end of the list? A. add() B. append() C. push() D. insert()

Correct Choice: B Rationale: append() is the standard method for adding a single element to the end of a list. push() is a JavaScript array method often used incorrectly by beginners.

  1. Which built-in function finds the smallest value in a sequence? A. small() B. least() C. min() D. lower() Correct Choice: C Rationale: min() calculates the minimum value in an iterable. lower() is a string method, not a mathematical function for sequences.
  2. When using open("data.txt"), what is the default mode the file is opened in? A. Read mode ("r") B. Write mode ("w") C. Append mode ("a") D. Binary mode ("b") Correct Choice: A Rationale: Python defaults to "r" (read) mode if no second argument is provided. This safety feature prevents accidental overwriting of data.
  3. What is the result of accessing "Python"[0]? A. y B. P C. n D. Error Correct Choice: B Rationale: Python uses zero-based indexing, meaning the first character is at index 0. Some students mistakenly believe indexing starts at 1.

B. 6

C. "6"

D. Error Correct Choice: A Rationale: Multiplying a string by an integer repeats the string. Students often expect a mathematical result of 6 if they forget the "3" is a string.

  1. Which keyword acts as a placeholder when a statement is syntactically required but no action is needed? A. null B. pass C. void D. continue Correct Choice: B Rationale: pass is a null operation used to fill empty code blocks. null is not a keyword in Python (the equivalent is None).

Question Set 31-

  1. How do you check if the key "id" exists in a dictionary named user? A. user.exists("id") B. user.has("id") C. "id" in user.keys D. "id" in user Correct Choice: D Rationale: The in operator checks for the presence of a key in a dictionary. Using .keys() is valid but less efficient and more verbose than the direct check.
  2. What is the result of 15 // 4? A. 3 B. 3.

C. 4

D. 3.

Correct Choice: A Rationale: Floor division // results in 3 (it drops the .75). Students frequently round up to 4 or keep the decimal.

  1. Which argument in the print() function allows you to change the character printed at the end of the line? A. sep B. end C. last D. break Correct Choice: B Rationale: The end parameter defaults to \n (newline). Setting end=" " allows multiple print statements to appear on the same line.
  2. What does bool([False]) evaluate to? A. False B. 0 C. True D. None Correct Choice: C Rationale: Any non-empty list is "truthy" in Python, regardless of the values inside. Students often see False inside and assume the whole list is False.
  3. Which string method is used to convert all characters to uppercase? A. toUpper() B. capitalize() C. upper() D. ALL()
  1. What is the value of 3 ** 2? A. 9 B. 6 C. 8 D. 5 Correct Choice: A Rationale: This is 3 squared (3 times 3). Many beginners mistakenly interpret ** as multiplication or addition.
  2. How do you remove whitespace from the beginning and end of a string? A. trim() B. cut() C. clear() D. strip() Correct Choice: D Rationale: strip() is the Pythonic way to clean up whitespace. trim() is common in other languages but non-existent in Python.

Question Set 41-

  1. Which library is part of the standard library and helps with generating random choices? A. math B. os C. random D. sys Correct Choice: C Rationale: The random module provides tools for shuffling, picking random items, and generating random numbers.
  2. How do you determine the total number of characters in a string? A. count()

B. len() C. size() D. length() Correct Choice: B Rationale: len() is the universal function for getting the size of any sequence, including strings.

  1. Which of the following creates a simple one-line anonymous function? A. lambda x: x + 1 B. def lambda(x): x + 1 C. func(x) => x + 1 D. anonymous x: x + 1 Correct Choice: A Rationale: Lambda functions are defined using the lambda keyword followed by parameters and a single expression. They are often used for quick operations.
  2. Which data structure is best for mapping unique keys to specific values? A. List B. Tuple C. Dictionary D. Set Correct Choice: C Rationale: Dictionaries (hash maps) are designed for key-value pair storage. Sets only store unique keys without associated values.
  3. What is the result of 10 == "10"? A. True B. 1 C. None D. False
  1. What is the type of the object []? A. tuple B. set C. list D. dict Correct Choice: C Rationale: Empty square brackets denote an empty list. Students often confuse them with curly braces or parentheses.
  2. Which logical operator returns True only if both conditions being compared are True? A. or B. and C. & D. both Correct Choice: B Rationale: The and keyword performs logical conjunction. The & symbol is for bitwise operations and should not be used for logical conditions.

Question Set 51-

  1. How do you capture text typed by a user into a variable? A. input() B. get() C. scan() D. read() Correct Choice: A Rationale: input() displays a prompt and returns the user's input as a string. Many forget that the input is always a string and needs conversion for math.
  2. To turn the float 5.9 into the integer 5 , you should use: A. floor(5.9)

B. round(5.9) C. integer(5.9) D. int(5.9) Correct Choice: D Rationale: The int() function truncates decimals toward zero. round() would return 6, and floor() requires importing the math module.

  1. Which method splits a string into a list based on a separator? A. divide() B. break() C. split() D. segment() Correct Choice: C Rationale: split() is the standard string method for breaking text into a list. It defaults to splitting by any whitespace.
  2. What is the result of type(False)? A. B. C. D. Correct Choice: B Rationale: False is a boolean literal. In Python, booleans are actually a subclass of integers, but their type is bool.
  3. To manually trigger an error in your code, you use which keyword? A. raise B. throw C. error D. alert
  1. Which of these correctly defines a tuple with exactly one element? A. (5) B. (5,) C. [5] D. tuple(5) Correct Choice: B Rationale: The trailing comma is necessary to distinguish a tuple from an expression in parentheses. Without the comma, (5) is just the integer 5.
  2. Which operator performs a bitwise AND? A. and B. && C. & D. | Correct Choice: C Rationale: The single ampersand & is used for bitwise AND operations on integers. The keyword and is for logical operations.

Question Set 61-

  1. What does math.sqrt(16) return? A. 4 B. 4. C. 16 D. 2. Correct Choice: B Rationale: Most mathematical functions in the math module return floats, even if the result is a whole number.
  2. How is a block of code within a function recognized? A. Indentation B. Parentheses

C. Curly braces D. Semicolons Correct Choice: A Rationale: Python uses whitespace indentation to mark the start and end of functions, loops, and classes.

  1. Which method removes all items from a list, leaving it empty? A. remove() B. pop() C. delete() D. clear() Correct Choice: D Rationale: clear() empties the list container without deleting the list variable itself.
  2. What is the output of 5 > 3 and 2 > 4? A. True B. Error C. False D. None Correct Choice: C Rationale: Since 2 > 4 is false, the entire and expression evaluates to False.
  3. Which keyword allows you to check an additional condition if the first if statement is false? A. else if B. elseif C. elif D. case Correct Choice: C Rationale: elif is short for "else if" and is the correct Python syntax. Other variations like elseif are common syntax errors.