144 Data Structures (Computer Science) questions asked in DSSSB TGT / PGT previous year papers, each with the correct answer and explanation. Read them free on UTpaper, or attempt the full papers as timed online tests.
Which of the following is an advantage of the linked list over an array?
ALinked list takes less storage to store the n elements as compared to array.
BLinked list takes less time to access the ith element as compared to array.
CInserting an element at beginning of the linked list takes less time as compared to
array.
DInserting an element as last element of the linked list takes less time as compared to
array.
Show answerHide answer
Correct answer: (C) Inserting an element at beginning of the linked list takes less time as compared to
array.
Explanation
Inserting at the beginning of a linked list only changes the head pointer, which is O(1). An array must shift every element, which is O(n). Array access by index is faster.
Which of the following library file functions in C program is/are correct?
fwrite()- This function is used for writing an entire block to a given file.
fseek()- This function is used for setting the file position pointer at the specified byte.
ftell()- This function is used to move the file position pointer to the beginning of the file.
Afwrite(), ftell()
Bfwrite(), fseek()
Cfseek(), ftell()
Dfwrite(), fseek(), ftell()
Show answerHide answer
Correct answer: (B) fwrite(), fseek()
Explanation
fwrite() writes a block of data, and fseek() sets the file position pointer. ftell() only returns the current position; rewind() is the function that moves to the beginning.
A 2D array B[4][5] is stored in row-major order. The base address of the array is 5000, and
each element occupies 2 bytes. What is the address of B[2][3]?
Consider the given memory representation of the singly linked list where start points to the address of first node of the linked list, which is memory location 0.
Memory representation of the singly linked list
Memory location | Item value | Pointer to next element
0 | A | 4
1 | E | 2
2 | B | NULL
3 | |
4 | C | 1
If a new node whose data value is X is inserted after the node whose data value is E in any free location available in the memory, then what will the memory representation be after inserting the new node?
AMemory representation of the linked list after inserting node X
Memory location | Item value | Pointer to next element
0 | A | 4
1 | E | 2
2 | B | NULL
3 | X | 1
4 | C | 3
BMemory representation of the linked list after inserting node X
Memory location | Item value | Pointer to next element
0 | A | 4
1 | E | 3
2 | B | NULL
3 | X | 2
4 | C | 1
CMemory representation of the linked list after inserting node X
Memory location | Item value | Pointer to next element
0 | A | 3
1 | E | 2
2 | B | NULL
3 | X | 4
4 | C | 1
DMemory representation of the linked list after inserting node X
Memory location | Item value | Pointer to next element
0 | A | 4
1 | E | 2
2 | B | 3
3 | X | NULL
4 | C | 1
Show answerHide answer
Correct answer: (B) Memory representation of the linked list after inserting node X
Memory location | Item value | Pointer to next element
0 | A | 4
1 | E | 3
2 | B | NULL
3 | X | 2
4 | C | 1
Explanation
The list is A(0) → C(4) → E(1) → B(2). X goes into free location 3. E's pointer changes to 3, and X points to B at 2, giving A → C → E → X → B as in option B.
Which of the following is INCORRECT about singly linked list data structure?
AIt is a linear data structure.
BThe elements of the linked list are sequentially accessible.
CThe list's head node is the first node, while the last node pointer field points to NULL to
denote the list's end.
DThe elements of the linked list are randomly accessible.
Show answerHide answer
Correct answer: (D) The elements of the linked list are randomly accessible.
Explanation
Option D is incorrect: a singly linked list has no index arithmetic, so you must walk from the head node by node to reach an element. It has sequential access, not random access.
What happens to the stack pointer (SP) when the CALL instruction is executed?
AIt remains unchanged.
BIt points to the next instruction in the subroutine.
CIt increases by 2 (or 4 in 32-bit systems)
DIt decreases by 2 (or 4 in 32-bit systems)
Show answerHide answer
Correct answer: (D) It decreases by 2 (or 4 in 32-bit systems)
Explanation
CALL pushes the return address onto the stack. The stack grows downward, so SP is decreased by 2 (or by 4 on 32-bit systems) before the address is stored.
Calculate the worst-case time complexity of searching for an element in a singly linked list
with 'n' elements.
AO(log 1)
BO(n)
CO(1 log)
DO(1)
Show answerHide answer
Correct answer: (B) O(n)
Explanation
A linked list must be traversed node by node from the head. In the worst case the key is the last node or is missing, which needs n comparisons, so the time is O(n).
What ensures the integrity of a digital signature?
AThe encryption algorithm used
BHash value of the original message
CThe Certificate Authority’s approval
DThe private key of the receiver
Show answerHide answer
Correct answer: (B) Hash value of the original message
Explanation
The sender signs a hash (message digest) of the message. The receiver recomputes the hash and compares it; any change to the message gives a different hash, so the hash value guarantees integrity.
In the context of array types, what is the key difference between int *arr[5] and int (*arr)
[5]?
ABoth are syntactically incorrect
BFirst is a pointer to an array, second is an array of pointers
CThey are identical in their functionality
DFirst is an array of pointers to integers, second is a pointer to an array of integers
Show answerHide answer
Correct answer: (D) First is an array of pointers to integers, second is a pointer to an array of integers
Explanation
[] binds tighter than *, so int *arr[5] is an array of 5 pointers to int. The parentheses in int (*arr)[5] make arr a single pointer to an array of 5 ints.
Which of the following is true about passing arguments by reference in PHP functions?
AArguments passed by reference cannot be modified within the function.
BThe original value of the argument is changed when passed by reference.
CPHP does not support passing arguments by reference.
DArguments passed by reference must be of type array.
Show answerHide answer
Correct answer: (B) The original value of the argument is changed when passed by reference.
Explanation
In PHP, a parameter declared with & (function f(&$x)) is a reference to the caller's variable, so changing it inside the function changes the original value. Any type can be passed by reference.
Suppose there are 15 elements in the array that apply bubble sort technique to sort the
elements of the array. What is the maximum number of comparisons, in worst case,
required to sort the elements of array in ascending order?
A120
B210
C105
D225
Show answerHide answer
Correct answer: (C) 105
Explanation
Bubble sort makes (n-1) + (n-2) + ... + 1 = n(n-1)/2 comparisons in the worst case. For n = 15 that is 15 x 14 / 2 = 105.
Consider a singly linked list containing n elements in which A is the address of the
starting node of the linked list. If we first convert the linked list to an array, then sort the
array elements, and then create a new linked list from the elements of the sorted array,
what is the number of operations required to traverse the linked list and store its data
values in an additional array before applying sorting?
AO(n log n)
BO(log n)
CO(n)
DO(n²)
Show answerHide answer
Correct answer: (C) O(n)
Explanation
Copying the data needs a single pass through the list, visiting each of the n nodes once, so it takes O(n) operations. The later sorting step is what needs O(n log n).
Suppose there are n elements in an array. If we want to delete the beginning element of an array, then how many shift operations are required to fill the gap left by the deleted element?
An*(n − 1)/2
Bn − 1
C1
Dn²
Show answerHide answer
Correct answer: (B) n − 1
Explanation
After deleting a[0], each of the remaining n - 1 elements must move one position left, so n - 1 shift operations are needed.
Consider the given memory representation of singly linked list where start points to the address of the starting node of the linked list, which is in memory location 0.
Memory representation of the linked list
Memory location | Item value | Pointer to next element
0 | A | 4
1 | E | 3
2 | B | NULL
3 | X | 2
4 | C | 1
If you traverse all the elements from the given linked list, then which of the following is correct order?
AA, B, C, E, X
BA, E, B, X, C
CA, C, X, E, B
DA, C, E, X, B
Show answerHide answer
Correct answer: (D) A, C, E, X, B
Explanation
Start at location 0 (A, next 4), then 4 (C, next 1), 1 (E, next 3), 3 (X, next 2), 2 (B, next NULL). So the order is A, C, E, X, B.
Which sorting algorithm performs the worst when the input array is nearly sorted?
ABubble Sort
BInsertion Sort
CHybrid Sort
DMax Sort
Show answerHide answer
Correct answer: (B) Insertion Sort
Explanation
Note: the answer key gives (B), but insertion sort is actually the best choice for a nearly sorted array: each element moves only a few places, so it runs in close to O(n) time. The question seems to have intended "best" rather than "worst".
In C programming, which of the following is correct about why all pointers should be
set to NULL if they are not pointing to anything?
AWhen a pointer is initialized to NULL, it does not help in avoiding dangling pointers
(pointers that reference invalid or deallocated memory).
BSetting pointers to NULL is merely a norm and has no effect on program execution.
CIf a pointer is not pointing to anything, the program won’t execute at all, giving a syntax
error message.
DWhen a pointer is initialized to NULL, it signifies that the pointer does not currently
reference any valid memory location.
Show answerHide answer
Correct answer: (D) When a pointer is initialized to NULL, it signifies that the pointer does not currently
reference any valid memory location.
Explanation
A NULL pointer explicitly marks that the pointer refers to no valid memory, so the code can test if (p == NULL) before use, avoiding accidental use of garbage or dangling addresses.
In an acyclic graph directory structure, what is a common method used to implement shared
directories or files?
AFile duplication
BSymbolic (soft) links
CHard links
DDirectory pointers
Show answerHide answer
Correct answer: (B) Symbolic (soft) links
Explanation
In an acyclic-graph directory, a shared file or subdirectory is usually implemented with a link, a special directory entry (symbolic link) that holds the path of the real file, so the same file appears in several directories without copying.
What does the first dimension and the second dimension represents in a two-dimensional
character array?
AThe first dimension stores the length of each name, and the second stores the number
of names.
BThe first dimension represents the number of names in the array, and the second
dimension represents the maximum length of each name.
CThe first dimension represents the maximum length of the names, and the second
dimension represents the number of names.
DThe first dimension holds the total number of characters, and the second dimension
stores the number of names.
Show answerHide answer
Correct answer: (B) The first dimension represents the number of names in the array, and the second
dimension represents the maximum length of each name.
Explanation
In char names[N][L], the first dimension is the number of strings (rows) and the second is the maximum length of each string, including the null character.
What will be the output of the C program given below?
#include <stdio.h>
int* func()
{
static int arr[5] = { 1, 2, 3, 4, 5 };
return arr;
}
int main()
{
int* ptr = func();
printf("Array Elements: ");
for (int i = 0; i < 5; i++) {
printf("%d ", *ptr);
}
return 0;
}
A1 1 3 4 1
B2 3 4 5 0
C1 2 3 4 5
D1 1 1 1 1
Show answerHide answer
Correct answer: (D) 1 1 1 1 1
Explanation
ptr is never incremented inside the loop, so *ptr always reads arr[0] = 1, printing 1 five times (after "Array Elements: "). To print 1 2 3 4 5 the loop would need ptr[i] or *(ptr + i).
Which sorting technique is best for sorting a linked list with n elements?
AInsertion sort
BBubble sort
CMerge sort
DSelection sort
Show answerHide answer
Correct answer: (C) Merge sort
Explanation
Merge sort suits linked lists: it needs only sequential access, splits the list by moving pointers, merges without extra arrays and runs in O(n log n). Other sorts are O(n²) or need random access.
Which of the following PHP functions is used to sort an array in ascending order without
affecting the keys?
Aksort()
Barray_sort()
Casort()
Dsort()
Show answerHide answer
Correct answer: (C) asort()
Explanation
asort() sorts an array by value in ascending order while keeping each key associated with its value. sort() re-indexes the keys, and ksort() sorts by key.
An array name is not a modifiable lvalue, so a string cannot be assigned with arr = "..." after declaration (and "consultancy" needs 12 chars, more than 6). Strings must be given at initialisation or copied with strcpy.
In singly linked list, each node of the list consists of ___________.
Adata value and address of next node
BNULL and address of the next node
CNULL and address of the previous node
Ddata value and address of previous node
Show answerHide answer
Correct answer: (A) data value and address of next node
Explanation
Each node of a singly linked list holds the data and a pointer (address) to the next node; the last node's pointer is NULL. A previous-node pointer is used in doubly linked lists.
Which of the following is a disadvantage of singly linked list?
AAccessing an element in a singly linked list requires traversal from the head, making it
slower than arrays.
BDeletion operation at the beginning of the linked list is performed in O(1) time.
CInsertion operation at the beginning of the linked list is performed in O (1) time.
DLinked lists have a variable size.
Show answerHide answer
Correct answer: (A) Accessing an element in a singly linked list requires traversal from the head, making it
slower than arrays.
Explanation
To reach the k-th element in a singly linked list you must traverse from the head, O(n), whereas arrays allow direct access by index. O(1) insertion and deletion at the start and variable size are advantages.
Which of the following is true with reference to Queue Data Structure in the C programming language?
(i) Insertion can take place from the front end.
(ii) Deletion can take place from the rear end.
ANeither (i) nor (ii)
BBoth (i) and (ii)
COnly (i)
DOnly (ii)
Show answerHide answer
Correct answer: (A) Neither (i) nor (ii)
Explanation
Neither statement is true. In a queue, insertion (enqueue) happens at the rear and deletion (dequeue) at the front.
In Python language, which of following mode for opening the file, opens the file for
both reading and writing and file pointer will be placed at the start of the file?
Aab
Brb
Cr⁺
Da⁺
Show answerHide answer
Correct answer: (C) r⁺
Explanation
"r+" opens a file for reading and writing with the file pointer at the beginning. "a+" places it at the end; "rb" is read-only and "ab" append-only.
Which of the following is a one-dimensional labelled array capable of holding any data
type (integers, strings, floating point numbers, python objects, etc) in data handling
with pandas?
ASeries
BMissing data
CData frames
DIteration
Show answerHide answer
Correct answer: (A) Series
Explanation
A pandas Series is a one-dimensional labelled array that can hold any data type. A DataFrame is two-dimensional.
What is the main difference between a stack and a queue data structure?
AA queue is more efficient for inserting and deleting elements at any position, while a stack is more efficient for inserting and deleting elements at the front and back.
BA stack is more efficient for inserting and deleting elements at any position, while a queue is more efficient for inserting and deleting elements at the front and back.
CA stack follows a LIFO (last-in, first-out) order, while a queue follows a FIFO (first-in, first-out) order.
DA queue follows a LIFO (last-in, first-out) order, while a stack follows a FIFO (first-in, first-out) order.
Show answerHide answer
Correct answer: (C) A stack follows a LIFO (last-in, first-out) order, while a queue follows a FIFO (first-in, first-out) order.
Explanation
A stack is LIFO: the last element pushed is the first popped. A queue is FIFO: the first element enqueued is the first dequeued.
What is the main difference between a depth-first search (DFS) and a breadth-tint search (BFS) algorithm?
ABFS is used for traversing a tree, while DFS is used for traversing a graph.
BDFS is used for traversing a tree, while BFS is used for traversing a graph.
CDFS explores as far as possible along each branch before backtracking, while BFS visits all the vertices at the current depth before moving on to the next level.
DBFS explores as far as possible along each branch before backtracking, while DFS visits all the vertices at the current depth before moving on to the next level.
Show answerHide answer
Correct answer: (C) DFS explores as far as possible along each branch before backtracking, while BFS visits all the vertices at the current depth before moving on to the next level.
Explanation
DFS goes as deep as possible along one branch before backtracking (using a stack). BFS visits all vertices at the current level before the next level (using a queue).
What are the data structures categorised as a linear list?
ABinary tree
BGraphs
CCircular queue
DArray
Show answerHide answer
Correct answer: (D) Array
Explanation
In a linear list, elements are arranged in a sequence where each has one successor. An array is the classic linear list. Trees and graphs are non-linear structures.
What is the name of the .NET collection class that enables an element to be accessed using a unique key?
ALinked list
BHashtable
CDouble linked list
DArray list
Show answerHide answer
Correct answer: (B) Hashtable
Explanation
The .NET Hashtable class stores key/value pairs and lets each element be accessed by its unique key using hashing. ArrayList and linked lists are accessed by index or position.
How to use these DSSSB TGT / PGT Computer Science questions
Every question here was asked in a real DSSSB TGT / PGT paper; the paper and shift are shown on the card, and the number opens the question on its own page. Try each one before tapping Show answer, then attempt the full DSSSB TGT / PGT papers as timed tests to check your speed under exam conditions.