1. Given a class \"Student\", which of the following is the correct statement to
ID: 3771742 • Letter: 1
Question
1. Given a class "Student", which of the following is the correct statement to create a pointer to an object of class "Student" in the heap? Assume that we use default constructor.
2.Given a "pointer sp" to an object of class "Student" with a public member function "GetName()", which the following is the correct statement to access the member function?
3.There is no difference in how we define the parameters of a recursive versus non-recursive function. Answer True or False.
4.
The following recursion function is CORRECT:
int NFact(int N) {
int factResult;
if (N <= 1) {
factResult = 1;
}
return N * NFact1(N - 1);
}
Explanation / Answer
Answers with Explanation:
(1) Given class "Student" and we are using default constructor
Correct statement to create pointer to an object of class Student is:
Student *sp = new Student(); //and must delete this pointer by statement delete sp when done
(2) Statement to access member function "GetName()" is:
sp->GetName() //Always use -> operator to access member data or functions when you are accessing it using pointer
(3) True
//There is no difference in parameter defination of recursive and non-recursive functions
(4) INCORRECT
int NFact(int N) {
int factResult;
if (N <= 1) {
factResult = 1; //when N <= 1,then you should return value 1 .This is base case of this recursive function
}
return N * NFact1(N - 1);
}
//Correct is
int NFact(int N) {
int factResult;
if (N <= 1) {
factResult = 1;
return factResult;
}
return N * NFact1(N - 1);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.