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.
Q3. WTF? q cannot be deleted, since it is _not_ a pointer
ReplyDeleteQ4. WTF? What's the deal. Making a site that looks like it useful. Than got lazy about it. Good thing you didnt forget the ads
Q5. WTF? The compiler will error because there is no <cstudio> header; secondly, cclass is not a keyword. Thirdly, `A, `~ and `B are malformed character literals, no matter how many times you use them.
Fourthly, the classes lack trailing `;` (both)
Lastly the whole shebang is unspecified, because the standard doesn't say in which order the global statics will be destructed. This is known as the Static Initialization Fiasco. The output may well become detached before the destructors for ::b and ::a run.
Q6. You can only compile one of the constructors. They both suck equally (since there is no protection against copying A nor B). That left, the initializer list is slightly less deadly - since it will leak 256 fewer bytes at construction. Never mind the leakage on destruction.
Q7. The correct answer isn't listed. It should be, nonstatic, nonconst member _fields_.
Q8. Poor you. And the question doesn't even have anything to do with C++
Q9. Unspecified. There is no vtable in the C++ standard. There might be (usually are) in an implementation, where this then becomes, implementation defined or undefined. I reckon most implementations will store a pointer to an error routine (aborting with 'a pure virtual function was called')
Q10. Runtime error? Most likely Bus Error or Segmentation Fault. That is _not_ a runtime error. Also, technically, it is just undefined behaviour, complete with nasal demons. Oh, and pedantically, there is no output. So the answer is ""
I say, this pop quiz could use some improvement. Good luck.