UGC NET CS Practice Set 5
UGC NET JRF Computer Science · 2026 · with answers and explanations
- questions
- 30
- Total marks
- 60
- Duration
- 180 minutes
- Negative marking
- No
Subjects: Computer Science
Question 1
Computer Science · 2 marksThe height of an AVL tree with n nodes is:
- A.O(n)
- B.O(log n)
- C.O(n log n)
- D.O(√n)
Correct answer: B
Explanation: An AVL tree maintains the property that the heights of left and right subtrees differ by at most 1. This balance guarantee ensures the height is always O(log n). Specifically, h < 1.4405 log₂(n+2) − 0.328.
Question 2
Computer Science · 2 marksWhich graph algorithm uses a priority queue and works for graphs with non-negative edge weights?
- A.Bellman-Ford algorithm
- B.Dijkstra's algorithm
- C.Floyd-Warshall algorithm
- D.DFS
Correct answer: B
Explanation: Dijkstra's algorithm uses a priority queue (min-heap) and requires all edge weights to be non-negative. It greedily selects the unvisited vertex with minimum distance. Time complexity is O((V+E) log V) with a binary heap.
Question 3
Computer Science · 2 marksThe time complexity of the dynamic programming solution for the 0/1 knapsack problem with n items and capacity W is:
- A.O(n × W)
- B.O(2^n)
- C.O(n²)
- D.O(n log n)
Correct answer: A
Explanation: The DP solution uses a table of size (n+1) × (W+1). Each cell is computed in O(1) time, so the total complexity is O(n × W). This is pseudo-polynomial because W is not polynomial in the input size (which includes log W bits for W).
Question 4
Computer Science · 2 marksWhich data structure is most efficient for implementing the Least Recently Used (LRU) cache?
- A.Array only
- B.Hash map combined with a doubly linked list
- C.Binary search tree
- D.Stack
Correct answer: B
Explanation: An LRU cache needs O(1) access (hash map) and O(1) insertion/deletion at both ends and arbitrary positions (doubly linked list). The hash map stores key → node mappings, and the doubly linked list maintains the access order.
Question 5
Computer Science · 2 marksThe amortized time complexity of inserting n elements into an initially empty dynamic array (that doubles in size when full) is:
- A.O(n) per insertion
- B.O(1) amortized per insertion
- C.O(log n) per insertion
- D.O(n log n) total
Correct answer: B
Explanation: While individual insertions may take O(n) when resizing, the total cost over n insertions is O(n). The doubling strategy ensures that expensive copies happen infrequently enough that the amortized cost per insertion is O(1) (shown by the aggregate or accounting method).
Question 6
Computer Science · 2 marksIn Kruskal's algorithm for minimum spanning tree, edges are processed in:
- A.Decreasing order of weight
- B.Increasing order of weight
- C.Random order
- D.BFS order
Correct answer: B
Explanation: Kruskal's algorithm sorts all edges in non-decreasing (increasing) order of weight and greedily adds the next lightest edge that doesn't form a cycle. Cycle detection is efficiently done using the Union-Find (Disjoint Set Union) data structure.
Question 7
Computer Science · 2 marksThe number of comparisons in the worst case for searching an element in a sorted array of n elements using binary search is:
- A.n
- B.⌊log₂ n⌋ + 1
- C.log₂ n
- D.n/2
Correct answer: B
Explanation: Binary search halves the search space with each comparison. In the worst case (element not found or at the boundary), the number of comparisons is ⌊log₂ n⌋ + 1. For n=16, this is 4+1 = 5 comparisons.
Question 8
Computer Science · 2 marksWhich sorting algorithm is NOT based on comparisons?
- A.Merge sort
- B.Heap sort
- C.Counting sort
- D.Quick sort
Correct answer: C
Explanation: Counting sort is a non-comparison sorting algorithm that works by counting occurrences of each value. It runs in O(n + k) time where k is the range of input values. Merge sort, heap sort, and quick sort are all comparison-based with an Ω(n log n) lower bound.
Question 9
Computer Science · 2 marksThe space complexity of an adjacency matrix representation of a graph with V vertices is:
- A.O(V)
- B.O(V²)
- C.O(V + E)
- D.O(E)
Correct answer: B
Explanation: An adjacency matrix uses a V×V 2D array to represent edges, requiring O(V²) space regardless of the number of edges. An adjacency list uses O(V + E) space, which is more efficient for sparse graphs.
Question 10
Computer Science · 2 marksAn infix expression A + B * C − D, when converted to postfix, becomes:
- A.ABC*+D−
- B.AB+C*D−
- C.ABCD*+−
- D.+A*BC−D
Correct answer: A
Explanation: Applying operator precedence: * has higher precedence than + and −. So A + (B*C) − D. Converting: B*C → BC*, then A + BC* → ABC*+, then ABC*+ − D → ABC*+D−.
Question 11
Computer Science · 2 marksThe time complexity of building a heap from an unsorted array of n elements (heapify) is:
- A.O(n log n)
- B.O(n)
- C.O(n²)
- D.O(log n)
Correct answer: B
Explanation: Building a heap using the bottom-up (sift-down) approach is O(n). Although there are O(n) nodes, most nodes are near the bottom and sift down only a short distance. The sum of sift-down distances is bounded by O(n), not O(n log n).
Question 12
Computer Science · 2 marksIn the Floyd-Warshall algorithm, the time complexity for an all-pairs shortest path computation on a graph with V vertices is:
- A.O(V²)
- B.O(V³)
- C.O(V² log V)
- D.O(V × E)
Correct answer: B
Explanation: Floyd-Warshall uses three nested loops over V vertices to consider all intermediate vertices for all pairs. Each iteration is O(1), giving a total of O(V³). It works with negative edges (but not negative cycles).
Question 13
Computer Science · 2 marksWhich of the following problems can be solved using a greedy algorithm to obtain an optimal solution?
- A.0/1 Knapsack problem
- B.Fractional Knapsack problem
- C.Travelling Salesman Problem
- D.Longest Common Subsequence
Correct answer: B
Explanation: The fractional knapsack problem can be solved optimally by a greedy algorithm: sort items by value/weight ratio and take items in decreasing order of this ratio, taking fractions if needed. The 0/1 knapsack, TSP, and LCS require DP or other approaches.
Question 14
Computer Science · 2 marksA queue implemented using a circular array of size n can hold at most how many elements (with one slot wasted to distinguish full from empty)?
- A.n
- B.n − 1
- C.n + 1
- D.n/2
Correct answer: B
Explanation: In a circular queue using an array of size n, one slot is typically left empty to distinguish between full and empty states (both would have front == rear otherwise). So the maximum number of elements is n − 1.
Question 15
Computer Science · 2 marksThe recurrence relation T(n) = 2T(n/4) + √n has the solution:
- A.Θ(√n log n)
- B.Θ(n)
- C.Θ(√n)
- D.Θ(n log n)
Correct answer: A
Explanation: By the Master Theorem: a=2, b=4, f(n)=n^(1/2). log_b(a) = log₄(2) = 1/2. f(n) = n^(1/2) = n^(log_b a). This is Case 2, so T(n) = Θ(n^(1/2) log n) = Θ(√n log n).
Question 16
Computer Science · 2 marksTopological sorting is possible on which type of graph?
- A.Undirected graph
- B.Directed Acyclic Graph (DAG)
- C.Complete graph
- D.Any directed graph
Correct answer: B
Explanation: Topological sorting is a linear ordering of vertices in a directed graph such that for every edge u→v, u comes before v. This is only possible if the graph has no directed cycles (i.e., it is a DAG). A cycle would create contradictory ordering requirements.
Question 17
Computer Science · 2 marksThe time complexity of the DFA-based pattern matching for a pattern of length m in a text of length n is:
- A.O(n × m)
- B.O(n)
- C.O(n + m)
- D.O(m log n)
Correct answer: B
Explanation: Once the DFA is built, pattern matching processes each character of the text exactly once (one state transition per character), giving O(n) matching time. Building the DFA takes O(m × |Σ|) time where |Σ| is the alphabet size, so total is O(m|Σ| + n).
Question 18
Computer Science · 2 marksA language is accepted by a deterministic finite automaton (DFA) if and only if it is:
- A.Context-free
- B.Context-sensitive
- C.Regular
- D.Recursively enumerable
Correct answer: C
Explanation: DFAs, NFAs, and regular expressions all recognize exactly the class of regular languages. This equivalence is established by the subset construction (NFA→DFA), Kleene's theorem (regex↔FA), and the Myhill-Nerode theorem.
Question 19
Computer Science · 2 marksThe minimum number of states in a DFA accepting the language of binary strings divisible by 3 is:
- A.2
- B.3
- C.4
- D.6
Correct answer: B
Explanation: A DFA for divisibility by k has exactly k states, representing remainders 0, 1, ..., k−1. For divisibility by 3, we need 3 states (remainder 0 is the accepting state). State transitions are determined by: new_remainder = (2 × old_remainder + bit) mod 3.
Question 20
Computer Science · 2 marksWhich of the following is NOT a regular language?
- A.The set of all binary strings with an even number of 1s
- B.The set {aⁿbⁿ | n ≥ 0}
- C.The set of all strings over {a,b} ending with 'ab'
- D.The empty set
Correct answer: B
Explanation: {aⁿbⁿ | n ≥ 0} is not regular — it requires matching the number of a's and b's, which needs unbounded counting. This is proved using the pumping lemma for regular languages. It is a context-free language recognized by a PDA.
Question 21
Computer Science · 2 marksA pushdown automaton (PDA) can recognize which class of languages?
- A.Regular languages only
- B.Context-free languages
- C.Context-sensitive languages
- D.All recursively enumerable languages
Correct answer: B
Explanation: A pushdown automaton (NFA + stack) recognizes exactly the class of context-free languages. The equivalence between CFGs and PDAs is a fundamental result. Deterministic PDAs recognize a proper subset (deterministic CFLs).
Question 22
Computer Science · 2 marksThe halting problem for Turing machines is:
- A.Decidable
- B.Undecidable but recognizable (semi-decidable)
- C.Neither decidable nor recognizable
- D.Decidable for restricted Turing machines
Correct answer: B
Explanation: The halting problem (does a given TM halt on a given input?) is undecidable (proved by Turing via diagonalization). However, it is recognizable (recursively enumerable): we can simulate the TM and accept if it halts. Its complement (non-halting) is not even recognizable.
Question 23
Computer Science · 2 marksIf L₁ is regular and L₂ is context-free, then L₁ ∩ L₂ is:
- A.Always regular
- B.Always context-free
- C.Always context-sensitive
- D.Not necessarily context-free
Correct answer: B
Explanation: The intersection of a regular language and a context-free language is always context-free. This is proved by the product construction: combining the DFA for L₁ with the PDA for L₂ to get a PDA for L₁ ∩ L₂. Note: the intersection of two CFLs is NOT necessarily context-free.
Question 24
Computer Science · 2 marksIn an LL(1) parser, the parsing table is constructed using:
- A.FIRST and FOLLOW sets
- B.Canonical collection of LR(0) items
- C.Regular expressions
- D.DFA transition table
Correct answer: A
Explanation: An LL(1) parser uses FIRST and FOLLOW sets to build its predictive parsing table. FIRST(α) gives the set of terminals that can begin strings derived from α. FOLLOW(A) gives terminals that can appear immediately after non-terminal A.
Question 25
Computer Science · 2 marksThe complexity class NP is defined as the set of problems that:
- A.Cannot be solved in polynomial time
- B.Can be verified in polynomial time by a deterministic TM
- C.Require exponential time to solve
- D.Are unsolvable
Correct answer: B
Explanation: NP (Nondeterministic Polynomial) is the class of decision problems for which a 'yes' answer can be verified in polynomial time given a certificate/witness. Equivalently, they can be solved in polynomial time by a nondeterministic Turing machine. P ⊆ NP, and whether P = NP is open.
Question 26
Computer Science · 2 marksWhich of the following problems is NP-complete?
- A.Shortest path in a graph
- B.Minimum spanning tree
- C.Boolean Satisfiability (SAT)
- D.Sorting
Correct answer: C
Explanation: SAT (Boolean Satisfiability) was the first problem proved NP-complete (Cook-Levin theorem). Shortest path and MST are in P. Sorting is also in P. SAT is NP-complete: it is in NP (a satisfying assignment is a polynomial-time verifiable certificate) and every NP problem reduces to it.
Question 27
Computer Science · 2 marksIn a lexical analyzer, which of the following is used to recognize tokens?
- A.Context-free grammar
- B.Regular expressions / Finite automata
- C.Pushdown automata
- D.Turing machine
Correct answer: B
Explanation: Tokens (identifiers, keywords, numbers, operators) are defined by regular expressions and recognized by finite automata (DFA). Tools like Lex/Flex convert regular expression specifications into DFA-based lexical analyzers.
Question 28
Computer Science · 2 marksAn LR(1) parser is more powerful than an SLR(1) parser because:
- A.It uses a larger stack
- B.It uses lookahead information in the items to resolve conflicts
- C.It can handle non-context-free languages
- D.It parses top-down instead of bottom-up
Correct answer: B
Explanation: LR(1) items include a lookahead symbol in each item, providing more precise information about when to reduce. SLR(1) uses FOLLOW sets (less precise) for reduction decisions. This makes LR(1) able to handle more grammars without conflicts.
Question 29
Computer Science · 2 marksCommon subexpression elimination is an example of:
- A.Machine-dependent optimization
- B.Machine-independent optimization
- C.Peephole optimization
- D.Loop optimization
Correct answer: B
Explanation: Common subexpression elimination (CSE) identifies expressions that are computed more than once and replaces redundant computations with the previously computed value. It is a machine-independent optimization because it operates on the intermediate code regardless of the target architecture.
Question 30
Computer Science · 2 marksThe grammar S → aSb | ε generates the language:
- A.{aⁿbⁿ | n ≥ 0}
- B.{(ab)ⁿ | n ≥ 0}
- C.{aⁿbᵐ | n, m ≥ 0}
- D.{ww^R | w ∈ {a,b}*}
Correct answer: A
Explanation: The grammar S → aSb adds one 'a' on the left and one 'b' on the right in each derivation, and S → ε terminates. After n applications of the first rule: S ⇒ aSb ⇒ aaSbb ⇒ ... ⇒ aⁿSbⁿ ⇒ aⁿbⁿ. So L = {aⁿbⁿ | n ≥ 0}.