C++ MCQs with explanations

CLASSES AND OBJECTS

1. C++ supports both classes and structures. Which of the following statements is FALSE in respect of both of them?

Explanation ≡ Correct answer: option d) Their names can be used like any other type's names.
In C++, both classes and structures create user-defined data types. The names of classes and structures can be used in a similar way to built-in data types like int or float. They can be used to declare variables, function return types, function parameters, and more, just like other data types. Therefore, option d) is false.

2. In a class definition, the members declared as private are accessible

Explanation ≡ Correct answer: option c) only to functions declared as private
Members declared as private within a class are accessible only to functions declared within the class as private. Private members are not accessible outside the class or from functions declared in other classes. Therefore, option c) is true.

3. By default, member functions of a class become

Explanation ≡ Correct answer: option b) public
By default, member functions in a class are considered public. Public member functions are accessible from outside the class and can be called by other functions and objects. If a member function needs to be private or protected, it must be explicitly declared as such.

4. In a class, a member declared as public is accessible

Explanation ≡ Correct answer: option c) to any function in the program
A member declared as public within a class is accessible from any function in the program, regardless of whether it is a member function of the same class or not. Public members can be accessed using objects of the class or directly if the member is a static member.

5. The dot operator is used to connect

Explanation ≡ Correct answer: option a) a class object and a member of that class
The dot operator (.) is used to connect a class object (instance) with a member (variable or function) of that class. Using the dot operator, you can access the members of a class through an object of that class. For example, "obj.memberVariable" or "obj.memberFunction()".

6. Declaration of members as private in C++ enables it to implement the concept

Explanation ≡ Correct answer: option d) encapsulation
The declaration of members as private in C++ enables it to implement the concept of encapsulation. Encapsulation is one of the fundamental principles of object-oriented programming, where the data (attributes) and methods (functions) that operate on the data are bundled together within a class and hidden from the outside world. This ensures data integrity and security.

7. Listed below are some of the possible characteristics of member functions. Which one of them is NOT TRUE?

Explanation ≡ Correct answer: option a) All the member functions should be defined within the class itself
Member functions can be declared within the class itself, but they can be defined both inside and outside the class. The function prototypes (declarations) are usually given in the class definition, and the actual function bodies (definitions) can be provided outside the class, typically in a separate source file.

8. State which of the following is false with respect to classes:

Explanation ≡ Correct answer: option d) They are removed from memory when not in use
Classes are not automatically removed from memory when not in use. Instead, objects of the class are created and destroyed as they go in and out of scope. The class definition remains in memory for the entire program's lifetime. Classes are used to define blueprints for creating objects, and objects themselves hold the actual data and state.

9. Which one of the following statements is FALSE?

Explanation ≡ Correct answer: option d) A private member function can only be called by another function that is a member of its class.
A private member function can only be called by other functions that are members of the same class. Private members are not accessible from outside the class, even from objects of the class. Public and protected members can be accessed by objects and functions outside the class, but private members can only be accessed by functions within the same class.

10. When called by its object, a member function declared as const

Explanation ≡ Correct answer: option d) cannot alter any data in the class.
When a member function is declared as const, it promises not to modify any data (non-static members) of the class. This is called the const-correctness of the member function. If the function attempts to modify non-const data in the class, it will result in a compilation error.

11. A member function declared as static

Explanation ≡ Correct answer: option c) can be called using only its class name
A member function declared as static is associated with the class rather than with any specific object of the class. It can be called using the class name without the need for any object. Static member functions can only access static members of the class; they do not have access to non-static members or the "this" pointer.

12. Which one of the following statements is not true for a member function declared as friend?

Explanation ≡ Correct answer: option d) It cannot be called using the objects of its class.
A member function declared as a friend can be invoked like a normal function without using any object of the class. It has access to the private and protected members of the class it is a friend of. However, it is not a member function of the class, so it cannot be called using objects of that class.

13. Which of the following is TRUE about the static member variable of a class?

Explanation ≡ Correct answer: option d) It cannot be assigned any initial value at the time of definition.
Static member variables are shared among all objects of a class. They are declared in the class but must be defined (allocated memory) outside the class. However, they cannot be initialized at the time of definition within the class. Instead, they need to be explicitly initialized in a separate source file outside the class definition.

14. Which one of the following is TRUE while passing objects as function arguments?

Explanation ≡ Correct answer: option d) All of the above.
While passing objects as function arguments, a copy of the entire object can be passed. The object can be passed by value, by reference, or by pointer. The address of the object can also be passed by taking the address of the object using the address-of operator (&). Furthermore, non-member functions can use objects as their arguments.

15. Which one of the following is FALSE?

Explanation ≡ Correct answer: option d) None of the above.
All of the statements mentioned in the options are true. The address of a class member can be assigned to a pointer variable. When making a member function const, the qualifier const must be used in both the declaration and definition. A class can be defined inside a function using the local class concept. So, option d) is false.


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 to open)
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