Pages

NIELIT A5-R5.1 : Data Structure through Object Oriented Programming Language Question Paper January, 2024

NIELIT Question Paper

A5-R5.1 : Data Structure through Object Oriented Programming Language

January, 2024  


PART - ONE

(Answer all the questions; each question carries ONE mark)

Data Structure Through Object Oriented Programming Language


1. Each question below gives a multiple choice of answers. Choose the most appropriate one and enter in the "OMR" answer sheet supplied with the question paper, following instructions therein.


1.1 Concept of Overloading relates to which Object oriented feature-

(A) Encapsulation

(B) Abstraction

(C) Inheritance

(D) Polymorphism.


1.2 What will be the output of following code in C++ ?

#include <iostream>

#include <string>

using namespace std;

int main()

{

char str1[6] = "Happy";

char str2[9] = "Birthday";

char str3[15] = str1+ " " + str2;

cout<<str3;

return 0;

}

(A) HappyBirthday

(B) Error

(C) Happy Birthday

(D) Birthday


1.3 What is the complexity of Binary Search in best case scenario-

(A) O(1)

(B) O(log N)

(C) O(n2)

(D) O(n log n)


1.4 Which algorithm follows the Divide and Conquer Strategy ?

(A) Bubble Sort

(B) Heap

(C) Quicksort

(D) All the above


1.5 Which statement is not true about Arrays ?

(A) Arrays are homogenous in nature

(B) Arrays are dynamic in nature

(C) Array elements are stored in contiguous memory

(D) Arrays are also called as subscripted variable


1.6 Values on the stack can be removed from :

(A) anywhere

(B) bottom position

(C) top position

(D) value cannot be removed


1.7. An algorithm that calls itself directly or indirectly is known as :

(A) Sub algorithm

(B) Recursion

(C) Polish notation

(D) Traversal algorithm


1.8 Which traversal method lists the nodes of binary search tree in ascending order?

(A) post-order

(B) in-order

(C) Pre-order

(D) None of the above


1.9 Which Data Structure is used in implementation of Quicksort ?

(A) stack

(B) set

(C) tree

(D) queue


1.10 Graph can be represented using :

(A) Adjacency Matrix

(B) Incidence Matrix

(C) Linklist

(D) Both (A) and (B)

2. Each statement below is either TRUE or FALSE. Choose the most appropriate one and enter your choice in the "OMR" answer sheet supplied with the question paper, following instructions therein.

2.1 Function malloc returns a pointer of type void * to the memory it allocates. If it is unable to allocate memory, it returns a NULL pointer.

2.2 Following are the steps for a post order traversal of a binary tree.
    Traverse the right subtree in post order
    Traverse the left subtree in post order
    Process the value in the node

2.3 The initial state of queue is w,x,y,z (where 'w' is at the front). The number of additions and deletions required for the final state of queue as z,y,x,w are 3 deletions and 3 additions.

2.4 The order of Binary Search algorithm is log(n).

2.5 In Doubly linklist insertion of a node at the beginning involved modification of three pointers.

2.6 BFS uses Queue Data Structure.

2.7 Linklist are not dynamic in nature.

2.8 A spanning tree consists of (n-1) edges, where 'n' is the number of vertices (or nodes).

2.9 We can use operator ":" to access the overridden function in C++.

2.10 Multiple inheritance leads to ambiguity problem.

 

 

Solution:

1. Concept of Overloading relates to which Object oriented feature-  
(D) Polymorphism.  
Explanation: Overloading is a type of polymorphism where two or more methods in the same class have the same name but different parameters.

2. What will be the output of the following code in C++?

#include <iostream>
#include <string>
using namespace std;
int main()
{
   char str1[6] = "Happy";
   char str2[9] = "Birthday";
   char str3[15] = str1 + " " + str2;
   cout << str3;
   return 0;
}

(B) Error  
Explanation: The code will result in a compilation error because you cannot directly add C-style strings using the `+` operator.

3. What is the complexity of Binary Search in the best-case scenario-  
(A) O(1)  
Explanation: In the best-case scenario, the target element is found in the middle of the array on the first comparison, which takes constant time, O(1).

4. Which algorithm follows the Divide and Conquer Strategy?  
(C) Quicksort.  
Explanation: Quicksort is a classic example of an algorithm that uses the divide and conquer strategy, where the array is partitioned, and the sub-arrays are sorted independently.

5. Which statement is not true about Arrays?  
(B) Arrays are dynamic in nature.  
Explanation: Arrays are not dynamic in nature; their size is fixed at the time of declaration. Dynamic arrays require special handling.

6. Values on the stack can be removed from:  
(C) top position.  
Explanation: A stack follows the Last-In-First-Out (LIFO) principle, meaning elements can only be removed from the top of the stack.

7. An algorithm that calls itself directly or indirectly is known as:  
(B) Recursion.  
Explanation: Recursion occurs when an algorithm calls itself either directly or through another function.

8. Which traversal method lists the nodes of a binary search tree in ascending order?  
(B) in-order.  
Explanation: In-order traversal of a binary search tree (BST) visits nodes in non-decreasing order.

9. Which Data Structure is used in the implementation of Quicksort?  
(A) stack.  
Explanation: Quicksort can be implemented using a stack, particularly in its iterative version to simulate the recursion stack.

10. Graph can be represented using:  
(D) Both (A) and (B).  
Explanation: Graphs can be represented using either an Adjacency Matrix or an Incidence Matrix.

 


2.1 Function malloc returns a pointer of type void * to the memory it allocates. If it is unable to allocate memory, it returns a NULL pointer.
Answer: TRUE
Explanation: This is correct. malloc returns a void * pointer, and if it fails to allocate memory, it returns NULL.

2.2 Following are the steps for a post-order traversal of a binary tree. Traverse the right subtree in post-order, Traverse the left subtree in post-order, Process the value in the node.
Answer: FALSE
Explanation: In post-order traversal, the correct sequence is to traverse the left subtree, then the right subtree, and finally process the node.

2.3 The initial state of queue is w,x,y,z (where 'w' is at the front). The number of additions and deletions required for the final state of queue as z,y,x,w are 3 deletions and 3 additions.
Answer: FALSE
Explanation: To reverse the queue, you would need to perform 3 deletions and 4 additions (re-inserting the deleted elements).

2.4 The order of Binary Search algorithm is log(n).
Answer: TRUE
Explanation: The time complexity of the Binary Search algorithm is O(log n), which is correct.

2.5 In Doubly linked list, insertion of a node at the beginning involves modification of three pointers.

Answer: TRUE
Explanation: To insert a node at the beginning of a doubly linked list, you need to update the new node’s next pointer, the previous head node's previous pointer, and the head pointer itself.

2.6 BFS uses Queue Data Structure.

Answer: TRUE
Explanation: Breadth-First Search (BFS) uses a queue to explore nodes level by level.

2.7 Linked lists are not dynamic in nature.
Answer: FALSE
Explanation: Linked lists are dynamic in nature because their size can change during runtime by adding or removing nodes.

2.8 A spanning tree consists of (n-1) edges, where 'n' is the number of vertices (or nodes).
Answer: TRUE
Explanation: A spanning tree of a graph with n vertices has exactly n-1 edges.

2.9 We can use operator ":" to access the overridden function in C++.
Answer: FALSE
Explanation: In C++, the scope resolution operator :: is used to access an overridden function, not ":".

2.10 Multiple inheritance leads to ambiguity problem.
Answer: TRUE
Explanation: Multiple inheritance can lead to ambiguity, especially when two base classes have methods with the same name and signature.

3.


 

4.




5.
a) Write a program that will create an Employee class with following data members -
Empid
Empname
Salary
    The class should have one parameterized constructor to initialize the data members values and one Function to display the data members value.
    Create an object of Employee class and display its data members value.
    
Here is a C++ program that creates an `Employee` class with the specified data members, a parameterized constructor to initialize them, and a function to display their values:

#include <iostream>
#include <string>
using namespace std;

class Employee {
private:
    int Empid;
    string Empname;
    double Salary;

public:
    // Parameterized constructor to initialize data members
    Employee(int id, string name, double sal) {
        Empid = id;
        Empname = name;
        Salary = sal;
    }

    // Function to display the data members' values
    void display() {
        cout << "Employee ID: " << Empid << endl;
        cout << "Employee Name: " << Empname << endl;
        cout << "Salary: $" << Salary << endl;
    }
};

int main() {
    // Creating an object of Employee class and initializing it
    Employee emp(101, "John Doe", 75000.50);

    // Displaying the data members' values
    emp.display();

    return 0;
}


Explanation:
- Employee Class:
  - Data Members:
    - Empid: Stores the employee ID.
    - `Empname`: Stores the employee name.
    - `Salary`: Stores the employee's salary.
  - Parameterized Constructor:
    - Initializes the data members with the values provided when an object of `Employee` class is created.
  - display() Function:
    - Outputs the values of the data members to the console.

- Main Function:
  - An `Employee` object `emp` is created with ID `101`, name `"John Doe"`, and salary `75000.50`.
  - The `display()` function is called on the `emp` object to print its data members' values.

Output:
When you run this program, it will output:

Employee ID: 101
Employee Name: John Doe
Salary: $75000.5


This demonstrates how the class encapsulates the data and provides a mechanism to initialize and access it.

 

 

***

No comments:

Post a Comment