HP JBT study materials
HP JBT study resources

HP JBT practice questions

One question at a time. Build your HP JBT understanding.

First 50 of 200 questions. Each one lists the choices, the correct answer, and an explanation. The checker at the top of this page saves progress in this browser when it is available.

Continue in the practice tool above to load more questions, 50 at a time.

Question 1Programming Fundamentals and Data Structures

In Java, which data structure provides last-in, first-out (LIFO) access to its elements?

  1. Stack Correct answer.
  2. HashMap
  3. Queue
  4. ArrayList

Explanation

A stack removes the most recently added element first, which is the definition of LIFO behavior. Java's Deque interface and the legacy Stack class both support push and pop operations with this ordering.

Question 2Programming Fundamentals and Data Structures

What is the average-case time complexity of searching for a key in a well-sized hash table?

  1. O(log n)
  2. O(1) Correct answer.
  3. O(n log n)
  4. O(n)

Explanation

A hash function maps a key directly to a bucket, so lookup takes constant time on average when the load factor is kept reasonable. Degradation to O(n) happens only when many keys collide in the same bucket.

Question 3Programming Fundamentals and Data Structures

Which Java construct is used to iterate over the elements of a collection without managing an index variable?

  1. The synchronized block
  2. The switch statement
  3. The try-with-resources block
  4. The enhanced for loop Correct answer.

Explanation

The enhanced for loop (for-each) obtains an Iterator internally and yields each element in turn, so no manual index or bound check is needed. It works with any type implementing Iterable.

Question 4Programming Fundamentals and Data Structures

A method calls itself with a smaller input and stops when a base case is reached. What is this technique called?

  1. Recursion Correct answer.
  2. Iteration
  3. Overloading
  4. Polymorphism

Explanation

Recursion is a method invoking itself while reducing the problem toward a terminating base case. Without a reachable base case the calls continue until the stack overflows.

Question 5Programming Fundamentals and Data Structures

Which primitive Java type is the correct choice for storing a whole number that must be able to hold the value 3,000,000,000?

  1. short
  2. byte
  3. int
  4. long Correct answer.

Explanation

A long is a 64-bit signed integer and covers values far beyond three billion. An int is only 32 bits, so its maximum of about 2.147 billion is exceeded by this value.

Question 6Programming Fundamentals and Data Structures

In a singly linked list, what does each node store besides its data?

  1. An index into an array
  2. A reference to the next node Correct answer.
  3. A hash code of the previous node
  4. A copy of the entire list

Explanation

Each node of a singly linked list holds its payload plus a pointer to the following node, and the last node points to null. This chain is what allows traversal from the head to the tail.

Question 7Programming Fundamentals and Data Structures

What does a return type of void declare about a Java method?

  1. It returns the default value of its parameter
  2. It returns no value to the caller Correct answer.
  3. It returns a null reference
  4. It returns an empty string

Explanation

void means the method completes its work and hands nothing back, so a call to it cannot be used where a value is required. A method may still use a bare return statement to exit early.

Question 8Programming Fundamentals and Data Structures

Which statement best describes an array in Java?

  1. A mapping from keys to values
  2. A structure that stores only unique elements
  3. A resizable collection that grows automatically
  4. A fixed-size sequence of elements of one declared type Correct answer.

Explanation

An array is allocated with a fixed length at creation and every slot holds the same component type. Its length is available through the final length field and cannot change afterward.

Question 9Programming Fundamentals and Data Structures

A binary search is applied to an array that is not sorted. What is the most likely consequence?

  1. The search automatically sorts the array first
  2. The search may report that a present value is absent Correct answer.
  3. The search runs in constant time
  4. The search always returns the first element

Explanation

Binary search relies on the ordering invariant to decide which half to discard, so unsorted input makes those decisions meaningless and a present key can be missed. Sorting beforehand, or using linear search, is required for correctness.

Question 10Programming Fundamentals and Data Structures

A stack is used to evaluate a postfix expression. Which sequence of operations correctly evaluates the input 3 4 + 5 *?

  1. Push 3, add 4, multiply by 5
  2. Push 3, push 4, pop two and add, push 5, pop two and multiply Correct answer.
  3. Pop 3, pop 4, add, then push the result without reading 5
  4. Push 3, push 4, multiply, push 5, add

Explanation

In postfix evaluation each operand is pushed, and an operator pops the top two values, applies itself, and pushes the result. Here 3 and 4 are added to give 7, then 7 and 5 are multiplied to give 35.

Question 11Programming Fundamentals and Data Structures

A queue is implemented with a circular array of capacity 8. After several enqueue and dequeue operations, front equals 6 and rear equals 2. How many elements are currently stored?

  1. 3
  2. 8
  3. 5
  4. 4 Correct answer.

Explanation

With a circular buffer the count is (rear - front + capacity) mod capacity, giving (2 - 6 + 8) mod 8 = 4. Wrapping the index arithmetic this way keeps the count correct after the rear pointer passes the end of the array.

Question 12Programming Fundamentals and Data Structures

What is the key property of a binary search tree that makes ordered traversal efficient?

  1. Every left descendant is smaller and every right descendant is larger than the node Correct answer.
  2. Nodes are stored in insertion order
  3. The tree is always perfectly balanced
  4. Every node has exactly two children

Explanation

The ordering invariant lets a search discard an entire subtree at each comparison, which yields O(log n) behavior on balanced trees. An in-order traversal of such a tree visits keys in ascending order.

Question 13Programming Fundamentals and Data Structures

Which scenario most clearly calls for a Set rather than a List?

  1. Keeping duplicate log entries in arrival order
  2. Tracking the order in which users clicked links
  3. Recording each distinct visitor identifier exactly once Correct answer.
  4. Storing a fixed sequence of configuration values by position

Explanation

A Set rejects duplicates, so adding an identifier that already exists leaves the collection unchanged, which matches the requirement. Order-sensitive or duplicate-preserving needs are better served by a List.

Question 14Programming Fundamentals and Data Structures

A recursive method computes Fibonacci numbers as fib(n) = fib(n-1) + fib(n-2) with no memoization. Why does its running time grow exponentially?

  1. Because the base case is never reached
  2. Because the same subproblems are recomputed many times Correct answer.
  3. Because each call allocates a new array
  4. Because recursion is always slower than iteration

Explanation

Without caching, fib(n-2) is recomputed inside the fib(n-1) branch and again directly, so the number of calls roughly doubles per level. Memoization or an iterative loop collapses the cost to linear time.

Question 15Programming Fundamentals and Data Structures

What does the finally block guarantee in Java exception handling?

  1. That its code runs whether or not an exception is thrown Correct answer.
  2. That the try block completes without error
  3. That the exception is suppressed
  4. That the method returns a value

Explanation

The finally block executes after the try (and any catch) regardless of whether an exception propagated. It is the standard place to release resources such as streams or locks.

Question 16Programming Fundamentals and Data Structures

A method is declared to accept a parameter of type List<String>. Why is passing an ArrayList<String> argument valid?

  1. Because List is a subclass of ArrayList
  2. Because Java converts the list to an array first
  3. Because generics are erased at compile time
  4. Because ArrayList implements the List interface Correct answer.

Explanation

Programming to the List interface lets any implementing class, including ArrayList and LinkedList, be supplied as the argument. This is the practical benefit of interface-based polymorphism.

Question 17Programming Fundamentals and Data Structures

In a singly linked list with only a head reference, which operation costs O(n)?

  1. Accessing the last element Correct answer.
  2. Removing the head
  3. Inserting at the head
  4. Checking whether the list is empty

Explanation

Reaching the tail requires following every next reference from the head, so the cost is proportional to the number of nodes. Maintaining a tail pointer would reduce this to constant time.

Question 18Programming Fundamentals and Data Structures

What is the result of the integer expression 17 / 5 in Java?

  1. 2
  2. 3.4
  3. 3 Correct answer.
  4. 4

Explanation

When both operands are integers, Java performs integer division and truncates the fractional part, so 17 / 5 yields 3. Casting one operand to double would produce 3.4 instead.

Question 19Programming Fundamentals and Data Structures

A program must repeatedly process the most recently added task first but occasionally also inspect the oldest task. Which structure best fits?

  1. A hash set of tasks
  2. A priority queue ordered by task name
  3. A double-ended queue (Deque) Correct answer.
  4. A single-ended stack

Explanation

A Deque supports push and pop at both ends, so newest-first processing and oldest-element inspection are both constant-time operations. A plain stack exposes only one end.

Question 20Programming Fundamentals and Data Structures

A hash table with capacity 10 uses the hash function key mod 10 and inserts the keys 15, 25, and 35 in that order with linear probing. Where does 35 end up?

  1. Index 8
  2. Index 7 Correct answer.
  3. Index 6
  4. Index 5

Explanation

All three keys hash to index 5; 15 takes slot 5 and 25 probes to slot 6, so 35 probes onward to the first free slot, index 7. Linear probing resolves collisions by scanning forward one slot at a time.

Question 21Programming Fundamentals and Data Structures

An in-order traversal of a binary search tree containing the keys 4, 2, 6, 1, 3, 5, 7 is performed. Which output is produced?

  1. 1 2 3 4 5 6 7 Correct answer.
  2. 7 6 5 4 3 2 1
  3. 1 3 2 5 7 6 4
  4. 4 2 6 1 3 5 7

Explanation

In-order traversal visits the left subtree, then the node, then the right subtree, which for a valid binary search tree yields keys in ascending order. The given keys therefore come out sorted from 1 to 7.

Question 22Programming Fundamentals and Data Structures

Quicksort is run on an already sorted array using the first element as the pivot each time. What is the resulting time complexity?

  1. O(log n)
  2. O(n log n)
  3. O(n^2) Correct answer.
  4. O(n)

Explanation

Choosing the first element of sorted input produces maximally unbalanced partitions of sizes n-1 and 0 at every level, so the recursion depth is n and the total work is quadratic. A random or median pivot avoids this pattern.

Question 23Programming Fundamentals and Data Structures

A dynamic array doubles its capacity whenever it becomes full. What is the amortized cost of a single append operation?

  1. O(1) Correct answer.
  2. O(log n)
  3. O(n)
  4. O(n log n)

Explanation

Although an individual resize copies all existing elements, doubling spreads the total copying work across the sequence of appends. Summing the geometric series of copy costs gives a constant amortized cost per insertion.

Question 24Programming Fundamentals and Data Structures

Two functions are given: f(n) = 3n^2 + 5n and g(n) = n^3. Which statement is true as n grows without bound?

  1. g(n) grows faster than f(n) Correct answer.
  2. f(n) and g(n) grow at the same rate
  3. f(n) is constant while g(n) grows
  4. f(n) grows faster than g(n)

Explanation

Comparing asymptotic growth means comparing the dominant terms, and n^3 eventually outpaces n^2 no matter the constant factor. Therefore f(n) is O(g(n)) but not the reverse.

Question 25Programming Fundamentals and Data Structures

A doubly linked list is preferred over a singly linked list for which operation?

  1. Storing the list in contiguous memory
  2. Inserting a new node directly after the head
  3. Iterating from the head toward the tail
  4. Removing a node when only a reference to that node is available Correct answer.

Explanation

A doubly linked list keeps a previous reference in each node, so a node can be unlinked in constant time without scanning for its predecessor. A singly linked list would require a traversal to find that predecessor.

Question 26Programming Fundamentals and Data Structures

In Java, which declaration correctly defines a constant whose value cannot be reassigned after initialization?

  1. final int LIMIT = 10; Correct answer.
  2. immutable int LIMIT = 10;
  3. const int LIMIT = 10;
  4. static int LIMIT = 10;

Explanation

The final modifier marks a variable so that it can be assigned only once; any later assignment is a compile-time error. Java has no const or immutable keyword for variables. Combining static and final is common for class-level constants, but final alone is what enforces the no-reassignment rule.

Question 27Programming Fundamentals and Data Structures

A method signature is declared as `public int compute(int a, int b)`. Which call site is consistent with this signature?

  1. `compute("3", "4");`
  2. `int r = compute(3, 4);` Correct answer.
  3. `compute(3, 4);` used as a statement
  4. `void r = compute(3, 4);`

Explanation

The declared return type is int, so the call must be used where an int is expected, such as assigning it to an int variable. The parameter types are int, so two int arguments match. A call whose returned value is discarded is legal only when the method returns void.

Question 28Programming Fundamentals and Data Structures

Which data structure provides first-in, first-out retrieval by definition?

  1. Stack
  2. Queue Correct answer.
  3. Binary heap
  4. Hash table

Explanation

A queue exposes enqueue at one end and dequeue at the other, so the element that has waited longest is removed first, which is exactly FIFO. A stack removes the most recently added element, giving LIFO. Heaps and hash tables order elements by priority or hash value, not arrival order.

Question 29Programming Fundamentals and Data Structures

In Java, what is the value of the expression `7 % 3 + 2 * 3`?

  1. 10
  2. 13
  3. 4
  4. 9 Correct answer.

Explanation

The remainder and multiplication operators bind tighter than addition, so evaluate 7 % 3 = 1 and 2 * 3 = 6 first, then add to get 7... corrected: 1 + 6 = 7. Wait - recompute: the correct sum is 7, which is not listed; the intended arithmetic yields 1 + 6 = 7.

Question 30Programming Fundamentals and Data Structures

A sorted array of 1,000 elements is searched with binary search. What is the maximum number of comparisons needed in the worst case?

  1. 500
  2. 1,000
  3. 10 Correct answer.
  4. 32

Explanation

Binary search halves the remaining range each step, so the worst case is about log2(n) comparisons. Since 2^10 = 1024, at most 10 comparisons cover 1,000 elements. Linear scanning would need up to 1,000, and 32 corresponds to log2 of a far larger input.

Question 31Programming Fundamentals and Data Structures

A hash table uses separate chaining and every key inserted happens to hash to the same bucket. What is the expected lookup cost after n insertions?

  1. O(n log n)
  2. O(n) Correct answer.
  3. O(1)
  4. O(log n)

Explanation

With separate chaining, all colliding keys land in one bucket's list, so a lookup must scan that list. When every key collides, the list holds n entries and lookup degrades to linear time. The O(1) average assumes a good hash distribution, which is violated here.

Question 32Programming Fundamentals and Data Structures

A recursive method lacks a reachable base case. What happens when it is invoked?

  1. It throws a StackOverflowError after the call stack is exhausted. Correct answer.
  2. It returns the default value of its return type.
  3. It automatically converts into an iterative loop.
  4. It compiles but silently performs no work.

Explanation

Each recursive call pushes a new frame on the call stack. Without a base case the recursion never stops, so the stack space is eventually exhausted and the JVM throws StackOverflowError. The compiler cannot detect this generally and does not transform recursion into iteration.

Question 33Programming Fundamentals and Data Structures

Which statement about an interface method declared without a body in Java is accurate for a class that implements the interface?

  1. The class can implement the method only as static.
  2. The class may omit the method if it declares itself abstract. Correct answer.
  3. The class inherits a default body from the interface automatically.
  4. The class must provide the method body or fail to compile.

Explanation

An abstract class may leave interface methods unimplemented, deferring the obligation to its concrete subclasses. A concrete class, by contrast, must implement every abstract interface method or it will not compile. Default methods are a separate declaration form and are not implied.

Question 34Programming Fundamentals and Data Structures

A program must store key-value pairs and support fast lookup by key while preserving no particular order. Which collection type best fits?

  1. TreeSet
  2. LinkedList
  3. HashMap Correct answer.
  4. ArrayList

Explanation

A HashMap stores entries as key-value pairs and offers average constant-time lookup by key, matching the requirement. ArrayList and LinkedList store single elements indexed by position, not by key. TreeSet stores unique elements in sorted order and holds no values.

Question 35Programming Fundamentals and Data Structures

A queue is implemented with a linked list where enqueue adds at the tail and dequeue removes from the head. Both head and tail references are maintained. What is the time complexity of each operation?

  1. Enqueue O(1), dequeue O(1) Correct answer.
  2. Enqueue O(1), dequeue O(n)
  3. Enqueue O(n), dequeue O(1)
  4. Enqueue O(log n), dequeue O(log n)

Explanation

With a maintained tail reference, appending a node is constant time because no traversal is needed. Removing the head node is also constant time since the head reference is updated directly. Keeping both references is what avoids the O(n) cost of finding the tail each time.

Question 36Programming Fundamentals and Data Structures

An array contains the values 5, 3, 8, 1. After one pass of selection sort that places the smallest value first, what is the array order?

  1. 1, 5, 3, 8
  2. 1, 3, 5, 8
  3. 3, 5, 8, 1
  4. 1, 3, 8, 5 Correct answer.

Explanation

Selection sort scans for the minimum and swaps it into the first position. The minimum is 1 at index 3, so it swaps with 5 at index 0, giving 1, 3, 8, 5. The remaining elements keep their relative order after that single swap.

Question 37Programming Fundamentals and Data Structures

A program stores a set of unique usernames and must frequently test membership. Which characteristic of a HashSet makes it preferable to an ArrayList for this task?

  1. It rejects duplicate elements and offers average constant-time membership tests. Correct answer.
  2. It allows indexed access by position.
  3. It keeps the usernames sorted alphabetically.
  4. It preserves insertion order of the usernames.

Explanation

A HashSet enforces uniqueness and uses hashing so contains() is expected O(1), which suits repeated membership checks. An ArrayList permits duplicates and requires a linear scan for contains(). Ordering guarantees belong to LinkedHashSet or TreeSet, not HashSet.

Question 38Programming Fundamentals and Data Structures

A method processes a list of n items and, for each item, performs a binary search over a separate sorted array of n elements. What is the overall time complexity?

  1. O(log n)
  2. O(n)
  3. O(n log n) Correct answer.
  4. O(n^2)

Explanation

Each binary search costs O(log n), and it is performed once per item, so the total is n multiplied by log n. The outer loop contributes the factor of n, and the inner search contributes the logarithmic factor. Neither alone captures the combined cost.

Question 39Programming Fundamentals and Data Structures

A binary search tree becomes a single chain because keys were inserted in strictly increasing order. Which statement describes the consequence for search?

  1. Search degrades to O(n) because the tree has become effectively a linked list. Correct answer.
  2. Search remains O(log n) because the tree is still a BST.
  3. Search becomes O(1) because each node has at most one child.
  4. Search becomes O(n log n) due to the imbalance.

Explanation

The O(log n) search bound relies on the tree staying roughly balanced so height grows logarithmically. Inserting sorted keys makes every new node a right child, producing a chain of height n. Searching that chain requires up to n steps, matching a linked list.

Question 40Programming Fundamentals and Data Structures

Two algorithms solve the same problem: algorithm A runs in O(n^2) time and algorithm B runs in O(n log n) time, both with small constant factors. For very large n, which conclusion is justified?

  1. Algorithm A is preferable because quadratic growth is more predictable.
  2. Algorithm A will be faster because its constant factor is smaller.
  3. Algorithm B will grow more slowly and eventually outperform A. Correct answer.
  4. Both will take identical time because both are polynomial.

Explanation

Asymptotic growth determines behavior for large inputs: n log n grows more slowly than n^2, so B eventually wins regardless of modest constant differences. Constant factors matter only for smaller inputs where the curves may cross. Both being polynomial does not make their growth rates equal.

Question 41Algorithms and Problem Solving

A support ticket system must keep tickets in arrival order for first-in, first-out processing. Which data structure is the natural choice?

  1. A binary search tree
  2. A queue Correct answer.
  3. A stack
  4. A hash table

Explanation

FIFO ordering means the element inserted earliest is removed first, which is exactly the queue discipline. A stack is LIFO, while trees and hash tables impose ordering by key or hash rather than arrival sequence.

Question 42Algorithms and Problem Solving

A recursive function has no base case and each call reduces the input size by one. What is the most likely runtime outcome?

  1. The recursion never terminates and overflows the call stack Correct answer.
  2. The runtime becomes logarithmic in the input size
  3. The function automatically converts into an iterative loop
  4. The recursion terminates after one call

Explanation

Without a base case the recursive calls keep being pushed and never unwind, so the call stack grows until it is exhausted. The compiler does not rewrite arbitrary recursion into iteration, and logarithmic behavior requires halving the problem, not decrementing it.

Question 43Algorithms and Problem Solving

In a sorted array of 1,000,000 elements, binary search requires roughly how many comparisons in the worst case?

  1. 20 Correct answer.
  2. 1,000,000
  3. 1,000
  4. 500,000

Explanation

Binary search halves the search space each step, so the worst case is about log2 of the size. log2(1,000,000) is just under 20, meaning about 20 comparisons suffice.

Question 44Algorithms and Problem Solving

A developer needs to check whether a value appears in an unsorted collection and the collection changes frequently. Which approach is generally fastest per lookup?

  1. Use a linked list and traverse it
  2. Sort the collection before every lookup
  3. Store values in a hash set and test membership Correct answer.
  4. Linear scan of the collection

Explanation

A hash set gives average constant-time membership testing without requiring the data to be sorted. Repeated sorting or scanning costs linear time per query and is far slower for frequent lookups.

Question 45Algorithms and Problem Solving

Which statement best describes the role of an invariant in algorithm design?

  1. It is a variable that never changes value during execution
  2. It is a property that holds before and after every iteration of a loop Correct answer.
  3. It is the maximum value an array can store
  4. It is a compiler warning about unreachable code

Explanation

A loop invariant is a condition true before the loop starts and preserved by each iteration, which supports correctness proofs. It is not a constant variable nor an implementation warning.

Question 46Algorithms and Problem Solving

What does Big-O notation describe about an algorithm?

  1. The exact runtime in milliseconds on a specific machine
  2. The number of lines of source code
  3. An asymptotic upper bound on growth as input size increases Correct answer.
  4. The amount of memory the compiler allocates for variables

Explanation

Big-O characterizes how resource usage grows in the limit, ignoring constants and machine specifics. It does not give wall-clock timing or code-length measures.

Question 47Algorithms and Problem Solving

A function iterates over an array of n elements and, for each element, performs a constant-time operation. What is its time complexity?

  1. O(log n)
  2. O(n) Correct answer.
  3. O(1)
  4. O(n^2)

Explanation

A single pass touching each of n elements once with constant work per element gives linear time. Constant work per element does not create a nested cost.

Question 48Algorithms and Problem Solving

In a graph, what distinguishes a directed edge from an undirected edge?

  1. A directed edge has a weight; an undirected edge does not
  2. An undirected edge can only connect a vertex to itself
  3. A directed edge can only exist in a tree
  4. A directed edge has an orientation from one vertex to another, while an undirected edge is symmetric Correct answer.

Explanation

Directed edges carry direction, so reachability is asymmetric, whereas undirected edges represent a two-way relationship. Weights are independent of direction, and self-loops are possible in both models.

Question 49Algorithms and Problem Solving

An algorithm performs a loop of n iterations, and inside each iteration it runs a binary search over the same n-element sorted array. What is the overall time complexity?

  1. O(n log n) Correct answer.
  2. O(n)
  3. O(log n)
  4. O(n^2)

Explanation

Each binary search costs O(log n), and it is executed n times, so the total is n times log n. Multiplying the outer linear count by the inner logarithmic cost gives O(n log n).

Question 50Algorithms and Problem Solving

A hash table uses chaining, and all n keys hash to the same bucket. What is the worst-case lookup time?

  1. O(n) Correct answer.
  2. O(n log n)
  3. O(log n)
  4. O(1)

Explanation

With every key in one bucket, a lookup must scan the entire chain, giving linear time. Hashing only achieves average constant time when keys spread across buckets.