Course Content
Week 1 Answers 2023 (Free)
0/1
Week 2 Answers 2023
0/1
Week 3 Answers 2023
0/1
Week 4 Answers 2023
0/1
Week 5 Answers 2023
0/1
Week 6 Answers 2023
0/1
Week 7 Answers 2023
0/1
Week 8 Answers 2023
0/1
Week 9 Answers 2023
0/1
Week 10 Answers 2023
0/1
Week 11 Answers 2023
0/1
Week 12 Answers 2023
0/1
PYQ [Week 1-12] NPTEL Programming In Modern C++ Assignment Answers 2023
    About Lesson

    1. Consider the code segment given below.
    #include <iostream>
    #include<cmath>
    int main() {
    int n = 4;
    _________ //LINE-1
    return 0;
    }

    Fill in the blank at LINE-1 such that the output is 16 2.
    a) std:: cout << std::pow(n, 2) < ” ” <‹ std: :sqrt (n)
    b) cout <‹ pow(n,2) <‹ ” ” <‹ sqrt (n)
    c) std:: cout << std::pow(n) « ” ” << std: :sqrt (n)
    d) cout << pow(n) << ” ” << sqrt (n)

    Answer :- a) std::cout << std::pow(n, 2) << " " << std::sqrt(n)
    
    The code segment uses the functions from the <cmath> library to calculate the square and square root of the variable "n" and then prints the results using std::cout. The pow() function calculates the power (in this case, the square) of a number, and the sqrt() function calculates the square root of a number. The output will be "16 2" for n = 4, indicating the square of 4 is 16, and the square root of 4 is 2.

    2. Consider the code segment given below.

    #include <iostream>
    #includevector>
    using namespace std;
    int main(){
    ___________; //LINE-1

    arr.resize (10);
    for (int i=0;i<10;i++)
    arr [i] = i+1;
    return 0;
    }

    Fill in the blank at LINE-1 such that the program will run successfully.

    a) vector<int> arr
    b) vector<int> arr (3)
    c) vector<int> arr [3]
    d) int arr [3]

    Answer :- a) vector<int> arr
    
    The code segment is using vectors from the <vector> library. In order to use a vector named "arr," it needs to be declared with the correct syntax, which is "vector<int> arr;".

    3. Consider the following code segment.

    #include ‹iostream>
    #include<cstring>
    using namespace std;

    int main() {
    string S = S “programming in modern C++”;
    cout << s. size() ; //LINE-1
    cout << strlen(s); //LINE-2
    cout << s. length(); //LINE-3
    cout << strlen(s.c_str()); //LINE-4
    return 0;
    }

    Which line/ will give compilation error/s?

    a) LINE-1
    b) LINE-2
    c) LINE-3
    d) LINE-4

    Answer :- b) LINE-2
    
    In the given code segment, there is a compilation error on LINE-2.
    
    The reason is that "s" is declared as "S" in lowercase, but it should be "S" in uppercase to match the declaration of the string variable "S" on LINE-5. Also, on LINE-1, there is a typo in the assignment statement, it should be "S = "programming in modern C++";" instead of "string S = S "programming in modern C++";".

    4. Consider the code segment given below.

    #include <iostream>
    #include <algorithm>
    using namespace std;
    int main () {
    int data[] = {50, 30, 40, 10, 20};
    sort (&data [2], &data [5]);
    for (int i = 0; i < 5; i++)
    cout <‹ data[i] << “
    return 0;
    }

    What will be the output?

    a) 10 20 30 40 50
    b) 10 30 40 50 20
    c) 50 30 10 20 40
    d) 50 10 20 30 40

    Answer :- c) 50 30 10 20 40
    
    In the given code segment, the "sort" function is used to sort elements of the array "data" starting from the element at index 2 (data[2]) up to the element before index 5 (data[4]). The sort function arranges these elements in ascending order, which results in the output:

    5. Consider the code segment given below.
    #include <iostream>
    #include <string>
    using namespace std;
    int main(void) {
    string stri = “Welcome “;
    string str2 = “students”;
    _________ //LINE-1
    cout << stri;
    return 0;
    }

    Choose the appropriate option to fill in the blank at LINE-1, such that the output of the code would be: Welcome students.

    a) stri += str2
    b) strcat (stri, str2)
    c) str1. append(str2)
    d) str1.insert (str2)

    Answer :- a) stri += str2
    
    The code snippet provided aims to concatenate the strings "stri" and "str2" to produce the output "Welcome students." The operator "+=" is used for string concatenation in C++.

    6. Consider the code segment given below.

    w1q6
    Answer :- b) CORETUPM

    7. Consider the Code segment below.

    w1q7
    Answer :- b) 0 2 6 8 4

    8. Consider the code segment given below.
    #include ‹iostream>
    #include <algorithm>
    using namespace std;
    int main () {
    int iarr [] = {10, 20, 50, 40, 10, 50};
    rotate(&iarr [0], &iarr[2], &iarr [6]) ;
    remove (&iarr[0], &iarr [6], 10); //LINE1
    for (int i = 0; i < 4; ++i) cout << iarr [i] << ” “;
    return 0;
    }

    What will be the output?

    a) 40 10 10 20
    b) 50 40 50 20
    C) 50 50 40 20
    d) 40 10 40 20

    Answer :- C) 50 40 50 20

    9. Consider the code segment given below.
    #include<iostream>
    #include<algorithm>
    #include<string.h>
    using namespace std;
    bool compare (char c1, char c2){
    return tolower (c1) > tolower(c2); //LINE-1
    }
    int main() {
    char arr1[20] “C++ Program”, arr2[20] = “C Program”;
    cout << lexicographical_compare (arr1, arr1+strlen(arr1), arr2, arr2+5, compare) ;
    return 0;
    }

    What will be the output?

    a) 1
    b) 0
    c) -1
    d) Compilation Error: function is not defined

    Answer :- a) 1
    
    The ranges are compared up to the first 5 characters of arr1 and arr2. Since "C++" is lexicographically greater than "C Pro" (comparing case-insensitively), the output of the lexicographical_compare function will be true, which is represented as 1.
    0% Complete