Programming Concepts Test
—
Part 1: Multiple Choice (10 points)
1. What is the output of the following code?
“`python
x = 10
y = 5
print(x + y)
“`
a) 10 b) 15 c) 5 d) 50 2. Which of the following is a valid variable name in Python?
a) 2ndValue b) value-2 c) second_value d) second value 3. What is the purpose of a loop in programming?
a) To store data b) To repeat a block of code c) To define a function d) To create a variable 4. What does the `return` keyword do in a function?
a) Ends the program b) Exits a loop c) Returns a value and exits the function d) Declares a variable 5. Which of the following data structures can hold key-value pairs?
a) List b) Tuple c) Dictionary d) Set —
Part 2: Short Answer (15 points)
1. Explain the difference between a `for` loop and a `while` loop.
(5 points)
2. Write a Python function that takes two numbers as arguments and returns their sum.
(5 points)
3. What is the purpose of using comments in code? Provide an example.
(5 points)
—
Part 3: Coding Problem (25 points)
1. Create a class called `Car` that has the following attributes:
– `make` (string)
– `model` (string)
– `year` (integer)
– `speed` (integer, default is 0)
The class should also have the following methods:
– accelerate: Increases the speed by 5.
– brake: Decreases the speed by 5 but not below 0.
– display_speed: Prints the current speed.
Write a program to create a `Car` object and demonstrate each of the methods.(25 points)
—