0% found this document useful (0 votes)
9 views9 pages

PCC VIVA QUESTIONS

The document provides a comprehensive overview of various data structures and algorithms, including stacks, queues, linked lists, binary trees, sorting algorithms, hashing, graphs, and memory management in C. It includes definitions, real-life applications, differences between data structures, and examples of algorithms with their time complexities. Additionally, it covers basic programming concepts, control flow, functions, pointers, file handling, error handling, and practical applications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views9 pages

PCC VIVA QUESTIONS

The document provides a comprehensive overview of various data structures and algorithms, including stacks, queues, linked lists, binary trees, sorting algorithms, hashing, graphs, and memory management in C. It includes definitions, real-life applications, differences between data structures, and examples of algorithms with their time complexities. Additionally, it covers basic programming concepts, control flow, functions, pointers, file handling, error handling, and practical applications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Viva Questions and Answers for Data Structures (Stack, Queue, Linked List, etc.

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:

• push(): Adds an element to the top of the stack.

• pop(): Removes the top element from the stack.

Q: Where is the stack used in real life?

A: The stack is used in situations like:

• Function calls in programming (function call stack).

• Undo operations in text editors.

• Reversing a word or sentence.

Q: What is a stack overflow?

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:

• enqueue(): Adds an element to the end of the queue.

• dequeue(): Removes an element from the front of the queue.

Q: What are the types of queues?

A: There are different types of queues:

• Simple Queue: Standard FIFO queue.

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

Q: Where are queues used in real life?

A: Queues are used in situations such as:

• Managing print jobs in printers.

• Task scheduling in operating systems.


• Customer service lines.

3. Linked List

Q: What is a linked list?

A: A linked list is a linear data structure where each element (node) contains two parts:

1. Data: The actual data.

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.

Q: What are the advantages of using a linked list over an array?

A:

• Dynamic Size: Linked lists can grow and shrink in size dynamically, unlike arrays, which
require a predefined size.

• Efficient Insertions/Deletions: Inserting or deleting elements can be done in constant time,


whereas in an array, it requires shifting elements.

4. Binary Tree

Q: What is a 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.

Q: What are the types of binary trees?

A:

• Full Binary Tree: Every node has 0 or 2 children.

• 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

Q: What is bubble sort?

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.

Q: What is the time complexity of bubble sort?

A: The time complexity of bubble sort in the worst case is O(n²), where n is the number of elements
in the array.

Q: What is quick sort?

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.

Q: What is a hash table?

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

Q: What is the difference between a directed and an undirected graph?

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

Q: What is memory allocation in C?

A: Memory allocation in C is done using functions like malloc(), calloc(), realloc(), and free(). These
functions dynamically allocate memory during runtime.

Q: What is a dangling pointer?

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

Q: What is time complexity?

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

Q: What is space complexity?

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: A pointer is a variable that stores the memory address of another variable.

Q: What are the types of functions in C?

A: In C, functions can be classified into:

• Standard library functions: Predefined functions (e.g., printf(), scanf()).

• User-defined functions: Functions that are created by the programmer.

11. File Handling

Q: How do you open and close a file 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);

Q: What are the different modes to open a file in C?

A: The common modes are:

• "r": Open a file for reading.

• "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).

• "rb", "wb", "ab": Open a file in binary mode.

1. Basic Programming Concepts

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.

Q: What is the difference between ++i and i++?

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.

A: Operators in C are symbols used to perform operations on variables and values.

• Arithmetic operators: +, -, *, /, %.

• Relational operators: ==, !=, >, <, >=, <=.

• Logical operators: && (AND), || (OR), ! (NOT).

2. Control Flow

Q: How does a switch statement work in C? Can you give an example?

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) {

case value1: // block of code;

case value2: // block of code;

default: // block of code;

Q: What is the difference between for, while, and do-while loops?

A:

• for loop: Used when the number of iterations is known beforehand.

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

3. Functions and Recursion

Q: What is a function in C? How do you pass parameters to a function?

A: A function is a block of code that performs a specific task. Parameters can be passed to a function
in two ways:

• Pass-by-value: The function receives a copy of the actual value.

• Pass-by-reference: The function receives the address of the actual value.

Q: Can you explain recursion with an example?

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;

return n * factorial(n - 1);

4. Pointers and Memory Management

Q: What is a pointer? Explain with an example.

A: A pointer is a variable that stores the memory address of another variable. Example:

c
Copy code

int x = 10;

int *ptr = &x; // ptr stores the address of x

Q: How do you allocate memory dynamically in C?

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

free(arr); // Deallocating memory

5. Data Structures

Q: What is a stack? How does it work?

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.

Q: What is a binary search tree (BST)?

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: What is an algorithm? How do you analyze the time complexity?

A: An algorithm is a step-by-step procedure to solve a problem. Time complexity is measured using


Big O notation, which gives an estimate of the runtime of an algorithm based on the size of the input.

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

Q: How is file handling done in C?


A: File handling in C uses functions like fopen(), fclose(), fprintf(), fscanf(), fread(), and fwrite(). Files
can be opened in different modes such as read, write, append, etc.

Copy code

FILE *file = fopen("file.txt", "r"); // Opening a file for reading

fclose(file); // Closing the file

8. Error Handling and Debugging

Q: What is a segmentation fault?

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.

Q: What are common types of errors in C?

A: Common errors include syntax errors, runtime errors (like segmentation faults), and logical errors
(incorrect output despite no program crashes).

9. Practical Application-Based Questions

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.

10. Complexity and Optimization

Q: Can you explain the concept of time complexity with an example?

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

Q: What is space complexity? How is it different from time complexity?

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.

You might also like