Tuesday, September 3, 2013

What is the difference between the declaration and the definition of a variable?

The definition is the one that actually allocates space, and provides an initialization
value, if any.

There can be many declarations, but there must be exactly one definition. A definition
tells the compiler to set aside storage for the variable. A declaration makes the
variable known to parts of the program that may wish to use it. A variable might
be defined and declared in the same statement. 

Saturday, August 31, 2013

Basic Questions on Classes and Objects

What happens when we try to compile the class definition in following code snippet?

class Birds {};
class Peacock : protected Birds {};

A. It will not compile because class body of Birds is not defined.
B. It will not compile because class body of Eagle is not defined.
C. It will not compile because a class cannot be protectedly inherited from other class.
D. It will compile succesfully. (ANS)


Which of the following statements is incorrect?

A. Friend keyword can be used in the class to allow access to another class.
B. Friend keyword can be used for a function in the public section of a class.
C. Friend keyword can be used for a function in the private section of a class.
D. Friend keyword can be used on main(). (ANS)


Which of the following statement is correct regarding destructor of base class?

A. Destructor of base class should always be static.
B. Destructor of base class should always be virtual. (ANS)
C. Destructor of base class should not be virtual.
D. Destructor of base class should always be private.


Which of the following statements is correct when a class is inherited publicly?

A. Public members of the base class become protected members of derived class.
B. Public members of the base class become private members of derived class.
C. Private members of the base class become protected members of derived class.
D. Public members of the base class become public members of derived class. (ANS)


Which of the following statements about virtual base classes is correct?

A. It is used to provide multiple inheritance.
B. It is used to avoid multiple copies of base class in derived class. (ANS)
C. It is used to allow multiple copies of base class in a derived class.
D. It allows private members of the base class to be inherited in the derived class.


Which of the following can be overloaded?

A. Object
B. Functions
C. Operators
D. Both B and C  (ANS)





Basic Questions of C++

 
Which of the following statement is correct?

A. C++ allows static type checking.
B. C++ allows dynamic type checking.


Which of the following factors supports the statement that reusability is a desirable feature of a language?
A. It decreases the testing time.
B. It lowers the maintenance cost.

Which of the following is a mechanism of static polymorphism?

A. Operator overloading
B. Function overloading
C. Templates

What happens if the base and derived class contains definition of a function withsame prototype?

Base class object will call base class function and derived class object will call derived class function.

In which of the following a virtual call is resolved at the time of compilation?

A. From inside the destructor.
B. From inside the constructor.

Which one of the following is the correct way to declare a pure virtual function?

virtual void Display(void) = 0;

Which of the following keyword is used to overload an operator?

operator

What will happen if a class is not having any name?

A. It cannot have a destructor.
B. It cannot have a constructor.


What is friend function?

Friend function can access public data members of the class.
Friend function can access protected data members of the class.
Friend function can access private data members of the class.

Which of the following is used to make an abstract class?

A. Declaring it abstract using static keyword.
B. Declaring it abstract using virtual keyword.
C. Making at least one member function as virtual function.
D. Making at least one member function as pure virtual function. (Ans D)

What is correct about the static data member of a class?

A. A static member function can access only static data members of a class.
B. A static data member is shared among all the object of the class.
C. A static data member can be accessed directly from main().
D. Both A and B. (Ans)

Friday, August 30, 2013

What are Trigraph characters?

These are used when you keyboard does not support some special characters


    ??=    #
    ??(    [
    ??)    ]
    ??<    {
    ??>    }
    ??!    |
    ??/    \
    ??'    ^
    ??-    ~

What does the typedef keyword do?

This keyword provides a short-hand way to write variable declarations. It is not
a true data typing mechanism, rather, it is syntactic "sugar coating".

For example


typedef struct node
{
  int value;
  struct node *next;
}mynode;


This can later be used to declare variables like this


mynode *ptr1;

and not by the lengthy expression

struct node *ptr1;



There are three main reasons for using typedefs:


 * It makes the writing of complicated declarations a lot easier. This helps in eliminating
a lot of clutter in the code.
 * It helps in achieving portability in programs. That is, if we use typedefs for
data types that are machine dependent, only the typedefs need to change when the
program is ported to a new platform.
 * It helps in providing better documentation for a program. For example, a node
of a doubly linked list is better understood as ptrToList than just a pointer to
a complicated structure. 

Does C have boolean variable type?

No, C does not have a boolean variable type. One can use ints, chars, #defines or
enums to achieve the same in C.


#define TRUE 1
#define FALSE 0

enum bool {false, true};


An enum may be good if the debugger shows the names of enum constants when examining
variables.

Do Global variables start out as zero?

Uninitialized variables declared with the "static" keyword are initialized to zero.
Such variables are implicitly initialized to the null pointer if they are pointers,
and to 0.0F if they are floating point numbers.

Local variables start out containing garbage, unless they are explicitly initialized.


Memory obtained with malloc() and realloc() is likely to contain junk, and must be
initialized. Memory obtained with calloc() is all-bits-0, but this is not necessarily
useful for pointer or floating-point values (This is in contrast to Global pointers
and Global floating point numbers, which start as zeroes of the right type).