[RESOLVED] C++ Questions
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: [RESOLVED] C++ Questions

  1. #1
    Join Date
    Jan 2014
    Posts
    1

    [RESOLVED] C++ Questions

    Hello Everyone,

    i'm a Computer Science student and i just finished my exams however i got some questions incorrect: i don't know the correct answer can anyone help me, also can you tell me why you choose that answer.

    **Question 1**

    Which statement initializes a pointer to point to a memory address in the heap?

    A. int p = new int;
    B. int& p = new int;
    C. int *p = new int;
    D. int* p;


    **Question 2**

    Here is a function declaration:
    void goo(int* x)
    {
    *x = 1;
    }

    Suppose that a is an int* variable pointing to some integer, and *a is equal to zero. What is printed if you cout << a; after the function call goo(a)?


    A. The address of a
    B. The address of x
    C. 0
    D. 1

    **Question 3**

    Suppose bag is a template class, what is the syntax for declaring a bag b of doubles?

    A. bag of doubles b;
    B. bag<double> b;
    C. double bag b;
    D. bag b;

    **Question 4**

    One difference between a queue and a stack is:

    A. Stacks require dynamic memory, but queues do not.
    B. Queues use two ends of the structure; stacks use only one.
    C. Stacks use two ends of the structure, queues use only one.
    D. Queues require dynamic memory, but stacks do not.

    **Question 5**

    LIFO means:

    A. the last element that comes out is the first one to go in
    B. the last element to go in is the first element to come out
    C. the last element that comes in is the first one to go in
    D. the last element to go in is the first element to come in

    **Question 6**

    With a single template class, we can implement a container class that can be instantiated with a different data type for each object.

    True
    False


    **Question 7**

    When you write a template class, where does the template prefix occur? (Select all that apply)

    A. Before the main function
    B. Before the template class definition
    C. Before each object declaration.
    D. Before each member function implementation.

    **Question 8**

    Consider this prototype for a template function:

    template <class Item> //line 1
    void foo(Item<x>); //line 2

    Something is wrong with this prototype. Which line has the error?


    A. line 1
    B. line 2
    C. None of the above. There are no errors in these lines.

    **Question 9**

    What is printed by these statements?

    int *p1 = new int;
    int *p2 = p1;
    *p1 = 3;
    *p2 = *p1;
    cout << p1 << endl;


    A. The address of p1
    B. unpredictable
    C. 3
    D. 4

    **Question 10**

    What is wrong with this block of code?

    int *p1 = new int; //line 1
    int *p2; //line 2
    *p2 = 4; //line 3
    p1 = p2; //line 4
    cout << p1 << p2 << endl; //line 5

    A. The p1 pointer is incorrectly instantiated with the new operator before accessing its memory location on line 1. Parentheses are missing from this statement.

    B. A pointer assignment to set the address of a pointer cannot be done with an integer value on line 3.

    C. None of the above. There are no errors in this block of code.

    D. The p2 pointer is not instantiated with the new operator before accessing its memory location on line 3.

    **Question 11**

    A queue is a __________ data structure.

    A. FIFO
    B. LIPO
    C. FILO
    D. LIFO

    **Question 12**

    Consider the following state of the object my_vector


    [0] [1] [2] [3] [4] [5]
    Bashful Doc Dopey Grumpy Sleepy happy

    Describe the effect of each of the following operations on object my_vector as shown above. Assume that function find returns the index of its second argument in the vector that is passed as the first argument.
    my_vector.push_back("Sneezy");
    int i = find(my_vector, "happy");
    my_vector[i] = "Happy";



    A. The vector grows and "Sneezy" is added to index [6].
    i contains the number 5
    Index 5 is changed to "Happy"
    B. "Sneezy" is added to index [0], replacing "Bashful".
    i contains the word "happy"
    Index 5 is changed to "Happy"
    C. The vector does not grow, but "Sneezy" is added out of bounds to index [6].
    i contains the number 5
    All indexes are changed to "Happy"
    D. A vector out-of-range error occurs

    **Question 13**

    What kind of a container is an object of type list?

    A. Associative
    B. Sequential

    **Question 14**

    What does the following code block do?

    vector<int> a_list;
    vector<int>::iterator iter;
    iter = min_element(a_list.begin(), a_list.end());
    cout << *iter << "\n";


    A. This code block declares a vector of integers and an iterator for a vector of integers. The iterator called iter is set to point to the smallest element in the vector and that value is displayed.

    B. This code block declares a vector of integers and an iterator for a vector of integers. The iterator called iter is set to point to the first element in the vector and that value is displayed.

    C. This code block declares a vector of iterators. The iterator with the smallest memory location is located and that memory location is displayed.

    D. This code block declares a vector of integers and an iterator for a vector of integers. The iterator called iter is set to point to the smallest element in the vector and that element is removed.


    **Question 15**

    Suppose that the foo class does not have an overloaded assignment operator. What happens when an assignment a=b; is given for two foo objects?

    A. The automatic assignment operator is used.
    B. Compiler error
    C. The memory location of object b is stored in object a
    D. None of the above.

    **Question 16**

    Suppose that p is a pointer variable that contains a pointer to a deleted location in the heap. What happens if your program tries to read or write *p?

    A. A syntax error always occurs at compilation time.
    B. A run-time error always occurs when *p is evaluated.
    C. A run-time error always occurs when the program finishes.
    D. The results are unpredictable


    **Question 17**

    Which of the following declarations is legal (will not generate an error)?

    A. int q = new *int;
    B. int *q;
    C. int q* = new int;
    D. int q = new int;

    Question 18

    Which keyword can be used to allow a non-member function to see the private variables of another class, even though these variables should probably not be made part of the public interface that the class supports?

    A. friend
    B. protected
    C. allow
    D. overload

    **Question 19**

    Consider the following main function:

    int main()
    {
    Time t(2, 50);
    t.setHour(5);
    t.setMinute(20);
    cout << "The time is " << t << endl;
    }

    What is necessary in the Time class to make this main function compile?

    A. A friend function called t is needed.

    B. The << operator must be overloaded for the Time class

    C. The Time class must have the data member t defined in the public area of the class.

    D. None of the above.


    **Question 20**

    One of the nice features of C++ is that you can give special meanings to operators, when they are used with user-defined classes.

    True
    False


    **Question 21**

    In which of the following ways can you write an operator overload? (Select all that apply)

    A. in a class member function
    B. in the main function
    C. in a nonmember function
    D. in a friend function

    **Question 22**

    What is printed by these statements?

    int p1 = 3;
    int &p2 = p1;
    p2 = p1;
    cout << p1 << endl;

    A. The address in the stack of p1
    B. The address in the stack of both p1 and p2
    C. 3
    D. An error occurs

    **Question 23**

    What is printed by these statements?

    int p1 = 3;
    int &p2 = p1;
    p2 = p1;
    cout << &p1 << endl;

    A. Error
    B. The address in the stack of p1
    C. 0
    D. 3

  2. #2
    Join Date
    Jul 2013
    Posts
    258

    Re: C++ Questions

    Good try loser!

  3. #3
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,330

    Re: C++ Questions

    and your answers are? We've not going to answer the questions for you - but if you provide your answers we could provide some advice.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  4. #4
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,845

    Re: C++ Questions

    At least this quesionnaire isn't made of the same amount of fail as some we have seen here before. There's still a couple questions that are so and so though.

    like #19 which is vague.
    One of the answers is correct in that it's needed, but is incomplete. does that mean the answer is D or do they expect that answer anyway since it needs that (and other stuff) to make it compile.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center