Software Development Exam  >  Software Development Tests  >  DSA in C++  >  Test: Graphs - 2 - Software Development MCQ

Test: Graphs - 2 - Software Development MCQ


Test Description

15 Questions MCQ Test DSA in C++ - Test: Graphs - 2

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

Which of the following statements about graphs is true?

Detailed Solution for Test: Graphs - 2 - Question 1

Graphs can have edges that connect a vertex to itself. Such edges are called self-loops.

Test: Graphs - 2 - Question 2

What is the time complexity of the Depth First Search (DFS) algorithm on a graph with V vertices and E edges?

Detailed Solution for Test: Graphs - 2 - Question 2

The time complexity of DFS on a graph with V vertices and E edges is O(V + E) as the algorithm visits each vertex and each edge once.

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

Which of the following algorithms is used to find the shortest path between two vertices in a weighted graph?

Detailed Solution for Test: Graphs - 2 - Question 3

Dijkstra's algorithm is used to find the shortest path between two vertices in a weighted graph.

Test: Graphs - 2 - Question 4

Which of the following algorithms can be used to find all the connected components in an undirected graph?

Detailed Solution for Test: Graphs - 2 - Question 4

Breadth First Search (BFS) can be used to find all the connected components in an undirected graph.

Test: Graphs - 2 - Question 5

In a directed acyclic graph (DAG), a topological sort is:

Detailed Solution for Test: Graphs - 2 - Question 5

Topological sort of a directed acyclic graph (DAG) arranges the vertices in a linear order such that for every directed edge (u, v), u comes before v in the order.

Test: Graphs - 2 - Question 6

What is the output of the following code?
#include <iostream>
#include <list>

int main() {
    std::list<int> adjList[3];
    adjList[0].push_back(1);
    adjList[1].push_back(2);
    adjList[2].push_back(0);

    for (int i = 0; i < 3; i++) {
        std::cout << "Vertex " << i << " is connected to: ";
        for (int v : adjList[i]) {
            std::cout << v << " ";
        }
        std::cout << std::endl;
    }

    return 0;
}

Detailed Solution for Test: Graphs - 2 - Question 6

The code initializes an adjacency list and adds connections between vertices. The output displays the connections for each vertex.

Test: Graphs - 2 - Question 7

What is the output of the following code?
#include <iostream>
#include <stack>

void topologicalSortUtil(int v, bool visited[], std::stack<int>& stack) {
    visited[v] = true;

    for (int u : adjList[v]) {
        if (!visited[u]) {
            topologicalSortUtil(u, visited, stack);
        }
    }

    stack.push(v);
}

int main() {
    // Graph initialization and input omitted for brevity

    std::stack<int> stack;
    bool visited[V] = {false};

    for (int i = 0; i < V; i++) {
        if (!visited[i]) {
            topologicalSortUtil(i, visited, stack);
        }
    }

    while (!stack.empty()) {
        std::cout << stack.top() << " ";
        stack.pop();
    }

    return 0;
}

Detailed Solution for Test: Graphs - 2 - Question 7

The code performs a topological sort using Depth First Search (DFS) and outputs the sorted order of vertices.

Test: Graphs - 2 - Question 8

What is the output of the following code?
#include <iostream>
#include <queue>

void bfs(int v, bool visited[], std::vector<int> adjList[]) {
    std::queue<int> queue;
    visited[v] = true;
    queue.push(v);

    while (!queue.empty()) {
        int u = queue.front();
        queue.pop();

        std::cout << u << " ";

        for (int i : adjList[u]) {
            if (!visited[i]) {
                visited[i] = true;
                queue.push(i);
            }
        }
    }
}

int main() {
    // Graph initialization and input omitted for brevity

    bool visited[V] = {false};

    bfs(0, visited, adjList);

    return 0;
}

Detailed Solution for Test: Graphs - 2 - Question 8

The code performs Breadth First Search (BFS) on the graph starting from vertex 0 and outputs the visited vertices in the order they are visited.

Test: Graphs - 2 - Question 9

What is the output of the following code?
#include <iostream>
#include <vector>

int main() {
    std::vector<std::vector<int>> matrix = {
        {0, 1, 1},
        {1, 0, 1},
        {1, 1, 0}
    };

    int count = 0;
    for (int i = 0; i < matrix.size(); i++) {
        for (int j = i + 1; j < matrix[i].size(); j++) {
            if (matrix[i][j] == 1) {
                count++;
            }
        }
    }

    std::cout << "Number of edges in the graph: " << count << std::endl;

    return 0;
}

Detailed Solution for Test: Graphs - 2 - Question 9

The code counts the number of edges in the graph represented by an adjacency matrix.

Test: Graphs - 2 - Question 10

What is the output of the following code?
#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> arr = {2, 4, 6, 8, 10};
    std::vector<int>::iterator it;
    
    it = std::find(arr.begin(), arr.end(), 5);
    
    if (it != arr.end()) {
        std::cout << "Element found at index: " << std::distance(arr.begin(), it) << std::endl;
    } else {
        std::cout << "Element not found" << std::endl;
    }
    
    return 0;
}

Detailed Solution for Test: Graphs - 2 - Question 10

The code searches for the element 5 in the vector using the 'find' function from the '<algorithm>' library. Since 5 is not present in the vector, the output is "Element not found".

Test: Graphs - 2 - Question 11

Which of the following algorithms can be used to find the minimum spanning tree (MST) of an undirected graph?

Detailed Solution for Test: Graphs - 2 - Question 11

Kruskal's algorithm is used to find the minimum spanning tree (MST) of an undirected graph.

Test: Graphs - 2 - Question 12

What is the output of the following code?
```cpp
#include <iostream>
#include <queue>

void dfs(int v, bool visited[], std::vector<int> adjList[]) {
    visited[v] = true;
    std::cout << v << " ";

    for (int i : adjList[v]) {
        if (!visited[i]) {
            dfs(i, visited, adjList);
        }
    }
}

int main() {
    // Graph initialization and input omitted for brevity

    bool visited[V] = {false};

    dfs(0, visited, adjList);

    return 0;
}
```

Detailed Solution for Test: Graphs - 2 - Question 12

The code performs Depth First Search (DFS) on the graph starting from vertex 0 and outputs the visited vertices in the order they are visited.

Test: Graphs - 2 - Question 13

What is the output of the following code?
```cpp
#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> arr = {5, 3, 9, 1, 7};
    std::sort(arr.begin(), arr.end(), std::greater<int>());
    
    for (int i : arr) {
        std::cout << i << " ";
    }
    
    return 0;
}
```

Detailed Solution for Test: Graphs - 2 - Question 13

The code sorts the vector 'arr' in descending order using 'std::sort' and the 'std::greater' comparator.

Test: Graphs - 2 - Question 14

What is the output of the following code?
```cpp
#include <iostream>
#include <vector>

int main() {
    std::vector<int> arr = {2, 4, 6, 8, 10};
    std::vector<int>::reverse_iterator it;
    
    for (it = arr.rbegin(); it != arr.rend(); ++it) {
        std::cout << *it << " ";
    }
    
    return 0;
}
```

Detailed Solution for Test: Graphs - 2 - Question 14

The code uses reverse iterators '(rbegin()' and 'rend())' to traverse the vector 'arr' in reverse order.

Test: Graphs - 2 - Question 15

What is the output of the following code?
```cpp
#include <iostream>
#include <stack>

void dfs(int v, bool visited[], std::vector<int> adjList[]) {
    std::stack<int> stack;
    visited[v] = true;
    stack.push(v);

    while (!stack.empty()) {
        int u = stack.top();
        stack.pop();

        std::cout << u << " ";

        for (int i : adjList[u]) {
            if (!visited[i]) {
                visited[i] = true;
                stack.push(i);
            }
        }
    }
}

int main() {
    // Graph initialization and input omitted for brevity

    bool visited[V] = {false};

    dfs(0, visited, adjList);

    return 0;
}
```

Detailed Solution for Test: Graphs - 2 - Question 15

The code performs Depth First Search (DFS) on the graph using a stack and outputs the visited vertices in the order they are visited.

153 videos|115 docs|24 tests
Information about Test: Graphs - 2 Page
In this test you can find the Exam questions for Test: Graphs - 2 solved & explained in the simplest way possible. Besides giving Questions and answers for Test: Graphs - 2, EduRev gives you an ample number of Online tests for practice

Top Courses for Software Development

153 videos|115 docs|24 tests
Download as PDF

Top Courses for Software Development