c++ Technical mcq
A 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.
(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.
normal contructor call ke liye :
copy contructor call ke liye :
(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.
(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
(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.
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.
output:
base constructor
Derived constructor
Derived Destructor
base Destructor
Other
(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
(A) *
(B) /
(C) %
(D) all have same precedence
Ans: D
(A) Yes
(B) No
(C) Compilation Error
(D) Runtime Error
Ans: A
(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.
(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.
Comments
Post a Comment