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

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.

What is the difference between enumeration variables and the preprocessor #defines?


Functionality                                                 Enumerations                       #defines

Numeric values assigned automatically?             YES                            NO
Can the debugger display the symbolic values?     YES                            NO
Obey block scope?                                               YES                               NO
Control over the size of the variables?               NO                            NO

Is char a[3] = "abc"; legal?

It declares an array of size three, initialized with the three characters 'a', 'b',
and 'c', without the usual terminating '\0' character. The array is therefore not
a true C string and cannot be used with strcpy, printf %s, etc. But its legal. 

Declare an array with only one element and still access elements beyond the first element (in a valid fashion)?

There is a way to do this. Using structures.


struct mystruct {
  int  value;
  int length;
  char string[1];
};


Now, when allocating memory to the structure using malloc(), allocate more memory
than what the structure would normally require!. This way, you can access beyond
string[0] (till the extra amount of memory you have allocated, ofcourse).

But remember, compilers which check for array bounds carefully might throw warnings.
Also, you need to have a length field in the structure to keep a count of how big
your one element array really is :).

A cleaner way of doing this is to have a pointer instead of the one element array
and allocate memory for it seperately after allocating memory for the structure.



struct mystruct {
  int  value;
 char *string; // Need to allocate memory using malloc() after allocating memory
for the strucure.
};

What is the difference between char *a and char a[]?

There is a lot of difference!


char a[] = "string";
char *a = "string";


The declaration char a[] asks for space for 7 characters and see that its known by
the name "a". In contrast, the declaration char *a, asks for a place that holds a
pointer, to be known by the name "a". This pointer "a" can point anywhere. In this
case its pointing to an anonymous array of 7 characters, which does have any name
in particular. Its just present in memory with a pointer keeping track of its location.




char a[] = "string";

   +----+----+----+----+----+----+------+
a: | s  |  t |  r |  i |  n |  g | '\0' |
   +----+----+----+----+----+----+------+
   a[0] a[1] a[2] a[3] a[4] a[5] a[6]


char *a = "string";

+-----+          +---+---+---+---+---+---+------+
|  a: | *======> | s | t | r | i | n | g | '\0' |
+-----+          +---+---+---+---+---+---+------+
Pointer          Anonymous array



It is curcial to know that a[3] generates different code depending on whether a is
an array or a pointer. When the compiler sees the expression a[3] and if a is an
array, it starts at the location "a", goes three elements past it, and returns the
character there. When it sees the expression a[3] and if a is a pointer, it starts
at the location "a", gets the pointer value there, adds 3 to the pointer value, and
gets the character pointed to by that value. 

If a is an array, a[3] is three places past a. If a is a pointer, then a[3] is three
places past the memory location pointed to by a. In the example above, both a[3]
and a[3] return the same character, but the way they do it is different! 

Doing something like this would be illegal.


char *p = "hello, world!";
p[0] = 'H';