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 1Structures and Pointers in C++
In C++, which statement correctly declares a structure named Student that can later be used to create variables?
- Student struct { int roll; char name[20]; };
- struct Student { int roll; char name[20]; }
- struct Student { int roll; char name[20]; }; Correct answer.
- struct { int roll; char name[20]; } Student;
Explanation
A structure definition must end with a semicolon after the closing brace, and the keyword struct comes before the tag name. The form struct Student { ... }; both defines the tag and makes Student usable as a type name in C++.
Question 2Structures and Pointers in C++
A structure variable declared as struct Book b; is stored in memory. Which statement about its members is correct?
- All members share one memory location.
- Each member has its own separate memory location within the structure. Correct answer.
- Members are stored in a linked list.
- Members are stored on the heap automatically.
Explanation
A structure is a collection of logically related data items, and each member occupies its own space inside the structure's memory block. The total size is at least the sum of member sizes, possibly more due to padding.
Question 3Structures and Pointers in C++
Given the declaration struct Point { int x; int y; }; and Point p = {3, 7};, what does the expression p.y evaluate to?
- 3
- 10
- 7 Correct answer.
- A compile error
Explanation
The dot operator accesses a named member of a structure variable. In the initialiser {3, 7}, the first value 3 goes to x and the second value 7 goes to y, so p.y is 7.
Question 4Structures and Pointers in C++
Which operator is used to access a member of a structure through a pointer to that structure?
- The address-of operator (&)
- The arrow operator (->) Correct answer.
- The dot operator (.)
- The scope resolution operator (::)
Explanation
When you hold a pointer to a structure, the arrow operator dereferences the pointer and selects the member in one step. Writing ptr->member is equivalent to (*ptr).member.
Question 5Structures and Pointers in C++
In C++, what does the declaration int *p; primarily specify about p?
- p is a constant integer.
- p is a variable that can hold the address of an integer. Correct answer.
- p is an integer variable holding a value.
- p is an array of integers.
Explanation
The asterisk in a declaration marks the variable as a pointer, meaning it stores an address. Here p is intended to hold the address of an int object, and its own type is int*.
Question 6Structures and Pointers in C++
Consider struct Employee { int id; float salary; }; and the declaration Employee e = {101, 25000.0};. Which expression correctly passes the whole structure to a function by value?
- display(e.salary);
- display(&e);
- display(e.id);
- display(e); Correct answer.
Explanation
Passing the structure variable name e copies the entire structure into the function parameter when the parameter is declared as Employee. Passing e.id or e.salary sends only a single member, and &e sends an address.
Question 7Structures and Pointers in C++
A function is declared as void update(struct Item *it);. Inside the function, which expression correctly changes the member price of the structure pointed to by it?
- it.price = 100;
- *it.price = 100;
- it->price = 100; Correct answer.
- &it.price = 100;
Explanation
The arrow operator combines dereferencing and member selection for a pointer to a structure. The equivalent long form would be (*it).price = 100; and both modify the original structure.
Question 8Structures and Pointers in C++
What is the size in bytes of the following structure on a typical 32-bit compiler where int is 4 bytes and char is 1 byte? struct Sample { int a; char b; };
- 8 Correct answer.
- 1
- 4
- 5
Explanation
Compilers align members to boundaries for efficient access, so an int member forces the structure size up to a multiple of 4. The int takes 4 bytes, the char takes 1 byte, and 3 padding bytes are added, giving 8 bytes.
Question 9Structures and Pointers in C++
An array of structures is declared as struct Student s[3];. Which statement about accessing the members of its elements is correct?
- The expression s.member[i] accesses member of element i.
- Each element must be accessed using a separate pointer.
- Array elements of a structure cannot hold different values.
- Members are accessed as s[i].member using the index and the dot operator. Correct answer.
Explanation
An array of structures stores several structure objects contiguously, and each element is reached by subscripting. The dot operator then selects a member of that element, as in s[0].roll.
Question 10Structures and Pointers in C++
What does the new operator return when used as int *p = new int; in C++?
- The value stored at the allocated memory.
- A pointer to the newly allocated memory block. Correct answer.
- A reference to an integer variable.
- The size in bytes of the allocated block.
Explanation
The new operator allocates memory from the free store and yields a pointer of the appropriate type. Here the returned int* is stored in p, and the allocated integer is uninitialised.
Question 11Structures and Pointers in C++
Consider int a = 10; int *p = &a; *p = 25;. What is the value of a after these statements execute?
- The address of a
- 25 Correct answer.
- 10
- 0
Explanation
The pointer p holds the address of a, so dereferencing p with *p refers to the same storage as a. Assigning 25 through *p therefore changes a to 25.
Question 12Structures and Pointers in C++
Which statement correctly explains why a structure is passed by reference (using a pointer or reference parameter) to a function that must modify its members?
- Passing by reference prevents the function from reading the members.
- Passing by value makes the structure members constant.
- Passing by value always causes a compile error for structures.
- Passing by value copies the structure, so changes affect only the copy. Correct answer.
Explanation
When a structure is passed by value, the function receives a separate copy, so any assignment to members is lost when the function returns. Passing a pointer or reference lets the function operate on the original object and modify it.
Question 13Structures and Pointers in C++
In C++, which statement about a structure containing a pointer member is correct?
- The pointer member is copied as an address, so two structures may point to the same data. Correct answer.
- The pointer member automatically copies the pointed-to data when the structure is copied.
- The pointer member is always set to null on copying.
- The pointer member cannot be a member of a structure.
Explanation
A default structure copy performs a memberwise copy, so a pointer member copies only the address it holds. Both structures then refer to the same dynamically allocated object, which is why deep copying is often needed.
Question 14Structures and Pointers in C++
A structure is defined as struct Node { int data; Node *next; };. What is the primary purpose of the member next in this definition?
- To allocate memory for the structure automatically.
- To store a duplicate of the data value.
- To hold the address of another Node, linking nodes together. Correct answer.
- To count the number of nodes created.
Explanation
A self-referential structure contains a pointer to its own type, which allows nodes to be chained into linked lists or trees. The next member stores the address of the following node, or a null pointer at the end.
Question 15Structures and Pointers in C++
Consider the code: int *p = new int[5]; for (int i = 0; i < 5; i++) p[i] = i * 2; delete p;. What is the defect in this program?
- The loop runs one iteration too many.
- The pointer p cannot be indexed with square brackets.
- The operator delete[] should be used instead of delete for an array allocated with new[]. Correct answer.
- The array should be indexed from 1.
Explanation
Memory allocated with new[] must be released with delete[] so that the runtime can destroy every element correctly. Using plain delete on an array is undefined behaviour, even though it may appear to work for trivial types.
Question 16Structures and Pointers in C++
A structure is declared as struct Record { int id; char name[20]; }; and a pointer Record *r = new Record; is created. Which expression correctly releases the allocated memory with no leak?
- delete r; Correct answer.
- delete[] r;
- free(r);
- r = NULL;
Explanation
A single object created with new must be released with delete, which calls the destructor and returns the memory. Because new Record allocated one object rather than an array, delete[] would be a mismatch.
Question 17Data Structures: Arrays, Stacks and Queues
In a linear array declared as A[1..20], what is the index of the last element?
- 19
- 0
- 20 Correct answer.
- 21
Explanation
A linear array declared with bounds 1..20 stores exactly 20 elements, numbered from the lower bound 1 up to the upper bound 20. The last element therefore sits at index 20, and its address is computed from the base address using this index.
Question 18Data Structures: Arrays, Stacks and Queues
Which statement about the memory used by a one-dimensional array in C++ is true?
- Its size can change freely during program execution.
- Each element is stored in a separate linked node.
- Its elements are stored in a random order in memory.
- Its elements occupy consecutive memory locations. Correct answer.
Explanation
An array is a static linear structure whose elements are placed in contiguous memory cells, which is why any element can be reached in constant time by address arithmetic. This contiguity is the defining property that separates arrays from linked structures.
Question 19Data Structures: Arrays, Stacks and Queues
A stack follows which principle for inserting and deleting elements?
- Priority-based removal
- Random access by index
- Last In First Out Correct answer.
- First In First Out
Explanation
In a stack, insertion (push) and deletion (pop) both happen at the same end called the top, so the element added most recently is the first one removed. This Last In First Out behaviour is the standard definition of a stack.
Question 20Data Structures: Arrays, Stacks and Queues
In a queue, at which positions do insertion and deletion take place?
- Insertion and deletion both at the rear
- Insertion at the front and deletion at the rear
- Insertion and deletion both at the front
- Insertion at the rear and deletion at the front Correct answer.
Explanation
A queue is a First In First Out structure, so new elements join at the rear and the oldest element leaves from the front. Keeping the two ends separate is what preserves the arrival order of the elements.
Question 21Data Structures: Arrays, Stacks and Queues
What is the value of the postfix expression 5 3 + 2 * ?
- 10
- 13
- 16 Correct answer.
- 30
Explanation
Scanning left to right, push 5 and 3, then the + operator pops them and pushes 8. Push 2, and the * operator pops 8 and 2 to push 16, which is the final result.
Question 22Data Structures: Arrays, Stacks and Queues
Which data structure is most suitable for reversing the characters of a string?
- Array
- Queue
- Stack Correct answer.
- Graph
Explanation
Pushing all characters onto a stack and then popping them returns them in reverse order because a stack removes the most recent item first. This makes the stack the natural structure for reversal.
Question 23Data Structures: Arrays, Stacks and Queues
What is the main disadvantage of a simple linear queue implemented with a fixed-size array?
- It requires a doubly linked list to work.
- It wastes space because the front positions cannot be reused. Correct answer.
- It always removes the newest element first.
- It cannot store more than two elements.
Explanation
In a simple linear queue the front index only moves forward, so cells vacated by deletions are never reused and the rear eventually reaches the end even when free space exists at the start. A circular queue solves this by wrapping the indices around.
Question 24Data Structures: Arrays, Stacks and Queues
An array is declared as int A[5] in C++ and stores values at A[0] to A[4]. If the base address is 1000 and each int takes 4 bytes, what is the address of A[3]?
- 1004
- 1012 Correct answer.
- 1003
- 1016
Explanation
The address of an element is base address plus index multiplied by the size of one element. Here 1000 + 3 × 4 = 1012, so A[3] begins at byte 1012.
Question 25Data Structures: Arrays, Stacks and Queues
A stack is stored in an array with top initially equal to -1. What does top equal immediately after one push operation?
- -1
- 2
- 0 Correct answer.
- 1
Explanation
With top = -1 the stack is empty, so the first push increments top to 0 and stores the element there. Therefore after one push the top index is 0.
Question 26Data Structures: Arrays, Stacks and Queues
What does the operation peek() or top() do on a stack?
- Inserts a new element at the top
- Checks whether the stack is empty
- Removes the top element and returns it
- Returns the top element without removing it Correct answer.
Explanation
peek() only reads the value at the top of the stack and leaves the structure unchanged, so the element remains available for a later pop. Removal is done by pop(), and insertion by push().
Question 27Data Structures: Arrays, Stacks and Queues
A circular queue is declared with MAX = 6, front = 2 and rear = 5. After one enqueue operation, what is the new value of rear?
- 0 Correct answer.
- 5
- 1
- 6
Explanation
In a circular queue the rear is advanced as rear = (rear + 1) % MAX. With rear = 5 and MAX = 6, the calculation gives (5 + 1) % 6 = 0, so the rear wraps around to index 0.
Question 28Data Structures: Arrays, Stacks and Queues
Which condition correctly identifies a full stack stored in an array of size N with indices 0 to N-1?
- top == 0
- top == N - 1 Correct answer.
- top == -1
- top == N
Explanation
The highest valid index in an array of size N is N - 1, so the stack is full when top has reached that index and no further cell is free. Attempting another push in this state causes stack overflow.
Question 29Data Structures: Arrays, Stacks and Queues
Which of these is a classic application of a queue in computing?
- Evaluating a postfix expression
- Reversing a linked list
- Checking balanced brackets in an expression
- Scheduling print jobs in the order they arrive Correct answer.
Explanation
Print jobs are served in arrival order, so a queue, which removes the oldest request first, matches the requirement exactly. The other listed tasks depend on last-in-first-out behaviour.
Question 30Data Structures: Arrays, Stacks and Queues
Convert the infix expression A + B * C to postfix form, assuming * has higher precedence than +.
- A B + C *
- A B C + *
- A B C * + Correct answer.
- A B * C +
Explanation
Because * binds more tightly than +, the multiplication B * C is performed first and becomes B C *. The addition then combines A with that result, giving the postfix string A B C * +.
Question 31Data Structures: Arrays, Stacks and Queues
A queue is stored in an array of size 5 with front = 3 and rear = 3, and it currently holds one element. What does dequeue do?
- Reports a queue underflow without removing anything
- Removes the element and leaves the queue empty Correct answer.
- Removes the element and sets front to 4
- Removes the element and sets rear to 4
Explanation
When front equals rear and the queue holds exactly one element, removing that element empties the queue. Implementations reset both front and rear to -1 in this situation so that the empty state is unambiguous.
Question 32Data Structures: Arrays, Stacks and Queues
What is the time complexity of accessing the k-th element of a one-dimensional array by index?
- O(n)
- O(k)
- O(log n)
- O(1) Correct answer.
Explanation
Because array elements sit in contiguous memory, the address of any element is computed directly as base + k × size, so the access takes constant time regardless of k. No traversal is needed.
Question 33Data Structures: Arrays, Stacks and Queues
Which sequence of stack operations on an initially empty stack causes stack underflow?
- pop() when top equals -1 Correct answer.
- push(10) when top equals -1
- push(10) followed by pop()
- peek() when top equals 0
Explanation
Underflow means trying to remove an element from an empty stack, which is detected by top being -1. Calling pop() in that state is invalid and is the classic underflow condition.
Question 34Data Structures: Arrays, Stacks and Queues
A queue is implemented with an array of size MAX. Which pair of conditions correctly tests empty and full for a circular queue that keeps one slot unused?
- front == rear for empty, rear == MAX - 1 for full
- front == -1 for empty, rear == front for full
- front == rear for empty, (rear + 1) % MAX == front for full Correct answer.
- front == rear - 1 for empty, rear == MAX for full
Explanation
In the circular queue that sacrifices one slot, emptiness is signalled when front equals rear, while fullness is detected when the next rear position would collide with front. Using (rear + 1) % MAX == front avoids confusing the full and empty states.
Question 35Data Structures: Arrays, Stacks and Queues
What is the postfix form of the infix expression (A + B) * (C - D)?
- A B + C D - * Correct answer.
- A B + C D * -
- A B C D + - *
- A B C + D - *
Explanation
The brackets force A + B and C - D to be evaluated first, producing A B + and C D - respectively, and the outer multiplication then joins them as A B + C D - *. The order of the two sub-expressions must be preserved.
Question 36Data Structures: Arrays, Stacks and Queues
A stack of capacity 4 is processed with the operations push(1), push(2), push(3), push(4), push(5). What happens on the fifth push?
- The oldest element 1 is automatically removed
- The stack doubles its capacity automatically
- The value 5 replaces the value at the bottom
- A stack overflow occurs because top has reached capacity - 1 Correct answer.
Explanation
An array-based stack of capacity 4 is full once top equals 3, so a fifth push cannot store anything and reports stack overflow. Automatic resizing and automatic eviction are features of other structures, not of a plain fixed array stack.
Question 37Data Structures: Arrays, Stacks and Queues
Consider evaluating the postfix expression 6 2 3 * - 4 2 / + with a stack. What is the final result?
- -2
- 2 Correct answer.
- 4
- 8
Explanation
First 2 * 3 gives 6, and 6 - 6 gives 0. Then 4 / 2 gives 2, and 0 + 2 gives 2, which is the value left on the stack.
Question 38Data Structures: Arrays, Stacks and Queues
In a circular queue of size 8, front = 6 and rear = 2, with the convention that front points to the first element and one slot is kept free. How many elements are currently stored?
- 5
- 6
- 4 Correct answer.
- 3
Explanation
The count is computed as (rear - front + MAX) % MAX, which gives (2 - 6 + 8) % 8 = 4. The queue therefore holds four elements, occupying indices 6, 7, 0 and 1.
Question 39Data Structures: Arrays, Stacks and Queues
Why does a circular queue use the modulo operation on its front and rear indices?
- To sort the elements stored inside the queue
- To increase the maximum capacity of the array
- To make the indices wrap around so freed cells at the start are reused Correct answer.
- To convert the queue into a stack when it becomes full
Explanation
Applying modulo MAX to the advancing indices makes them return to 0 after reaching the last cell, so space released at the front becomes available again. This removes the wasted-space problem of a simple linear queue without changing the array size.
Question 40Data Structures: Arrays, Stacks and Queues
A programmer implements a queue using two stacks, where enqueue pushes onto stack S1 and dequeue pops from S2, transferring all elements from S1 to S2 when S2 is empty. Which statement about this design is correct?
- Elements leave in last-in-first-out order, so it behaves as a stack
- Each element is moved at most twice, so the amortised cost per operation is O(1) Correct answer.
- The design fails because a queue cannot be built from stacks
- Each dequeue always requires transferring every element, giving O(n) per operation
Explanation
An element is pushed once onto S1, moved once to S2, and popped once, so the total work is constant per element even though a single dequeue may occasionally cost O(n). Averaged over all operations, the amortised cost is O(1).
Question 41Linked Lists and Memory Management
In a singly linked list, each node stores a data field and which other component that allows traversal to the next node?
- A copy of the head pointer of the list
- A count of the total number of nodes
- An index giving the node's position in an array
- A pointer holding the address of the next node Correct answer.
Explanation
A singly linked list node is a self-referential structure whose second field is a pointer (link) storing the address of the following node. Traversal works by following these links from the head until a NULL link marks the end. The other components listed are not part of node structure in a singly linked list.
Question 42Linked Lists and Memory Management
A linked list is preferred over an array when the program mainly needs which operation?
- Frequent random access to the k-th element
- Compact storage with no pointer overhead
- Frequent insertion and deletion at arbitrary positions Correct answer.
- Direct binary search over sorted data
Explanation
Inserting or deleting at a known position in a linked list only requires adjusting a few pointers, whereas an array may need to shift many elements. Random access and binary search, by contrast, need the constant-time indexing that arrays provide. Pointer storage per node is extra overhead, so compactness favours arrays.
Question 43Linked Lists and Memory Management
What does the head pointer of a linked list represent?
- The address of the first node of the list Correct answer.
- The node holding the largest data value
- The last node, whose link is NULL
- The total number of nodes present
Explanation
The head is an external pointer that stores the address of the first node; it is the only entry point for traversal. If the list is empty, the head holds NULL. It does not store a value, a count, or the tail unless a separate tail pointer is maintained.
Question 44Linked Lists and Memory Management
Which condition correctly tests whether a singly linked list with head pointer 'head' is empty?
- head->next == head
- head == NULL Correct answer.
- head->data == 0
- head->next == NULL
Explanation
An empty list has no first node, so the head pointer itself is NULL. Testing head->next would dereference a NULL pointer when the list is empty, which is invalid. The circular condition head->next == head describes a one-node circular list, not an empty one.
Question 45Linked Lists and Memory Management
A new node 'newNode' is to be inserted at the very beginning of a non-empty singly linked list. Which pair of statements is correct?
- newNode->next = head; head = newNode; Correct answer.
- head = newNode; newNode->next = head;
- head->next = newNode; newNode->next = head;
- newNode->next = NULL; head->next = newNode;
Explanation
The new node must first be linked to the current first node, then the head must be updated to point to the new node. Reversing the order loses the address of the old first node. The other orders either create a self-loop or overwrite the old link before it is used.
Question 46Linked Lists and Memory Management
In a singly linked list, what is the time complexity of deleting the node that immediately follows a given node, when a pointer to that given node is already available?
- O(log n), using binary search on links
- O(n^2), because all later nodes shift
- O(n), because the list must be traversed
- O(1), because only links are changed Correct answer.
Explanation
Deletion after a known node requires only redirecting that node's link to skip the successor, which is a constant number of pointer operations. No traversal or shifting is needed because nodes are not stored contiguously. This is the main advantage of linked deletion over array deletion.
Question 47Linked Lists and Memory Management
A singly linked list must be traversed to display all node values. Which loop correctly does this for head pointer 'temp'?
- while (temp->next != NULL) { print(temp->data); temp = temp->next; }
- while (temp != NULL) { print(temp->data); temp = temp->next; } Correct answer.
- for (temp = head; temp->next; temp++) { print(temp->data); }
- while (temp->data != NULL) { print(temp->data); temp++; }
Explanation
Correct traversal visits each node while the current pointer is not NULL, printing the data and then moving to the next node. The condition must be tested on the pointer, not on the link of the current node, otherwise the last node is skipped. Pointer arithmetic such as temp++ is meaningless for linked nodes.
Question 48Linked Lists and Memory Management
A node is to be inserted after a node pointed to by 'prev' in a singly linked list. Which sequence is correct?
- prev->next = newNode; newNode->next = prev;
- prev = newNode->next; newNode->next = prev;
- newNode->next = prev->next; prev->next = newNode; Correct answer.
- newNode->next = prev; prev->next = newNode;
Explanation
The new node must first take over the link currently held by prev, then prev must be redirected to the new node. This preserves the remainder of the list after the insertion point. Assigning in the opposite order loses the address of the following node.
Question 49Linked Lists and Memory Management
In a doubly linked list node with fields prev and next, which statement correctly links a new node between nodes A and B, where B = A->next?
- A->next = newNode; newNode->prev = A; newNode->next = B;
- newNode->next = A; newNode->prev = B; A->prev = newNode; B->next = newNode;
- newNode->prev = B; newNode->next = A; B->next = newNode;
- newNode->prev = A; newNode->next = B; A->next = newNode; B->prev = newNode; Correct answer.
Explanation
Both directions must be updated: the new node points back to A and forward to B, then A's next and B's prev are redirected to the new node. All four links are needed to keep the doubly linked list consistent. The other options reverse directions or omit the backward link from B.
Question 50Linked Lists and Memory Management
Which statement about a circular linked list is correct?
- The last node points back to the first node instead of NULL. Correct answer.
- The last node's link is NULL, as in a linear list.
- The list can only store integer data.
- Traversal must always stop at the head node after one full cycle.
Explanation
In a circular linked list the final node's link stores the address of the first node, forming a closed ring. Traversal therefore cannot rely on a NULL terminator and must stop when the pointer returns to the starting node. This structure is useful for round-robin scheduling.