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 marks

The height of an AVL tree with n nodes is:

  1. A.O(n)
  2. B.O(log n)
  3. C.O(n log n)
  4. 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 marks

Which graph algorithm uses a priority queue and works for graphs with non-negative edge weights?

  1. A.Bellman-Ford algorithm
  2. B.Dijkstra's algorithm
  3. C.Floyd-Warshall algorithm
  4. 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 marks

The time complexity of the dynamic programming solution for the 0/1 knapsack problem with n items and capacity W is:

  1. A.O(n × W)
  2. B.O(2^n)
  3. C.O(n²)
  4. 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 marks

Which data structure is most efficient for implementing the Least Recently Used (LRU) cache?

  1. A.Array only
  2. B.Hash map combined with a doubly linked list
  3. C.Binary search tree
  4. 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 marks

The amortized time complexity of inserting n elements into an initially empty dynamic array (that doubles in size when full) is:

  1. A.O(n) per insertion
  2. B.O(1) amortized per insertion
  3. C.O(log n) per insertion
  4. 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 marks

In Kruskal's algorithm for minimum spanning tree, edges are processed in:

  1. A.Decreasing order of weight
  2. B.Increasing order of weight
  3. C.Random order
  4. 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 marks

The number of comparisons in the worst case for searching an element in a sorted array of n elements using binary search is:

  1. A.n
  2. B.⌊log₂ n⌋ + 1
  3. C.log₂ n
  4. 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 marks

Which sorting algorithm is NOT based on comparisons?

  1. A.Merge sort
  2. B.Heap sort
  3. C.Counting sort
  4. 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 marks

The space complexity of an adjacency matrix representation of a graph with V vertices is:

  1. A.O(V)
  2. B.O(V²)
  3. C.O(V + E)
  4. 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 marks

An infix expression A + B * C − D, when converted to postfix, becomes:

  1. A.ABC*+D−
  2. B.AB+C*D−
  3. C.ABCD*+−
  4. 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 marks

The time complexity of building a heap from an unsorted array of n elements (heapify) is:

  1. A.O(n log n)
  2. B.O(n)
  3. C.O(n²)
  4. 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 marks

In the Floyd-Warshall algorithm, the time complexity for an all-pairs shortest path computation on a graph with V vertices is:

  1. A.O(V²)
  2. B.O(V³)
  3. C.O(V² log V)
  4. 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 marks

Which of the following problems can be solved using a greedy algorithm to obtain an optimal solution?

  1. A.0/1 Knapsack problem
  2. B.Fractional Knapsack problem
  3. C.Travelling Salesman Problem
  4. 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 marks

A 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)?

  1. A.n
  2. B.n − 1
  3. C.n + 1
  4. 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 marks

The recurrence relation T(n) = 2T(n/4) + √n has the solution:

  1. A.Θ(√n log n)
  2. B.Θ(n)
  3. C.Θ(√n)
  4. 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 marks

Topological sorting is possible on which type of graph?

  1. A.Undirected graph
  2. B.Directed Acyclic Graph (DAG)
  3. C.Complete graph
  4. 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 marks

The time complexity of the DFA-based pattern matching for a pattern of length m in a text of length n is:

  1. A.O(n × m)
  2. B.O(n)
  3. C.O(n + m)
  4. 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 marks

A language is accepted by a deterministic finite automaton (DFA) if and only if it is:

  1. A.Context-free
  2. B.Context-sensitive
  3. C.Regular
  4. 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 marks

The minimum number of states in a DFA accepting the language of binary strings divisible by 3 is:

  1. A.2
  2. B.3
  3. C.4
  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 marks

Which of the following is NOT a regular language?

  1. A.The set of all binary strings with an even number of 1s
  2. B.The set {aⁿbⁿ | n ≥ 0}
  3. C.The set of all strings over {a,b} ending with 'ab'
  4. 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 marks

A pushdown automaton (PDA) can recognize which class of languages?

  1. A.Regular languages only
  2. B.Context-free languages
  3. C.Context-sensitive languages
  4. 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 marks

The halting problem for Turing machines is:

  1. A.Decidable
  2. B.Undecidable but recognizable (semi-decidable)
  3. C.Neither decidable nor recognizable
  4. 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 marks

If L₁ is regular and L₂ is context-free, then L₁ ∩ L₂ is:

  1. A.Always regular
  2. B.Always context-free
  3. C.Always context-sensitive
  4. 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 marks

In an LL(1) parser, the parsing table is constructed using:

  1. A.FIRST and FOLLOW sets
  2. B.Canonical collection of LR(0) items
  3. C.Regular expressions
  4. 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 marks

The complexity class NP is defined as the set of problems that:

  1. A.Cannot be solved in polynomial time
  2. B.Can be verified in polynomial time by a deterministic TM
  3. C.Require exponential time to solve
  4. 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 marks

Which of the following problems is NP-complete?

  1. A.Shortest path in a graph
  2. B.Minimum spanning tree
  3. C.Boolean Satisfiability (SAT)
  4. 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 marks

In a lexical analyzer, which of the following is used to recognize tokens?

  1. A.Context-free grammar
  2. B.Regular expressions / Finite automata
  3. C.Pushdown automata
  4. 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 marks

An LR(1) parser is more powerful than an SLR(1) parser because:

  1. A.It uses a larger stack
  2. B.It uses lookahead information in the items to resolve conflicts
  3. C.It can handle non-context-free languages
  4. 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 marks

Common subexpression elimination is an example of:

  1. A.Machine-dependent optimization
  2. B.Machine-independent optimization
  3. C.Peephole optimization
  4. 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 marks

The grammar S → aSb | ε generates the language:

  1. A.{aⁿbⁿ | n ≥ 0}
  2. B.{(ab)ⁿ | n ≥ 0}
  3. C.{aⁿbᵐ | n, m ≥ 0}
  4. 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}.

View all papers — UGC NET JRF Computer Science

Practice this paper with a timer

Attempt it online, get scored instantly, and see which subjects are costing you marks.

Start practising free