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 file stream function will move the file pointer to Nth record in a binary file F created with the help of objects of class EMP?
AF.tellg(N)
BF.seekp(N)
CF.seekp((N−1)* sizeof(EMP))
DF.seekp(sizeof(EMP)/N)
Show answerHide answer
Correct answer: (C) F.seekp((N−1)* sizeof(EMP))
Explanation
Records are stored one after another, so the Nth record begins after N−1 records, at byte offset (N−1)*sizeof(EMP). seekp() moves the put pointer there; tellg() only reports the current position.
Consider the following expression tree.
Tree: root is '+'; its left child is 'a' and its right child is '*'; the '*' node has left child 'b' and right child 'c'.
In-order, pre-order and post-order traversal of this tree will result into which of the following expressions, respectively.
The diagram below represents
Figure: a log-scale graph of 'Transistors per chip' (10³ to 10⁹) on the vertical axis against years 1970 to 2010 on the horizontal axis; the plotted points rise along a straight line on the log scale (doubling at regular intervals), ending at a '1 billion transistor CPU' near 2010.
AMoore' law.
BNewton Raphson method.
CBoyle's law.
DGregor law.
Show answerHide answer
Correct answer: (A) Moore' law.
Explanation
The log-scale graph shows transistors per chip doubling at regular intervals, which is Moore's law.
In the context of Visual Basic, multiple controls of the same type can be grouped into an array, in the same manner as a collection of data items. Such a grouping is known as :
AControl array
BPrimary array
CSecondary array
DAn integer array
Show answerHide answer
Correct answer: (A) Control array
Explanation
A control array is a group of controls of the same type that share one name and are told apart by an Index property.
The five items P,Q,R,S and T are pushed in a stack, one after the other starting from P. The stack is popped four times and each element is inserted in a queue. Then two elements are deleted from the queue and pushed back on the stack. Now one item is popped from the stack, The popped item is :
AS
BP
CQ
DR
Show answerHide answer
Correct answer: (A) S
Explanation
After pushing P to T, popping four times sends T, S, R, Q to the queue, leaving P on the stack. Dequeuing two gives T and S, which are pushed back (stack P, T, S). The next pop returns S.
The concatenation of two lists is to be performed in O(1) time. Which of the following implementations of lists could be used?
ASingly linked list
BDoubly linked list
CCircular doubly linked list
DArray implementation of list
Show answerHide answer
Correct answer: (C) Circular doubly linked list
Explanation
In a circular doubly linked list the last node is reached directly through head->prev. So two lists can be joined by changing a few pointers in O(1) time, without traversing.
Which topologies of networks is represented in the following diagram ?
Diagram: a main cable with two computers attached to it branches out into three separate cables (lines); each branch cable has several computers attached to it (4 on the upper branch, 5 on the middle branch, 3 on the lower branch).
ABUS
BRING
CSTAR
DTREE
Show answerHide answer
Correct answer: (D) TREE
Explanation
Several bus segments branch out from a main backbone cable, making a hierarchy of buses. This is a tree topology.
Which of the following search algorithm requires a sorted array ?
ALinear search
BHash search
CBinary search
DAll of these
Show answerHide answer
Correct answer: (C) Binary search
Explanation
Binary search repeatedly halves the search range by comparing with the middle element, which only works if the array is sorted. Linear search works on unsorted data.
The figure below represents a
Figure: an arrow points to a root node R; R has two arrows going down, one to a triangle (subtree) labelled A on the left and one to a triangle (subtree) labelled B on the right.
ABinary tree
BRecursive tree
CInsert sort
DUnitary tree
Show answerHide answer
Correct answer: (A) Binary tree
Explanation
The figure shows a root with a left subtree A and a right subtree B. This is the recursive definition of a binary tree.
A self-referential structure has a pointer to its own type: struct node { int data; struct node *link; };. A member of type node itself would be infinitely recursive.
Which of the following algorithms use recursion for sorting an array of integers?
ABubble sort and Insertion sort
BBubble sort and Quick sort
CBubble sort and merge sort
DQuick sort and merge sort
Show answerHide answer
Correct answer: (D) Quick sort and merge sort
Explanation
Quick sort and merge sort are divide-and-conquer algorithms and are normally written recursively. Bubble sort and insertion sort are simple iterative algorithms.
Consider the following three binary trees, each with 7 nodes.
Tree A: root has a left child that is a leaf and a right child X; X has a left child Y and a right child that is a leaf; Y has two leaf children.
Tree B: root has a left child L and a right child R; L has only a left child (a leaf); R has only a left child Z; Z has two leaf children.
Tree C: root has two children, and each of them has two leaf children (a full tree of height 2).
Then A is ______ binary tree, B is ______ binary tree and C is ______ binary tree.
AStrictly, strictly, complete
BComplete, not strictly, strictly
CStrictly, not strictly, strictly
DStrictly, not strictly, complete
Show answerHide answer
Correct answer: (D) Strictly, not strictly, complete
Explanation
Tree A is strictly binary because every node has 0 or 2 children. Tree B is not, because L and R each have only one child. Tree C is a full tree of height 2 with every level filled, so it is complete.
The elements of the triangular array are stored as a vector in the order A[1, 1], A[2, 1], A[2, 2], A[3, 1], A[3, 2], A[3, 3] ......... A[n, n].
Assuming that A[1, 1] is stored at location 1, addressing function for A[i, j] is given by:
A(i−1)/2 + j
B((i−1)*i)/2 + j
C(i×i)/2 + j
D(i*j−1)/2
Show answerHide answer
Correct answer: (B) ((i−1)*i)/2 + j
Explanation
Rows 1 to i−1 hold 1 + 2 + … + (i−1) = i(i−1)/2 elements. A[i, j] is the j-th element of row i, so its location is i(i−1)/2 + j. Check: A[1,1] gives 0 + 1 = 1.
What is the meaning of the following declaration in C programming language?
int (*p) [5];
AIt will result in compile error because there should not be any parenthesis, i.e., "int*p[5]" is valid
Bp is a pointer to 5 integers
Cp is a pointer to integer array
Dp is a pointer to an array of 5 integers
Show answerHide answer
Correct answer: (D) p is a pointer to an array of 5 integers
Explanation
The parentheses bind * to p first, so p is a pointer, and that pointer points to an array of 5 ints. Without parentheses, int *p[5] would be an array of 5 pointers to int.
A register in the microprocessor that keeps track of the answer or result of any arithmetic or logic operation is the ______.
AAccumulator
BStack pointer
CProgram counter
DInstruction pointer
Show answerHide answer
Correct answer: (A) Accumulator
Explanation
The accumulator holds an operand and stores the result of arithmetic and logic operations. The program counter holds the next instruction's address, and the stack pointer holds the top of the stack.
Zero address instructions are necessarily used in ______ architecture.
AStack
BCISC
CRISC
DParallel
Show answerHide answer
Correct answer: (A) Stack
Explanation
A stack machine takes its operands implicitly from the top of the stack. So instructions like ADD need no address fields, which makes them zero-address instructions.
Which of the following file stream function will not move the file pointer to (N+1)th record of a binary file F created with the help of objects of class EMPLOYEE ?
AF. seekp (N*sizeof (EMPLOYEE));
BF. seekg (F.tellg( )*N);
CF. seekg (sizeof (EMPLOYEE)*N);
DPOS = sizeof (EMPLOYEE)*N;
Show answerHide answer
Correct answer: (B) F. seekg (F.tellg( )*N);
Explanation
seekg(F.tellg()*N) multiplies the current position by N, which is not the offset of the (N+1)th record. The correct offset is N*sizeof(EMPLOYEE).
The following figure is a ______ tree after zig_Zig rotations.
Tree: root X; X has left child A and right child P; P has left child B and right child G; G has left child C and right child D.
AHeap
BBubble
CSplay
DBinary
Show answerHide answer
Correct answer: (C) Splay
Explanation
Zig-zig rotations are the restructuring steps of a splay tree, which move an accessed node up to the root.
The following postfix expression with single-digit operands is evaluated using a stack:
823^/23*+51*-
(Note that ^ is the exponentiation operator.) The top two elements of the stack after the first * is evaluated, are
A6, 1
B5, 7
C3, 2
D1, 5
Show answerHide answer
Correct answer: (A) 6, 1
Explanation
8 2 3 ^ gives 2³ = 8, then 8 / 8 = 1. Next 2 and 3 are pushed and * gives 6. The stack now holds 1, 6, so the top two elements are 6 and 1.
For a tree with just one node, the root node is the height of a binary tree is defined to zero; if there are 2 levels of nodes, the height is 1 and so on.
A Binary Search Tree is built according to the usual rules with the following six key inserted one at a time as given :
B, I, N, A, R, Y. What is the height of the tree?
A5
B2
C3
D4
Show answerHide answer
Correct answer: (D) 4
Explanation
Inserting B, I, N, A, R, Y gives root B with A on the left and the chain I → N → R → Y to the right. The longest path B-I-N-R-Y has 4 edges, so the height is 4.
Observe the following graph and find the missing statement in the Python code.
import matplotlib.pyplot as pl
______
pl. show()
Graph: a single line joining the points (0.0, 5), (1.0, 2), (2.0, 3), (3.0, 1); x-axis 0.0 to 3.0, y-axis 1.0 to 5.0.
Apl.bar ([5, 2, 3, 1])
Bpl.scatter ([5, 2, 3, 1])
Cpl.plot ([5, 2, 3, 1])
Dpl.line ([5, 2, 3, 1])
Show answerHide answer
Correct answer: (C) pl.plot ([5, 2, 3, 1])
Explanation
pl.plot([5, 2, 3, 1]) draws a line graph, using the indexes 0-3 as x values. bar and scatter give different charts, and pl.line does not exist.
The number of pointer changes required for inserting a node between two given nodes in the linked implementation of a doubly linked list is ______.
AOne
BTwo
CThree
DFour
Show answerHide answer
Correct answer: (D) Four
Explanation
The new node's next and prev pointers are set, and the next pointer of the previous node and the prev pointer of the following node are updated. That makes 4 pointer changes.
Which of the following options is the correct code for traversing a link list staring at pointer P, upto element X ?
AWhile (P == NULL || P → Element ! = X)
P = P → Next ;
BWhile (P == NULL && P → Element ! = X)
P = P → Next ;
CWhile (P ! = NULL || P → Element ! = X)
P = P → Next ;
DWhile (P ! = NULL && P → Element ! = X)
P = P → Next ;
Show answerHide answer
Correct answer: (D) While (P ! = NULL && P → Element ! = X)
P = P → Next ;
Explanation
The loop must stop at the end of the list or when X is found, so it continues only while P != NULL && P→Element != X. Checking for NULL first also avoids dereferencing a null pointer.
Consider the following two C++ statements and the accompanying contexts.
(A) return P→next = = NULL; // P is a pointer to an element in the link list L.
(B) return S → next == NULL; // S is a stack implemented as a link list.
If the return value is TRUE in both cases, which of the following is correct ?
AP points to the last element in L and S is empty.
BP points to the last element in L and S is full.
CL is empty and S is full.
DBoth L and S are empty.
Show answerHide answer
Correct answer: (B) P points to the last element in L and S is full.
Explanation
Answer as per the book's key (b). Note that S→next == NULL would normally mean the stack is empty, so this question is doubtful.
Given the following program fragment to add two integer arrays A and B, each of size N. The sum is stored in integer array C of the same size.
for (i=1; i<=N; i++)
C[i] = A[i]+B[i];
The running time of this for loop is ______.
AO(1)
BO(N)
CO(2N)
DO(N²)
Show answerHide answer
Correct answer: (B) O(N)
Explanation
The loop body does constant work and runs N times, so the running time is linear, O(N). O(2N) also simplifies to O(N).
The figure compares pairs, moves the smaller key up each level and puts the minimum (06) at the root. This tournament-style structure is tree (tournament) sort.
For the following binary tree inorder traversal yields the expression :
Binary tree: root is '−'; its left child is '+' and its right child is '/'.
'+' has left child 'a' and right child '*'; '*' has children 'b' (left) and 'd' (right).
'/' has children 'e' (left) and 'f' (right).
A− + * / abdef
Ba + bd * − ef /
Cabdef* / + −
Da + b * d − e/f
Show answerHide answer
Correct answer: (D) a + b * d − e/f
Explanation
Inorder traversal visits left subtree, root, then right subtree. Left subtree gives a + b * d, then the root −, then the right subtree e / f, giving a + b * d − e/f.
Which of the following data structures is most suitable for evaluating postfix expressions?
ATree
BStack
CLinked list
DQueue
Show answerHide answer
Correct answer: (B) Stack
Explanation
Operands are pushed onto a stack. When an operator is read, the top two operands are popped, the operator is applied and the result is pushed back, so a stack is the natural structure for postfix evaluation.
Which statement is true about condition of overflow in the circular Queue?
I. Front = 1 and Rear = N
II. Front = Rear + 1
AOnly I
BOnly II
CBoth I and II
DNeither I nor II
Show answerHide answer
Correct answer: (C) Both I and II
Explanation
In a 1-based circular queue of size N, it is full (overflow) when Front = 1 and Rear = N, or when Front = Rear + 1 (rear has wrapped round to just behind front).
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.