wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

tmp-Queue and Stack

Total questions: 87

Worksheet time: 3575secs

Name
Class
Date
1.

If the elements “A”, “B”, “C” and “D” are placed in a queue and are deleted one at a time, in what order will they be removed?

a)

ABCD

b)

DCBA

c)

DCAB

d)

ABDC

2.

In linked list implementation of a queue, the important condition for a queue to be empty is?

a)

FRONT is null

b)

REAR is null

c)

LINK is empty

d)

None of the mentioned

3.
Are stacks FIFO or FILO?
a)
FILO
b)
FIFO
c)
LIFO
d)
LILO
4.

How many stacks are needed to implement a queue. Consider the situation where no other data structure like arrays, linked list is available to you.

a)

1

b)

2

c)

3

d)

4

5.

How many queues are needed to implement a stack. Consider the situation where no other data structure like arrays, linked list is available to you.

a)

1

b)

2

c)

3

d)

4

6.

Which of the following is true about linked list implementation of queue?

a)

In push operation, if new nodes are inserted at the beginning of linked list, then in pop operation, nodes must be removed from end.

b)

In push operation, if new nodes are inserted at the end, then in pop operation, nodes must be removed from the beginning.

c)

Both of the above

d)

None of the above

7.

Suppose a circular queue of capacity (n – 1) elements is implemented with an array of n elements. Assume that the insertion and deletion operation are carried out using REAR and FRONT as array index variables, respectively. Initially, REAR = FRONT = 0. The conditions to detect queue full and queue empty are

a)

Full: (REAR+1) mod n == FRONT, empty: REAR == FRONT

b)

Full: (REAR+1) mod n == FRONT, empty: (FRONT+1) mod n == REAR

c)

Full: REAR == FRONT, empty: (REAR+1) mod n == FRONT

d)

Full: (FRONT+1) mod n == REAR, empty: REAR == FRONT

8.

Consider the following operation along with Enqueue and Dequeue operations on queues, where k is a global parameter.

What is the worst case time complexity of a sequence of n MultiDequeue() operations on an initially empty queue?

a)

O(n)

b)

O(n+k)

c)

O(nk)

d)

O(n^2)

9.

Suppose implementation supports an instruction REVERSE, which reverses the order of elements on the stack, in addition to the PUSH and POP instructions. Which one of the following statements is TRUE with respect to this modified stack?

a)

A queue cannot be implemented using this stack.

b)

A queue can be implemented where ENQUEUE takes a single instruction and DEQUEUE takes a sequence of two instructions.

c)

A queue can be implemented where ENQUEUE takes a sequence of three instructions and DEQUEUE takes a single instruction.

d)

A queue can be implemented where both ENQUEUE and DEQUEUE take a single instruction each.

10.

Let Q denote a queue containing sixteen numbers and S be an empty stack. Head(Q) returns the element at the head of the queue Q without removing it from Q. Similarly Top(S) returns the element at the top of S without removing it from S. Consider the algorithm given below.


The maximum possible number of iterations of the while loop in the algorithm is

a)

16

b)

32

c)

64

d)

256

11.

Following is C like pseudo code of a function that takes a number as an argument, and uses a stack S to do processing.


What does the above function do in general?

a)

Prints binary representation of n in reverse order

b)

Prints binary representation of n

c)

Prints the value of Logn

d)

Prints the value of Logn in reverse order

12.

The following postfix expression with single digit operands is evaluated using a stack:

Note that ^ is the exponentiation operator. The top two elements of the stack after the first * is evaluated are:

a)

6,1

b)

5,7

c)

3,2

d)

1,5

13.

If the sequence of operations - push (1), push (2), pop, push (1), push (2), pop, pop, pop, push (2), pop are performed on a stack, the sequence of popped out values

a)

2,2,1,1,2

b)

2,2,1,2,2

c)

2,1,2,2,1

d)

2,1,2,2,2

14.

The five items: A, B, C, D, and E are pushed in a stack, one after other starting from A. The stack is popped four items and each element is inserted in a queue. The 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

a)

A

b)

B

c)

C

d)

D

e)

E

15.

Consider the following operations performed on a stack of size 5 : Push (a); Pop() ; Push(b); Push(c); Pop(); Push(d); Pop();Pop(); Push (e) Which of the following statements is correct?

a)

Underflow occurs

b)

Stack operations are performed smoothly

c)

Overflow occurs

d)

None of the above

16.

Which of the following is not an inherent application of stack?

a)

Implementation of recursion

b)

Evaluation of a postfix expression

c)

Job scheduling

d)

Reverse a string

17.

If you're building a system for online voting where each vote is unique, but the order in which votes are cast doesn't matter, which interface would be the best pick?

a)

List Interface

b)

Set Interface

c)

Queue Interface

d)

Map Interface

18.

When designing an application to manage a restaurant's waiting list where customers are served based on their arrival, which interface would be best suited?

a)

List Interface

b)

Set Interface

c)

Queue Interface

d)

Map Interface

19.

What distinguishes the LinkedHashSet class from the HashSet class in the Java Collections framework?

a)

LinkedHashSet maintains insertion order

b)

LinkedHashSet allows duplicate values

c)

LinkedHashSet uses a tree structure for storage

d)

LinkedHashSet provides faster access times

20.

If you need a data structure that can be accessed and modified by multiple threads safely, which class would you select?

a)

ArrayList Class

b)

Vector Class

c)

LinkedList Class

d)

HashSet Class

21.

When you need a resizable array with efficient random access, which class is the most relevant choice?

a)

LinkedList Class

b)

ArrayList Class

c)

HashSet Class

d)

TreeSet Class

22.

If you need a collection that ensures a first-in-first-out (FIFO) behavior, which interface would be the best fit?

a)

Set Interface

b)

List Interface

c)

Map Interface

d)

Queue Interface

23.

Which interface ensures element order based on their natural ordering or by a provided comparator?

a)

Set Interface

b)

List Interface

c)

SortedSet Interface

d)

Queue Interface

24.

When designing a data structure that allows duplicates, maintains insertion order, and provides random access, which interface is the most suitable?

a)

Set Interface

b)

LinkedHashSet Class

c)

List Interface

d)

TreeSet Class

25.

Which interface is best suited to represent a group of objects as a single unit?

a)

List Interface

b)

Set Interface

c)

Collection Interface

d)

Queue Interface

26.

What's the primary advantage of using the SortedSet Interface over the standard Set Interface?

a)

It allows duplicates

b)

It processes items in a FIFO manner

c)

It sorts items based on their natural order or a defined comparator

d)

It allows random access

27.

Which of the following statements are true with respect to sets as objects of Hashset?

a)

Set is always an ordered group of objects.

b)

Set allows duplicate objects.

c)

Set does not allow duplicate objects.

d)

Set supports iterators.

28.

Which of the following statements are correct in relation to collections in Java?

a)

A collection can have objects of different types in it.

b)

The Collection is a class.

c)

A collection can have only one type of object in it.

d)

A collection is same as a standard array.

29.

Which of the following interfaces declares a core method that all collections will have?

a)

EventListener

b)

Comparator

c)

Set

d)

Collection

30.

Which of the following statements are correct with respect to a Map in Java?

a)

A map can have iterators for accessing its elements.

b)

The elements of a map are accessed by the respective keys.

c)

Maps are the same as a SortedSet.

d)

None of these.

31.

Which of the following interfaces are not a part of the Java's Collection Framework?

a)

SortedList

b)

Set

c)

SortedMap

d)

List

32.

Which of the following statements are correct with respect to a List in Java?

a)

A List is implemented in the "first-in, first-out" (FIFO) order.

b)

A List is implemented in the "first-in, last-out" (FILO) order.

c)

Lists support duplicate items.

d)

None of these.

33.

Which of the following interfaces must contain a unique element?

a)

List

b)

Set

c)

Array

d)

Collection

34.

Which of the following method deletes all the elements from invoking (calling) collections?

a)

reset()

b)

clear()

c)

delete()

d)

refresh()

35.

Which of the following statements are true with respect to a stack?

a)

A Stack is implemented in the "first-in, first-out" (FIFO) order.

b)

A Stack is implemented in the "first-in, last-out" (FILO) order.

c)

A Stack supports random access.

d)

None of these.

36.

Which of the following classes implement the Queue interface?

a)

HashSet

b)

TreeSet

c)

LinkedList

d)

None of these

37.

What is Collection in Java?

a)

A group of objects

b)

A group of classes

c)

A group of interfaces

d)

None of the mentioned

38.

Which of these interface is not part of Java’s collection framework?

a)

List

b)

Sorted List

c)

Set

d)

Sorted Map

39.

 What will be the output of the  Java program?

a)

{0, 1, 3, 4}

b)

{0, 1, 2, 4}

c)

{0, 1, 2, 3, 4}

d)

{0, 0, 0, 3, 4}

40.

Which of these return type of hasNext() method of an iterator?

a)

Integer

b)

Double

c)

Boolean

d)

Collections Object

41.

Which of these exceptions is thrown by remover() method?

a)

IOException

b)

SystemException

c)

ObjectNotFoundExeception

d)

IllegalStateException

42.

Which of these methods deletes all the elements from invoking collection?

a)

clear();

b)

reset();

c)

delete();

d)

remove();

43.

Which of these interface declares core method that all collections will have?

a)

Collection

b)

EventListner

c)

Comparator

d)

Set

e)

List

44.

Which of these is an incorrect form of using method max() to obtain maximum element?

a)

max(Collection c)

b)

max(Collection c, Comparator comp)

c)

max(Comparator comp)

d)

max(List c)

45.

Which of these methods can convert an object into a List?

a)

SetList()

b)

ConvertList()

c)

CopyList()

d)

singletonList()

46.

Which of these is true about unmodifiableCollection() method?

a)

unmodifiableCollection() returns a collection that cannot be modified.

b)

unmodifiableCollection() method is available only for List and Set.

c)

unmodifiableCollection() is defined in Collection class.

d)

None of the mentioned.

47.

Which of these is static variable defined in Collections?

a)

EMPTY_SET

b)

EMPTY_LIST

c)

EMPTY_MAP

d)

All of the mentioned

48.

What implementation of Iterator can traverse a collection in both directions?

a)

Iterator

b)

ListIterator

c)

SetIterator

d)

MapIterator

49.

Which is faster and uses less memory?

a)

ListEnumeration

b)

Iterator

c)

Enumeration

d)

ListIterator

50.

What will be output of given code

a)

a followed by concurrentModification Exception

b)

a b c

c)

a b

d)

a c

51.

Which interface does java.util.Hashtable implement?

a)

Java.util.Map

b)

Java.util.List

c)

Java.util.HashTable

d)

Java.util.Collection

52.

You need to store elements in a collection that guarantees that no duplicates are stored and all elements can be accessed in natural order.

Which interface provides that capability?

a)

java.util.Map

b)

java.util.Set

c)

java.util.List

d)

java.util.Collection

53.

You need to store elements in a collection that guarantees that no duplicates are stored and all elements can be accessed in natural order.

Which interface provides that capability?

a)

java.util.Map

b)

java.util.Set

c)

java.util.List

d)

java.util.Collection

54.

Which collection class allows you to grow or shrink its size and provides indexed access to its elements, but whose methods are not synchronized?

a)

java.util.HashSet

b)

java.util.LinkedHashSet

c)

java.util.List

d)

java.util.ArrayList

55.

The default capacity of a Vector is:

a)

10

b)

12

c)

8

d)

0

56.

The Comparable interface contains which called?

a)

toCompare

b)

compare

c)

compareTo

d)

compareWith

57.

Find the output

public class TreeMapTest {

public static void main(String args[]) {

Map<Integer, String> m = new TreeMap<Integer, String>();

m.put(11, "audi");

m.put(null, null);

m.put(11, "bmw");

m.put(null, "ferrari");

System.out.println(m.size());

System.out.println(m);

}

a)

Nullpointer exception

b)

compiler error

c)

2

d)

Null value

58.

public class MyClass {

public static void main(String args[]) {

Map<String, String> hashMap = new HashMap<String, String>();

hashMap.put(new String("a"), "audi");

hashMap.put(new String("a"), "ferrari");

System.out.println(hashMap);

}

}

a)

a=audi

b)

a=audi

a=ferrari

c)

a=ferrari

d)

Run time Error

59.

Deque and Queue are derived from

a)

Abstract class

b)

Collection

c)

Abstactcollection

d)

List

60.

What will be output of given code

a)

1 2 3

b)

1 followed by exception

c)

Compile time error

d)

run time error

61.

Which is best suited to a multi-threaded environment?

a)

WeakHashMap

b)

Hashtable

c)

HashMap

d)

ConcurrentHashMap

62.

TreeMap implements?

a)

Dictionary

b)

HashMap

c)

AbstractMap

d)

NavigableMap

63.

Which of these is synchronized?

a)

TreeMap

b)

HashMap

c)

Hashtable

d)

All of the above

64.

TreeMap

a)

doesn't allow null key

b)

allow many null values

c)

Both

d)

allow many null key

65.

How can you sort given HashMap on basis of values

a)

Implement Comparator interface and override its compare method

b)

It is not possible

c)

Implement Comparator interface and override its compareTo method

d)

Implement Comparator interface and override its comparable method

66.

What does Collections.sort internally uses when number of elements are less than 7?

a)

Insertion sort

b)

Merge sort

c)

Quick sort

d)

None

67.

What does Collections.sort internally uses when number of elements are greater than 7?

a)

Insertion sort

b)

Merge sort

c)

Quick sort

d)

None

68.

In linked list implementation of a queue, where does a new element be inserted?

a)

At the head of link list

b)

At the centre position in the link list

c)

At the tail of the link list

d)

None of the mentioned

69.

In linked list implementation of a queue, the important condition for a queue to be empty is?

a)

FRONT is null

b)

REAR is null

c)

LINK is empty

d)

None of the mentioned

70.

In a circular queue, how do you increment the rear end of the queue?

a)

rear++

b)

(rear+1) % Number of Item

c)

(rear % Number of Item)+1

d)

rear–

71.

Which is the application of a queue?

a)

CPU allocation

b)

Sharing printer

c)

Graph traversal

d)

All

72.

If REAR, FRONT are the queue variables, then identify correct statement while inserting a value

a)

FRONT=1

b)

REAR=REAR+1

c)

REAR=REAR-1

d)

FRONT=FRONT-1

73.

If elements are inserted in the order -10,-2,-3,4,-1,6 then to delete value -3 how many dequeue operations are required?

(a)  

74.

Time complexity of enqueue is

a)

O(n)

b)

O(1)

c)

O(nlogn)

d)

O(logn)

75.

Number of queues required to implement a stack is

a)

1

b)

2

c)

3

d)

cannot be implemented

76.

A circular queue is implemented using an array of size 10. The array index starts with 0, front is 6, and rear is 9. The insertion of next element takes place at the array index

a)

7

b)

9

c)

10

d)

0

77.

In linked list implementation of a queue, where does a new element be inserted?

a)

At the head of link list

b)

At the centre position in the link list

c)

At the tail of the link list

d)

None of the mentioned

78.

In linked list implementation of a queue, from where is the item deleted?

a)

At the head of link list

b)

At the centre position in the link list

c)

At the tail of the link list

d)

None of the mentioned

79.

Let the following circular queue can accommodate maximum six elements with the following data


front = 2 rear = 4

queue = _______; L, M, N, ___, ___


What will happen after ADD O operation takes place?

a)

front = 2 rear = 5

queue = ______; L, M, N, O, ___

b)

front = 3 rear = 5

queue = L, M, N, O, ___

c)

front = 3 rear = 4

queue = ______; L, M, N, O, ___

d)

front = 2 rear = 4

queue = L, M, N, O, ___

80.

What is the reason for using a "circular queue" instead of a regular one?

a)

running time of enqueue() is improved

b)

reuse empty spaces

c)

you can traverse all the elements more efficiently

d)

none of the above

81.

One difference between a queue and a stack is:

a)

Queues require dynamic memory, but stacks do not

b)

Stacks require dynamic memory, but queues do not.

c)

Queues use two ends of the structure; stacks use only one.

d)

Stacks use two ends of the structure, queues use only one.

82.

If the numbers 5, 10, 3, 42 are enqueued onto a queue in that order, what does dequeue return?

a)

5

b)

10

c)

3

d)

42

83.

The essential condition which is checked before insertion in a linked queue is?

a)

Underflow

b)

Overflow

c)

Front value

d)

Rear value

84.

A queue of characters currently contained A,B,C,D What would be the contents of queue after the following operationDELETE, ADD W, ADD X, DELETE, ADD Y.

a)

A,B,C,W,Y

b)

A,B,C,D,W

c)

C,D,W,X,Y

d)

W,Y,X,C,D

85.

If front=rear ,then the queue is?

a)

full

b)

undeflow

c)

overflow

d)

empty

86.

Insertion operation is done at only one end and

deletion operation is done at both the ends

a)

Input Restricted Queue

b)

Output Restricted Queue

c)

Priority Queue

d)

None of these

87.

Deletion operation is done at only one end and insertion

operation is done at both the ends

a)

Input Restricted Queue

b)

Output Restricted Queue

c)

Priority Queue

d)

None of these