Software Development Exam  >  Software Development Tests  >  DSA in C++  >  Test: Time and Space Complexity - 2 - Software Development MCQ

Test: Time and Space Complexity - 2 - Software Development MCQ


Test Description

15 Questions MCQ Test DSA in C++ - Test: Time and Space Complexity - 2

Test: Time and Space Complexity - 2 for Software Development 2024 is part of DSA in C++ preparation. The Test: Time and Space Complexity - 2 questions and answers have been prepared according to the Software Development exam syllabus.The Test: Time and Space Complexity - 2 MCQs are made for Software Development 2024 Exam. Find important definitions, questions, notes, meanings, examples, exercises, MCQs and online tests for Test: Time and Space Complexity - 2 below.
Solutions of Test: Time and Space Complexity - 2 questions in English are available as part of our DSA in C++ for Software Development & Test: Time and Space Complexity - 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: Time and Space Complexity - 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: Time and Space Complexity - 2 - Question 1

Which of the following asymptotic notations represents the worst-case time complexity of an algorithm?

Detailed Solution for Test: Time and Space Complexity - 2 - Question 1

The O-notation represents the worst-case time complexity of an algorithm. It indicates that the time taken by the algorithm grows linearly with the input size.

Test: Time and Space Complexity - 2 - Question 2

The time complexity of an algorithm is expressed as O(log n). This indicates that the algorithm has a ________ time complexity.

Detailed Solution for Test: Time and Space Complexity - 2 - Question 2

O(log n) represents logarithmic time complexity. It indicates that the time taken by the algorithm increases slowly as the input size grows.

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

Which of the following asymptotic notations represents the upper bound of the time complexity of an algorithm?

Detailed Solution for Test: Time and Space Complexity - 2 - Question 3

O-notation represents the upper bound of the time complexity. It provides an upper limit on the growth rate of an algorithm.

Test: Time and Space Complexity - 2 - Question 4

In terms of time complexity, which of the following statements is correct?

Detailed Solution for Test: Time and Space Complexity - 2 - Question 4

O(log n) grows slower than O(sqrt(n)). Therefore, O(log n) is faster in terms of time complexity.

Test: Time and Space Complexity - 2 - Question 5

The time complexity of an algorithm is given by T(n) = 5n^2 + 3n + 2. What is the dominant term in this equation?

Detailed Solution for Test: Time and Space Complexity - 2 - Question 5

In the given equation, 5n^2 has the highest power of n. Hence, it is the dominant term.

Test: Time and Space Complexity - 2 - Question 6

What will be the output of the following code?
int sum = 0;
for (int i = 0; i < n; i++) {
    for (int j = 1; j < n; j = j * 2) {
        sum++;
    }
}
cout << sum;

Detailed Solution for Test: Time and Space Complexity - 2 - Question 6

The outer loop runs n times, and the inner loop runs log(n) times. Therefore, the total number of iterations is n * log(n), resulting in a time complexity of O(log n).

Test: Time and Space Complexity - 2 - Question 7

What will be the output of the following code?
int func(int n) {
    if (n <= 0) {
        return 1;
    }
    return func(n / 2) + func(n / 2);
}
int result = func(8);
cout << result;

Detailed Solution for Test: Time and Space Complexity - 2 - Question 7

The function recursively calls itself twice for each value. Since the input is halved at each step, the total number of recursive calls is 2^3 = 8. Therefore, the output is 2^8 = 256.

Test: Time and Space Complexity - 2 - Question 8

What will be the output of the following code?
int n = 100;
int count = 0;
while (n > 0) {
    n = n / 2;
    count++;
}
cout << count;

Detailed Solution for Test: Time and Space Complexity - 2 - Question 8

The given code divides n by 2 repeatedly until n becomes 0. The number of divisions required is log2(n) + 1. Therefore, for n = 100, log2(100) + 1 = 6 + 1 = 7.

Test: Time and Space Complexity - 2 - Question 9

What will be the output of the following code?
int n = 5;
for (int i = 1; i <= n; i++) {
    for (int j = i; j <= n; j = j * 2) {
        cout << j << " ";
    }
    cout << endl;
}

Detailed Solution for Test: Time and Space Complexity - 2 - Question 9

The outer loop iterates from 1 to n. For each value of i, the inner loop iterates from i to n, doubling the value of j at each iteration. The output is printed after each inner loop.

Test: Time and Space Complexity - 2 - Question 10

What will be the output of the following code?
int n = 4;
int count = 0;
for (int i = n; i >= 1; i = i / 2) {
    for (int j = 1; j <= i; j++) {
        count++;
    }
}
cout << count;

Detailed Solution for Test: Time and Space Complexity - 2 - Question 10

The outer loop starts with n and is divided by 2 in each iteration until it becomes 1. The number of outer loop iterations is log2(n) + 1. Therefore, for n = 4, log2(4) + 1 = 2 + 1 = 3. The inner loop runs 1, 2, 4 times. Hence, the total count is 3 * 4 = 12.

Test: Time and Space Complexity - 2 - Question 11

What is the time complexity of the following code snippet?
int func(int n) {
    if (n <= 0) {
        return 0;
    }
    return 1 + func(n / 2);
}
int result = func(16);

Detailed Solution for Test: Time and Space Complexity - 2 - Question 11

The function is recursively called with n/2 until n <= 0. The number of recursive calls is proportional to the logarithm of n. Hence, the time complexity is O(log n).

Test: Time and Space Complexity - 2 - Question 12

What is the space complexity of the following code snippet?
void func(int n) {
    if (n <= 0) {
        return;
    }
    cout << n << " ";
    func(n - 1);
    func(n - 1);
}
func(4);

Detailed Solution for Test: Time and Space Complexity - 2 - Question 12

The function func is recursively called twice for each value of n. The maximum depth of the recursion is n. Therefore, the space complexity is O(n).

Test: Time and Space Complexity - 2 - Question 13

Which of the following statements about time complexity is false?

Detailed Solution for Test: Time and Space Complexity - 2 - Question 13

The statement is false. The time complexity of an algorithm can vary based on the input size and the algorithm's implementation. It is not always constant.

Test: Time and Space Complexity - 2 - Question 14

What is the time complexity of the following code snippet?
int sum = 0;
for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j = j * 2) {
        sum++;
    }
}

Detailed Solution for Test: Time and Space Complexity - 2 - Question 14

The outer loop runs n times, and the inner loop doubles the value of j in each iteration until it reaches n. Therefore, the total number of iterations is n * log(n). Hence, the time complexity is O(n^2).

Test: Time and Space Complexity - 2 - Question 15

What is the space complexity of the following code snippet?
int fib(int n) {
    if (n <= 1) {
        return n;
    }
    return fib(n - 1) + fib(n - 2);
}
int result = fib(5);

Detailed Solution for Test: Time and Space Complexity - 2 - Question 15

The given code implements the Fibonacci sequence using recursion. It recursively calls the function twice for each value until n <= 1. The number of function calls grows exponentially with n. Hence, the space complexity is O(2^n).

153 videos|115 docs|24 tests
Information about Test: Time and Space Complexity - 2 Page
In this test you can find the Exam questions for Test: Time and Space Complexity - 2 solved & explained in the simplest way possible. Besides giving Questions and answers for Test: Time and Space Complexity - 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