Monday, January 5, 2009

C/C++ Questions List Ask in Interview

C/C++ Questions

1.    What is the output of printf("%d")

2.    What will happen if I say delete this

3.    Difference between "C structure" and "C++ structure".

4.    Diffrence between a "assignment operator" and a "copy constructor"

5.    What is the difference between "overloading" and "overridding"?

6.    Explain the need for "Virtual Destructor".

7.    Can we have "Virtual Constructors"?

8.    What are the different types of polymorphism?

9.    What are Virtual Functions? How to implement virtual functions in "C"

10.                       What are the different types of Storage classes?

11.                       What is Namespace?

12.                       What are the types of STL containers?.

13.                       Difference between "vector" and "array"?

14.                       How to write a program such that it will delete itself after exectution?

15.                       Can we generate a C++ source code from the binary file?

16.                       What are inline functions?

17.                       Talk sometiming about profiling?

18.                       How many lines of code you have written for a single program?

19.                       What is "strstream" ?

20.                       How to write Multithreaded applications using C++?

21.                       Explain "passing by value", "passing by pointer" and "passing by reference"

22.                       Write any small program that will compile in "C" but not in "C++"

23.                       Have you heard of "mutable" keyword?

24.                       What is a "RTTI"?

25.                       Is there something that I can do in C and not in C++?

26.                       Why preincrement operator is faster than postincrement?

27.                       What is the difference between "calloc" and "malloc"?

28.                       What will happen if I allocate memory using "new" and free it using "free" or allocate sing "calloc" and free it using "delete"?

29.                       What is Memory Alignment?

30.                       Explain working of printf.

31.                       Difference between "printf" and "sprintf".

32.                       What is "map" in STL?

33.                       When shall I use Multiple Inheritance?

34.                       What are the techniques you use for debugging?

35.                       How to reduce a final size of executable?

36.                       Give 2 examples of a code optimization.

 

 

Some companies certainly ask for these puzzles. Specially Microsoft. Here are my favorite puzzles. 

1.       Write a "Hello World" program in 'C' without using a semicolon.

2.       Write a C++ program without using any loop (if, for, while etc) to print numbers from 1 to 100 and 100 to 1;

3.       C/C++ : Exchange two numbers without using a temporary variable.

4.       C/C++ : Find if the given number is a power of 2.

5.       C/C++ : Multiply x by 7 without using multiplication (*) operator.

6.       C/C++ : Write a function in different ways that will return f(7) = 4 and f(4) = 7

7.       Remove duplicates in array

8.       Finding if there is any loop inside linked list.

9.       Remove duplicates in an no key access database without using an array

10.   Convert (integer) number in binary without loops.

11.   Write a program whose printed output is an exact copy of the source. Needless to say, merely echoing the actual source file is not allowed.

12.   From a 'pool' of numbers (four '1's, four '2's .... four '6's), each player selects a number and adds it to the total. Once a number is used, it must be removed from the pool. The winner is the person whose number makes the total equal 31 exactly.

13.   Given an array (group) of numbers write all the possible sub groups of this group.

C/C++ Questions

These are sample questions. Personally I hate asking questions related to code. If you want some old question papers of some Indian IT industries , You many find it here

  1. What is the output of printf("%d")
  2. What will happen if I say delete this
  3. Difference between "C structure" and "C++ structure".
  4. Diffrence between a "assignment operator" and a "copy constructor"
  5. What is the difference between "overloading" and "overridding"?
  6. Explain the need for "Virtual Destructor".
  7. Can we have "Virtual Constructors"?
  8. What are the different types of polymorphism?
  9. What are Virtual Functions? How to implement virtual functions in "C"
  10. What are the different types of Storage classes?
  11. What is Namespace?
  12. What are the types of STL containers?.
  13. Difference between "vector" and "array"?
  14. How to write a program such that it will delete itself after exectution?
  15. Can we generate a C++ source code from the binary file?
  16. What are inline functions?
  17. Talk sometiming about profiling?
  18. How many lines of code you have written for a single program?
  19. What is "strstream" ?
  20. How to write Multithreaded applications using C++?
  21. Explain "passing by value", "passing by pointer" and "passing by reference"
  22. Write any small program that will compile in "C" but not in "C++"
  23. Have you heard of "mutable" keyword?
  24. What is a "RTTI"?
  25. Is there something that I can do in C and not in C++?
  26. Why preincrement operator is faster than postincrement?
  27. What is the difference between "calloc" and "malloc"?
  28. What will happen if I allocate memory using "new" and free it using "free" or allocate sing "calloc" and free it using "delete"?
  29. What is Memory Alignment?
  30. Explain working of printf.
  31. Difference between "printf" and "sprintf".
  32. What is "map" in STL?
  33. When shall I use Multiple Inheritance?
  34. What are the techniques you use for debugging?
  35. How to reduce a final size of executable?
  36. Give 2 examples of a code optimization.
C++ 
  1. What is a class?
  2. What is an object?
  3. What is the difference between an object and a class?
  4. What is the difference between class and structure?
  5. What is public, protected, private?
  6. What are virtual functions?
  7. What is friend function?
  8. What is a scope resolution operator?
  9. What do you mean by inheritance?
  10. What is abstraction?
  11. What is polymorphism? Explain with an example.
  12. What is encapsulation?
  13. What do you mean by binding of data and functions?
  14. What is function overloading and operator overloading?
  15. What is virtual class and friend class?
  16. What do you mean by inline function?
  17. What do you mean by public, private, protected and friendly?
  18. When is an object created and what is its lifetime?
  19. What do you mean by multiple inheritance and multilevel inheritance? Differentiate between them.
  20. Difference between realloc() and free?
  21. What is a template?
  22. What are the main differences between procedure oriented languages and object oriented languages?
  23. What is R T T I ?
  24. What are generic functions and generic classes?
  25. What is namespace?
  26. What is the difference between pass by reference and pass by value?
  27. Why do we use virtual functions?
  28. What do you mean by pure virtual functions?
  29. What are virtual classes?
  30. Does c++ support multilevel and multiple inheritance?
  31. What are the advantages of inheritance?
  32. When is a memory allocated to a class?
  33. What is the difference between declaration and definition?
  34. What is virtual constructors/destructors?
  35. In c++ there is only virtual destructors, no constructors. Why?
  36. What is late bound function call and early bound function call? Differentiate.
  37. How is exception handling carried out in c++?
  38. When will a constructor executed?
  39. What is Dynamic Polymorphism? 
  40. Write a macro for swapping integers.
  41. How do you link a C++ program to C functions?
  42. Explain the scope resolution operator.
  43. What are the differences between a C++ struct and C++ class?
  44. How many ways are there to initialize an int with a constant?
  45. How does throwing and catching exceptions differ from using setjmp and longjmp?
  46. What is a default constructor?
  47. What is a conversion constructor?
  48. What is the difference between a copy constructor and an overloaded assignment operator?
  49. When should you use multiple inheritance?
  50. What is a virtual destructor?
  51. Explain the ISA and HASA class relationships. How would you implement each in a class design?
  52. When is a template a better solution than a base class?
  53. What is a mutable member?
C++
  1. What is an explicit constructor?
  2. What is the Standard Template Library?
  3. Describe run-time type identification.
  4. What problem does the namespace feature solve?
  5. Write a function of factorial
  6. Use recursion to write a function of factorial.
  7. Write a function that reverse the words in the sentence ,for example  "This is a string "   becomes  "string a is This".
  8. You have a linked list. How can you tell that there is no cycling in it?
  9. How would you write a program to find a file on disk. 
  10. What is the difference between basic building blocks in C and C++? 
  11. What is virtual destructor and when do you need to use it?"
  12. Write code for Reverse linked list.
  13. What is the purpose of the preprocessor directive #error?
  14. How do you code an infinite loop in C ?
  15. What are the uses of the keyword static?

DATA STRUCTURE

  1. What is a data structure?
  2. What does abstract data type means?
  3. Evaluate the following prefix expression  " ++ 26 + - 1324" (Similar types can be asked)
  4. Convert the following infix expression to post fix notation  ((a+2)*(b+4)) -1  (Similar types can be asked)
  5. How is it possible to insert different type of elements in stack?
  6. Stack can be described as a pointer. Explain.
  7. Write a Binary Search program
  8. Write programs for Bubble Sort, Quick sort
  9. Explain about the types of linked lists
  10. How would you sort a linked list?
  11. Write the programs for Linked List (Insertion and Deletion) operations
  12. What data structure would you mostly likely see in a non recursive implementation of a recursive algorithm?
  13. What do you mean by Base case, Recursive case, Binding Time, Run-Time Stack and Tail Recursion?
  14. Explain quick sort and merge sort algorithms and derive the time-constraint relation for these.
  15. Explain binary searching, Fibinocci search.
  16. What is the maximum total number of nodes in a tree that has N levels? Note that the root is level (zero)
  17. How many different binary trees and binary search trees can be made from three nodes that contain the key values 1, 2 & 3?
  18. A list is ordered from smaller to largest when a sort is called. Which sort would take the longest time to execute?
  19. A list is ordered from smaller to largest when a sort is called. Which sort would take the shortest time to execute?
  20. When will you  sort an array of pointers to list elements, rather than sorting the elements themselves?
  21. The element being searched for is not found in an array of 100 elements. What is the average number of comparisons needed in a sequential search to determine that the element is not there, if the elements are completely unordered?
  22. What is the average number of comparisons needed in a sequential search to determine the position of an element in an array of 100 elements, if the elements are ordered from largest to smallest?
  23. Which sort show the best average behavior?
  24. What is the average number of comparisons in a sequential search?

DATA STRUCTURE

  1. Which data structure is needed to convert infix notations to post fix notations?
  2. What do you mean by:
    • Syntax Error
    • Logical Error
    • Runtime Error

How can you correct these errors?

  1. In which data structure, elements can be added or removed at either end, but not in the middle?
  2. How will inorder, preorder and postorder traversals print the elements of a tree?
  3. Parenthesis are never needed in prefix or postfix expressions. Why?

Which one is faster? A binary search of an orderd set of elements in an array or a sequential search of the elements

Tricky C Questions

1.    How do you write a program which produces its own source code as its output?

2.    How can I find the day of the week given the date?

3.    Why doesn’t C have nested functions?

4.    What is the most efficient way to count the number of bits which are set in a value?

5.    How can I convert integers to binary or hexadecimal?

6.    How can I call a function, given its name as a string?

7.    How do I access command-line arguments?

8.    How can I return multiple values from a function?

9.    How can I invoke another program from within a C program?

10.                        How can I access memory located at a certain address?

11.                        How can I allocate arrays or structures bigger than 64K?

12.                        How can I find out how much memory is available?

13.                        How can I read a directory in a C program?

14.                        How can I increase the allowable number of simultaneously open files?

What’s wrong with the call fopen(”c:\newdir\file.dat

Good C interview Questions

15.                        How do you decide which integer type to use?

16.                        What should the 64-bit integer type on new, 64-bit machines be?

17.                        What’s the best way to declare and define global variables?

18.                        What does extern mean in a function declaration?

19.                        What’s the auto keyword good for?

20.                        I can’t seem to define a linked list node which contains a pointer to itself.

21.                        How do I declare an array of N pointers to functions returning pointers to functions returning pointers to characters?

22.                        How can I declare a function that returns a pointer to a function of its own type?

23.                        My compiler is complaining about an invalid redeclaration of a function, but I only define it once and call it once. What’s happening?

24.                        What can I safely assume about the initial values of variables which are not explicitly initialized?

25.                        Why can’t I initialize a local array with a string?

26.                        What is the difference between char a[] = “string”; and char *p = “string”; ?

27.                        How do I initialize a pointer to a function?

 

C interview questions

28.                        What does static variable mean?

29.                        What is a pointer?

30.                        What is a structure?

31.                        What are the differences between structures and arrays?

32.                        In header files whether functions are declared or defined?

33.                        What are the differences between malloc() and calloc()?

34.                        What are macros? What are the advantages and disadvantages?

35.                        Difference between pass by reference and pass by value?

36.                        What is static identifier?

37.                        Where are the auto variables stored?

38.                        Where does global, static, local, register variables, free memory and C Program instructions get stored?

39.                        Difference between arrays and linked list?

40.                        What are enumerations?

41.                        Describe about storage allocation and scope of global, extern, static, local and register variables?

42.                        What are register variables? What are the advantage of using register variables?

43.                        What is the use of typedef?

44.                        Can we specify variable field width in a scanf() format string? If possible how?

45.                        Out of fgets() and gets() which function is safe to use and why?

46.                        Difference between strdup and strcpy?

47.                        What is recursion?

48.                        Differentiate between a for loop and a while loop? What are it uses?

49.                        What are the different storage classes in C?

50.                        Write down the equivalent pointer expression for referring the same element a[i][j][k][l]?

51.                        What is difference between Structure and Unions?

52.                        What the advantages of using Unions?

53.                        What are the advantages of using pointers in a program?

54.                        What is the difference between Strings and Arrays?

55.                        In a header file whether functions are declared or defined?

56.                        What is a far pointer? where we use it?

57.                        How will you declare an array of three function pointers where each function receives two ints and returns a float?

58.                        What is a NULL Pointer? Whether it is same as an uninitialized pointer?

59.                        What is a NULL Macro? What is the difference between a NULL Pointer and a NULL Macro?

60.                        What does the error ‘Null Pointer Assignment’ mean and what causes this error?

61.                        What is near, far and huge pointers? How many bytes are occupied by them?

62.                        How would you obtain segment and offset addresses from a far address of a memory location?

63.                        Are the expressions arr and *arr same for an array of integers?

64.                        Does mentioning the array name gives the base address in all the contexts?

65.                        Explain one method to process an entire string as one unit?

66.                        What is the similarity between a Structure, Union and enumeration?

67.                        Can a Structure contain a Pointer to itself?

68.                        How can we check whether the contents of two structure variables are same or not?

69.                        How are Structure passing and returning implemented by the complier?

70.                        How can we read/write Structures from/to data files?

71.                        What is the difference between an enumeration and a set of pre-processor # defines?

72.                        What do the ‘c’ and ‘v’ in argc and argv stand for?

73.                        Are the variables argc and argv are local to main?

74.                        What is the maximum combined length of command line arguments including the space between adjacent arguments?

75.                        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?

76.                        Does there exist any way to make the command line arguments available to other functions without passing them as arguments to the function?

77.                        What are bit fields? What is the use of bit fields in a Structure declaration?

78.                        To which numbering system can the binary number 1101100100111100 be easily converted to?

79.                        Which bit wise operator is suitable for checking whether a particular bit is on or off?

80.                        Which bit wise operator is suitable for turning off a particular bit in a number?

81.                        Which bit wise operator is suitable for putting on a particular bit in a number?

82.                        Which bit wise operator is suitable for checking whether a particular bit is on or off?

83.                        Which one is equivalent to multiplying by 2?

1.    Left shifting a number by 1

2.    Left shifting an unsigned int or char by 1?

84.                        Write a program to compare two strings without using the strcmp() function.

85.                        Write a program to concatenate two strings.

86.                        Write a program to interchange 2 variables without using the third one.

87.                        Write programs for String Reversal. The same for Palindrome check.

88.                        Write a program to find the Factorial of a number.

89.                        Write a program to generate the Fibonacci Series?

90.                        Write a program which employs Recursion?

91.                        Write a program which uses command line arguments.

92.                        Write a program which uses functions like strcmp(), strcpy(), etc.

93.                        What are the advantages of using typedef in a program?

94.                        How would you dynamically allocate a one-dimensional and two-dimensional array of integers?

95.                        How can you increase the size of a dynamically allocated array?

96.                        How can you increase the size of a statically allocated array?

97.                        When reallocating memory if any other pointers point into the same piece of memory do you have to readjust these other pointers or do they get readjusted automatically?

98.                        Which function should be used to free the memory allocated by calloc()?

99.                        How much maximum can you allocate in a single call to malloc()?

100.                    Can you dynamically allocate arrays in expanded memory?

101.                    What is object file? How can you access object file?

102.                    Which header file should you include if you are to develop a function which can accept variable number of arguments?

103.                    Can you write a function similar to printf()?

104.                    How can a called function determine the number of arguments that have been passed to it?

105.                    Can there be at least some solution to determine the number of arguments passed to a variable argument list function?

106.                    How do you declare the following:

1.    An array of three pointers to chars

2.    An array of three char pointers

3.    A pointer to array of three chars

4.    A pointer to function which receives an int pointer and returns a float pointer

5.    A pointer to a function which receives nothing and returns nothing

107.                    What do the functions atoi(), itoa() and gcvt() do?

108.                    Does there exist any other function which can be used to convert an integer or a float to a string?

109.                    How would you use qsort() function to sort an array of structures?

110.                    How would you use qsort() function to sort the name stored in an array of pointers to string?

111.                    How would you use bsearch() function to search a name stored in array of pointers to string?

112.                    How would you use the functions sin(), pow(), sqrt()?

113.                    How would you use the functions memcpy(), memset(), memmove()?

114.                    How would you use the functions fseek(), freed(), fwrite() and ftell()?

115.                    How would you obtain the current time and difference between two times?

116.                    How would you use the functions randomize() and random()?

117.                    How would you implement a substr() function that extracts a sub string from a given string?

118.                    What is the difference between the functions rand(), random(), srand() and randomize()?

119.                    What is the difference between the functions memmove() and memcpy()?

120.                    How do you print a string on the printer?

121.                    Can you use the function fprintf() to display the output on the screen?

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

123.                    How do you detect a loop in linked list?

124.                    What is the difference between main() in C and main() in C++?

125.                     what will be printed out when the following code is executed:

main()
{
printf("%x",-1<<4);
}

 

 

No comments:

Blog List