PCC VIVA QUESTIONS
PCC VIVA QUESTIONS
1. Stack
Q: What is a stack?
A: A stack is a linear data structure that follows the Last In, First Out (LIFO) principle. The last
element inserted is the first to be removed. It has two main operations:
A: A stack overflow occurs when more elements are pushed onto the stack than the stack can hold,
causing a memory error.
2. Queue
Q: What is a queue?
A: A queue is a linear data structure that follows the First In, First Out (FIFO) principle. The first
element inserted is the first to be removed. It has two primary operations:
• Circular Queue: A queue that wraps around when the end is reached.
• Priority Queue: Elements are dequeued based on priority rather than order of insertion.
3. Linked List
A: A linked list is a linear data structure where each element (node) contains two parts:
2. Next Pointer: A reference (or pointer) to the next node in the list. It supports efficient
insertion and deletion at any point in the list.
Q: What is the difference between singly linked list and doubly linked list?
A:
• Singly Linked List: Each node has a pointer to the next node.
• Doubly Linked List: Each node has two pointers: one to the next node and one to the
previous node, making traversal in both directions possible.
A:
• Dynamic Size: Linked lists can grow and shrink in size dynamically, unlike arrays, which
require a predefined size.
4. Binary Tree
A: A binary tree is a tree data structure where each node has at most two children, referred to as the
left and right children.
A:
• Complete Binary Tree: All levels are completely filled except possibly the last one, which is
filled from left to right.
• Perfect Binary Tree: All internal nodes have two children, and all leaf nodes are at the same
level.
Q: What is the difference between a binary tree and a binary search tree (BST)?
A:
• Binary Tree: A tree where each node can have at most two children, but there are no
constraints on the values of the nodes.
• Binary Search Tree: A binary tree where for every node, the left child has a smaller value and
the right child has a larger value than the node.
5. Sorting Algorithms
A: Bubble sort is a simple sorting algorithm that repeatedly compares adjacent elements and swaps
them if they are in the wrong order. This process is repeated for each element until the array is
sorted.
A: The time complexity of bubble sort in the worst case is O(n²), where n is the number of elements
in the array.
A: Quick sort is a divide-and-conquer sorting algorithm that divides the array into smaller sub-arrays
based on a pivot element and then recursively sorts the sub-arrays.
6. Hashing
Q: What is hashing?
A: Hashing is a technique used to convert a given key into an index (or hash code) in a hash table,
allowing for efficient data retrieval. It is used in operations like searching, insertion, and deletion.
A: A hash table is a data structure that stores key-value pairs, where a hash function computes an
index to store the value.
7. Graphs
Q: What is a graph?
A: A graph is a non-linear data structure that consists of a set of nodes (vertices) and edges
(connections between nodes).
A:
• Directed Graph: The edges have a direction, meaning they go from one vertex to another.
• Undirected Graph: The edges do not have a direction, meaning the relationship between
nodes is bidirectional.
Q: What is a weighted graph?
A: A weighted graph is a graph in which each edge has an associated weight (or cost), which
represents the cost to traverse that edge.
8. Memory Management
A: Memory allocation in C is done using functions like malloc(), calloc(), realloc(), and free(). These
functions dynamically allocate memory during runtime.
A: A dangling pointer is a pointer that does not point to a valid memory location. It typically happens
after a memory location has been freed but the pointer still holds the address of the freed memory.
9. Algorithm Analysis
A: Time complexity represents how the running time of an algorithm grows as the size of the input
increases. It's typically expressed using Big-O notation (e.g., O(n), O(log n)).
A: Space complexity represents the amount of memory an algorithm uses in relation to the size of
the input.
10. Miscellaneous
Q: What is a pointer in C?
A: In C, files are opened using the fopen() function, and closed using the fclose() function.
Copy code
FILE *file = fopen("filename.txt", "r");
fclose(file);
• "w": Open a file for writing (creates a new file or truncates existing file).
• "a": Open a file for appending (creates a new file if it doesn't exist).
Q: What are variables and data types? Can you explain their importance?
A: A variable is a memory location where data is stored. Data types specify the kind of data a variable
can store. In C, basic data types include int, float, char, etc. Variables are important for storing and
manipulating data during program execution.
A: ++i is a pre-increment operator, which increments the value of i before it is used in an expression.
i++ is a post-increment operator, which increments the value of i after it is used in an expression.
Q: What are operators in C? Explain arithmetic, relational, and logical operators with examples.
• Arithmetic operators: +, -, *, /, %.
2. Control Flow
A: A switch statement is used to execute one of the multiple possible blocks of code based on the
value of an expression.
c
Copy code
switch (expression) {
A:
• while loop: Used when the number of iterations is not known and is based on a condition.
• do-while loop: Similar to while, but ensures that the loop body executes at least once.
A: A function is a block of code that performs a specific task. Parameters can be passed to a function
in two ways:
A: Recursion is when a function calls itself to solve a smaller instance of the problem. Example:
Factorial using recursion:
Copy code
int factorial(int n) {
if (n == 0) return 1;
A: A pointer is a variable that stores the memory address of another variable. Example:
c
Copy code
int x = 10;
A: Memory can be allocated dynamically using functions like malloc(), calloc(), and freed using free().
Copy code
int *arr = (int*) malloc(5 * sizeof(int)); // Allocating memory for an array of 5 integers
5. Data Structures
A: A stack is a linear data structure that follows the Last In, First Out (LIFO) principle. Operations
include push() to add an element and pop() to remove an element from the top of the stack.
Q: What is a queue? Explain the difference between a normal queue and a circular queue.
A: A queue is a linear data structure that follows the First In, First Out (FIFO) principle.
• Normal queue: When the queue is full, new elements can't be added.
• Circular queue: When the queue is full, it uses the free spaces at the front of the queue to
add new elements.
A: A binary search tree (BST) is a tree data structure in which each node has at most two children,
and the left child’s key is smaller than the parent’s key, and the right child’s key is larger.
6. Algorithms
Q: Can you explain the bubble sort algorithm? What is its time complexity?
A: Bubble sort repeatedly compares and swaps adjacent elements if they are in the wrong order. Its
time complexity is O(n²) in the worst case.
7. File Handling
Copy code
A: A segmentation fault occurs when a program tries to access restricted or invalid memory. This
often happens when you try to dereference a NULL pointer or access memory outside of the
allocated range.
A: Common errors include syntax errors, runtime errors (like segmentation faults), and logical errors
(incorrect output despite no program crashes).
Q: Can you explain a real-world scenario where a circular queue would be useful?
A: A circular queue is used in situations like managing processes in a CPU where the first process that
arrives is the first to be executed, but once a process completes, the CPU can start executing the next
process in a continuous loop.
Q: How would you implement a linked list to store a list of students’ names and roll numbers?
A: A linked list can be used where each node contains a student’s name and roll number, and the
next pointer links the student to the next node in the list. The linked list allows dynamic memory
allocation as students are added or removed.
A: Time complexity represents the amount of time an algorithm takes to complete, relative to the
size of the input. For example, the time complexity of a linear search is O(n), while binary search has
a time complexity of O(log n).
A: Space complexity refers to the amount of memory used by an algorithm. Time complexity
measures how the running time of an algorithm increases with input size, while space complexity
measures how memory usage increases with input size.