Exercise: Questions on Class and Object


Questions on Class and Object : Question 1 :
What is the difference between struct and class in C++?

All members of a structure are public and structures don't have constructors and destructors
Members of a class are private by default and members of struct are public by default. When deriving a struct from a class/struct, default access-specifier for a base class/struct is public and when deriving a class, default access specifier is private.
All members of a structure are public and structures don't have virtual functions
All of the above
Show Answer
Questions on Class and Object : Question 2 :
Predict the output of following C++ program
#include<iostream>
using namespace std;
 
class Empty {};
 
int main()
{
  cout << sizeof(Empty);
  return 0;
}

A non-zero value
0
Compiler Error
Runtime Error
Show Answer
Questions on Class and Object : Question 3 :
class Test {
    int x; 
};
int main()
{
  Test t;
  cout << t.x;
  return 0;
}

0
Garbage Value
Compiler Error
Show Answer
Questions on Class and Object : Question 4 :
Which of the following is true?

All objects of a class share all data members of class
Objects of a class do not share non-static members. Every object has its own copy.
Objects of a class do not share codes of non-static methods, they have their own copy
None of the above
Show Answer
Questions on Class and Object : Question 5 :
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.
#include<iostream>
using namespace std;

class Test
{
    static int x;
    int *ptr;
    int y;
};

int main()
{
    Test t;
    cout << sizeof(t) << " ";
    cout << sizeof(Test *);
}

12 4
12 12
8 4
8 8
Show Answer
Questions on Class and Object : Question 6 :
Which of the following is true about the following program
#include <iostream>
class Test
{
public:
    int i;
    void get();
};
void Test::get()
{
    std::cout << "Enter the value of i: ";
    std::cin >> i;
}
Test t;  // Global object
int main()
{
    Test t;  // local object
    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;
}
Contributed by Pravasi Meet

Compiler Error: Cannot have two objects with same class name
Compiler Error in Line "::t.get();"
Compiles and runs fine
Show Answer
Questions on Class and Object : Question 7 :
A member function can always access the data in __________ , (in C++).

the class of which it is member
the object of which it is a member
the public part of its class
the private part of its class
Show Answer
Questions on Class and Object : Question 8 :
Which of the following is not correct for virtual function in C++ ?

Must be declared in public section of class.
Virtual function can be static.
Virtual function should be accessed using pointers.
Virtual function is defined in base class.
Show Answer
Questions on Class and Object : Question 9 :
Which of the following is not correct (in C++) ?
  1. Class templates and function templates are instantiated in the same way
  2. Class templates differ from function templates in the way they are initiated
  3. Class template is initiated by defining an object using the template argument
  4. Class templates are generally used for storage classes

(1)
(2), (4)
(2), (3), (4)
(4)
Show Answer
Questions on Class and Object : Question 10 :
Which of the following cannot be passed to a function in C++ ?

Constant
Structure
Array
Header file
Show Answer