Sunday, November 27, 2011

Test your C++ Programming skills Part 1






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:


Q3. What is the error in the code snippet below?
int main()
{
int *p;
int q;
p = new int;
delete q;
return 0;
}

Ans:


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:



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:


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:


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:


Q8. Choose the correct option:
Objects have
a) Behaviour
b) State
c) both a and b
d) None

Ans:


Q9. The vtable entry for a pure virtual function in C++ is:
a) Zero
b) NULL
c) One
d) No entry in vtable

Ans:


Q10. Find the o/p of this:
main() {

   int i;
   float *pf;

   pf = (float *) &i;
   *pf = 100.00;
}

Ans:


Q11. Can we declare a static function as virtual?

Ans:


Q12. Is it compulsory to override a virtual function?

Ans:


Q13.Can we get the typeid of void pointers?

Ans:











1 comment:

  1. Q3. WTF? q cannot be deleted, since it is _not_ a pointer

    Q4. 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.

    ReplyDelete