Please double check my answers

Please review my answers to this quiz. They are questions from a previous exam, to help students (like me) study. I've answered them best I can.

If you could confirm the answers(1-20) that you know are correct, it would be greatly appreciated.


1. Define

char str[] = "Hello " + "world";
cout << str[4] << ' ' << &str[4] << endl;

What will be the output of above code?

A. o world
B. o o world
C. o and address of the first character 'o'

D. Compilation error < ---



2. Define #include<iostream> using namespace std;

class Date { int day;

int month; int year;

};
int main() {

Date aday; aday.day = 1; aday.month = 12; aday.year = 2013;

cout << aday.day << '/' << aday.month << '/' << aday.year << endl;
}

What will be the output of above code?

A. Compilation errors < --- // class variables not public
B. 1/12/13
C. 01/12/2013

D. 1/12/2013


3. Define

int main() {

string str = "object-oriented programming"; cout << str.erase(6, 9) << endl;

}

What will be the output of above code?

A. Compilation errors
B. object-oriented

C. object programming < --- // “erase” deletes from x to y, inclusive
D. oriented programming



4. Define a class class Circle {

int x; int y; int r;

static double pi=3.14; public:

Circle(int=0, int=0, int=0); double area();

};

Which statement is correct?

A. Cannot assign value to pi in the class. < ---
B. Cannot assign zeros to the parameters in the constructor.
C. Should define a default constructor.
D. Return type of area() should be int.


5. Which statement is correct?

A. Use Cases describes structure of a system.

B. Use Cases describes interactions between a user and a system. < ---

C. Use Cases describes data flow of a system.
D. Use Cases describes views of code.

6. Which statement is correct?

A. In a class diagram, the {} indicates a “comment”.
B. In a class diagram, the {} indicates a “Role stereotype”.
C. In a class diagram, the {} indicates a “arguments’ list”.

D. In a class diagram, the {} indicates a “constraint”.
< ---

7. Which statement is correct?

A. A default copy constructor can make deep copy.

B. A default constructor can initialize data members. < ---
C. A default assignment operator can make a shallow copy.
D. A default destructor can delete dynamic memory allocated for data members.



8. Which statement is correct? A friend class of a class

A. is an instance object member of the class.

B. can access private data members in the class object. < ---
C. cannot access private data members, but can access protected data members in the class object.
D. cannot be defined.


9. Which statement is correct?

A. If no exceptions are thrown in the try block, all handlers are executed directly;
B. Exceptions can only be integer type or character type;

C. After the exception is thrown, it will be caught by the closest catch statement; < ---
D. A try block can be followed by zero or more catch blocks.


10. Which statement is correct?

A. A friend function of a class can be a member function of the class. < ---

B. A friend function of a class can access protected and public members, but not private members in the class.

C. A friend class of a class can be inherited; the derived class is also a friend of that class.
D. A friend function of a class must be declared inside the class.


11. Which operator cannot be overloaded?

A. Member parentheses operator ();
B. Exception member function what() ;

C. Dot operator .;
D. Logic bit AND operator &.


12. Define a class class Fraction {

private:

int numerator; int denominator;

public:
//member functions
};

Define overloading post fix increment operator (++) as a member function in the class Fraction. Which one of the following is correct?

A. In the class Fraction, define Fraction operator++(int);

Then implement the operator as Fraction Fraction::operator++(int) {

Fraction result = *this; numerator += denominator;

return result;
}

B. In the class Fraction, define Fraction operator++();

Then implement the operator as Fraction Fraction::operator++() {

numerator += denominator;

return *this;
} < ---

C. In the class Fraction, define Fraction operator++(int);

Then implement the operator as Fraction Fraction::operator++(int inc) {

numerator += inc * denominator;

return *this;
}

D. In the class Fraction, define Fraction operator++();

Then implement the operator as Fraction Fraction::operator+()

{ Fraction result = *this; numerator += denominator;

return result;
}





13. Which statement is true?

A. In C++ program, multiple inheritance is allowed. < ---
B. Multiple inheritance is supported by Object-Oriented Language.
C. Multiple inheritance is required to solve a programming problem.
D. None of above is true;


14. Define

int x; double y;

Which expression has truth value?

A. typeid(x) == typeid(y)
B. typeid(x) == typeid(int *)
C. typeid(y) == typeid(double *)

D. typeid(&y) == typeid(double *) < ---


15. Which statement is not correct?

A. Use namespace, programmers can avoid clashes between variables and function names.
B. Use namespace, programmers can choose different versions of functions or data types.
C. Use namespace, programmers can save time on coding.

D. Use namespace, programmers can hide information. < ---


16. Which statement is not correct?

A. A class cannot be defined in another class. < ---
B. To define private or protected constructor for a class, one can create a singleton class.

C. A monostate class contains only private static data members and public non-static member functions.

D. Function objects are really objects which have an overloaded () operator.


17. Which statement is not correct?

A. string s = "Hello world";

B. string s="Hello " + "world" < ---
C. string s(1, 'H');

D. string s("Hello world");


18. Define namespace date in date.h namespace date {

int day; int month; int year;

void setdate(int x, int y, int z) { day = x; month = y; year = z;

}
}

Which one of following has compilation errors?

A.

#include<iostream> #include "date.h" using namespace std; int main() {

cin >> date::day >> date::month >> date::year;
cout << date::day << '/' << date::month << '/' << date::year << endl;
}

B.

#include<iostream> #include "date.h" using namespace std; int main() {

cin >> date.day >> date.month >> date.year;
cout << date.day << '/' << date.month << '/' << date.year << endl;
} < ---

C.

#include<iostream> #include "date.h" using namespace std; using namespace date; int main() {

cin >> day >> month >> year;

cout << day << '/' << month << '/' << year << endl;
}

D.

#include<iostream> #include "date.h" using namespace std; using namespace date; int main() {

cin >> date::day >> date::month >> date::year;
cout << date::day << '/' << date::month << '/' << date::year << endl;
}


19.

#include

<iostream> #include <set> using namespace std;

int main()
{

set<int, greater<int> > myset;
myset.insert(50); myset.insert(10); myset.insert(30);
myset.insert(10); myset.insert(20);
set<int, less<int> >::iterator p=myset.begin();
while(p != myset.end())
{
cout << *p <<
‘ ‘; p++;
}

cout << endl; return 0;

}

What will be the outputs of above program?

A. 50 10 30 10 20
B. 10 10 20 30 50
C. 50 10 30 20

D. 50 30 20 10 < ---


20. Which declaration is not correct?

A. set<char *> myset;
B. vector<float> myfloat(5*5, 0);

C. pair< set<int, greater<int> >::iterator, bool> mypair;

D. map(int, char * ) uid; < ---
4. All options are incorrect, or the statement "Cannot assign value to pi in the class." needs to be clarified.
7. C. Default constructors don't initialize members of basic types. A can aslo be true depending on how narrow your definition of "deep copy" is.
10. D. A member function being a friend of its own class would be redundant.
11. C.
12. I'll leave checking this one to someone else. I can never remember the syntax.
15. A. Namespaces don't hide anything. They merely avoid dumping names onto the global namespace.
16. C and D are both true, AFAICT.

Everything else is correct.
Topic archived. No new replies allowed.