Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a class named Car.
C++
class [1] { public: int speed; };
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using lowercase names for class names.
Using unrelated names like Vehicle or Speed.
β Incorrect
The class name should be Car to match the declaration.
2fill in blank
mediumComplete the code to create an object named myCar of class Car.
C++
Car [1]; Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using the class name instead of the object name.
Using generic names like object.
β Incorrect
The object name should be myCar as requested.
3fill in blank
hardFix the error in the code to access the speed attribute of myCar.
C++
myCar.[1] = 100;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using uppercase attribute names.
Adding parentheses to access attributes.
β Incorrect
The attribute name is speed and accessed without parentheses.
4fill in blank
hardFill both blanks to define a constructor that sets speed to 0.
C++
class Car { public: int speed; [1] [2] { speed = 0; } };
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Adding a return type like void to the constructor.
Using parentheses without braces for the body.
β Incorrect
The constructor has the same name as the class Car() and uses braces {} for the body.
5fill in blank
hardFill all three blanks to create a method named accelerate that increases speed by 10.
C++
class Car { public: int speed; void [1][2] [3] { speed += 10; } };
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using incorrect method names.
Omitting parentheses or braces.
β Incorrect
The method name is accelerate, followed by parentheses () and braces {} for the body.