ProgrammingHardIBM · Deloitte pattern
Which member function fails to compile?
Which member function fails to compile?
class C {
int a = 0;
mutable int hits = 0;
public:
int get() const { hits++; return a; }
void set(int v) const { a = v; }
void reset() { a = 0; hits = 0; }
int peek() const { return a + hits; }
};- A. reset
- B. get
- C. set
- D. peek
Correct answer: C. set
A const member function treats the object as const, so it cannot assign to ordinary members: set fails. get may change hits only because hits is mutable. reset is not const, and peek only reads.
More OOP with C++ and Java questions
- Which C++ operator can be overloaded only as a non-static member function, never as a free function?
- What is printed? java List<Integer> l = Arrays.asList(1, 2, 3); try { l.set(0, 9); System.out.print(l); l.add(4); } catch…
- What is printed? java Map<String, Integer> m = new HashMap<>(); System.out.print(m.put("a", 1)); System.out.print(" " + m.put("a", 2))…
- What does sizeof print for an empty class in C++? cpp class Empty { }; int main() { cout << sizeof(Empty); }
- What is printed? java System.out.print("apple".compareTo("apricot") + " " + "app".compareTo("apple") + " " + "b".compareTo("a"));
- What is printed? java class P { static void sm() { System.out.print("P"); } void im() { System.out.print("P"); } } class C extends P {…