Q1. The class that in C++ for file input is?
Ans:
ifstream
Q2. How would you define a class with a static data member and initialise it correctly?
Ans:
class bad { static I;}
int bad::I=100;
int bad::I=100;
Q3. What is the error in the code snippet below?
int main()
{
int *p;
int q;
p = new int;
delete q;
return 0;
}
Ans:
"p" cannot be initialized to "new int" since "p" is an integer pointer.
Q4.
class Foo {
char *buf;
public:
Foo (char const * b = "default") {
buf = new char[srd::strlen(b)+1];
std::strcpy(buf, b);
}
~Foo() {
delete[] buf;
}
};
Foo Func(Foo f) {
return f;
}
Referring to the code above, when the "func" is called, the program may crash or have any unexpected behaviour. What is the reason of this problem?
Ans:
Try yourself
Q5. Find the o/p:
#include < cstudio >
cclass A {
public:
A() { std::putchar('A); }
~A() { std::putchar('~); std::putchar('A); }
}
class B {
public:
B() { std::putchar('B); }
~B() {std::putchar('~); std::putchar('B); }
}
A a;
static B b;
int main() {
static B b; return 0;
}
Ans:
Try yourself...
Q6.
class A {
public:
A() { std::memset(buf, 0, sizeof(buf)); }
protected:
char buf[256];
};
class B {
public:
B(const A& arg) : a(arg) {} //Ctr1
B(const A& arg) { a = arg;} //Ctr2
protected:
A a;
};
Consider the two constructors given for B in the sample. Only one of the constructors can be used because they have the same signature, which one?
Ans:
Try yourself...
Q7. Choose the correct option:
Mutable keyword can be applied to:
a) non-static data members of a class
b) non-const data members of a class
c) Both a and b
d) None of above
Ans:
Try yourself...
Q8. Choose the correct option:
Objects have
a) Behaviour
b) State
c) both a and b
d) None
Ans:
You tell me...
Q9. The vtable entry for a pure virtual function in C++ is:
a) Zero
b) NULL
c) One
d) No entry in vtable
Ans:
Try yourself
Q10. Find the o/p of this:
main() {
int i;
float *pf;
pf = (float *) &i;
*pf = 100.00;
}
Ans:
Runtime error
Q11. Can we declare a static function as virtual?
Ans:
No.
The virtual function mechanism is used on the specific object that determines which virtual function to call. Since the static functions are not any way related to objects, they can not be declared as virtual.
The virtual function mechanism is used on the specific object that determines which virtual function to call. Since the static functions are not any way related to objects, they can not be declared as virtual.
Q12. Is it compulsory to override a virtual function?
Ans:
No.
If we do not override the virtual function in the derived class then the VTABLE of the derived class would contain the address of the base class virtual function.
If we do not override the virtual function in the derived class then the VTABLE of the derived class would contain the address of the base class virtual function.
Q13.Can we get the typeid of void pointers?
Ans:
No.
At runtime, type identification doesn't work with void pointers, because void * truly means no type information at all.
At runtime, type identification doesn't work with void pointers, because void * truly means no type information at all.