C-programming
C-programming
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.
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.
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.
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;
};
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.
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.
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.
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.
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.
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.