1

Class and Object

Question 1
What is the difference between struct and class in C++?
A
All members of a structure are public and structures don't have constructors and destructors
B
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.
C
All members of a structure are public and structures don't have virtual functions
D
All of the above
Class and Object    
Discuss it


Question 1 Explanation: 
Question 2
Predict the output of following C++ program
#include<iostream>
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
Class and Object    
Discuss it


Question 3
class Test {
    int x; 
};
int main()
{
  Test t;
  cout << t.x;
  return 0;
}
A
0
B
Garbage Value
C
Compiler Error
Class and Object    
Discuss it


Question 3 Explanation: 
In C++, the default access is private. Since x is a private member of Test, it is compiler error to access it outside the class.
Question 4
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 the above
Class and Object    
Discuss it


Question 4 Explanation: 
Every object maintains a copy of non-static data members. For example, let Student be a class with data members as name, year, batch. Every object of student will have its own name, year and batch. On a side note, static data members are shared among objects. All objects share codes of all methods. For example, every student object uses same logic to find out grades or any other method.
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 *);
}
A
12 4
B
12 12
C
8 4
D
8 8
Class and Object    
Discuss it


Question 5 Explanation: 
For a compiler where pointers take 4 bytes, the statement "sizeof(Test *)" returns 4 (size of the pointer ptr). The statement "sizeof(t)" returns 8. Since static is not associated with each object of the class, we get (8 not 12).
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
A
Compiler Error: Cannot have two objects with same class name
B
Compiler Error in Line "::t.get();"
C
Compiles and runs fine
Class and Object    
Discuss it


Question 6 Explanation: 
The above program compiles & runs fine. Like variables it is possible to create 2 objects having same name & in different scope.
There are 6 questions to complete.
1

Company Wise Coding Practice    Topic Wise Coding Practice