0
0
C++programming~20 mins

Getter and setter methods in C++ - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Getter and Setter Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of getter and setter usage in C++ class

What is the output of this C++ program that uses getter and setter methods?

C++
#include <iostream>
class Box {
private:
    int length;
public:
    void setLength(int l) {
        length = l;
    }
    int getLength() {
        return length;
    }
};

int main() {
    Box b;
    b.setLength(10);
    std::cout << b.getLength() << std::endl;
    return 0;
}
A10
B0
CCompilation error
DGarbage value
Attempts:
2 left
πŸ’‘ Hint

Check what value is assigned by the setter before calling the getter.

❓ Predict Output
intermediate
2:00remaining
Effect of missing setter call on getter output

What will this program print if the setter is never called?

C++
#include <iostream>
class Box {
private:
    int length = 5;
public:
    void setLength(int l) {
        length = l;
    }
    int getLength() {
        return length;
    }
};

int main() {
    Box b;
    std::cout << b.getLength() << std::endl;
    return 0;
}
A0
B5
CCompilation error
DGarbage value
Attempts:
2 left
πŸ’‘ Hint

Look at the default value of the private variable.

πŸ”§ Debug
advanced
2:00remaining
Identify the error in getter and setter methods

What error will this code produce when compiled?

C++
#include <iostream>
class Box {
private:
    int length;
public:
    void setLength(int l) {
        length = l;
    }
    int getLength() const {
        return length;
    }
};

int main() {
    Box b;
    b.setLength(7);
    std::cout << b.getLength() << std::endl;
    return 0;
}
ANo error, output is 7
BRuntime error: uninitialized variable
CCompilation error: calling non-const method on const object
DCompilation error: cannot call non-const setter from const method
Attempts:
2 left
πŸ’‘ Hint

Check if const correctness is properly used.

πŸ“ Syntax
advanced
2:00remaining
Which option causes a compilation error in getter/setter?

Which of the following setter method definitions will cause a compilation error?

Avoid setLength(int l) { length = l; }
Bvoid setLength(int l) override { length = l; }
Cvoid setLength(int l) noexcept { length = l; }
Dvoid setLength(int l) const { length = l; }
Attempts:
2 left
πŸ’‘ Hint

Consider what const means for member functions.

πŸš€ Application
expert
3:00remaining
Determine the final value after multiple setter calls

Given the following code, what is the final output?

C++
#include <iostream>
class Counter {
private:
    int count = 0;
public:
    void setCount(int c) {
        if (c >= 0) {
            count = c;
        }
    }
    int getCount() {
        return count;
    }
};

int main() {
    Counter c;
    c.setCount(5);
    c.setCount(-3);
    c.setCount(10);
    std::cout << c.getCount() << std::endl;
    return 0;
}
A5
B-3
C10
D0
Attempts:
2 left
πŸ’‘ Hint

Notice the condition inside the setter method.