Docsity

Exam 3 with Solutions | C++ Programming | CGS 3464, Exams of Computer Science

Toggle sidebar
bg1
CGS3464 – Exam#3 – 4/14/04 Name: _______________________
Professor Jodi Neely-Ritz Signature: _______________________
1. Write a function for an integer division with exception handling. The exception should
handle the case when the denominator is 0.
Answer:
int a,b,excep;
//enclose within main:
try{
if (b==0) throw excep;
}
catch excep
{
cout<<”divide by 0 attempoted”;
}
// 10
2. what is the output of the following code.
#include<iostream>
using namespace std;
int main()
{
int a=9,b=99,*p1,*p2;
p1=&a;
p2=&b;
cout<<a<<","<<b;
p1=p2;
cout<<a<<","<<b;
*p1=*p2;
cout<<a<<","<<b;
}
//10
Answer:
9, 99
9, 99
9, 99
3. Write a recursive function that calculates the sum till n (i.e. 0 to n).
// 10
int rec(x)
{
if(x==0) return 0;
1
pf3
pf4

Partial preview of the text

Download Exam 3 with Solutions | C++ Programming | CGS 3464 and more Exams Computer Science in PDF only on Docsity!

Professor Jodi Neely-Ritz Signature: _______________________

1. Write a function for an integer division with exception handling. The exception should handle the case when the denominator is 0. Answer: int a,b,excep; //enclose within main: try{ if (b==0) throw excep; } catch excep { cout<<”divide by 0 attempoted”; } // 10 2. what is the output of the following code. #include using namespace std; int main() { int a=9,b=99,p1,p2; p1=&a; p2=&b; cout<<a<<","<<b; p1=p2; cout<<a<<","<<b; p1=p2; cout<<a<<","<<b; } // Answer: 9, 99 9, 99 9, 99 3. Write a recursive function that calculates the sum till n (i.e. 0 to n). // 10 int rec(x) { if(x==0) return 0;

Professor Jodi Neely-Ritz Signature: _______________________ return(x+rec(x-1)); }

  1. Write a function using templates for calculating the sum. It should work for integers and floats. //10 Answer: template class; and normal addition function with 2 template class parameters.
  2. In a linked list, each node has a pointer that point to a. The node itself b. The next node c. The previous node d. The first node in the list. // 5 Answer: b
  3. What are namespaces and what is the namespace that contains cout, cin etc. // 5 namespace: blocks under which variable or functions are defined. std.
  4. What is the output of the following code. #include using namespace std; int rec(int x) { if (x==0) return x; if(x<0) return(rec(x+1)); if(x>0) return(rec(x-1)); } int main() { int a=100,b; b=rec(a); cout<<b; } // 10 Answer: 0
  5. What is the error in the following code

Professor Jodi Neely-Ritz Signature: _______________________ #include using namespace std; int main() { int a=10,b=20,aa; aa=&b; a=(aa); cout<<a<<b<<aa; } // 10 Answer: 200 20 20 12.Explain the terms try, catch and throw in the context of exception handling // 5 try block: block within which might contain problematic code catch: Once exception is thrown, catch block begins execution. throw: to throw an exception 13.What does the following function do and when can this function lead to infinite recursion. int recc(int x) { if(x==1) return(x); else return(x*recc(x-1)); } // 5 Answer: calculates factorial, infinite recursion when x<1. // Total 100 points.