Software Development Exam  >  Software Development Tests  >  Test: Trees - 1 - Software Development MCQ

Test: Trees - 1 - Software Development MCQ


Test Description

15 Questions MCQ Test - Test: Trees - 1

Test: Trees - 1 for Software Development 2024 is part of Software Development preparation. The Test: Trees - 1 questions and answers have been prepared according to the Software Development exam syllabus.The Test: Trees - 1 MCQs are made for Software Development 2024 Exam. Find important definitions, questions, notes, meanings, examples, exercises, MCQs and online tests for Test: Trees - 1 below.
Solutions of Test: Trees - 1 questions in English are available as part of our course for Software Development & Test: Trees - 1 solutions in Hindi for Software Development course. Download more important topics, notes, lectures and mock test series for Software Development Exam by signing up for free. Attempt Test: Trees - 1 | 15 questions in 30 minutes | Mock test for Software Development preparation | Free important questions MCQ to study for Software Development Exam | Download free PDF with solutions
Test: Trees - 1 - Question 1

Which of the following statements about trees is correct?

Detailed Solution for Test: Trees - 1 - Question 1

A tree is a non-linear data structure consisting of nodes connected by edges. It has a hierarchical structure with a root node and children nodes.

Test: Trees - 1 - Question 2

Which of the following is true about a binary tree?

Detailed Solution for Test: Trees - 1 - Question 2

A binary search tree is a binary tree where for each node, the value of all nodes in its left subtree is less than its value, and the value of all nodes in its right subtree is greater than its value.

1 Crore+ students have signed up on EduRev. Have you? Download the App
Test: Trees - 1 - Question 3

In a binary search tree, the elements are stored in such a way that:

Detailed Solution for Test: Trees - 1 - Question 3

A binary search tree is a binary tree where for each node, the value of all nodes in its left subtree is less than its value, and the value of all nodes in its right subtree is greater than its value.

Test: Trees - 1 - Question 4

Which of the following traversal techniques visits the left subtree, then the root, and finally the right subtree?

Detailed Solution for Test: Trees - 1 - Question 4

In the implementation of a binary tree in C++, the NULL keyword is used to represent an empty node or a null pointer.

Test: Trees - 1 - Question 5

Which of the following operations is not typically performed on a binary search tree?

Detailed Solution for Test: Trees - 1 - Question 5

The code creates a binary search tree and inserts four nodes with values 10, 20, 5, and 15. The statement "cout << root->left->right->data << endl;" prints the value of the right child of the left child of the root, which is 5.

Test: Trees - 1 - Question 6

Code and Output:
#include <iostream>
using namespace std;

struct Node {
   int data;
   Node* left;
   Node* right;
};

int main() {
   Node* root = new Node();
   root->data = 10;
   root->left = new Node();
   root->left->data = 20;
   root->right = new Node();
   root->right->data = 30;

   cout << root->left->data << endl;
   return 0;
}
What will be the output of the above code?

Detailed Solution for Test: Trees - 1 - Question 6

The code defines a function 'sumOfLeafNodes' that calculates the sum of leaf nodes in a binary tree. The tree is created with five nodes and the leaf nodes have values 40 and 50. Therefore, the sum of leaf nodes is 40 + 50 = 90.

Test: Trees - 1 - Question 7

Code and Output:
#include <iostream>
using namespace std;

struct Node {
   int data;
   Node* left;
   Node* right;
};

void preorderTraversal(Node* root) {
   if (root == NULL)
      return;

   cout << root->data << " ";
   preorderTraversal(root->left);
   preorderTraversal(root->right);
}

int main() {
   Node* root = new Node();
   root->data = 10;
   root->left = new Node();
   root->left->data = 20;
   root->right = new Node();
   root->right->data = 30;

   preorderTraversal(root);
   return 0;
}
What will be the output of the above code?

Detailed Solution for Test: Trees - 1 - Question 7

The code defines a binary tree structure using the 'Node' struct, where each node has an integer value ('data') and pointers to its left and right child nodes. In the 'preorderTraversal' function, a recursive pre-order traversal is performed on the tree.

In the 'main' function, a binary tree is constructed with a root node having a value of 10. The left child of the root is assigned a value of 20, and the right child is assigned a value of 30.

The 'preorderTraversal' function is then called with the root node as the argument, which prints the data of the current node, recursively calls 'preorderTraversal' for the left subtree, and then recursively calls 'preorderTraversal' for the right subtree.

Test: Trees - 1 - Question 8

Code and Output:
#include <iostream>
using namespace std;

struct Node {
   int data;
   Node* left;
   Node* right;
};

Node* insert(Node* root, int key) {
   if (root == NULL) {
      Node* newNode = new Node();
      newNode->data = key;
      newNode->left = NULL;
      newNode->right = NULL;
      return newNode;
   }

   if (key < root->data)
      root->left = insert(root->left, key);
   else if (key > root->data)
      root->right = insert(root->right, key);

   return root;
}

int main() {
   Node* root = NULL;
   root = insert(root, 10);
   root = insert(root, 20);
   root = insert(root, 5);
   root = insert(root, 15);

   cout << root->left->right->data << endl;
   return 0;
}
What will be the output of the above code?

Detailed Solution for Test: Trees - 1 - Question 8

The code inserts nodes with values 10, 20, 5, and 15 into a binary search tree. The statement "cout << root->left->right->data << endl;" prints the value of the right child of the left child of the root, which is 20.

Test: Trees - 1 - Question 9

Code and Output:
#include <iostream>
using namespace std;

struct Node {
   int data;
   Node* left;
   Node* right;
};

int sumOfLeafNodes(Node* root) {
   if (root == NULL)
      return 0;

   if (root->left == NULL && root->right == NULL)
      return root->data;

   return sumOfLeafNodes(root->left) + sumOfLeafNodes(root->right);
}

int main() {
   Node* root = new Node();
   root->data = 10;
   root->left = new Node();
   root->left->data = 20;
   root->right = new Node();
   root->right->data = 30;
   root->left->left = new Node();
   root->left->left->data = 40;
   root->left->right = new Node();
   root->left->right->data = 50;

   cout << sumOfLeafNodes(root) << endl;
   return 0;
}
What will be the output of the above code?

Detailed Solution for Test: Trees - 1 - Question 9

The code defines a function 'sumOfLeafNodes' that calculates the sum of leaf nodes in a binary tree. The tree is created with five nodes, and the leaf nodes have values 40 and 50. Therefore, the sum of leaf nodes is 40 + 50 = 90.

Test: Trees - 1 - Question 10

Code and Output (HOTS):
#include <iostream>
using namespace std;

struct Node {
   int data;
   Node* left;
   Node* right;
};

bool isBinarySearchTree(Node* root, int minValue, int maxValue) {
   if (root == NULL)
      return true;

   if (root->data < minValue || root->data > maxValue)
      return false;

   return isBinarySearchTree(root->left, minValue, root->data - 1) &&
          isBinarySearchTree(root->right, root->data + 1, maxValue);
}

int main() {
   Node* root = new Node();
   root->data = 10;
   root->left = new Node();
   root->left->data = 5;
   root->right = new Node();
   root->right->data = 15;
   root->right->left = new Node();
   root->right->left->data = 12;
   root->right->right = new Node();
   root->right->right->data = 18;

   bool isBST = isBinarySearchTree(root, INT_MIN, INT_MAX);

   cout << isBST << endl;
   return 0;
}

What will be the output of the above code?

Detailed Solution for Test: Trees - 1 - Question 10

The code defines a struct 'Node' which represents a node in a binary tree. The function 'isBinarySearchTree' is used to check if the given binary tree is a valid binary search tree.

In the 'main' function, a binary tree is created with the following structure:
      10
     /  \
    5    15
        /  \
       12   18

The function 'isBinarySearchTree' is then called with the root of the tree and the minimum and maximum values as arguments. The function recursively checks if each node satisfies the binary search tree property, which states that for any node, all the values in its left subtree should be less than its value, and all the values in its right subtree should be greater than its value.

In this case, all the nodes in the tree satisfy the binary search tree property, so the function returns true. The variable isBST is assigned the value true, which is equivalent to 1. Finally, 'isBST' is printed, resulting in the output 1.

Test: Trees - 1 - Question 11

Code and Output (HOTS):
#include <iostream>
using namespace std;

struct Node {
   int data;
   Node* left;
   Node* right;
};

void printPathsToLeaf(Node* root, int path[], int pathLen) {
   if (root == NULL)
      return;

   path[pathLen] = root->data;
   pathLen++;

   if (root->left == NULL && root->right == NULL) {
      for (int i = 0; i < pathLen; i++)
         cout << path[i] << " ";
      cout << endl;
   } else {
      printPathsToLeaf(root->left, path, pathLen);
      printPathsToLeaf(root->right, path, pathLen);
   }
}

int main() {
   Node* root = new Node();
   root->data = 10;
   root->left = new Node();
   root->left->data = 20;
   root->right = new Node();
   root->right->data = 30;
   root->left->left = new Node();
   root->left->left->data = 40;
   root->left->right = new Node();
   root->left->right->data = 50;

   int path[100];
   printPathsToLeaf(root, path, 0);
   return 0;
}
What will be the output of the above code?

Detailed Solution for Test: Trees - 1 - Question 11

The code defines a function 'printPathsToLeaf' that prints all the paths from the root to the leaf nodes in a binary tree. The tree is created with six nodes, and the function is called with the root node. The output is the paths from the root to the leaf nodes: 10 20 40, 10 20 50, and 10 30.

Test: Trees - 1 - Question 12

#include <iostream>
using namespace std;

struct Node {
   int data;
   Node* left;
   Node* right;
};

Node* findLCA(Node* root, int n1, int n2) {
   if (root == NULL)
      return NULL;

   if (root->data == n1 || root->data == n2)
      return root;

   Node* leftLCA = findLCA(root->left, n1, n2);
   Node* rightLCA = findLCA(root->right, n1, n2);

   if (leftLCA && rightLCA)
      return root;

   return (leftLCA != NULL) ? leftLCA : rightLCA;
}

int main() {
   Node* root = new Node();
   root->data = 10;
   root->left = new Node();
   root->left->data = 20;
   root->right = new Node();
   root->right->data = 30;
   root->left->left = new Node();
   root->left->left->data = 40;
   root->left->right = new Node();
   root->left->right->data = 50;
   root->right->left = new Node();
   root->right->left->data = 60;
   root->right->right = new Node();
   root->right->right->data = 70;

   Node* lca = findLCA(root, 40, 70);

   cout << lca->data << endl;
   return 0;
}

What will be the output of the above code?

Detailed Solution for Test: Trees - 1 - Question 12

The code defines a function 'findLCA' that finds the lowest common ancestor (LCA) of two nodes in a binary tree. The tree is created with eight nodes, and the function is called to find the LCA of nodes with values 40 and 70. The output is the value of the LCA, which is 20.

Test: Trees - 1 - Question 13

#include <iostream>
using namespace std;

struct Node {
   int data;
   Node* left;
   Node* right;
};

void convertToMirror(Node* root) {
   if (root == NULL)
      return;

   convertToMirror(root->left);
   convertToMirror(root->right);

   Node* temp = root->left;
   root->left = root->right;
   root->right = temp;
}

int main() {
   Node* root = new Node();
   root->data = 10;
   root->left = new Node();
   root->left->data = 20;
   root->right = new Node();
   root->right->data = 30;
   root->left->left = new Node();
   root->left->left->data = 40;
   root->left->right = new Node();
   root->left->right->data = 50;

   convertToMirror(root);

   cout << root->left->data << endl;
   return 0;
}
What will be the output of the above code?

Detailed Solution for Test: Trees - 1 - Question 13

The code defines a function 'convertToMirror' that converts a binary tree into its mirror image. The tree is created with five nodes, and the function is called to convert the tree. The statement "cout << root->left->data << endl;" prints the value of the left child of the root, which is 20.

Test: Trees - 1 - Question 14

Which traversal technique can be used to obtain a sorted sequence of elements from a binary search tree?

Detailed Solution for Test: Trees - 1 - Question 14

Inorder traversal of a binary search tree visits the nodes in ascending order of their values. Therefore, it can be used to obtain a sorted sequence of elements from a binary search tree.

Test: Trees - 1 - Question 15

Which of the following statements about binary search trees (BSTs) is correct?

Detailed Solution for Test: Trees - 1 - Question 15

In a binary search tree, the values in the left subtree are always smaller than the values in the right subtree. This property allows for efficient search, insertion, and deletion operations.

Information about Test: Trees - 1 Page
In this test you can find the Exam questions for Test: Trees - 1 solved & explained in the simplest way possible. Besides giving Questions and answers for Test: Trees - 1, EduRev gives you an ample number of Online tests for practice

Top Courses for Software Development

Download as PDF

Top Courses for Software Development