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

This set of MCQ(multiple choice questions) focuses on the An Introduction to Programming Through C++ NPTEL 2022 Week 5 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 5 Assignment Answers” contains 10 questions.

Now, start attempting the quiz.

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

Q1. Consider the following program.

#include<simplecpp>
int func(int a) {
	a = 1;
	return 0;
}
main_program {
	int a = 10;
	func(a);
	cout << a << endl;
}

What is the output of the program?

a) 1
b) 10
c) 0
d) Can’t be determined

Answer: b) 10

Q2. Consider the following program

#include<simplecpp>
int func(int &a){
a += 10;
return a * 10;
}
main_program {
int b = 1, a = 1;
a = func(b);
cout << b << endl;
}

What is the output of the program?

a) 11
b) 110
c) 1
d) Can’t be determined

Answer: a) 11

Q3. Consider the following program.

#include<simplecpp>
int func(int *a){			//1
	return a * 10; 		//2
} 					//3
					//4
main_program{		//5
	int a = 1; 			//6
	int b = *(&a); 				//7
	cout << func(&a); 			//8
} 							//9

The above program is invalid. What will happen when the program is compiled? Select the line number where the compiler will throw an error.

a) It will compile correctly and the output will be 1
b) It will compile correctly and the output will be 10
c) It will throw an error at line 2
d) It will throw an error at line 7

Answer: c) It will throw an error at line 2

Q4. Consider the following program.

int f(int a, int b)
{
if(a>b) return a-b;
else return f(a+1,b/2);
}

What is the maximum number of activation frames that might be present for the call f(2,12), where f is defined as follows? (Include the activation from the main_program in both cases in the count).

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

Answer: d) 4

Q5.

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

Answer: b) 2

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

Q6. Consider the following program.

#include <iostream>
using namespace std;
int mystery(int a, int b)
{
if (b == 0)
return 0;
if (b % 2 == 0)
return mystery(a + a, b/2);
return mystery(a + a, b/2) + a;
}

What value is returned by mystery(a,b) where the mystery function is as defined above?

a) 2*(a+b)+1
b) a*b+a
c) a*b
d) None of the above

Answer: c) a*b

Q7. What does the following function do if arguments passed are (&a,&b) where a and b are integers?

void func1(int *ptr_a, int *ptr_b){
	int * tem;
	tem=ptr_b;
	ptr_b=ptr_a;
	ptr_a=tem;
}

a) Swaps value of a and b
b) Swaps pointers to a and b
c) Doesn’t change anything

Answer: c) Doesn’t change anything

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

Given below is an implementation of multiplication of two numbers via addition. It is based on the idea that a*n = a+a…n times.

int mult_add(int a, int n){
	if(n==BLANK-A) return 0
else{
	int recurse = mult_add(BLANK-B, BLANK-C);
	int ans = a+ recurse;
	return ans;
	}
}

Q8. What is BLANK-A?

Answer: 0

Q9. What is BLANK_B?

Answer: a

Q10. What is BLANK_C?

Answer: n-1

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

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

Programming Assignment 5.1

Write a function that takes a number b, and a number N, and returns true/false indicating whether N is a power of b, ie bk=N for some non negative integer k. The function returns true if N can be expressed as a power of b, and false otherwise.Use the following function signature:
bool isPower(int b,int N).
The following code will be there, but won’t appear in your editor :
main_program{
int b,N;
cin>>b>>N;
if(isPower(b,N))
cout<<“Yes”<<endl;
else
cout<<“No”<<endl;
}
This invisible code will test your function. This code will read the values b and N from the test case and print out “Yes” if your function returns true and “No” otherwise.

You don’t need to write/copy the main program. You just need to write the isPower function, without the main_program or any of the header files.
Note :
 Ensure that the name of the function is isPower and it takes the two parameters and returns true or false without printing anything.
Explanation of Sample Testcase 1
Input : b=2, N=1024
Output : 1024 can be expressed as 210. So the function returns true and Yes gets printed out by the invisible code.

CODE:

bool isPower(int b, int N) {
  int p = b;
  while(p < N)
    p *= b;
  if(p == N)
    return 1;
  else
    return 0;
}

Programming Assignment 5.2

Write a void function that takes two value parameters : a and b, and two reference parameters :q and r. It performs the integer division of a by b, and stores the quotient in q and the remainder in r. All the values : a, b, q and r can be stored in the int data type.Use the following function signature :
void div(int a, int b, int &q, int &r).
The following code will be there, but won’t appear in your editor :
main_program{
int a,b,q,r;
cin>>a>>b;
div(a,b,q,r);
cout<<q<<” “<<r<<endl;
}
This invisible code will test your function. This code will read the values a and b from the test case, call your div function and will print out the modified values of q and r.
You don’t need to write/copy the main program. You just need to write the div function, without the main_program or any of the header files.
Note : Ensure that the name of the function is div, and it takes the four parameters and stores the quotient and remainder of the division a/b in q and r without printing anything.
Explanation of Sample Testcase 1
Input : a=30, b=7
Output : 30/7 gives 4 as the quotient and 2 as the remainder. After the function returns, contains 4 and r contains 2

CODE:

void div(int a, int b, int &q, int &r) {
  *(&q) = a/b;
  *(&r) = a%b;
}

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

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


DISCLAIMER: Use these answers only for the reference purpose. Quizermania doesn't claim these answers to be 100% correct. So, make sure you submit your assignments on the basis of your knowledge.

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.

Leave a Comment

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