Object Oriented Programming C++ Quiz!

Approved & Edited by ProProfs Editorial Team
The editorial team at ProProfs Quizzes consists of a select group of subject experts, trivia writers, and quiz masters who have authored over 10,000 quizzes taken by more than 100 million users. This team includes our in-house seasoned quiz moderators and subject matter experts. Our editorial experts, spread across the world, are rigorously trained using our comprehensive guidelines to ensure that you receive the highest quality quizzes.
Learn about Our Editorial Process
| By RukminiSN
R
RukminiSN
Community Contributor
Quizzes Created: 1 | Total Attempts: 6,424
Questions: 10 | Attempts: 6,592

SettingsSettingsSettings
Object Oriented Programming C++ Quiz! - Quiz


Questions and Answers
  • 1. 

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

    • A.

      Class C is friend of class A

    • B.

      Class B cannot be a friend of any other class

    • C.

      Class A is friend of class C

    • D.

      None of the above

    Correct Answer
    D. None of the above
    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".

    Rate this question:

  • 2. 

     Where does keyword ‘friend’ should be placed?

    • A.

      Function definition

    • B.

      Main function

    • C.

      Function declaration

    • D.

      None of the mentioned

    Correct Answer
    C. Function declaration
    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.

    Rate this question:

  • 3. 

    How does C++ compiler differs between overloaded postfix and prefix operators?

    • A.

      C++ doesn't allow both operators to be overlaoded in a class

    • B.

      A postfix ++ has a dummy parameter

    • C.

      A prefix ++ has a dummy parameter

    • D.

      By making prefix ++ as a global function and postfix as a member function.

    Correct Answer
    B. A postfix ++ has a dummy parameter
    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.

    Rate this question:

  • 4. 

    Assume a class C with obj1, obj2, and obj3. For the statement obj3 = 1 - obj2 to work correctly, the overloaded operator must:

    • A.

      return a value

    • B.

      Create a named temporary object

    • C.

      Use the object of which it is a member as an operand

    • D.

      Both (a) and (c)

    Correct Answer
    D. Both (a) and (c)
    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.

    Rate this question:

  • 5. 

    Which of the following is true statement?

    • A.

      Friend function can access public data only.

    • B.

      Scope of friend function is limited within the class where it is declared.

    • C.

      A friend function can't be called using the object of the class.

    • D.

      Like class member, it can access the class members directly.

    Correct Answer
    D. Like class member, it can access the class members directly.
    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.

    Rate this question:

  • 6. 

    Predict the output:#include<iostream.h>  class Point{    int x, y;public:   Point(int i = 0, int j = 0) { x = i; y = j; }   int getX() { return x; }   int getY() { return y; }}; int main(){    Point p1;    Point p2 = p1;    cout << "x = " << p2.getX() << " y = " << p2.getY();    return 0;}

    • A.

      Compiler Error

    • B.

      X = 0 y = 0

    • C.

      X = garbage value y = garbage value

    • D.

      None

    Correct Answer
    B. X = 0 y = 0
    Explanation
    The code defines a class named Point with two private variables x and y, and a constructor that sets their initial values to 0. In the main function, an object p1 of class Point is created without any arguments, so the constructor is called and the values of x and y are set to 0. Then, another object p2 is created and assigned the value of p1. Since p1 has x and y values of 0, p2 will also have x and y values of 0. Therefore, when p2.getX() and p2.getY() are called, they will return 0. The output will be "x = 0 y = 0".

    Rate this question:

  • 7. 

    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;}

    • A.

      0

    • B.

      3

    • C.

      10

    • D.

      None of the above

    Correct Answer
    B. 3
    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.

    Rate this question:

  • 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;}

    • A.

      40

    • B.

      80

    • C.

      0

    • D.

      4

    Correct Answer
    B. 80
    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.

    Rate this question:

  • 9. 

    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; }

    • A.

      0

    • B.

      Garbage Value

    • C.

      42

    • D.

      10

    Correct Answer
    C. 42
    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.

    Rate this question:

  • 10. 

    Are these statements "Objects of a class do not share non-static members. Every Object has its own copy." 

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    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.

    Rate this question:

Quiz Review Timeline +

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

  • Current Version
  • Mar 27, 2024
    Quiz Edited by
    ProProfs Editorial Team
  • Nov 03, 2016
    Quiz Created by
    RukminiSN

Related Topics

Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.