An Introduction to Programming Through C++ | NPTEL 2022 | Week 2 Answers

This set of MCQ(multiple choice questions) focuses on the An Introduction to Programming Through C++ NPTEL 2022 Week 2 Answers.

Course layout

Answers COMING SOON! Kindly Wait!

NOTE: You can check your answer immediately by clicking show answer button. An Introduction to Programming Through C++ NPTEL 2022 Week 2 Assignment Answers” contains 12 questions.

Now, start attempting the quiz.

An Introduction to Programming Through C++ NPTEL 2022 Week 2 Assignment Answers

We want to print the following pattern up to N lines :
*
**
***
****
.
.
**********(N times)

Given is an incomplete code for it.

int N;
cin>>N;
int num=blank1;
repeat(blank2){
num=num+1;
repeat(blank3){
​​cout<<“*”;
}
cout<<endl;
}

Q1. What should come in blank 1?

a) 0
b) 1
c) N
d) (N*(N+1))/2

Answer: a) 0

Q2. What should come in blank 2?

a) num
b) N
c) N + num
d) (N*(N+1))/2

Answer: b) N

Q3. What should come in blank 3?

a) num
b) N
c) N + num
d) (N*(N+1))/2

Answer: a) num

Q4. Which of the following creates a picture different from others (ignore the final position/orientation of the turtles)?
OPTIONS =

a) initCanvas(“win1”,1000,1000);
Turtle t1;
repeat(4){
t1.forward(100);
t1.right(90);
}

b) initCanvas(“win1”,1000,1000);
Turtle t1,t2;
t2.right(90);
repeat(2){
t1.forward(100);
t2.forward(50);
t1.right(90);
t2.forward(50);
t2.left(90);
}

c) initCanvas(“win1”,1000,1000);
Rectangle r1(500,500,100,100);

d) initCanvas(“win1”,1000,1000);
Rectangle r1(550,550,100,100);

Answer: c)

Q5. What is the correct(closest) output of the following code snippet?
int a = 360;
cout << a/100.0+20;

OPTIONS =

a) 23
b) 3
c) 23.6
d) 0

Answer: c) 23.6

An Introduction to Programming Through C++ NPTEL Week 2 Assignment Answers

Q6. What is the correct(closest) output of the following code snippet?
int a = 360;
cout << a/100+20.0;

OPTIONS =

a) 23
b) 3
c) 23.6
d) 24

Answer: a) 23

Q7. Which of the following data types usually has the lowest number of bits allocated?
OPTIONS =

a) int
b) float
c) char
d) unsigned int

Answer: c) char

An Introduction to Programming Through C++ NPTEL Week 2 Assignment Answers

Q8. Which of the following is a correct identifier?
OPTIONS =

a) hello-world
b) char
c) 7wonders
d) first_var

Answer: d) first_var

Q9. Which of the following is NOT a valid identifier?
OPTIONS =

a) myname
b) my-name
c) my00name
d) my_name

Answer: b) my-name

Q10. Which of the following are valid expressions for C++ ?
OPTIONS =

a) 1
b) 2(a + b)
c) a+1 / b + 10
d) c % 10 / 2

Answer: a), c), d)

An Introduction to Programming Through C++ NPTEL week 2 Assignment Solutions

Q11. What does the following code snippet calculate?
int n; cin>>n;
int sum = 0;
int term = 1;
repeat(n){
sum+=term;
term+=2;
}
cout<<sum;

OPTIONS=

a) N(N+1)/2
b) 2N-1
c) (N-1)*(N-1)
d) N*N

Answer: d) N*N

An Introduction to Programming Through C++ NPTEL week 2 Assignment Solutions

Q12. The following code snippet is supposed to print the sum of first N natural numbers but it doesn’t. Why is it so?

int n; cin>>n;
int sum = 0, term = 1;
repeat(n){
int sum = 0;
sum+=term;
term++;
}
cout << sum;

OPTIONS=

a) “sum” variable defined inside the repeat block shadows the “sum” variable that is printed.
b) term variable is not updated correctly.
c) sum variable outside the repeat block is not initialised correctly.
d) repeat block is executing for more than n times.

Answer: a) “sum” variable defined inside the repeat block shadows the “sum” variable that is printed.

An Introduction to Programming Through C++ NPTEL Week 2 Programming Assignment Answers

Programming Assignment 2.1

Write a program that given the digits of two numbers A and B prints out the digits of the sum.    The first input is N, the number of digits in A as well as B.  After this,

the digits of A and B will be given one by one from least significant to the most significant.  Your program should print the ith digit of the sum before reading the (i+1)th digit of A and B. You should print N+1 digits for the sum, where the most significant digit can be 0. The length N of the numbers can be large, so in general it won’t be possible to store A or B in the “int” or “long long” data type.  Your program should mimic the manual addition process you learned in primary school, i.e. add digits consecutively together with the carry if any, with initially there being no carry.  Note that if a computer has to do arithmetic on numbers with hundreds of digits, it will be done in the manner of your program.

For example, the sum of the numbers 12945 and 89597 is 102542.  Then the first input will be 5, the number of digits in A,B.  Then the least significant digits will be given, 5 and 7.   Then next least significant, 4, 9 and so on.  This example is shown in full in the third Sample Test Case below.
Note: Please do not include the header files. Start writing your code from main_program. Input

### First line contains an integer ###
N- the number of digits in the numbers
0<N<100

### Followed by N lines ###
A1 B1
A2 B2
.
.
AN BN

Ai is the ith least significant digit of the number A. 
Bi is the ith least significant digit of the number B.
0<= Ai , Bi <=9

Output

The output should be N+1 digits of the sum A+B starting from the least significant. The (N+1)th digit can be 0 in case there is no carry. Note that each digit must appear in a new line. 

If S=A+B, the output should be
S1
S2
.
.
SN
SN+1Where Si is the ith least significant digit of the sum S.

CODE:

int main() {
  int N, i, A, B;
  int C = 0, D, S;
  cin >> N;
  
  repeat(N)
  {
    cin >> A >> B;
    S = A + B + C;
    
    if(S > 9) {
      D = S % 10;
      C = S / 10;
    }
    else {
      D = S;
      C = 0;
    }
    
    cout << D << endl;
  }
  
  cout << C << endl;
}

Programming Assignment 2.2

We buy different quantities of N different items.  You are to write a program that

given the unit cost of each item and the quantity of the item purchased, prints the total amount spent in purchasing all the items. It is guaranteed that all the costs, qty and the total amount can fit into the “int” data type, so use it throughout your program.

Note: Please do not include the header files. Start writing your code from main_program.

Input

### First line contains an integer ###
N- the number of items
0<N<20

### Followed by N lines ###
c1 q1
c2 q2
.
.
cN qN
ci  is the unit cost of the ith item 
qi is the qty purchased of the ith item
0<= ci , qi <=100

Output

An integer denoting the total amount spent on purchasing the items.

CODE:

int main() {
  
  int n, c, q;
  int sum = 0;
  
  cin >> n;
  
  repeat(n) {
    cin >> c >> q;
    sum += c * q;
  }
  
  cout << sum << endl;
  
}

<< Prev- An Introduction to Programming Through C++ Week 1 Solutions

>> Next- An Introduction to Programming Through C++ Week 3 Solutions


Programming in Java NPTEL week 1 quiz answers

NPTEL – Python for Data Science assignment solutions

Nptel – Deep Learning assignment solutions

Social Networks nptel assignment answers

NPTEL answers: Programming in Modern C++

The above question set contains all the correct answers. But in any case, you find any typographical, grammatical or any other error in our site then kindly inform us. Don’t forget to provide the appropriate URL along with error description. So that we can easily correct it.

Thanks in advance.

For discussion about any question, join the below comment section. And get the solution of your query. Also, try to share your thoughts about the topics covered in this particular quiz.

2 thoughts on “An Introduction to Programming Through C++ | NPTEL 2022 | Week 2 Answers”

  1. int main(){
    int t ; cin>>t ;int sum=0 ;
    while(t–){

    int a , b ; cin>>a >> b;
    sum =sum+a +b ;
    cout<<sum%10<<endl ;
    sum/=10;
    }
    cout<<sum<<endl;
    return 0;

    }

Leave a Comment

Your email address will not be published. Required fields are marked *