Practice Quiz

18
1. Question : (TCO 4) What is the final number output for the following code snippet? for (i = 1 ; i <= 20 ; i = i + 2); cout << “ “ << i ; 10 19 20 This is an illegal set-up of for loop parameters. Question 2. Question : (TCO 4) Which of the following is not a C++ logical operator? # && ││ ! Question 3. Question : (TCO 4) What is the value of data after the following code executes? unsigned char data = 0xA5; data = data & 0xF0; 0xA0 0xF0 0xF5 0x5A Question 4. Question : (TCO 4) Why does this code snipper give a compile error? int loopCounter = 0; int numberOfScores = 20; while (loopcounter < numberOfScores) cout << “\nInside the loop”;

Transcript of Practice Quiz

  • 1. Question : (TCO 4) What is the final number output for the following code snippet? for (i = 1 ; i
  • The value of the control loop control variable does not change the loop body.

    The curly brackets are missing around the loop body.

    The loopCounter variable is not spelled consistently.

    All of the above

    Question 5. Question : (TCO 4) If firstName and lastName are string object variables, which statement can be used to combine (append or concatenate) the two into a single string variable?

    fullName = firstName + lastName;

    fullName = firstName, lastName;

    fullName = firstName & lastName;

    fullName = firstName && lastName;

    Question 6. Question : (TCO 4) What is wrong with the following switch statement? switch(x) { case 0: cout

  • Question 7. Question : (TCO 4) Given the code below, struct student { string name; double gpa; }; which statement declares an array of 10 students, called myClass?

    const int N=10; struct student x[N];

    const int N=10; struct myClass students[N];

    int N=10; struct student myClass[N];

    const int N=10; struct student myClass[N];

    Question 8. Question : (TCO 4) The code below computes the length of a C-style string. What should the value of FLAG be? char name[20]= "Lancilot"; int length=0; while(name[length] != FLAG) { length = length+1; } cout

  • double

    pitcher

    const pointer to pitcher

    Question 10. Question : (TCO 4) Given the following code fragment, what is the data type of thisStudent? struct student { string name; double gpa; }; student thisStudent;

    string

    const pointer to student

    student

    double

    Question 11. Question : (TCO 4) If the function square(x) returns the square of x, then x should be passed by

    value so it is protected (cannot be changed).

    reference so it is protected (cannot be changed).

    pointer so it is protected (cannot be changed).

    None of the above. X cannot be protected; square can change the value of x regardless of how it is passed.

    Question 12. Question : (TCO 4) Identify the correct prototype for a function that receives an array of doubles, computes the sum of all the elements of the array, and returns the sum to the calling function.

    void computeSum(double array[], int size, double * sum);

  • void computeSum(double array[], int size, double & sum);

    double computeSum(double array[], int size);

    All of the above

    Comments:

    Question 13. Question : (TCO 4) The function call below is designed to return, in variable result, the product of x and y. All the variables are of type int. What is the correct prototype for such a function? myFunction(result, x, y);

    void myFunction(int &, int, int) ;

    void myFunction(int, int, int) ;

    void myFunction(const int *, const int, const int) ;

    void myFunction(int *, const int *, const int *) ;

    Question 14. Question : (TCO 4) Which of the following functions is taking an array of MyStruct structures as a pass by value parameter?

    void MyFunc( MyStruct data[ ] );

    void MyFunc( MyStruct &data[ ] );

    void MyFunc( MyStruct *data[ ] ) ;

    Not allowed in C++

    Question 15. Question : (TCO 4) Which of the following is called automatically each time an object is created?

    Compiler

    Builder

    Constructor

  • Destructor

    Question 16. Question : (TCO 4) Which of the following is called automatically each time an object goes out of scope?

    Compiler

    Builder

    Constructor

    Destructor

    Question 17. Question : (TCO 4) Creating classes with private data members is an example of

    Student Answer:

    encapsulation.

    polymorphism.

    inheritance.

    abstraction.

    Question 18. Question : (TCO 4) When organizing a program into three files (main, class implementation, and class header), which is not a .cpp file?

    Student Answer:

    Main

    None are .cpp

    Class header

    Class implementation

  • Question 19. Question : (TCO 4) Assume you have a class, called Simple, that has two data members, a and b, both of type int. What can be said about the function prototype given below? (Choose the best answer.) Simple(int=0, int=0);

    It is the prototype for the default constructor.

    It is the prototype for the 2-argument constructor.

    It is both the 0-argument, and the 2-argument, constructor.

    It is incorrect.

    Question 20. Question : (TCO 4) Which of the following is a valid declaration to overload the following function? int whatever (double x);

    double whatever (double x);

    int whatever (int x);

    int whatever2 (double x);

    int overload (double x);

    Question 21. Question : (TCO 4) What is the default behavior for the overloaded operator and the copy constructor functions?

    There is no default behavior. The user has to write both functions.

    It is shallow copy.

    It is deep copy.

    Both functions are empty stubs.

  • Question 22. Question : (TCO 4) Assume you have to write a class that makes use of dynamic memory allocation (the class needs to allocate and deallocate memory). According to best practices, which function should you overwrite for proper memory management?

    Assignment operator function

    Copy constructor

    Assignment operator and copy constructor

    None of the above

    Question 23. Question : (TCO 4) Assume you have to write a class that makes use of dynamic memory allocation (the class needs to allocate and de-allocate memory). According to best practices, where would the keyword new be placed?

    Classes are smart data types. The compiler takes care of calling new/delete.

    In the constructor

    In the destructor

    In a separate, dedicated function

    Question 24. Question : (TCO 4) What is the output of the following code snippet? int *list = new int[5]; int *ptr; for (int i = 0; i < 5; i ++) list [ i] = i+ 1; ptr = list; delete [ ] list; cout

  • Question 25. Question : (TCO 4) What is the output of the following code snippet? int value = 10; int * iptr = &value; *iptr = 5; Cout

  • Comments:

    Question 3. Question : (TCO 4) What is the result of 5 % 7?

    Student Answer:

    0

    2

    5

    7

    Comments:

    Question 4. Question : (TCO 4) If firstName and lastName are string object variables, which statement can be used to combine (append or concatenate) the two into a single string variable?

    Student Answer:

    fullName = firstName + lastName;

    fullName = firstName, lastName;

    fullName = firstName & lastName;

    fullName = firstName && lastName;

    Comments:

    Question 5. Question : (TCO 4) A variable declared before and outside all function blocks

    Student Answer:

    is visible only in main.

    is visible to all functions.

    is visible to all functions except main.

    is not visible to any functions.

    Comments:

  • Question 6. Question : (TCO 4) How many times will variable i be printed to the screen? for (i = 1 ; i
  • const pointer to student

    student

    double

    Comments:

    Question 9. Question : (TCO 4) Which type of error does the following code fragment cause? const int MAX = 500; int main (void) { int foo [MAX]; for (int i = 0; i

  • array of doubles, finds the position of the largest value in the array, and returns the position to the calling function.

    Student Answer:

    void findPosition(double array[], int size, double * position);

    void findPosition (double array[], int size, int * position);

    double findPosition (double array[], int size);

    All of the above

    Comments:

    Question 12. Question : (TCO 4) When a variable is passed by reference

    Student Answer:

    the variable can be changed by the calling and the called function.

    the parameter name is an alias of the variable passed.

    the called function can change the value of the variable in

    the calling program.

    All of the above

    Comments:

    Question 13. Question : (TCO 4) What is the output of the following code? void func(int x[ ]) { x[0] = x[0] * 3; x[1[ = x[1] * 3; x[2] = x[2] * 3; } void main () { int x[ ] = {1, 2, 3}; func(x); cout

  • 3 6 9

    3 3 3

    Comments:

    Question 14. Question : (TCO 4) The function call below is designed to return, in variable result, the product of x and y. All the variables are of type int. What is the correct prototype for such a function? myFunction(&result, &x, &y);

    Student Answer:

    void myFunction(int, int, int) ;

    void myFunction(int *, int *, int *) ;

    void myFunction(const int *, const int *, const int *) ;

    void myFunction(int *, const int *, const int *) ;

    Comments:

    Question 15. Question : (TCO 4) Creating one class from another in a parent/child hierarchy is an example of

    Student Answer:

    encapsulation.

    polymorphism.

    inheritance.

    abstraction.

    Comments:

    Question 16. Question : (TCO 4) Variables defined to be of a user-declared class are referred to as

    Student Answer:

    attributes.

    member variables.

    primitive variables.

  • objects.

    Comments:

    Question 17. Question : (TCO 4) Assume you have a class, called Simple, that has two data members, a and b, both of type int. What can be said about the function prototype given below? (Choose the best answer.) Simple(int=0, int=0);

    Student Answer:

    It is the prototype for the default constructor.

    It is the prototype for the 2-argument constructor.

    It is both the 0-argument, and the 2-argument, constructor.

    It is incorrect.

    Comments:

    Question 18. Question : (TCO 4) Given the following class definition and lines of code, Line 1 in main is a call to what? class Distance { private: int feet; double inches; public: Distance( ); Distance(int initFt, double initIn); void setFeet(int feetIn); void setInches(double inchesIn); int getFeet() const; double getInches( ) const; }; int main( ) { Distance d1; //Line 1 const int MAX = 100; //Line 2 Distance list[MAX]; //Line 3 Distance d2(1, 2.3); //Line 4 Distance * pDist; //Line 5 d1.feet = 5; //Line 6 // etc. assume the remaining code is correct }

    Student Answer:

    The 0-argument Distance constructor

  • The 2-argument, int, double, Distance constructor

    The 2-argument, double, int, Distance constructor

    The 1-argument, int, Distance constructor

    Comments:

    Question 19. Question : (TCO 4) A derived class is typically an example of

    Student Answer:

    a has a relationship.

    an is a relationship.

    a uses a relationship.

    a is used relationship.

    Comments:

    Question 20. Question : (TCO 4) The word const inside the parentheses of the following declaration of isEqual bool isEqual (const Distance & rightSideObject) const;

    Student Answer:

    ensures the member variables of the called objects are protected (cannot be changed).

    ensures the argument passed in to the function is protected (cannot be changed).

    ensures the return value of the function is protected (cannot be changed).

    ensures the return value is routed to the proper variable.

    Comments:

    Question 21. Question : (TCO 4) Given the following definitions and statements, void myFunction (double * dptr);; double data [10]; which of the following statements correctly calls the function passing in the address of the data array?

  • Student Answer:

    myFunction(data);

    myFunction(&data);

    myFunction(*data);

    myFunction(data[0]);

    Comments:

    Question 22. Question : (TCO 4) What is the output of the following code snippet? int value = 10; int * iptr = &value; *iptr = 5; Cout

  • 7852

    Comments:

    Question 24. Question : (TCO 4) Given a class called Employee and the following definitions and statements, void myFunction (Employee * eptr); Employee emp; which of the following statements correctly calls the function passing in the address of the data array?

    Student Answer:

    myFunction(emp);

    myFunction(&emp);

    myFunction(*emp);

    myFunction(**emp);

    Comments:

    Question 25. Question : (TCO 4) What is the output of the following code snippet? int *list = new int[5]; int *ptr; for (int i = 0; i < 5; i ++) list [ i] = i+ 1; ptr = list; delete [ ] list; cout