Classes & Objects

Last Updated :
Discuss
Comments

Question 1

What will be the output?

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

  • A

    0

  • B

    Garbage Value

  • C

    Compiler Error

  • D

    None of the Above

Question 2

What does the this pointer in C++ represent?

  • A

    Pointer to class name

  • B

    Pointer to current object

  • C

    Pointer to base class

  • D

    Pointer to constructor

Question 3

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. CPP
#include
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

Question 4

Which of the following is true about the following program

CPP
#include 
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;
}
  • A

    Compiler Error: Cannot have two objects with same class name

  • B

    Compiler Error in Line "::t.get();"

  • C

    Compiles and runs fine

  • D

    Runtime Error

Question 5

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

Question 6

What is the correct way to define a class in C++?

  • A

    class MyClass { int x; void func(); }

  • B

    class MyClass { public: int x; void func(); };

  • C

    class MyClass { public: int x; void func(); }

  • D

    class MyClass { public int x; void func(); };

Question 7

In C++, which operator is used to access a member function or variable from an object?

  • A

    ->

  • B

    ::

  • C

    .

  • D

    *

Question 8

Given the following class:

C++
class A {
  int x;
public:
  A(int v) { x = v; }
  int getX() { return x; }
};

What will A obj(5); cout << obj.getX(); print?

  • A

    0

  • B

    5

  • C

    Compiler error

  • D

    Garbage value

Tags:

There are 8 questions to complete.

Take a part in the ongoing discussion