Class and Object

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


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;
}
Tick
A non-zero value
Cross
0
Cross
Compiler Error
Cross
Runtime Error


Question 3
class Test {
    int x; 
};
int main()
{
  Test t;
  cout << t.x;
  return 0;
}
Cross
0
Cross
Garbage Value
Tick
Compiler Error


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?
Cross
All objects of a class share all data members of class
Tick
Objects of a class do not share non-static members. Every object has its own copy.
Cross
Objects of a class do not share codes of non-static methods, they have their own copy
Cross
None of the above


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 *);
}
Cross
12 4
Cross
12 12
Tick
8 4
Cross
8 8


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
Cross
Compiler Error: Cannot have two objects with same class name
Cross
Compiler Error in Line \"::t.get();\"
Tick
Compiles and runs fine


Question 6-Explanation: 
The above program compiles & runs fine. Like variables it is possible to create 2 objects having same name & in different scope.
Question 7
A member function can always access the data in __________ , (in C++).
Tick
the class of which it is member
Cross
the object of which it is a member
Cross
the public part of its class
Cross
the private part of its class


Question 7-Explanation: 
A member function can access it\'s class member variables, irrespective of the access specifier in which the member variable is declared.So, a member function can always access the data in the class of which it is a member. So, option (A) is correct.
Question 8

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

Cross

Must be declared in public section of class.

Tick

Virtual function can be static.

Cross

Virtual function should be accessed using pointers.

Cross

Virtual function is defined in base class.



Question 8-Explanation: 

(B) Virtual functions cannot be declared as static. Static member functions in C++ do not participate in dynamic binding (polymorphism), which is a fundamental feature of virtual functions. Static functions are resolved at compile time based on the type of the pointer or reference, whereas virtual functions are resolved at runtime based on the actual type of the object they're invoked on.

So, the correct choice is (B) Virtual function can be static.

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
Cross
(1)
Cross
(2), (4)
Tick
(2), (3), (4)
Cross
(4)


Question 9-Explanation: 

In C++ class template and function, template are similar in the way they are initiated. Class template are not used for storage class. Class templates and function templates are instantiated in the same way and Class template is not initiated by defining an object using the template. 
So (2), (3), (4) are not correct in C++. 
So, option (C) is correct.

Question 10
Which of the following cannot be passed to a function in C++ ?
Cross
Constant
Cross
Structure
Cross
Array
Tick
Header file


Question 10-Explanation: 
Header file can not be passed to a function in C++. While array, constant and structure can be passed into a function. So, option (D) is correct.
There are 17 questions to complete.


  • Last Updated : 28 Sep, 2023

Share your thoughts in the comments
Similar Reads