0% found this document useful (0 votes)
2 views8 pages

C-programming

Uploaded by

Neha Bhati
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
2 views8 pages

C-programming

Uploaded by

Neha Bhati
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 8

C- Programming Interview Questions

1. What are the basic data types in C? TCS, Infosys


The basic data types in C are int (integer), float (floating point), double (double precision
floating point), char (character), and void (empty type, used for functions).

2. What is a pointer, and how is it used in C? Microsoft, Intel


A pointer is a variable that stores the memory address of another variable. Pointers are
used in C for dynamic memory allocation, arrays, structures, and functions, and to
optimize program performance.

3. What is the difference between malloc() and calloc()? Amazon, IBM


a. malloc(size_t size): Allocates a block of memory of specified size but doesn’t
initialize it.
b. calloc(size_t num, size_t size): Allocates memory for an array of num elements
and initializes all bytes to zero.

4. Explain the concept of null pointers. Dell, Capgemini


A null pointer is a pointer that doesn’t point to any memory location. It is often used to
signify that the pointer is not assigned or is used for error handling.

5. What is the difference between == and = in C? Wipro, HCL


a. = is the assignment operator, used to assign the value on the right to the variable
on the left.
b. == is the equality operator, used to compare if two values are equal.

6. How do you pass a pointer to a function in C? Google, Cisco


You pass a pointer to a function by specifying the pointer type in the function’s
parameter list. Example: void function(int *ptr) { /* ... */ }.

7. What is a segmentation fault? Microsoft, Oracle


A segmentation fault occurs when a program tries to access a memory segment that it’s
not allowed to access. This typically happens due to dereferencing a null or uninitialized
pointer or accessing memory out of bounds.

8. How can you determine the size of a data type in C? Qualcomm, Nvidia
The sizeof() operator is used to determine the size (in bytes) of a data type or object in
memory. Example: sizeof(int) returns the size of an integer.

9. What is the use of the const keyword? Adobe, Infosys


The const keyword is used to declare variables whose value cannot be changed after
initialization. It helps in preventing accidental modification of the value.

10. What is the purpose of the static keyword in C? SAP, Accenture


The static keyword has two uses:
a. For local variables, it extends their lifetime to the entire run of the program.
b. For global variables and functions, it restricts their visibility to the file in which
they are declared.

11. How does a function pointer work in C? Google, Amazon


A function pointer points to the address of a function. It allows for functions to be passed
as arguments, stored in arrays, and invoked dynamically. Syntax: return_type
(*pointer_name)(argument_types).

12. What is the difference between struct and union? Microsoft, IBM
a. struct: All members have their own memory locations and can hold different
values simultaneously.
b. union: All members share the same memory location, so only one member can
hold a value at a time.

13. What are storage classes in C? Qualcomm, Cisco


Storage classes define the scope (visibility) and lifetime of variables. Types include auto,
register, static, and extern.

14. Explain how you can handle errors in C programming. Intel, Dell
Errors can be handled using return codes, errno variable, and checking the return values
of functions (e.g., NULL for pointers). Standard library functions like perror() and
strerror() can also help.

15. What is the purpose of the volatile keyword? Nvidia, Samsung


The volatile keyword tells the compiler that a variable may change at any time,
preventing the compiler from optimizing code that reads or writes to it, ensuring the most
up-to-date value is always used.

16. What is a macro, and how is it used in C? Apple, Facebook


A macro is a fragment of code defined using the #define preprocessor directive. It allows
text substitution. Example: #define PI 3.14 will replace PI with 3.14 wherever it appears.

17. What is type casting in C? Provide an example. Wipro, TCS


Type casting is converting a variable from one type to another. Example: float f = 3.14;
int i = (int)f; casts the float f to an integer i, resulting in i = 3.
18. How do you use the sizeof() operator? HCL, Capgemini
sizeof() is used to find the size of a data type or object. Example: sizeof(int) returns the
number of bytes allocated for an integer on that machine.

19. What is the difference between a global and a static variable in C? IBM, Adobe
a. Global variables are accessible throughout the program from any function.
b. Static variables retain their value between function calls but are limited in scope
to the function or file they are defined in.

20. How do you create and use an array of pointers? Amazon, Microsoft
An array of pointers is an array where each element is a pointer. Example: int *arr[3];
creates an array of three integer pointers.

21. How would you implement a linked list in C? Provide a code example. Google,
Facebook
struct Node {
int data;
struct Node* next;
};

struct Node* head = NULL;


struct Node* second = NULL;
head = (struct Node*)malloc(sizeof(struct Node));
second = (struct Node*)malloc(sizeof(struct Node));
head->data = 1;
head->next = second;
second->data = 2;
second->next = NULL;

22. What is dynamic memory allocation? How do you manage it in C? Intel, Qualcomm
Dynamic memory allocation allows allocating memory during runtime using functions
like malloc(), calloc(), realloc(), and deallocating using free(). This helps manage
memory more efficiently.

23. Explain the concept of recursion with an example in C. Amazon, Oracle


Recursion is a function calling itself. Example: Calculating factorial:
int factorial(int n) {
if (n <= 1) return 1;
else return n * factorial(n - 1);
}
24. How can you implement a stack using arrays in C? Cisco, SAP
A stack using arrays involves using an array to store elements and an integer to track the
index of the top element. Example:
#define MAX 100
int stack[MAX];
int top = -1;

void push(int value) {


if (top < MAX - 1) stack[++top] = value;
}

int pop() {
if (top >= 0) return stack[top--];
else return -1;
}

25. What are the advantages and disadvantages of using pointers in C? Dell, Apple
a. Advantages: Direct memory access, efficient array handling, dynamic memory
allocation, and function argument passing.
b. Disadvantages: Risk of memory leaks, dangling pointers, and segmentation
faults.

26. How can you implement a queue using linked lists in C? Nvidia, Microsoft
A queue using linked lists involves nodes pointing to the next node. Operations are
performed at the head and tail:
struct Node {
int data;
struct Node* next;
};

struct Queue {
struct Node *front, *rear;
};

27. What is a binary tree, and how can you implement it in C? Google, IBM
A binary tree is a hierarchical structure with nodes, each having at most two children.
Implementation:
struct Node {
int data;
struct Node *left, *right;
};
struct Node* newNode(int data) {
struct Node* node = (struct Node*)malloc(sizeof(struct Node));
node->data = data;
node->left = node->right = NULL;
return node;
}

28. What is a hash table? How would you implement one in C? Amazon, Facebook
A hash table is a data structure that stores key-value pairs using a hash function.
Implementation involves arrays and linked lists to handle collisions.

29. How do you reverse a linked list in C? Qualcomm, Cisco


By iterating through the list and reversing the direction of pointers:
void reverse(struct Node** head) {
struct Node* prev = NULL;
struct Node* current = *head;
struct Node* next = NULL;
while (current != NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
*head = prev;
}

30. Explain how to detect a loop in a linked list. Microsoft, Apple


Using Floyd’s Cycle-Finding Algorithm: Two pointers move at different speeds. If
there’s a loop, they will eventually meet.

31. What is the use of the extern keyword in C? IBM, SAP


extern is used to declare a global variable or function in another file. It extends the
visibility of the C variables and functions.

32. How do you achieve file handling in C? Dell, Adobe


Using FILE pointers and functions like fopen(), fclose(), fread(), fwrite(), fprintf(),
fscanf(), etc., to handle files.
33. What is a race condition, and how do you prevent it in C? Intel, Nvidia
A race condition occurs when multiple threads access shared data without proper
synchronization, leading to unexpected results. It is prevented using synchronization
mechanisms like mutexes, semaphores, etc.

34. What are semaphores, and how are they used in C? Microsoft, Qualcomm
Semaphores are synchronization tools used to control access to shared resources. They
prevent race conditions by signaling the availability of resources.

35. Explain the difference between a process and a thread. Amazon, Cisco
a. A process is an independent program with its own memory space.
b. A thread is a segment of a process that can run concurrently with other threads
within the same process and shares the same memory space.

36. What is inter-process communication (IPC), and how is it implemented in C?


Google, Apple
IPC allows processes to communicate and synchronize. Common methods include pipes,
message queues, shared memory, and sockets.

37. What is a mutex, and how does it work in C? Nvidia, Microsoft


A mutex is a mutual exclusion object used to prevent multiple threads from
simultaneously accessing a shared resource. It works by locking a section of code so only
one thread can execute it at a time.

38. How do you implement a thread pool in C? IBM, Oracle


A thread pool involves pre-creating a number of threads and assigning tasks from a queue
to available threads. Threads are reused to handle multiple tasks.

39. Explain the producer-consumer problem and how to solve it in C. Dell, Samsung
The producer-consumer problem involves two types of threads: producers, which
generate data, and consumers, which use the data. It is solved using synchronization
mechanisms like semaphores or condition variables to ensure producers don’t overwrite
buffers and consumers don’t read empty buffers.

40. What is a deadlock, and how can it be avoided in C programs? Google, Qualcomm
A deadlock occurs when two or more threads are blocked forever, each waiting for a
resource held by another. It can be avoided using resource hierarchy, timeout, or avoiding
circular wait conditions.

41. How do you measure program performance in C? Facebook, Apple


Program performance can be measured using profiling tools, timers (e.g., clock()), and
analyzing memory usage. Functions like gettimeofday() can also measure execution time.
42. What is memory alignment, and why is it important? Amazon, Intel
Memory alignment refers to aligning data structures to specific memory boundaries. It is
important for performance reasons and to avoid hardware-level faults.

43. What is the purpose of the #pragma directive? SAP, IBM


#pragma provides additional information to the compiler, such as optimization
instructions, warnings suppression, and alignment requirements.

44. How do you handle signal handling in C? Microsoft, Cisco


Signal handling is managed using the signal() function to define a handler for a signal,
and using sigaction() for more complex scenarios. Signals like SIGINT, SIGTERM can
be handled gracefully.

45. What are the differences between heap and stack memory? Qualcomm, Dell
a. Stack Memory: Automatically managed, fast, and used for static memory
allocation (local variables). It has a limited size.
b. Heap Memory: Manually managed (using malloc(), free()), slower than stack,
and used for dynamic memory allocation. It has a larger size.

46. What is the difference between synchronous and asynchronous I/O? Nvidia, Google
a. Synchronous I/O: Operations block the execution until the I/O operation
completes.
b. Asynchronous I/O: Operations allow other processing to continue before the
transmission has finished, improving performance.

47. How do you implement a priority queue in C? Amazon, Facebook


A priority queue can be implemented using a heap data structure. The heap can be a max
heap or a min heap, and insertion and extraction are performed to maintain heap
properties.

48. What are memory leaks, and how can they be detected? Microsoft, Intel
Memory leaks occur when dynamically allocated memory is not freed, leading to wasted
memory. Tools like Valgrind or AddressSanitizer can detect memory leaks.

49. What is endianness, and why does it matter? Qualcomm, Nvidia


Endianness refers to the byte order used to represent data. Little-endian means the least
significant byte is stored first, while big-endian stores the most significant byte first. It
matters for data consistency in systems and network communication.

50. Explain the concept of reentrancy in C. Cisco, Samsung


Reentrancy means that a function can be interrupted in the middle of its execution and
safely called again (“re-entered”) before its previous executions are complete. Reentrant
functions do not rely on static data or shared resources without synchronization.

You might also like