ProgrammingVery hardMicrosoft/Google SDE MCQ · GATE CS
What is printed?
What is printed?
struct P {
int x;
explicit P(int v) : x(v) { }
explicit operator bool() const { return x != 0; }
};
int main() {
P a(0), b(3);
if (b) cout << "T";
bool c = static_cast<bool>(a);
cout << c << !a;
}- A. T01
- B. T00
- C. T11
- D. T10
Correct answer: A. T01
An explicit conversion to bool is still used in contextual conversions such as if (b) and the operand of !, so if (b) prints T and !a is 1. static_cast<bool>(a) gives 0. Only something like bool c = a; would fail to compile.
More OOP with C++ and Java questions
- Which of the marked lines are compile errors? cpp int g(int x) { return x; } double g(int x) { return 2.0 x; } // line 1 void h(int x) { }…
- 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 is printed? java Map<String, Integer> m = new HashMap<>(); m.merge("k", 5, Integer::sum); m.merge("k", 5, Integer::sum); m.merge("k"…
- What is printed? java TreeMap<Integer, String> t = new TreeMap<>(); for (int k : new int[]{40, 10, 30, 20}) t.put(k, "v")…
- What is printed? java Deque<Integer> d = new ArrayDeque<>(); d.push(1); d.push(2); d.offer(3); d.addFirst(4); System.out.print(d.pop() +…
- What is printed? java Integer[] arr = {1, 2, 3}; List<Integer> l = Arrays.asList(arr); arr[0] = 7; l.set(2, 9); System.out.print(l + " " +…