C++ MCQs - Classes & Objects

This section focuses on the "Classes And Objects" in C++ programming langauge. These Multiple Choice Questions (MCQ) should be practiced to improve the C++ programming skills required for various interviews (campus interview, walk-in interview, company interview), placement, entrance exam and other competitive examinations.

1. Which of the following is not correct for virtual function in C++ ?.

A. Virtual function can be static.
B. Virtual function should be accessed using pointers
C. Virtual function is defined in base class
D. Must be declared in public section of class

View Answer


2. How can we make a class abstract?

A. By declaring it abstract using the static keyword
B. By declaring it abstract using the virtual keyword.
C. By making at least one member function as pure virtual function
D. By making all member functions constant

View Answer


3. How many specifiers are present in access specifiers in class?

A. 2
B. 1
C. 4
D. 3

View Answer


4. Which of these following members are not accessed by using direct member access operator?

A. Public
B. Private
C. Protected
D. Both B & C

View Answer


5. Which other keywords are also used to declare the class other than class?

A. Struct
B. Union
C. Object
D. Both struct & union

View Answer


6. Which of the following is true?

A. All objects of a class share all data members of class
B. Objects of a class do not share non-static members. Every object has its own copy
C. Objects of a class do not share codes of non-static methods, they have their own copy
D. None of these

View Answer


7. Which of the following can be overloaded?

A. Object
B. Operators
C. Both A & B
D. None of the above

View Answer


8. Which is also called as abstract class?

A. Virtual function
B. Derived class
C. Pure virtual function
D. None of the mentioned

View Answer


9. What is the output of this program?

         
Note:Includes all required header files
using namespace std;
  
class Empty {};
  
int main()
{
  cout << sizeof(Empty);
  return 0;
}

A. A non-zero value.
B. 0
C. Compiler Error
D. Runtime Error

View Answer


10. What is the output of this program?

         
Note:Includes all required header files
using namespace std;
 
class Empty { };
 
class Derived: Empty { int a; };
 
int main()
{
    cout << sizeof(Derived);
    return 0;
}

A. 1
B. 2
C. 4
D. Error

View Answer


11. What is the output of this program?

       
Note:Includes all required header files 
class Test 
{
  int x; 
};

int main()
{
  Test t;
  cout << t.x;
  return 0;
}

A. 0
B. Garbage value
C. Runtime error
D. Complier error

View Answer


12. Assume that an integer and a pointer each takes 4 bytes. Also, assume that there is no alignment in objects. Predict the output following program.

         
Note:Includes all required header files
using namespace std;
 
class Test
{
    static int x;
    int *ptr;
    int y;
};
 
int main()
{
    Test t;
    cout << sizeof(t) << " ";
    cout << sizeof(Test *);
}

A. 12 12
B. 12 4
C. 8 4
D. 8 8

View Answer


13. Which of the following is true about the following program

#include <iostream>
using namespace std;
class LFC 
{ 
public: 
    int i; 
    void get(); 
}; 
void LFC::get() 
{ 
    std::cout << "Enter the value of i: "; 
    std::cin >> i; 
} 
LFC t;  
int main() 
{ 
    LFC t;  
    t.get(); 
    std::cout << "value of i in local t: "<<t.i<<'\n'; 
    ::t.get();  
    std::cout << "value of i in global t: "<<::t.i<<'\n'; 
    return 0; 
}

A. Compiles and runs fine
B. Compiler Error in Line "::t.get();"
C. Compiler Error: Cannot have two objects with same class name
D. Runtime error

View Answer


14. What will be the output of this program?

         
Note:Includes all required header files
using namespace std;
 
class sample
{
    int x;
}
 
int main()
{
    sample obj;
    obj.x=100;
    cout<<"x="<< obj.x; 
}

A. 10
B. 100
C. Error
D. None of the above

View Answer


15. What will be the output of this program?

        
Note:Includes all required header files
using namespace std;
//Empty class
class test
{
};
 
int main()
{
   test testObj;
   cout<<"size ="<< sizeof(testObj);
   return 0;
}

A. Error
B. size =Garbage
C. size =1
D. Compile but no output

View Answer


16. What will be the output of the following program?

#include <iostream>
using namespace std;
class LFC 
{ 
    public: 
      int x; 
}; 
int main() 
{ 
    LFC *p = new LFC(); 

    (*p).x = 5; 
    cout<< (*p).x << " " << p->x << " " ; 

    p->x = 10; 
    cout<< (*p).x << " " << p->x ; 

    return 0; 
} 
 

A. 5 5 10 10
B. Garbage garbage 10 10
C. 5 5 Garbage garbage
D. Garbage garbage Garbage garbage

View Answer


17. How to access the object in the class?

A. Ternary operator
B. Scope resolution operator
C. Direct member access operator
D. None of the above

View Answer


18. When struct is used instead of the keyword class means, what will happen in the program?

A. Access is public by default
B. Access is private by default
C. Access is protected by default
D. None of the mentioned

View Answer


19. Which of the following is not a member of class?

A. Static Function.
B. Friend Function
C. Const Function
D. Virtual Function

View Answer


20. Constructor is executed when _____.

A. An object goes out of scope.
B. A class is declared
C. An object is created
D. An object is used

View Answer


21. How many ways of reusing are there in class hierarchy?

A. 1
B. 3
C. 4
D. 2

View Answer


22. Where does the object is created?

A. Class
B. Constructor
C. Destructors
D. Attributes

View Answer


23. Which of the following is a valid class declaration?

A. Class A { int x; };
B. Class B { }
C. Public class A { }
D. Object A { int x; };

View Answer


24. Which of the following is not correct (in C++) ?i) Class templates and function templates are instantiated in the same way ii) Class templates differ from function templates in the way they are initiated iii) Class template is initiated by defining an object using the template argument iv) Class templates are generally used for storage classes.

A. i
B. i & ii
C. ii ,iii, iv
D. iv

View Answer


25. What will be the output of the following program?

#include <iostream>

using namespace std;
class LFC
{
    static int x; 
    public:
    static void Set(int xx)
    {
        x = xx; 
    }
    void Display() 
    {
        cout<< x ;
    }
};
int LFC::x = 0; 
int main()
{
    LFC::Set(33);
    LFC::Display();
    return 0; 
}

A. The program will print the output 0.
B. The program will print the output 33.
C. The program will print the output Garbage.
D. The program will report compile time error.

View Answer


26. What will be the output of the following program?

         
Note:Includes all required header files
class course
{
    int x, y; 
    public:
    course(int xx)
    {
        x = ++xx;
    }
    void Display()
    {
        cout<< --x << " ";
    }
};
int main()
{
    course obj(20);
    obj.Display();
    int *p = (int*)&obj ;
    *p = 5;
    obj.Display();
    return 0; 
}

A. 20 4
B. 21 4
C. 20 5
D. 21 5

View Answer


27. Which of the following statements is correct when a class is inherited publicly?

A. Public members of the base class become protected members of derived class.
B. Public members of the base class become private members of derived class.
C. Private members of the base class become protected members of derived class.
D. Public members of the base class become public members of derived class.

View Answer


28. What does the cerr represent?

A. Standard error stream
B. Standard logging stream
C. Input stream
D. Output stream

View Answer


29. Which of the following keywords is used to control access to a class member?

A. Default
B. Break
C. Protected
D. Asm

View Answer


30. Which of the following statements is incorrect?

A. Destructor of base class should always be static
B. Destructor of base class should always be virtual.
C. Destructor of base class should not be virtual.
D. Destructor of base class should always be private.

View Answer


31. Which operator can not be overloaded?

A. +
B. ::
C. -
D. *

View Answer


32. When Virtual Table is created?

A. Every Class has VTable
B. Class inherited from other Class
C. Class has atleast one Virtual Function
D. When a Class Overrides the function of Base class

View Answer


33. What is the size of empty class?

A. 0
B. 2
C. 4
D. 1

View Answer





Also check :


Discussion



* You must be logged in to add comment.