C PROGRAMMING

C programming interview questions and answers for freshers

C programming interview questions and answers.

Below are 10 basic C programming interview questions and answers that should be prepared and practiced before an interview. I will post Q & A on c arrays, sorting and searching algorithms, loops, operators, user defined functions and other most useful logic in my next post.

C programming interview questions and answers
C programming interview questions and answers

Contents covered

  1. Pointers in c programming
  2. Number patterns in c programming
  3. Searching algorithms in c programming
  4. Sorting algorithms in C programming

 

Pointers in c programming

(C programming interview questions and answers)

1. What is the output of this C code?

A. nullp nullq

B. Depends on the compiler

C. x nullq where x can be p or nullp depending on the value of NULL

D. p q
Answer: Option A

2. What is the output of this C code?

A. Compile time error

B. Segmentation fault/runtime crash

C. 10

D. Undefined behaviour
Answer: Option A

3. What is the output of this C code?

A. Compile time error

B. Undefined behaviour

C. 10

D. 0.000000
Answer: Option D

4. What is the output of this C code?

A. 10

B. Compile time error

C. Segmentation fault/runtime crash since pointer to local variable is returned

D. Undefined behaviour

Answer: Option A

5. What is the output of this C code?

A. 10

B. Compile time error

C. Segmentation fault/runtime crash

D. Undefined behaviour

Answer: Option A

6. What is the output of this C code?

A. 10,10

B. 10,11

C. 11,10

D. 11,11

Answer: Option D

7. Comment on the following?

 

A. You cannot change the value pointed by ptr

B. You cannot change the pointer ptr itself

C. Both (a) and (b)

D. You can change the pointer as well as the value pointed by it

Answer: Option A

8. Which is an indirection operator among the following?

 

A. &

B. *

C. ->

D. .

Answer: Option B

9. Which of the following does not initialize ptr to null (assuming variable declaration of a as int a=0)?

A. int *ptr = &a;

B. int *ptr = &a – &a;

C. int *ptr = a – a;

D. All of the mentioned

Answer: Option A

10. What is the output of this C code?

A. Same address

B. Different address

C. Compile time error

D. Varies

Answer: Option A

Number patterns in c programming

(C programming interview questions and answers)

11. Number Pattern 1

 

12. Number pattern 2

 

13. Number pattern 3

 

14. Number pattern 4

 

15. Number pattern 5

16. Number pattern 6

17. Number pattern 7

18. Number pattern 8

19. Number pattern 9

20.  Number pattern 10

N=39714

Searching algorithms in c programming

(C programming interview questions and answers)

Linear Search

Linear search is the most simple search algorithm. Here each element is traversed one by one linearly. One of the major advantage of this search is that it takes deliberately less time if the element is present at start. But it takes a lot of time is the element is present in the last.

Binary Search

This algorithm is much more faster than linear search. Binary search is faster than linear search but list should be sorted, hashing is faster than binary search and perform searches in constant time.

Sorting algorithms in C programming

Bubble Sort

One of the simplest sorting techniques is used here. In bubble sort sorting of elements start from start of the array to end by comparing and switching each pair of array elements. The comparing and switching of pairs occurs n number of times where n is the length of array.

Insertion Sort

If there are n elements to be sorted. Then, this procedure is repeated n-1 times to get sorted list of array.

Quick Sort

Quick sort is a divide and conquer algorithm. It keeps on diving the main array in to three parts and follow dividing sub arrays :
1.     Elements less than pivot element.
2.     Pivot element.
3.     Elements greater than pivot element.
Then it Recursively sort the sub-list of lesser elements and the sub-list of greater elements.

Merge Sort

Merge Sort is also a divide and algorithm. It divides input array in two halves, calls itself for the two halves and then merges the two sorted halves. The merg() function is used for merging two halves. The merge(arr, l, m, r) is key process that assumes that arr[l..m] and arr[m+1..r] are sorted and merges the two sorted sub-arrays into one.

Heap Sort

In heap sort we build a heap of elements from the given element then partially sort the heap by removing the largest element from the end of the heap. After removing the largest element, it reconstructs the heap, removes the largest remaining item, and places it in the next open position from the end of the partially sorted array. This is repeated until there are no items left in the heap and the sorted array is full.

 

Share

Leave a Comment

Your email address will not be published. Required fields are marked *