0
0
C++programming~20 mins

Access specifiers in C++ - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Access Specifiers Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of accessing private member in derived class
What is the output of this C++ code?
C++
#include <iostream>
using namespace std;

class Base {
private:
    int x = 10;
public:
    int getX() { return x; }
};

class Derived : public Base {
public:
    void printX() {
        // cout << x << endl; // line A
        cout << getX() << endl;
    }
};

int main() {
    Derived d;
    d.printX();
    return 0;
}
A0
BCompilation error due to accessing private member x
C10
DRuntime error
Attempts:
2 left
πŸ’‘ Hint
Private members are not accessible directly in derived classes, but public methods can access them.
❓ Predict Output
intermediate
2:00remaining
Output of protected member access in derived class
What will this program print?
C++
#include <iostream>
using namespace std;

class Parent {
protected:
    int val = 5;
};

class Child : public Parent {
public:
    void show() {
        cout << val << endl;
    }
};

int main() {
    Child c;
    c.show();
    return 0;
}
ACompilation error: val is inaccessible
B5
C0
DRuntime error
Attempts:
2 left
πŸ’‘ Hint
Protected members are accessible in derived classes.
❓ Predict Output
advanced
2:00remaining
Accessing private member from outside class
What error does this code produce?
C++
#include <iostream>
using namespace std;

class Sample {
private:
    int secret = 42;
};

int main() {
    Sample s;
    cout << s.secret << endl;
    return 0;
}
ACompilation error: 'secret' is private within this context
B42
CRuntime error
D0
Attempts:
2 left
πŸ’‘ Hint
Private members cannot be accessed outside their class.
❓ Predict Output
advanced
2:00remaining
Output of mixed access specifiers in inheritance
What is the output of this program?
C++
#include <iostream>
using namespace std;

class A {
public:
    int x = 1;
protected:
    int y = 2;
private:
    int z = 3;
};

class B : public A {
public:
    void print() {
        cout << x << ' ' << y << ' ';
        // cout << z << ' '; // line X
    }
};

int main() {
    B b;
    b.print();
    return 0;
}
ACompilation error due to accessing private member z
B1 2 3
CRuntime error
D1 2
Attempts:
2 left
πŸ’‘ Hint
Private members are not accessible in derived classes.
🧠 Conceptual
expert
2:00remaining
Number of accessible members in derived class
Given the class below, how many members are accessible directly in class Derived?
C++
class Base {
public:
    int a;
protected:
    int b;
private:
    int c;
};

class Derived : public Base {
    void func() {
        // Which members can be accessed here?
    }
};
A2 members: a and b
B3 members: a, b, and c
C1 member: a only
D0 members
Attempts:
2 left
πŸ’‘ Hint
Private members are not accessible in derived classes.