0
0
C++programming~10 mins

Real-world modeling in C++ - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a class named Car.

C++
class [1] {
public:
    int speed;
};
Drag options to blanks, or click blank then click option'
ACar
BVehicle
CEngine
DDrive
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using a different class name that doesn't match the problem description.
Forgetting to write the class name after the 'class' keyword.
2fill in blank
medium

Complete the code to add a constructor that sets the speed of the Car.

C++
class Car {
public:
    int speed;
    Car([1] s) {
        speed = s;
    }
};
Drag options to blanks, or click blank then click option'
Abool
Bfloat
Cstring
Dint
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using float or string instead of int for speed.
Forgetting to specify the type of the constructor parameter.
3fill in blank
hard

Fix the error in the method that increases the speed by a given amount.

C++
class Car {
public:
    int speed;
    void accelerate(int amount) {
        speed [1] amount;
    }
};
Drag options to blanks, or click blank then click option'
A+=
B-=
C*=
D/=
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using '-=' which decreases speed instead of increasing.
Using '*=' or '/=' which multiply or divide speed.
4fill in blank
hard

Fill both blanks to create a method that returns true if the car is moving.

C++
class Car {
public:
    int speed;
    bool isMoving() {
        return speed [1] [2];
    }
};
Drag options to blanks, or click blank then click option'
A>
B0
C==
D<
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using '==' instead of '>' which only checks if speed equals zero.
Using '<' which checks if speed is less than zero, which is not correct here.
5fill in blank
hard

Fill all three blanks to create a method that sets the speed only if the new speed is valid (non-negative).

C++
class Car {
public:
    int speed;
    void setSpeed(int newSpeed) {
        if (newSpeed [1] [2]) {
            speed [3] newSpeed;
        }
    }
};
Drag options to blanks, or click blank then click option'
A>=
B0
C=
D>
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using '>' instead of '>=' which excludes zero as valid speed.
Using '==' which checks equality instead of comparison.
Using '+=' instead of '=' which would add instead of set.