c++ programming questions, most points to most complete! Question 17.17. What is
ID: 3771781 • Letter: C
Question
c++ programming questions, most points to most complete!
Question 17.17. What is the output of the following code fragment?
int f1(int base, int limit)
{
if(base > limit)
return -1;
else
if(base == limit)
return 1;
else
return base * f1(base+1, limit);
}
int main()
{
cout << f1(2,4)<<endl;
return 0;
} (Points : 2)
2
3
-1
6
Question 18.18. If your program makes too many recursive function calls, your program will cause a ___________ (Points : 2)
stack underflow
activation overflow
stack overflow
syntax error
Question 19.19. In a node type named MyNode, which of the following correctly declares a pointer to a node of that type? (Points : 2)
MyNode ptr;
MyNode* ptr;
ptr myNode*;
MyNode.data* ptr;
Question 20.20. The arrow operator (->) specifies (Points : 2)
a member of a struct or class that is pointed to by a pointer variable
the pointer member only of a struct or a class that is pointed to by a pointer variable
less than or equal
is known as NULL
Question 21.21. To remove an item from the stack, we call the ____ function (Points : 2)
pop
top
push
empty
Question 22.22. Given the following stack declaration, which of the following function definitions would correctly push an item onto the stack?
struct StackFrame
{
char data;
StackFrame *link;
};
typedef StackFrame* StackFramePtr;
class Stack
{
public:
Stack( );
Stack(const Stack& a_stack);
~Stack( );
void push(char the_symbol);
char pop( );
bool empty( ) const;
private:
StackFramePtr top;
}; (Points : 2)
void Stack::push(char the_symbol)
{
StackFrame temp_ptr;
temp_ptr = new StackFrame;
temp_ptr->data = the_symbol;
temp_ptr->link = top;
top = temp_ptr;
}
void Stack::push(char the_symbol)
{
StackFramePtr temp_ptr;
temp_ptr->data = the_symbol;
temp_ptr->link = top;
top = temp_ptr;
}
void Stack::push(char the_symbol)
{
StackFramePtr temp_ptr;
temp_ptr = new StackFrame;
temp_ptr->data = the_symbol;
temp_ptr->link = top;
}
void Stack::push(char the_symbol)
{
StackFramePtr temp_ptr;
temp_ptr = new StackFrame;
temp_ptr->data = the_symbol;
temp_ptr->link = top;
top = temp_ptr;
}
Question 23.23. Given a linked list and assuming there are at least two nodes in the list, which of the following sets of statements would implement a function to return and remove the last item in the list? (Points : 2)
NodePtr here;
here=head;
while(here->link != NULL)
{
here = here ->link;
}
return here->data;
delete here;
NodePtr here;
here=head;
while(here->link != NULL)
{
here = here ->link;
}
delete here;
return here->data;
int tmp;
NodePtr here, there;
here=head;
while(here->link != NULL)
{
there = here;
here = here ->link;
}
there->link = NULL;
tmp=here->data;
delete here;
return tmp;
int tmp;
NodePtr here, there;
here=head;
while(here->link != NULL)
{
there = here;
here = here ->link;
}
there->link = NULL;
tmp=here->data;
return tmp;
delete here;
Explanation / Answer
Question 17.17
The output of the given code fragment will be 6 .
Output:
Explanation:
The function call is f1(2,4). Then base =2 limit=4.
As base is less than limit, it returns 2 * f1(3, 4).
As base is still less than limit, it returns 2 *3* f1(4, 4).
Now the function call will be f1(4,4). As base is equal, it returns 1.
So finally, it returns 2*3*1=6
Question 18.18.
When a program makes too many recursive function calls, then the program will cause a stack overflow.
Explanation:
Every time a recursive function is called, the values are stored in a stack. The size of the stack is limited and is specified by the operating system. When a program makes too many recursive function calls, all the values have to be stored in a stack. Then the condition stack overflow occurs.
Question 19.19
MyNode* ptr; will correctly declares a pointer to a node of that type.
Question 20.20.
The arrow operator (->) specifies a member of a struct or class that is pointed to by a pointer variable.
Explanation:
When you create a pointer variable of type structure, then the elements of the strcutre are accessed using ->.
For example:
struct emp
{
int empno;
char ename[20];
float salary;
char job[20];
}
emp *e;
Then the elements of the structure can be accessed using -> as e->empno etc.
Question 21.21.
Stack is a data structure in which the last item stored is the first item extracted. The last element that was inserted into the stack (sometimes referred as top) is the element always retrieved from the stack.
The two primary stack operations are push and pop.
push: This operation causes a value to be stored, or pushed onto the stack
pop: This operation removes (the value stored in stack at position top) from the stack
Hence, to remove an item from the stack, we call the pop function.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.