Object Oriented Programming C++ Quiz!

  • ISO/IEC 14882
  • ACM/IEEE CS2013
Reviewed by Godwin Iheuwa
Godwin Iheuwa, MS (Computer Science) |
Database Administrator
Review Board Member
Godwin Iheuwa, a Database Administrator at MTN Nigeria, holds an MS in Computer Science, specializing in Agile Methodologies and Database Administration from the University of Bedfordshire and a Bachelor's in Computer Science from the University of Port Harcourt. His proficiency in SQL Server Integration Services (SSIS) and SQL Server Management Studio contributes to his expertise in database management.
, MS (Computer Science)
By RukminiSN
R
RukminiSN
Community Contributor
Quizzes Created: 1 | Total Attempts: 8,504
| Attempts: 8,504 | Questions: 10
Please wait...
Question 1 / 10
0 %
0/100
Score 0/100
1. Are these statements "Objects of a class do not share non-static members. Every Object has its own copy." 

Explanation

In object-oriented programming, each instance of a class (object) typically has its own set of non-static member variables. These member variables represent the object's state, and each object maintains its own copy of these variables. Therefore, changes made to one object's member variables do not affect the member variables of other objects of the same class.

However, it's important to note that static members are shared among all instances of a class. So, if a member variable is declared as static, it will be shared among all objects of that class.

Submit
Please wait...
About This Quiz
Object Oriented Programming C++ Quiz! - Quiz

Dive into the intricacies of Object Oriented Programming in C++ with this focused quiz! Assess your understanding of key concepts like friend classes, operator overloading, and scope resolution. Ideal for learners aiming to sharpen their C++ programming skills and grasp advanced topics effectively.

Tell us your name to personalize your report, certificate & get on the leaderboard!
2. Which of the following is used to define a member function outside of the class in C++?

Explanation

In C++, the scope resolution operator (::) is used to define a member function outside of its class definition. When a function is declared inside a class but defined separately, the :: operator is used to specify that the function belongs to that class. For example, ClassName::FunctionName links the function definition to the class. This allows for better code organization and separation of class declarations and definitions.

Submit
3. Predict the output -    #include <iostream> class Test { public:     int x; }; int main() {     Test t;     t.x = 42;  // Initializing the member variable x     std::cout << t.x;     return 0; }

Explanation



An object of the Test class is created with the variable name t.

The member variable x of the t object is set to the value 42 (t.x = 42;).

The value of t.x (which is now 42) is then printed to the console using std::cout.

The program returns 0, indicating successful execution.
Submit
4. Assume a class C with obj1, obj2, and obj3. For the statement obj3 = 1 - obj2 to work correctly, the overloaded operator must:

Explanation

The correct answer is both (a) and (c). In order for the statement obj3 = 1 - obj2 to work correctly, the overloaded operator must return a value and use the object of which it is a member as an operand. Returning a value ensures that the result of the operation can be assigned to obj3, and using the object as an operand allows the operator to access the necessary data and perform the desired calculation.

Submit
5. Predict the output:#include<iostream.h>  int i;  class A{public:    ~A()    {        i=10;    }};  int foo(){    i=3;    A ob;    return i;}  int main(){    cout << foo() << endl;    return 0;}

Explanation

The output of the code will be 3. The function `foo()` initializes the global variable `i` to 3 and creates an object `ob` of class A. The destructor of class A is called when `foo()` ends, and it sets the value of `i` to 10. However, since `i` is a global variable, its value is not affected by the destructor and remains 3. Finally, the value of `i` is printed in the `main()` function, resulting in the output of 3.

Submit
6.  Where does keyword 'friend' should be placed?

Explanation

The keyword 'friend' should be placed in the function declaration. This is because the 'friend' keyword is used in C++ to grant access to private and protected members of a class to other classes or functions. By placing the 'friend' keyword in the function declaration, we are specifying that the function is a friend of the class and can access its private and protected members.

Submit
7. If class A is friend of class B and if class B is friend of class C, which of the following is true?

Explanation

If class A is a friend of class B and class B is a friend of class C, it does not necessarily mean that class C is a friend of class A. The friendship relationship is not transitive in this case. Therefore, the statement "Class C is a friend of class A" is not true. Additionally, there is no information given about whether class B can be a friend of any other class, so the statement "Class B cannot be a friend of any other class" cannot be determined. Therefore, the correct answer is "None of the above".

Submit
8. Assume that an integer takes 4 bytes and there is no alignment in following classes, predict the output.
#include<iostream>  class base {    int arr[10];}; class b1: public base { }; class b2: public base { }; class derived: public b1, public b2 {}; int main(void){  cout << sizeof(derived);  return 0;}

Explanation

The output of the program is 80. This is because the derived class inherits from both b1 and b2, which in turn inherit from the base class. Each instance of the base class takes up 40 bytes (10 integers * 4 bytes per integer), and since there is no alignment, both b1 and b2 also take up 40 bytes each. Therefore, when the derived class is created, it will have the combined size of all its base classes, which is 80 bytes.

Submit
9. Which of the following is true statement?

Explanation





When a function is declared as a friend within a class, it is granted special access privileges. This means that it can access both public and private members of the class directly, just like any other member function of the class. This feature allows the friend function to manipulate class data effectively, enhancing the flexibility and power of the class design.
Submit
10. How does C++ compiler differs between overloaded postfix and prefix operators?

Explanation

The correct answer is that a postfix ++ operator in C++ has a dummy parameter. This means that when overloading the postfix ++ operator, a dummy parameter is added to the function signature. This is done to differentiate between the prefix and postfix ++ operators, as they have different behaviors and return values. The dummy parameter is not used in the function implementation, but it serves as a way for the compiler to distinguish between the two overloaded operators.

Submit
View My Results
Godwin Iheuwa |MS (Computer Science) |
Database Administrator
Godwin Iheuwa, a Database Administrator at MTN Nigeria, holds an MS in Computer Science, specializing in Agile Methodologies and Database Administration from the University of Bedfordshire and a Bachelor's in Computer Science from the University of Port Harcourt. His proficiency in SQL Server Integration Services (SSIS) and SQL Server Management Studio contributes to his expertise in database management.

Quiz Review Timeline (Updated): Aug 23, 2024 +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Aug 23, 2024
    Quiz Edited by
    ProProfs Editorial Team

    Expert Reviewed by
    Godwin Iheuwa
  • Nov 03, 2016
    Quiz Created by
    RukminiSN
Cancel
  • All
    All (10)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
Are these statements "Objects of a class do not share non-static...
Which of the following is used to define a member function outside of...
Predict the output -  ...
Assume a class C with obj1, obj2, and obj3. For the statement obj3 = 1...
Predict the output:#include<iostream.h>  int...
 Where does keyword 'friend' should be placed?
If class A is friend of class B and if class B is friend of class C,...
Assume that an integer takes 4 bytes and there is no alignment in...
Which of the following is true statement?
How does C++ compiler differs between overloaded postfix and prefix...
Alert!

Advertisement