Monday, December 31, 2012

Top 50 C, C++ Interview Questions asked in Interviews!

Top 50 C, C++ Interview Questions asked in Interviews!
  1. What does static variable mean?
  2. What is a pointer?
  3. What is a structure?
  4. What are enumerations?
  5. Describe about storage allocation and scope of global, extern, static, local and register variables?
  6. What are register variables? What are the advantage of using register variables?
  7. What is difference between Structure and Unions?
  8. What the advantages of using Unions?
  9. What are the advantages of using pointers in a program?
  10. What is the difference between Strings and Arrays?
  11. In a header file whether functions are declared or defined?
  12. What is a far pointer? where we use it?
  13. How will you declare an array of three function pointers where each function receives two ints and returns a float?
  14. What is a NULL Pointer? Whether it is same as an uninitialized pointer?
  15. What is a NULL Macro? What is the difference between a NULL Pointer and a NULL Macro?
  16. What does the error ‘Null Pointer Assignment’ mean and what causes this error?
  17. What is near, far and huge pointers? How many bytes are occupied by them?
  18. How would you obtain segment and offset addresses from a far address of a memory location?
  19. If we want that any wildcard characters in the command line arguments should be appropriately expanded, are we required to make any special provision? If yes, which?
  20. Does there exist any way to make the command line arguments available to other functions without passing them as arguments to the function?
  21. What are bit fields? What is the use of bit fields in a Structure declaration?
  22. To which numbering system can the binary number 1101100100111100 be easily converted to?
  23. Which bit wise operator is suitable for checking whether a particular bit is on or off?
  24. Which bit wise operator is suitable for turning off a particular bit in a number?
  25. Which bit wise operator is suitable for putting on a particular bit in a number?
  26. Which bit wise operator is suitable for checking whether a particular bit is on or off?
  27. Which header file should you include if you are to develop a function which can accept variable number of arguments?
  28. Can you write a function similar to printf()?
  29. How can a called function determine the number of arguments that have been passed to it?
  30. Can there be at least some solution to determine the number of arguments passed to a variable argument list function?
  31. How would you use the functions sin(), pow(), sqrt()?
  32. How would you use the functions memcpy(), memset(), memmove()?
  33. How would you use the functions fseek(), freed(), fwrite() and ftell()?
  34. How would you obtain the current time and difference between two times?
  35. How would you use the functions randomize() and random()?
  36. How would you implement a substr() function that extracts a sub string from a given string?
  37. What is the difference between the functions rand(), random(), srand() and randomize()?
  38. What is the difference between the functions memmove() and memcpy()?
  39. How do you print a string on the printer?
  40. Can you use the function fprintf() to display the output on the screen?
  41. What is a linklist and why do we use it when we have arrays? - I feel the correct answer should be linklist is used in cases where you don’t know the memory required to store a data structure and need to allocate is dynamically on demand.
  42. How do you detect a loop in linked list?
  43. What is the difference between main() in C and main() in C++?
  44. what will be printed out when the following code is executed:
main()
{
printf("%x",-1<<4);
}
  1. In header files whether functions are declared or defined?
  2. What are the differences between malloc() and calloc()?
  3. What are macros? What are the advantages and disadvantages?
  4. Difference between pass by reference and pass by value?
  5. What is static identifier?
  6. Where are the auto variables stored?

Saturday, December 22, 2012

Wishing You a Very Happy BirthDay Dear Sachin Sir.....






                                                                    Mr.Sachin Kapoor



              I found guidance friendship discipline and love, everything, in one person And that person is you
                     

                                                           "Happy Birthday Sachin Sir"

Tuesday, December 11, 2012

Virtual Constructor/Distructor

What is virtual constructors/destructors?
C++ Interview Questions and Answers

 Answer1

Virtual destructors:

If an object (with a non-virtual destructor) is destroyed explicitly by applying the delete operator to a base-class pointer to the object, the base-class destructor function (matching the pointer type) is called on the object.
There is a simple solution to this problem declare a virtual base-class destructor.
This makes all derived-class destructors virtual even though they don’t have the same name as the base-class destructor. Now, if the object in the hierarchy is destroyed explicitly by applying the delete operator to a base-class pointer to a derived-class object, the destructor for the appropriate class is called. Virtual constructor: Constructors cannot be virtual. Declaring a constructor as a virtual function is a syntax error. 

Answer2

Virtual destructors:

 If an object (with a non-virtual destructor) is destroyed explicitly by applying the delete operator to a base-class pointer to the object, the base-class destructor function (matching the pointer type) is called on the object.
There is a simple solution to this problem – declare a virtual base-class destructor. This makes all derived-class destructors virtual even though they don’t have the same name as the base-class destructor. Now, if the object in the hierarchy is destroyed explicitly by applying the delete operator to a base-class pointer to a derived-class object, the destructor for the appropriate class is called.

Virtual constructor: Constructors cannot be virtual. Declaring a constructor as a virtual function is a syntax error. Does c++ support multilevel and multiple inheritance?
Yes.

What are the advantages of inheritance?

• It permits code reusability.
• Reusability saves time in program development.
• It encourages the reuse of proven and debugged high-quality software, thus reducing problem after a system becomes functional.
What is the difference between declaration and definition?
The declaration tells the compiler that at some later point we plan to present the definition of this declaration.
E.g.: void stars () //function declaration
The definition contains the actual implementation.
E.g.: void stars () // declarator
{
for(int j=10; j>=0; j--) //function body
cout<<”*”;
cout<<endl; }

What is the difference between goto and longjmp() and setjmp()?

What is the difference between goto and longjmp() and setjmp()?

 

 

A goto statement implements a local jump of program execution, and the longjmp() and setjmp() functions implement a nonlocal, or far, jump of program execution. 
Generally, a jump in execution of any kind should be avoided because it is not considered good programming practice to use such statements as goto and longjmp in your program. 
A goto statement simply bypasses code in your program and jumps to a predefined position. To use the goto statement, you give it a labeled position to jump to. This predefined position must be within the same function. You cannot implement gotos between functions. 
When your program calls setjmp(), the current state of your program is saved in a structure of type jmp_buf. Later, your program can call the longjmp() function to restore the program’s state as it was when you called setjmp().Unlike the goto statement, the longjmp() and setjmp() functions do not need to be implemented in the same function. 
However, there is a major drawback to using these functions: your program, when restored to its previously saved state, will lose its references to any dynamically allocated memory between the longjmp() and the setjmp(). This means you will waste memory for every malloc() or calloc() you have implemented between your longjmp() and setjmp(), and your program will be horribly inefficient. It is highly recommended that you avoid using functions such as longjmp() and setjmp() because they, like the goto statement, are quite often an indication of poor programming practice.

Wednesday, November 28, 2012

Difference between declaration,assignment & initialization


There is few things in C sound like the same but all are carrying different meaning.
I found many people who have got good programming knowledge of C (i don't... Haha).
But still they are not clear with very basic terminology and believe me, for clearing the interview this is the imp thing that interviewer looks into you .

Sometime what happens .....we know what we have been asked for ....but we start answering something else because we know that thing by some different name(terminology)...

You should be very much clear with the standard terminology that you are going to use in Interview.....

Once one of my friend has gone to interview as fresher in Quinnox Bangalore. He answered some questions well but the moment interviewer asked

Can you please explain the difference between Initialization,Assignment and Declaration???


He was like bla bla bla .....

So it happens in many cases, specially if you are fresher Be ready for such kind of question.....

Anyway we are discussing here:

Initialization , assignment , declaration

******************************************************
declaration means creating a variable, each variable that are used in program must declare.

Let's assume we are creating one variable.


                int a;


so this comes under variable declaration......
we can say we are creating a new variable....
creating variable is local word don't use in interview.
we are just declaring it so that we can use it in further part of programming...
once we declare the variable we can't declare the same variable again otherwise compiler will get ambiguity..

later if we need to put some value inside this variable....
So we go like this...
 
 

              a=10;


This is purely assignment....and assignment can be done n no. of times in a program.there is no such restriction on it.

Assignment: throwing away the old value of a variable and replacing it with a new one.It can be done anywhere in the whole program....


"=" is known as assignment operator(right to left).

In this example earlier "a" was holding garbage value.now we replaced it with 10.

 

Initialization: Declaring a variable and assigning a value at the declaration time only is called initializing a variable


eg:

       

        int a=10;      //initializing variable....

      

Initialization = declaration + assignment    at single step.....


Hope that Diagram makes you more clear about these 3 term.


In later post we'll discuss about
Function calling
Function Definition
and Function declaration


and of course how many ways to read ( int *p;) in front of interviewer.

Sunday, November 25, 2012

Variable in C

Variable in C


First thing in any programming language is Variable because in any program of any language
we must require variable for keeping the data and proceeding it further so let's understand What variable is...


A variable is named location of data having one address provided by compiler. In other word we can say variable is container (storage area) of data.

What is Data ?

Any thing which we can store ,can be taken as data like (any number,any character, any string ,any file/img etc.)
 
In real world we have used various type containers for specific purpose. For example we have used suitcase to store
clothes, match box to store match sticks etc. In the same way variables are used to store
data.

For example integer variables are used to store integer kind of value ,char variables is used to store characters etc. On the basis of how many data a variable will store, we can categorize the all C variable in three groups.

(a)Variables which can store only one data at time. Example: integer variables, char variables, pointer variables etc.

(b)Variables which can store more than one data of similar type at a time. Example: array variables

(c) Variables, which can store more than one value of dissimilar type at a time. Example: structure or union variables.

Properties of variable in c:

Every variable in C have three most fundamental attributes. They are:

1. Name

2. Value

3. Address
Name of a variable:

 Every variable in C has its own name. A variable without any name is not possible in C.
 Most important properties of variables name are its unique names.
Not two variables in C can have same name with same visibility.
if at all we dare to have the same name of two variable compiler will confuse while processing its data
so it will throw compile time error.
 

For example:


#include<stdio.h>
int main()
{
    auto int a=5;   //Visibility is within main block
    static int a=10; //Visibility is within main block

/* Two variables of same name */
    printf("%d",a);
    return 0;
}


Output: compilation error




But it is possible that two variable with same name but different visibility. In this case variable name can access only that variable which is more local. In C there is not any way to access global variable if any local variable is present of same name. For example:

(a)

#include<stdio.h>
int a=50; //Visibility is whole the program

int main()
{
    int a=10; //Visibility within main block
    printf("%d",a);
    return 0;
}

Output: 10


(b)

#include<stdio.h>

int main()
{
    int a=10; //Visibility within main block.
    {
        a+=5; //Accessing outer local variable a.
        int a=20; //Visibility within inner block.
        a+=10; //Accessing inner local variable a.   
        printf(“%d”,a);//Accessing inner local variable a.
    }

    printf(“%d”,a); //Accessing outer local variable a.
    return 0;
}

Output: 30 15

Address of the variable we need not to worry about that because it automatically allots by the compiler
we can not predict it...although we can also play with addresses once we enter into pointer concept(Back Bone) of C Laungauge...
Welcome everyone in this blog ..This blog is basically for those who are keen to learn C laungauge in easy steps by sachin sir.

About Sachin Sir:

He is the well known faculty in bhopal for his great contribution towards teaching C C++ and other courses.
His way of teaching is absolutely perfect there is no doubt in it..He actually understands in what way student can get the point and exactly in that way he starts teaching the subject.This is something for which every student looks to it .

Every Engineering student in Bhopal believes that it is just like a wasting time if you haven't learn C C++ by sachin sir after spending 4yr of you engg.