c++ Technical mcq

C++ Constructors

Which of the followings is/are automatically added to every class, if we do not write our own.
(A) Copy Constructor
(B) Assignment Operator
(C) A constructor without any parameter
(D) All of the above

Explanation: In C++, if we do not write our own, then compiler automatically creates a default constructor, a copy constructor and a assignment operator for every class.

Output of following program?
class Point {
    Point() { cout << "Constructor called"; }
};                                        
 
int main()
{
   Point t1;
   return 0;
}
(A) Compiler Error
(B) Runtime Error
(C) Constructor called


Explanation: By default all members of a class are private. Since no access specifier is there for Point(), it becomes private and it is called outside the class when t1 is constructed in main.

class Point {
public:
    Point() { cout << "Constructor called"; }
};
 
int main()
{
   Point t1;
   return 0;
}
output: Constructor called

class Point {
public:
    Point() { cout << "Constructor called"; }
};
 
int main()
{
   Point t1, *t2;
   return 0;
}

output: Constructor called

normal contructor call ke liye :

1. sirf Point t;
2. yadi koi * se declare to: e.g.
Point *t; t=new Point();

copy contructor call ke liye :

jb * se declare tbhi hoga : e.g.
method 1:
Point *t1,*t2; t2=new Point(*t1);

method 2:
Point *t1; Point *t2=new Point(*t1);

method 3:

Point *t1; Point t2=*t1;


class Point {
public:
    Point() { cout << "Normal Constructor called\n"; }
    Point(const Point &t) { cout << "Copy constructor called\n"; }
};
  
int main()
{
   Point *t1, *t2; // No constructor call
   t1 = new Point();  //Normal Constructor called
   t2 = new Point(*t1);  //Copy constructor called
   Point t3 = *t1;  // Copy constructor called
   Point t4;    //Normal Constructor called
   t4 = t3;    //  Assignment operator call
   return 0;
}

int main()
{
     Point *t1;

    Point *t2=new Point(*t1);
   return 0;
}


_______________________
class Temp 
{
public:
    int val;
};
 
int main()
{
   Temp a;
   a={10};
   cout<<a.val;
    return 0;
}
output : 10

class Temp 
{
public:
    int val;
};
 
int main()
{
   Temp a= {10};

   cout<<a.val;
    return 0;
}
output : 10

class Temp 
{
public:
    int val;
};
 
int main()
{
   Temp a= {10};
   Temp b=a;

   cout<<b.val;
    return 0;
}
output : 10

What is the output of following program?


class Point
{
    int x, y;
public:
   Point(const Point &p) { x = p.x; y = p.y; }
   int getX() { return x; }
   int getY() { return y; }
};
  
int main()
{
    Point p1;
    Point p2 = p1;
    cout << "x = " << p2.getX() << " y = " << p2.getY();
    return 0;
}

(A) x = garbage value y = garbage value
(B) x = 0 y = 0
(C) Compiler Error


Explanation: There is compiler error in line “Point p1;”. The class Point doesn’t have a constructor without any parameter. If we write any constructor, then compiler doesn’t create the default constructor.


class Point
{
    int x, y;
public:
   Point(int i = 0int j = 0) { x = i; y = j; }
   int getX() { return x; }
   int getY() { return y; }
};
  
int main()
{
    Point p1;
    Point p2 = p1;
    cout << "x = " << p2.getX() << " y = " << p2.getY();
    return 0;
}
(A) Compiler Error
(B) x = 0 y = 0
(C) x = garbage value y = garbage value

Explanation: Compiler creates a copy constructor if we don’t write our own. Compiler writes it even if we have written other constructors in class. So the above program works fine. Since we have default arguments, the values assigned to x and y are 0 and 0
Predict the output of following program.
class Test
{
public:
   Test()
   { cout << "Constructor called"; }
};
  
int main()
{
    Test *= (Test *malloc(sizeof(Test));
    return 0;
}

(A) Constructor called
(B) Empty
(C) Compiler Error
(D) Runtime error

Explanation: Unlike new, malloc() doesn’t call constructor (See this)

If we replace malloc() with new, the constructor is called, see this.

class Test
{
public:
   Test()
   { cout << "Constructor called"; }
};
  
int main()
{
    Test *= new Test();
    return 0;
}
output: Constructor called


class Test
{
public:
  Test() { cout << "Hello from Test() "; }
} a;

int main()
{
  cout << "Main Started ";
  return 0;
}

 Output : Hello from Test() Main Started

There is a global object ‘a’ which is constructed before the main functions starts, so the constructor for a is called first, then main()’ execution begins.


class base
{
public:
       base()
       {          
           cout<<"base constructor"<<endl;  // 1
       }
       ~base()
       {
     cout<<"base Destructor "<<endl;   //4
       }
};
class derivedpublic base
{
public:
       derived()
       {     cout<<"Derived constructor "<<endl;  //2
       }
       ~derived()
       {     cout<<"Derived Destructor "<<endl;  //3
       }
};
int main()
{
derived object;
return 0
}


output:   

base constructor

Derived constructor 

Derived Destructor 

base Destructor 


-------





Other


How many loops are there in C++ 98?

(A) 2

(B) 3
(C) 4
(D) 1

Ans: B

Hint:
There are three types in loops in C++ 98. After this C++ version, for loop with ranges was added in C++ 11.

?:   it is ternary operator   


What is size of int data type in cpp?

(D) Depends on Compiler

Ans: D

Hint:
If someone ask you the size of int, then you should say it depends on compiler. If it is 16 bits compiler like Turbo C++, the size is 2 bytes while if it is 32 bit compiler like Dev-C++, g++ or Visual Studion, the size is 4 bytes.

Which operator can not be overloaded?

(A) +
(B) -
(C) *
(D) ::

Hint:
The operators :: (scope resolution), . (member access), .* (member access through pointer to member), and ?: (ternary conditional) cannot be overloaded.

Which operator has more precedance  in below list?

(A) +
(B) -
(C) ++
(D) *

Ans: C

Which operator has highest precedence in * / % ?

(A) *
(B) /
(C) %
(D) all have same precedence

Ans: D

  int a=10;
  int b,c;
  b = a++;  // b me 10 jakr, a ab 11
  c = a;     // kyu ki a ab 11 ho gya isliye c me gya 11
  cout<<a<<" "<<b<<" "<<c; 

11 10 11

Can a Structure contain pointer to itself?

(A) Yes
(B) No
(C) Compilation Error
(D) Runtime Error

Ans: A

What is size of void in bytes?    ans is 0

int main()
{
 cout<<"Ankit";;;;;;;;;;;;;;;;;;;;;;;;;; 
 return 0;                                                                          
}

output: Ankit

What should be the output?

int main() {
    int new = -10;
    cout<<"new is: "<<new;
    return 0;
}
 Compilation Error 
kyu ki new operator ko var name nhi de skte n, iski jagah valid var use kr lo, -10 mil jayega output me

int main()
{
    int a = 1;
    switch(a)
    {
    case 1: cout<<"One";
    case 2: cout<<"Two";
    case 3: cout<<"Three";
    default: cout<<"Default";
    }
    return 0;
}
output :  OneTwoThreeDefault 
kyu ki break; use nhi hua case me

When Virtual Table is created?

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

Ans: D

What is the size of empty class?

Empty class means, a class that does not contain any data members (e.g. int a , float b, char c and string d etc.) However, an empty class may contain member functions.

In C++, Size of empty structure/class is of one byte , to make them distinguishable.

According to C standard, it was decided to keep size of empty structure to zero. 

Can a class contain another class in it ?
yes, it is called nested class 

NULL and Void Pointer 

Can we assign null to void pointer?

(A) No
(B) Yes

Ans: B

Exceptions are Caught at ?

(A) Compilation Time
(B) Run Time
(C) Linking Time
(D) No

Ans: B

What is the difference between using #define and const for creating a constant? Does any have a performance advantage over the other? 

Naturally I prefer using the const but I'm going to consider the #define if it has suitable advantages.

Is there any difference in below two statements?


int a = 10;

int a(10);

(A) Yes
(B) No

Ans: B

Can we reference a pointer

(A) Yes
(B) No

Ans: B

Pointers: A pointer is a variable that holds memory address of another variable. A pointer needs to be de referenced with * operator to access the memory location it points to.
References: A Reference can be called as a constant pointer that becomes de referenced implicitly. When we access the reference it means we are accessing the storage.

Dangling, Void, Null and Wild Pointers in C/C++


Only the member functions or the friend functions are allowed to access the private data members of a class. 

---------------------





Comments

Popular posts from this blog

c++ oops

Takeoff (hackerearth.3, datastructure, array 1-D)

Aptitude tricks