MCQs on New Features in C++ Standards with Explanation

NEW FEATURES ANSI C++ STANDARD

1. One of the new data types introduced by ANSI C++ standard is

Explanation:

Correct answer: c) bool

Option c is the correct answer. The ANSI C++ standard introduced the boolean data type, which represents logical values true (1) and false (0). The boolean data type is commonly used in C++ to store and manipulate Boolean (true/false) values.


2. Which of the following operators is normally used to change a pointer type object to integer type object

Explanation:

Correct answer: a) static_cast operator

Option a is the correct answer. The static_cast operator in C++ is used to perform safe and explicit type conversions between compatible types. It can be used to convert a pointer type object to an integer type object or vice versa. For example, you can use static_cast<int>(ptr) to convert a pointer ptr to an integer.


3. Consider the following code segment:

int x = 10, y = 20;
bool b1, b2;
b1 = x == y;
b2 = x < y;
bool b3 = 15;
y = x + y + b1 + b2 + b3;
cout << y;
Explanation:

Correct answer: c) 33

The code calculates the value of y based on the boolean variables b1 and b2 along with the integer value b3. Here's the step-by-step explanation:

  • b1 = x == y; assigns true (1) to b1 because x is not equal to y.
  • b2 = x < y; assigns true (1) to b2 because x is less than y.
  • bool b3 = 15; is incorrect because you cannot assign an integer value (15) directly to a boolean variable (bool b3). The correct statement should be bool b3 = true; or bool b3 = false; to represent the boolean value.
  • y = x + y + b1 + b2 + b3; calculates the value of y as 10 + 20 + 1 + 1 + true (which is treated as 1). So, y becomes 33.
Therefore, the correct answer is option c) 33.


4. Which of the following operators is used to obtain the type of an object?

Explanation:

Correct answer: b) typeid

Option b is the correct answer. The typeid operator in C++ is used to obtain the type of an object at runtime. It returns a reference to a type_info object that contains information about the type of the object. The type_info class is part of the typeinfo header and is used for type identification and dynamic type comparison (RTTI - Run-Time Type Information) in C++.


5. What will be the output of the following code, when executed?

class X { };
class Y { };
int main()
{
    X x1, x2;
    Y y1, y2;
    if (typeid(x1) == typeid(y1))
        cout << "Same types";
    else
        cout << "Different types";
}
Explanation:

Correct answer: b) Same types

The code uses the typeid() operator to compare the types of objects x1 and y1. Since x1 and y1 are of different classes (class X and class Y), the condition in the if statement evaluates to false. Therefore, the else part will be executed, and the output will be "Different types". So, the correct answer is option a) Different types.


6. Consider the code segment:

class Test
{
    float x;
public:
    explicit Test(float y)
    {
        x = y;
    }
};

What are the changes required in the definition of the class Test, if it is to be implemented by the main ( ) as given below.

int main()
{
    Test t1 = 35.65;
    cout << x;
}
Explanation:

Correct answer: b. The keyword explicit should be removed

Option b is the correct answer. The explicit keyword is used to declare a constructor as an explicit constructor, which means that the constructor cannot be used for implicit type conversions. When the keyword explicit is used with a constructor, it prevents the compiler from automatically invoking the constructor during implicit conversions. In the given code, the constructor Test(float y) is marked as explicit. Therefore, the line Test t1 = 35.65; will result in a compilation error because it tries to perform an implicit conversion from float to Test, but the constructor is explicit. To make it work without any compilation errors, the explicit keyword should be removed from the constructor declaration in the class definition. After that, the line Test t1 = 35.65; will create a Test object and initialize its data member x with the value 35.65. Additionally, to access the data member x in the main() function, it should be accessed using the object t1, like t1.x.


7. We can modify the value of a data member of an object declared constant by declaring it using the keyword

Explanation:

Correct answer: d) mutable

Option d is the correct answer. The mutable keyword in C++ is used to modify data members of a constant (const) object. When a data member is declared as mutable, it can be modified even if the object itself is constant. This is useful when you have a constant object, but there are certain data members that you want to be modifiable even for constant objects. For example:

class MyClass
{
    mutable int counter; // mutable data member

public:
    MyClass() : counter(0) {}

    void increment() const
    {
        ++counter; // Allowed even for const objects
    }

    int getCount() const
    {
        return counter; // Allowed even for const objects
    }
};


8. The correct format for defining a namespace is:

Explanation:

Correct answer: b. namespace namespace_name {…}

Option b is the correct answer. The correct format for defining a namespace in C++ is namespace namespace_name { … }, where namespace_name is the name of the namespace, and the code to be included within the namespace is enclosed within the curly braces. For example:

namespace MyNamespace
{
    // code here
}
This defines a namespace called MyNamespace in which you can put C++ code. Namespaces are used to avoid naming conflicts and to organize code into logical groups.


9. We use namespaces to

Explanation:

Correct answer: a) divide a long program into different modules

Option a is the correct answer. Namespaces in C++ are used to divide a long program into different logical modules or units. By placing related code within a namespace, we can avoid naming conflicts between different parts of the program. Namespaces provide a way to group related classes, functions, variables, and other program elements together and provide better organization and management of code. For example:

namespace MathLib
{
    // code related to math operations
}

namespace FileSystem
{
    // code related to file system operations
}

int main()
{
    MathLib::calculate(); // Using function from MathLib namespace
    FileSystem::readFile(); // Using function from FileSystem namespace
}


10. Given the namespace definition

namespace S
{
    int count;
}

How can we access the variable count outside the namespace?

Explanation:

Correct answer: a) S::count = 10;

Option a is the correct answer. To access the variable count outside the namespace S, you should use the scope resolution operator :: along with the namespace name. The correct statement to access and modify the variable is S::count = 10;. This way, you can access the variable count from the namespace S without the need for any using directives. The other options are incorrect because using namespace S; will make all the elements of namespace S available directly without the need to qualify them with S::, and using namespace ::count; is not a valid syntax for accessing a variable outside the namespace.



Your Score is 10

THE STUDENT FRIENDLY BOOK TO LEARN C++. 
Let us C++ by Famous author Yashavant Kanetkar
Indian writer


Topic wise C++ MCQs Index ≡
Click on particular topic to visit that page
  1. Principles of Object-Oriented Programming 
  2. Beginning with C++
  3. Tokens, Expressions, and Control Structures
  4. Functions in C++
  5. Classes and Objects
  6. Constructors and Destructors
  7. Operator Overloading
  8. Inheritance
  9. Pointers, Virtual Functions, and Polymorphism
  10. Managing Console I/O Operations
  11. Working with Files
  12. Templates
  13. Exception Handling
  14. Standard Template Library
  15. Manipulating Strings
  16. New Features in ANSI C++ Standards
  17. Object-Oriented System Development

Post a Comment

0 Comments