Which of the following statements about C++ type checking and static member functions are correct? Select the most accurate combination based on the C++ language rules.

Difficulty: Easy

Correct Answer: Both A and B.

Explanation:


Introduction:
C++ performs compile-time type checking and also offers limited runtime type identification features. This question verifies understanding of static versus dynamic type checking and clarifies a common misconception about const qualification of static member functions.


Given Data / Assumptions:

  • Language: C++.
  • Static type checking occurs at compile time.
  • Dynamic checks exist via RTTI (typeid, dynamic_cast).
  • Static member functions have no implicit this pointer.


Concept / Approach:
“Static type checking” means errors are caught at compile time by the type system. “Dynamic type checking” in C++ refers to features like dynamic_cast on polymorphic types and typeid, which consult runtime type information. A static member function cannot be declared const because const applies to the implicit object parameter (this), which static functions do not have.


Step-by-Step Solution:
1) Assess statement A: true — the C++ compiler enforces static type rules at compile time.2) Assess statement B: true — RTTI enables certain runtime checks (e.g., dynamic_cast failures).3) Assess statement C: false — static members lack a this pointer; const has no meaning here.4) Therefore the correct combined choice is “Both A and B.”


Verification / Alternative check:
Code examples: dynamic_cast(basePtr) returns nullptr if the runtime type is not Derived, demonstrating runtime checking; compile-time mismatches (e.g., adding int to std::string) fail statically.


Why Other Options Are Wrong:
A only: ignores legitimate runtime checks via RTTI.B only: overlooks pervasive compile-time checking in C++.C: impossible by language rules; const qualifies the object state through this.


Common Pitfalls:
Confusing dynamic typing (as in Python) with C++’s RTTI; C++ remains statically typed even though it supports certain runtime checks.


Final Answer:
Both A and B.

More Questions from OOPS Concepts

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion