NPTEL An Introduction To Programming Through C++ Assignment 5 Answers (Week 5)
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: a. 1
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. Use the definition of f given in the above question. What does the call f(3,32) return?
a. 1
b. 2
c. 3
d. 4
Answer: b. 2
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
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
Disclaimer: These answers are provided only for the purpose to help students to take references. This website does not claim any surety of 100% correct answers. So, this website urges you to complete your assignment yourself.
Also Available:
NPTEL An Introduction To Programming Through C++ Assignment 4 Answers
An Introduction to Artificial Intelligence NPTEL Assignment 5 Answers