Question 1. 1. The block of code that checks if an unusual situation or error oc
ID: 3772292 • Letter: Q
Question
Question 1.1. The block of code that checks if an unusual situation or error occurs is called (Points : 2) the try blockthe catch block
a function
an error block Question 2.2. When an unusual situation or error occurs, then the ________ statement is executed. (Points : 2) try
throw
error
exiting Question 3.3. Which type of exception is thrown if a call to the new operator fails? (Points : 2) ArithmeticError
DivideByZero
bad_alloc
MemoryError Question 4.4. You should use exception handling (Points : 2) in all your programs
only when you can not handle the exception with simpler control structures
only when you use classes
in every function Question 5.5. Which is the correct way to tell the compiler that the class being declared (ChildClass) is derived from the base class (BaseClass)? (Points : 2) class ChildClass::public BaseClass
class ChildClass:public BaseClass
class ChildClass childOf public BaseClass
class ChildClass derived BaseClass Question 6.6. If the member variables in a base class are private, then (Points : 2) they can be directly accessed or changed in the derived class
the derived class must use any accesssor or modifier functions from the base class
making them private causes a syntax error.
you must declare them in the derived class also. Question 7.7. Using inheritance allows us to (Points : 2) eliminate duplicate code
make our classes more modular
use polymorphism
all of the above
none of the above Question 8.8. If you have the following class definitions, which of the following is the proper way to construct an object of the derived class?
class Pet
{
public:
Pet();
void printPet();
string getName();
void setName(string newName);
private:
string name;
};
class Dog:public Pet
{
public:
Dog();
void printPet();
void setType(string newType);
string getType();
private:
string type;
}; (Points : 2) Dog::Dog():Pet(),type("MUTT")
{
}
Dog::Dog()
{
name="Rover";
}
Pet::Dog():Pet(),type("MUTT")
{
}
Dog::Pet():Pet(),type("MUTT")
{
} Question 9.9. If a base class has public member functions that are not listed by a derived class, then these functions (Points : 2) are not available to the derived class
are inherited unchanged in the derived class
are private to the derived class
do not exist in the derived class Question 10.10. If you have a copy constructor in the base class, but do not have a copy constructor for the derived class, then (Points : 2) you will have a syntax error
a copy constructor for the derived class is automatically created for you
you can not use pointer variables
the default constructor is used Question 11.11. Which of the following would correctly call the base class (BaseClass) assignment operator from the derived class (DerivedClass) assignment operator? (Points : 2) leftSide=rightSide;
rightSide=BaseClass.rightSide;
BaseClass::operator=(rightSide);
DerivedClass::rightSide=BaseClass::rightSide; Question 12.12. Given a class A that derives from a class B that derives from a class C, when an object of class A goes out of scope, in which order are the destructors called? (Points : 2) C, B, then A
A, B, then C
unable to determine
depends on how the code is written for the destructors Question 13.13. Given the following classes and code, what is the output of the last statement shown?
class Pet
{
public:
virtual void print();
string name;
private:
};
class Dog: public Pet
{
public:
void print();
string breed;
};
void Pet::print()
{
cout << "My name is " << name;
}
void Dog::print()
{
Pet::print();
cout << ", and my breed is a "<< breed << endl;
}
Pet* pPtr;
Dog* dPtr=new Dog;
dPtr->name= "Rover";
dPtr->breed="Weiner";
pPtr= dPtr;
pPtr->print(); (Points : 2) My name is Rover
My name is Rover, and my breed is a Weiner
, and my breed is a Weiner
nothing Question 14.14. Which of the following should be virtual if a base class uses dynamic memory allocation? (Points : 2) the constructor
the copy constructor
the print function
the destructor Question 15.15. If a base class has a non-virtual member function named print, and a pointer variable of that class is pointing to a derived object, then the code ptr->print( ); calls (Points : 2) the base class print function
the derived print function
both the derived and base print functions
it causes a run-time error Question 16.16. 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+2, limit);
}
int main()
{
cout << f1(2,4)<<endl;
return 0;
} (Points : 2) -1
2
1
0
It depends 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; Question 24.24. Data is removed from a stack in the _____ _______ it was entered. (Points : 2) same order
reverse order
alternating order
sorted order Question 25.25. In order to create a namespace called student, you use (Points : 2) namespace student
{
//code goes here
}
namespace student
//code goes here
}
student namespace
{
//code goes here
}
{
student namespace
//code goes here
} Question 26.26. Why will the following code not compile?
namespace ns1
{
void print();
void display1(){};
}
namespace ns2
{
void print();
void display2(){};
}
int main()
{
using namespace ns1;
using namespace ns2;
display1();
display2();
print();
return 0;
} (Points : 2) We have not included the iostream library
The call to print is ambiguous
We have not used namespace std
It will compile Question 27.27. Since accessor functions in a class do not modify or mutate the data members of the object, the function should have the __________ modifier. (Points : 2) reference
friend
const
private Question 28.28. Given the following class, what is syntactically wrong with the implementation of the display function?
class Rational
{
public:
Rational();
Rational(int numer, int denom);
Rational(int whole);
int getNumerator();
int getDenominator();
friend void display(ostream& out, const Rational& value);
private:
int numerator;
int denominator;
};
void display(ostream& out, const Rational& value)
{
out << value.getNumerator() << '/"<<value.getDenominator();
} (Points : 2) nothing
value must be not be passed by reference
The get functions are not const functions
out should be pass by value Question 29.29. When overloading an operator, which of the following is true? (Points : 2) One of the arguments must be an object of the class.
The operator can be a friend or a member of the class.
The operator does not have to be a friend or a member of the class.
All of the above
None of the above Question 30.30. Which of the following function declarations would be correct to overload the multiply operator for the Rational numbers class? (Points : 2) friend Rational operator times(const Rational &left, const Rational &right);
Rational operator times(const Rational &left, const Rational &right);
friend Rational operator *(const Rational &left, const Rational &right);
Rational operator *(const Rational &left, const Rational &right); Question 31.31. Given the following class and array declaration, how would you print out the age of the 10th person in the array?
class personClass
{
public:
void setAge(int newAge);
void setGender( char newGender);
void setSalary(float newSalary);
int getAge();
char getGender();
float getSalary();
private:
int age;
char gender;
float salary;
};
personClass people[100]; (Points : 2) cout << people[10];
cout << people[9];
cout << people[9].age;
cout << people[9].getAge(); Question 32.32. When you have a class which has a dynamic array as one of it's members, you should also include _____________ in the class. (Points : 2) a copy constructor
a default constructor
a destructor
the assignment operator
all of the above Question 33.33. Given the following class definition and the following member function header, which is the correct way to output the private data?
class Person
{
public:
void outputPerson(ostream& out);
private:
int age;
float weight;
int id;
};
void Person::outputPerson(ostream& out)
{
//what goes here?
}
(Points : 2) out << person.age << person.weight << person.id;
out << person;
out << age << weight << id;
outputPerson(person); Question 34.34. A member function of a class should be made private (Points : 2) always
only if it will never be used
if it will only be used by other members of the class
never, it is illegal to make a member function private. Question 35.35. A class member function that automatically initializes the data members of a class is called (Points : 2) the init function
an operator
a constructor
a cast Question 36.36. Given the following class definition, what is missing?
class ItemClass
{
public:
ItemClass(int newSize, float newCost);
int getSize();
float getCost();
void setSize(int newSize);
void setCost(float newCost);
private:
int size;
float cost;
}; (Points : 2) nothing
a default constructor
accessor functions
mutator functions Question 37.37. Given the following class, what would be the best declaration for a mutator function that allows the user of the class to change the age?
class Wine
{
public:
Wine();
int getAge();
float getCost();
private:
int age;
float cost;
} (Points : 2) int getAge(int newAge);
Wine();
void setAge();
void setAge(int newAge); Question 38.38. In a class, all members are ____________ by default (Points : 2) public
private
global
all of the above Question 39.39. A derived class has access to (Points : 2) the private functions and variables of its ancestor classes
the public functions and variables of its ancestor classes
only the functions and variables defined in its class Question 40.40. Which of the following assigns to p1 the pointer to the address of value? (Points : 2) *p1=&value;
p1=value;
p1=&value;
&p1 = *value; Question 41.41. What is the output of the following code fragment?
float *p1;
p1 = new float(3);
cout << *p1; (Points : 2) unknown, the address p1 points to is not initialized
unknown, the code is illegal, p1 points to a dynamic array
3.0
0.0 Question 42.42. Given that a typedef for IntPtr defines a pointer to an integer, what would be the correct declaration for a function that expects a reference to an integer pointer? (Points : 2) void f1 (IntPtr& ptr);
void f1 (IntPtr&* ptr);
void f1 (IntPtr*& ptr);
All of the above Question 43.43. Which of the following statements correctly returns the memory from the dynamic array pointer to by p1 to the freestore? (Points : 2) delete p1[];
delete *p1;
delete [] p1;
delete p1; Question 44.44. In which case would you consider using a dynamic array? (Points : 2) If the array is small, and the size is known before the program runs.
If the program needs to get the size of the array from the user.
If the array size is big, but known at compile time
You should always use a dynamic array Question 45.45. A character array terminated with the null character is most correctly called (Points : 2) a c-string
a character array
a string
a string Question 46.46. strcmp(first, second) returns (Points : 2) true if first=second, false otherwise
nothing, it's a void function
>0 if first < second, 0 if first > second, <0 otherwise
<0 if first < second, 0 if first == second, positive otherwise Question 47.47. Given the following code, what is the correct statement to insert the string str2 into str1, directly after the 'd'?
string str1="abcdefg";
string str2="ABCDE"; (Points : 2) str2.insert(4,str1);
str1.insert(4,str2);
insert(str1,4)=str2;
insert(str2,4)=str1; Question 48.48. What is the value of numbers.size() after the following code?
vector<float> numbers; (Points : 2) 0
10
100
NULL
Unknown Question 49.49. Given an array of integers of size 5, how does the computer know where the 3rd indexed variable is located? (Points : 2) It adds 3 to the base address of the array.
It adds space for 3 integers to the base address of the array.
It remembers where all the indexed variables of the array are located.
None of the above Question 50.50. What is wrong with the following code fragment?
const int SIZE =5;
float scores[SIZE];
for(int i=0; i<=SIZE;i++)
{
cout << "Enter a score ";
cin >> scores[i];
} (Points : 2) Array indexes start at 1 not 0
Arrays must be integers
Array indexes must be less than the size of the array
Should be cin >> scores[0]; Question 51.51. Which of the following will read values from the keyboard into the array? (Assume the size of the array is SIZE). (Points : 2) cin >> array;
cin >> array[];
cin >> array[SIZE];
for(i=0;i<SIZE;i++)
cin >> array[i]; Question 52.52. Which of the following is the correct way to determine if a file stream named inFile opened correctly? (Points : 2) if( inFile.open() )
if( inFile.fail() )
if( inFile.opened() )
if( inFile.failed() ) Question 53.53. The member function setf stands for (Points : 2) set the file name
set the flags
set the format
nothing Question 54.54. What is the output of the following code?
char ch='G';
cout << tolower(ch) << endl; (Points : 2) G
g
the integer value of 'g'
the integer value of 'G' Question 55.55. The postcondition of a function (Points : 2) determines how the function will complete its job.
tells what must be true before the function executes.
declares the function for the compiler.
tells what will be true after the function executes. Question 56.56. What is wrong with the following function body?
void calculate(int count, float price, float& cost)
{
if (count < 0)
cost=0.0;
else
cost=count*price;
return;
} (Points : 2) nothing
void functions may not have a return statement.
void functions must return a value
can not mix reference and value parameters Question 57.57. Given the following function definitions and program fragments, what is the output?
void f1(int& z, int &q)
{
int temp;
temp=q;
q=z;
z=temp;
}
void f2( int& a, int& b)
{
if( a<b)
f1(a,b);
else
a=b;
}
int x=3, y=4;
f2(y,x);
cout << x <<" " << y << endl; (Points : 2) 3 3
4 3
3 4
4 4 Question 58.58. What is the output of the following program fragment?
cout << static_cast<float>(3/4) << endl; (Points : 2) 3
0.5
0.0
0.75 Question 59.59. Which of the following functions is a properly overloaded function of the following?
int doSomething(int first, float second); (Points : 2) float doSomething(int first, float second);
int doSomething( int next, float last);
int doSomething(int first, int second, float third);
int doSome(int first, float second); Question 60.60. What is the output of the following code fragment?
double size, volume=16.0;
size = sqrt(sqrt(volume)) / 3;
cout.setf(ios::fixed)
cout.setf(ios::showpoint);
cout.precision(2);
cout << size; (Points : 2) 0.6666667
0.67
0.00
0 Question 61.61. Information Hiding is analogous to using (Points : 2) an algorithmic design
a black-box methodology
formal parameters
actual parameters Question 62.62. What is the output of the following function call?
//function body
int factorial(int n)
{
int product=0;
while(n > 0)
{
product = product * n;
n—;
}
return product;
}
//function call
cout << factorial(4); (Points : 2) 4
0
24
48 Question 63.63. Which boolean operation is described by the following table?
A B Operation
True True True
True False True
False True True
False False False
(Points : 2) OR
AND
NOT
XOR
none of the above
Question 64.64. If x is 0, what is the value of (!x ==0)? (Points : 2) false
true
unable to determine Question 65.65. Which of the following boolean expressions tests to see if x is between 2 and 15 (including 2 and 15)? (Points : 2) (2 <=x || x <=15)
(x<=15 || x>=2)
(2 <= x <= 15)
(x >=2 && x <=15) Question 66.66. Given the following enumerated data type definition, what is the value of SAT?
enum myType{SUN=3,MON=1,TUE=3,WED,THUR,FRI,SAT,NumDays}; (Points : 2) unknown
7
6
8
5 Question 67.67. What is the output of the following code fragment if x is 15?
if(x < 20)
if(x <10)
cout << "less than 10 ";
else
cout << "large "; (Points : 2) less than 10
nothing
large
no output, syntax error Question 68.68. Given the following code, what is the final value of i?
int i,j;
for(i=0;i<4;i++)
{
for(j=0;j<3;j++)
{
if(i==2)
break;
}
} (Points : 2) 3
4
5
2
0 Question 69.69. What is the value of x after the following statements?
float x;
x = 15/4; (Points : 2) 3.75
4.0
3.0
60 Question 70.70. What is the value of x after the following statement?
float x;
x = 3.0 / 4.0 + 3 + 2 / 5 (Points : 2) 5.75
5.0
1.75
3.75 Question 71.71. What is the correct way to write the condition y < x < z? (Points : 2) (y < x < z)
( (y < x) && z)
((y > x) || (y < z))
((y < x) && (x < z)) Question 72.72. Given the following code fragment, and an input value of 3, what is the output that is generated?
int x;
cout <<"Enter a value ";
cin >> x;
if(x=0)
{
cout << "x is zero ";
}
else
{
cout << "x is not zero ";
} (Points : 2) x is zero
x is not zero
unable to determine
x is 3 Question 73.73. Given the following code fragment, what is the final value of y?
int x, y;
x = -1;
y = 0;
while(x <= 3)
{
y += 2;
x += 1;
} (Points : 2) 2
10
6
8 Question 74.74. What is the final value of x after the following fragment of code executes?
int x=0;
do
{
x++;
}while(x > 0); (Points : 2) 8
infinite loop
10
11
9 Question 75.75. How many parameters are there in a unary operator implemented as a friend? (Points : 2) 0
1
2
as many as you need Question 1.1. The block of code that checks if an unusual situation or error occurs is called (Points : 2) the try block
the catch block
a function
an error block Question 2.2. When an unusual situation or error occurs, then the ________ statement is executed. (Points : 2) try
throw
error
exiting Question 3.3. Which type of exception is thrown if a call to the new operator fails? (Points : 2) ArithmeticError
DivideByZero
bad_alloc
MemoryError Question 4.4. You should use exception handling (Points : 2) in all your programs
only when you can not handle the exception with simpler control structures
only when you use classes
in every function Question 5.5. Which is the correct way to tell the compiler that the class being declared (ChildClass) is derived from the base class (BaseClass)? (Points : 2) class ChildClass::public BaseClass
class ChildClass:public BaseClass
class ChildClass childOf public BaseClass
class ChildClass derived BaseClass Question 6.6. If the member variables in a base class are private, then (Points : 2) they can be directly accessed or changed in the derived class
the derived class must use any accesssor or modifier functions from the base class
making them private causes a syntax error.
you must declare them in the derived class also. Question 7.7. Using inheritance allows us to (Points : 2) eliminate duplicate code
make our classes more modular
use polymorphism
all of the above
none of the above Question 8.8. If you have the following class definitions, which of the following is the proper way to construct an object of the derived class?
class Pet
{
public:
Pet();
void printPet();
string getName();
void setName(string newName);
private:
string name;
};
class Dog:public Pet
{
public:
Dog();
void printPet();
void setType(string newType);
string getType();
private:
string type;
}; (Points : 2) Dog::Dog():Pet(),type("MUTT")
{
}
Dog::Dog()
{
name="Rover";
}
Pet::Dog():Pet(),type("MUTT")
{
}
Dog::Pet():Pet(),type("MUTT")
{
} Question 9.9. If a base class has public member functions that are not listed by a derived class, then these functions (Points : 2) are not available to the derived class
are inherited unchanged in the derived class
are private to the derived class
do not exist in the derived class Question 10.10. If you have a copy constructor in the base class, but do not have a copy constructor for the derived class, then (Points : 2) you will have a syntax error
a copy constructor for the derived class is automatically created for you
you can not use pointer variables
the default constructor is used Question 11.11. Which of the following would correctly call the base class (BaseClass) assignment operator from the derived class (DerivedClass) assignment operator? (Points : 2) leftSide=rightSide;
rightSide=BaseClass.rightSide;
BaseClass::operator=(rightSide);
DerivedClass::rightSide=BaseClass::rightSide; Question 12.12. Given a class A that derives from a class B that derives from a class C, when an object of class A goes out of scope, in which order are the destructors called? (Points : 2) C, B, then A
A, B, then C
unable to determine
depends on how the code is written for the destructors Question 13.13. Given the following classes and code, what is the output of the last statement shown?
class Pet
{
public:
virtual void print();
string name;
private:
};
class Dog: public Pet
{
public:
void print();
string breed;
};
void Pet::print()
{
cout << "My name is " << name;
}
void Dog::print()
{
Pet::print();
cout << ", and my breed is a "<< breed << endl;
}
Pet* pPtr;
Dog* dPtr=new Dog;
dPtr->name= "Rover";
dPtr->breed="Weiner";
pPtr= dPtr;
pPtr->print(); (Points : 2) My name is Rover
My name is Rover, and my breed is a Weiner
, and my breed is a Weiner
nothing Question 14.14. Which of the following should be virtual if a base class uses dynamic memory allocation? (Points : 2) the constructor
the copy constructor
the print function
the destructor Question 15.15. If a base class has a non-virtual member function named print, and a pointer variable of that class is pointing to a derived object, then the code ptr->print( ); calls (Points : 2) the base class print function
the derived print function
both the derived and base print functions
it causes a run-time error Question 16.16. 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+2, limit);
}
int main()
{
cout << f1(2,4)<<endl;
return 0;
} (Points : 2) -1
2
1
0
It depends 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; Question 24.24. Data is removed from a stack in the _____ _______ it was entered. (Points : 2) same order
reverse order
alternating order
sorted order Question 25.25. In order to create a namespace called student, you use (Points : 2) namespace student
{
//code goes here
}
namespace student
//code goes here
}
student namespace
{
//code goes here
}
{
student namespace
//code goes here
} Question 26.26. Why will the following code not compile?
namespace ns1
{
void print();
void display1(){};
}
namespace ns2
{
void print();
void display2(){};
}
int main()
{
using namespace ns1;
using namespace ns2;
display1();
display2();
print();
return 0;
} (Points : 2) We have not included the iostream library
The call to print is ambiguous
We have not used namespace std
It will compile Question 27.27. Since accessor functions in a class do not modify or mutate the data members of the object, the function should have the __________ modifier. (Points : 2) reference
friend
const
private Question 28.28. Given the following class, what is syntactically wrong with the implementation of the display function?
class Rational
{
public:
Rational();
Rational(int numer, int denom);
Rational(int whole);
int getNumerator();
int getDenominator();
friend void display(ostream& out, const Rational& value);
private:
int numerator;
int denominator;
};
void display(ostream& out, const Rational& value)
{
out << value.getNumerator() << '/"<<value.getDenominator();
} (Points : 2) nothing
value must be not be passed by reference
The get functions are not const functions
out should be pass by value Question 29.29. When overloading an operator, which of the following is true? (Points : 2) One of the arguments must be an object of the class.
The operator can be a friend or a member of the class.
The operator does not have to be a friend or a member of the class.
All of the above
None of the above Question 30.30. Which of the following function declarations would be correct to overload the multiply operator for the Rational numbers class? (Points : 2) friend Rational operator times(const Rational &left, const Rational &right);
Rational operator times(const Rational &left, const Rational &right);
friend Rational operator *(const Rational &left, const Rational &right);
Rational operator *(const Rational &left, const Rational &right); Question 31.31. Given the following class and array declaration, how would you print out the age of the 10th person in the array?
class personClass
{
public:
void setAge(int newAge);
void setGender( char newGender);
void setSalary(float newSalary);
int getAge();
char getGender();
float getSalary();
private:
int age;
char gender;
float salary;
};
personClass people[100]; (Points : 2) cout << people[10];
cout << people[9];
cout << people[9].age;
cout << people[9].getAge(); Question 32.32. When you have a class which has a dynamic array as one of it's members, you should also include _____________ in the class. (Points : 2) a copy constructor
a default constructor
a destructor
the assignment operator
all of the above Question 33.33. Given the following class definition and the following member function header, which is the correct way to output the private data?
class Person
{
public:
void outputPerson(ostream& out);
private:
int age;
float weight;
int id;
};
void Person::outputPerson(ostream& out)
{
//what goes here?
}
(Points : 2) out << person.age << person.weight << person.id;
out << person;
out << age << weight << id;
outputPerson(person); Question 34.34. A member function of a class should be made private (Points : 2) always
only if it will never be used
if it will only be used by other members of the class
never, it is illegal to make a member function private. Question 35.35. A class member function that automatically initializes the data members of a class is called (Points : 2) the init function
an operator
a constructor
a cast Question 36.36. Given the following class definition, what is missing?
class ItemClass
{
public:
ItemClass(int newSize, float newCost);
int getSize();
float getCost();
void setSize(int newSize);
void setCost(float newCost);
private:
int size;
float cost;
}; (Points : 2) nothing
a default constructor
accessor functions
mutator functions Question 37.37. Given the following class, what would be the best declaration for a mutator function that allows the user of the class to change the age?
class Wine
{
public:
Wine();
int getAge();
float getCost();
private:
int age;
float cost;
} (Points : 2) int getAge(int newAge);
Wine();
void setAge();
void setAge(int newAge); Question 38.38. In a class, all members are ____________ by default (Points : 2) public
private
global
all of the above Question 39.39. A derived class has access to (Points : 2) the private functions and variables of its ancestor classes
the public functions and variables of its ancestor classes
only the functions and variables defined in its class Question 40.40. Which of the following assigns to p1 the pointer to the address of value? (Points : 2) *p1=&value;
p1=value;
p1=&value;
&p1 = *value; Question 41.41. What is the output of the following code fragment?
float *p1;
p1 = new float(3);
cout << *p1; (Points : 2) unknown, the address p1 points to is not initialized
unknown, the code is illegal, p1 points to a dynamic array
3.0
0.0 Question 42.42. Given that a typedef for IntPtr defines a pointer to an integer, what would be the correct declaration for a function that expects a reference to an integer pointer? (Points : 2) void f1 (IntPtr& ptr);
void f1 (IntPtr&* ptr);
void f1 (IntPtr*& ptr);
All of the above Question 43.43. Which of the following statements correctly returns the memory from the dynamic array pointer to by p1 to the freestore? (Points : 2) delete p1[];
delete *p1;
delete [] p1;
delete p1; Question 44.44. In which case would you consider using a dynamic array? (Points : 2) If the array is small, and the size is known before the program runs.
If the program needs to get the size of the array from the user.
If the array size is big, but known at compile time
You should always use a dynamic array Question 45.45. A character array terminated with the null character is most correctly called (Points : 2) a c-string
a character array
a string
a string Question 46.46. strcmp(first, second) returns (Points : 2) true if first=second, false otherwise
nothing, it's a void function
>0 if first < second, 0 if first > second, <0 otherwise
<0 if first < second, 0 if first == second, positive otherwise Question 47.47. Given the following code, what is the correct statement to insert the string str2 into str1, directly after the 'd'?
string str1="abcdefg";
string str2="ABCDE"; (Points : 2) str2.insert(4,str1);
str1.insert(4,str2);
insert(str1,4)=str2;
insert(str2,4)=str1; Question 48.48. What is the value of numbers.size() after the following code?
vector<float> numbers; (Points : 2) 0
10
100
NULL
Unknown Question 49.49. Given an array of integers of size 5, how does the computer know where the 3rd indexed variable is located? (Points : 2) It adds 3 to the base address of the array.
It adds space for 3 integers to the base address of the array.
It remembers where all the indexed variables of the array are located.
None of the above Question 50.50. What is wrong with the following code fragment?
const int SIZE =5;
float scores[SIZE];
for(int i=0; i<=SIZE;i++)
{
cout << "Enter a score ";
cin >> scores[i];
} (Points : 2) Array indexes start at 1 not 0
Arrays must be integers
Array indexes must be less than the size of the array
Should be cin >> scores[0]; Question 51.51. Which of the following will read values from the keyboard into the array? (Assume the size of the array is SIZE). (Points : 2) cin >> array;
cin >> array[];
cin >> array[SIZE];
for(i=0;i<SIZE;i++)
cin >> array[i]; Question 52.52. Which of the following is the correct way to determine if a file stream named inFile opened correctly? (Points : 2) if( inFile.open() )
if( inFile.fail() )
if( inFile.opened() )
if( inFile.failed() ) Question 53.53. The member function setf stands for (Points : 2) set the file name
set the flags
set the format
nothing Question 54.54. What is the output of the following code?
char ch='G';
cout << tolower(ch) << endl; (Points : 2) G
g
the integer value of 'g'
the integer value of 'G' Question 55.55. The postcondition of a function (Points : 2) determines how the function will complete its job.
tells what must be true before the function executes.
declares the function for the compiler.
tells what will be true after the function executes. Question 56.56. What is wrong with the following function body?
void calculate(int count, float price, float& cost)
{
if (count < 0)
cost=0.0;
else
cost=count*price;
return;
} (Points : 2) nothing
void functions may not have a return statement.
void functions must return a value
can not mix reference and value parameters Question 57.57. Given the following function definitions and program fragments, what is the output?
void f1(int& z, int &q)
{
int temp;
temp=q;
q=z;
z=temp;
}
void f2( int& a, int& b)
{
if( a<b)
f1(a,b);
else
a=b;
}
int x=3, y=4;
f2(y,x);
cout << x <<" " << y << endl; (Points : 2) 3 3
4 3
3 4
4 4 Question 58.58. What is the output of the following program fragment?
cout << static_cast<float>(3/4) << endl; (Points : 2) 3
0.5
0.0
0.75 Question 59.59. Which of the following functions is a properly overloaded function of the following?
int doSomething(int first, float second); (Points : 2) float doSomething(int first, float second);
int doSomething( int next, float last);
int doSomething(int first, int second, float third);
int doSome(int first, float second); Question 60.60. What is the output of the following code fragment?
double size, volume=16.0;
size = sqrt(sqrt(volume)) / 3;
cout.setf(ios::fixed)
cout.setf(ios::showpoint);
cout.precision(2);
cout << size; (Points : 2) 0.6666667
0.67
0.00
0 Question 61.61. Information Hiding is analogous to using (Points : 2) an algorithmic design
a black-box methodology
formal parameters
actual parameters Question 62.62. What is the output of the following function call?
//function body
int factorial(int n)
{
int product=0;
while(n > 0)
{
product = product * n;
n—;
}
return product;
}
//function call
cout << factorial(4); (Points : 2) 4
0
24
48 Question 63.63. Which boolean operation is described by the following table?
A B Operation
True True True
True False True
False True True
False False False
(Points : 2) OR
AND
NOT
XOR
none of the above
Question 64.64. If x is 0, what is the value of (!x ==0)? (Points : 2) false
true
unable to determine Question 65.65. Which of the following boolean expressions tests to see if x is between 2 and 15 (including 2 and 15)? (Points : 2) (2 <=x || x <=15)
(x<=15 || x>=2)
(2 <= x <= 15)
(x >=2 && x <=15) Question 66.66. Given the following enumerated data type definition, what is the value of SAT?
enum myType{SUN=3,MON=1,TUE=3,WED,THUR,FRI,SAT,NumDays}; (Points : 2) unknown
7
6
8
5 Question 67.67. What is the output of the following code fragment if x is 15?
if(x < 20)
if(x <10)
cout << "less than 10 ";
else
cout << "large "; (Points : 2) less than 10
nothing
large
no output, syntax error Question 68.68. Given the following code, what is the final value of i?
int i,j;
for(i=0;i<4;i++)
{
for(j=0;j<3;j++)
{
if(i==2)
break;
}
} (Points : 2) 3
4
5
2
0 Question 69.69. What is the value of x after the following statements?
float x;
x = 15/4; (Points : 2) 3.75
4.0
3.0
60 Question 70.70. What is the value of x after the following statement?
float x;
x = 3.0 / 4.0 + 3 + 2 / 5 (Points : 2) 5.75
5.0
1.75
3.75 Question 71.71. What is the correct way to write the condition y < x < z? (Points : 2) (y < x < z)
( (y < x) && z)
((y > x) || (y < z))
((y < x) && (x < z)) Question 72.72. Given the following code fragment, and an input value of 3, what is the output that is generated?
int x;
cout <<"Enter a value ";
cin >> x;
if(x=0)
{
cout << "x is zero ";
}
else
{
cout << "x is not zero ";
} (Points : 2) x is zero
x is not zero
unable to determine
x is 3 Question 73.73. Given the following code fragment, what is the final value of y?
int x, y;
x = -1;
y = 0;
while(x <= 3)
{
y += 2;
x += 1;
} (Points : 2) 2
10
6
8 Question 74.74. What is the final value of x after the following fragment of code executes?
int x=0;
do
{
x++;
}while(x > 0); (Points : 2) 8
infinite loop
10
11
9 Question 75.75. How many parameters are there in a unary operator implemented as a friend? (Points : 2) 0
1
2
as many as you need
Explanation / Answer
Question 1.1. The block of code that checks if an unusual situation or error occurs is called
the try block
the catch block
a function
an error block
When there is a chance that the code may raise an exception, then such code is enclosed on the try block.
Question 2.2. When an unusual situation or error occurs, then the ________ statement is executed.
try
throw
error
exiting
When there is a chance that the code may raise an exception, then such code is enclosed on the try block. Every try block is followed by a catch block that executes when an exception is raised.
Question 3.3. Which type of exception is thrown if a call to the new operator fails?
ArithmeticError
DivideByZero
bad_alloc
MemoryError
If a call to the new operator fails then it throws bad_alloc exception. It indicates that it failed to allocate memory for the new object to be created.
Question 4.4. You should use exception handling
in all your programs
only when you can not handle the exception with simpler control structures
only when you use classes
in every function
It is better to use exception handling only when the unusual situation or error cannot be handled by control structures.
Question 5.5. Which is the correct way to tell the compiler that the class being declared (ChildClass) is derived from the base class (BaseClass)?
class ChildClass::public BaseClass
class ChildClass:public BaseClass
class ChildClass childOf public BaseClass
class ChildClass derived BaseClass
The syntax of a dervided class is
class child_class_name: access_specifier base_class_name
{
body of child_class_name
}
Question 6.6. If the member variables in a base class are private, then
they can be directly accessed or changed in the derived class
the derived class must use any accesssor or modifier functions from the base class
making them private causes a syntax error.
you must declare them in the derived class also
Question 7.7. Using inheritance allows us to
eliminate duplicate code
make our classes more modular
use polymorphism
all of the above
none of the above
The main advantage of inheritance is it eliminates code redundancy. Also Using inheritance, the classes can be made modular.
Question 9.9. If a base class has public member functions that are not listed by a derived class, then these functions
are not available to the derived class
are inherited unchanged in the derived class
are private to the derived class
do not exist in the derived class
Question 10.10. If you have a copy constructor in the base class, but do not have a copy constructor for the derived class, then
you will have a syntax error
a copy constructor for the derived class is automatically created for you
you can not use pointer variables
the default constructor is used
Question 11.11. Which of the following would correctly call the base class (BaseClass) assignment operator from the derived class (DerivedClass) assignment operator?
leftSide=rightSide;
rightSide=BaseClass.rightSide;
BaseClass::operator=(rightSide);
DerivedClass::rightSide=BaseClass::rightSide;
Question 16.16
The output of the given code fragment will be 2 .
The function call is f1(2,4). Then base =2 limit=4.
As base is less than limit, it returns 2 * f1(4, 4).
Now the function call will be f1(4,4). As base is equal, it returns 1.
So finally, it returns 2*1=2
Question 17.17
The output of the given code fragment will be 6 .
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. If your program makes too many recursive function calls, your program will cause a ___________
stack underflow
activation overflow
stack overflow
syntax error
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. In a node type named MyNode, which of the following correctly declares a pointer to a node of that type?
MyNode ptr;
MyNode* ptr;
ptr myNode*;
MyNode.data* ptr;
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
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
The arrow operator (->) specifies a member of a struct or class that is pointed to by a pointer variable.
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.To remove an item from the stack, we call the ____ function
pop
top
push
empty
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.
Question 24.24. Data is removed from a stack in the _____ _______ it was entered.
same order
reverse order
alternating order
sorted order
Stack is a data structure in which the last item stored is the first item extracted.
Question 34.34. A member function of a class should be made private
always
only if it will never be used
if it will only be used by other members of the class
never, it is illegal to make a member function private.
Question 38.38. In a class, all members are ____________ by default
public
private
global
all of the above
Question 39.39. A derived class has access to
the private functions and variables of its ancestor classes
the public functions and variables of its ancestor classes
only the functions and variables defined in its class
Question 40.40. Which of the following assigns to p1 the pointer to the address of value?
*p1=&value;
p1=value;
p1=&value;
&p1 = *value;
Question 67.67. What is the output of the following code fragment if x is 15?
if(x < 20)
if(x <10)
cout << "less than 10 ";
else
cout << "large "; (Points : 2)
less than 10
nothing
large
no output, syntax error
Question 68.68. Given the following code, what is the final value of i?
int i,j;
for(i=0;i<4;i++)
{
for(j=0;j<3;j++)
{
if(i==2)
break;
}
}
3
4
5
2
0
Question 69.69. What is the value of x after the following statements?
float x;
x = 15/4;
3.75
4.0
3.0
60
Question 70.70. What is the value of x after the following statement?
float x;
x = 3.0 / 4.0 + 3 + 2 / 5
5.75
5.0
1.75
3.75
Question 2.2. When an unusual situation or error occurs, then the ________ statement is executed.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.