Embercode
✕
C++ OperatorsQuick quiz

Shifts and narrowing

7 questions. Covers Compound assignment narrowing.

Questions 1 to 5 of 7

  1. 1. What does this program print? ``cpp #include <iostream> int main() { int a = -21; std::cout << (a >> 2) << " " << (a / 4) << "\n"; } ``
  2. 2. What does this program print? ``cpp #include <iostream> int main() { short s = 30000; s += 5000; std::cout << s << "\n"; } ``
  3. 3. The program should print sent 300, but it prints sent 44. What is the bug? ``cpp #include <iostream> int main() { unsigned char packets = 0; for (int k = 0; k < 300; ++k) { packets += 1; } std::cout << "sent " << static_cast<int>(packets) << "\n"; } ``
  4. 4. The program should print total 3, but it prints total 0. What is the bug? ``cpp #include <iostream> int main() { int total = 0; for (int k = 0; k < 10; ++k) { total += 0.3; } std::cout << "total " << total << "\n"; } ``
  5. 5. True or False: g++ -std=c++20 -Wall warns about the line c += 300; in this program. ``cpp #include <iostream> int main() { char c = 100; c += 300; std::cout << static_cast<int>(c) << "\n"; } ``

5 question(s) left on this page.