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 or unrelated names for the class.
β 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.
β Incorrect
The object name should be myCar as specified.
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 or method-like syntax for attributes.
β Incorrect
The attribute name is speed and accessed without parentheses.
4fill in blank
hardFill both blanks to define a method named accelerate that increases speed by 10.
C++
void [1]() { speed [2] 10; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using subtraction operator or wrong method name.
β Incorrect
The method name is accelerate and the operator to increase speed is +=.
5fill in blank
hardFill all three blanks to create an object and call its accelerate method.
C++
Car [1]; [2].speed = 50; [3]();
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using different object names or missing parentheses when calling methods.
β Incorrect
The object is named myCar, so use it to set speed and call the method accelerate with dot notation.