Check my answers on Stacks and Queues Please

This is a discussion on Check my answers on Stacks and Queues Please within the C++ Programming forums, part of the General Programming Boards category; Could someone please look over my questions/answers that I have put in bold. Or the answer I put after A:. ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    14

    Check my answers on Stacks and Queues Please

    Could someone please look over my questions/answers that I have put in bold. Or the answer I put after A:. I am preparing for a test and want to make sure I know what to do. Thanks
    Code:
    1) Client code needs a stack variable S capable of storing up to ten integers. Declare the stack variable S.
    A) Stack(10) S; B) Stack S C) Stack S=10; D)Stack S(10); E) None of the above
    
    2) Add the integer 7 to top of stack variable S assuming stack is not full.
    A:
    S.Push(7);
    
    3) Remove the top most integer from top of stack variable S and store value into previously declared integer variable W assuming stack is not empty.
    A:
    W=S.Pop();
    
    4) Output "Full" if previously declared stack variable S is full.
    A:
    if(S.IsFull())
    cout << "Is Full";
    
    5) Return the top most value on the stack without modifying the contents in the stack.
    A:
    int temp = S.Push();
    S.pop(temp);
    
    6) Using two Stack variables, S1 and S2, where contents of S1 are Top{1,2,3} and contents of S2 are Top{4,5}. Describe S2 after following statement: S2=S1;
    A) {1,2,3} B){4,5} C){3,2,1} D)Error: illegal statement E)None of the above.
    
    7) Which describes what happened to the original contents in S2?
    A) S2 remains stored in S2 and can be accessed using public member functions
    B. S2 was automatically deallocated by Stack destructor
    C) S2 remains in memory allocated to the client but is inaccessible
    D) S2 remains in memory allocated to client and is accessible through the variable S1 and public member function
    E) None of above
    
    -Dont believe it is C,D,E. I chose B. 
    
    8) Suppose the client code wishes to create a Queue object named Q. Which of the following code
    segments successfully completes that task?
    A) Q = Queue; C) ~Queue Q; E) None of the answers provided
    B) Queue Q; D) Queue Q(10);
    
    9) Suppose the client code includes a Queue object named Q. Which of the following code
    segments successfully uses the Add method to enqueue the integer value 3 to the object Q ?
    A) Add(Q, 3); C) Q.Add(3); E) None of the answers provided
    B) Q.Add('3'); D) Both B and C
    
    10) Suppose the client code includes a Queue object named Q. Which of the following code
    segments successfully uses the Remove method to dequeue the next integer value stored
    in Q and place that integer into the variable W ?
    A) W = Remove(Q); C) Q.Remove(W); E) None of the answers provided
    B) W = Q.Remove(); D) W.Remove(Q);

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,791
    In the future, don't put your questions inside code tags. You can see the result when you did it.
    I agree with all answers, except:
    6) I believe the answer is not illegal syntax since the question says the contents. However, I am unsure of what they really contain. So it could be A or E. It depends on the context behind the question, I suppose.
    7) This really is a matter of semantics and/or implementation details. Data could be left in memory, but inaccessible, but I doubt that's what they're after, so E it is. The point is that the class would throw away the old data and replace it with the new. It will absolutely not call the destructor.
    9) This one is plainly wrong '3' is a character, hence it violates the question.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    The first one can be incorrect too! We don't know Stack implementation (it obviously isn't std::stack), so it theoretically could be B, C, as well as D.
    I never put signature, but I decided to make an exception.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,169
    Code:
    5) Return the top most value on the stack without modifying the contents in the stack.
    A:
    int temp = S.Push();
    S.pop(temp);
    So...S.Push() returns....a value you're storing in temp? What value? And then you're passing a value to pop()...

    This whole scenario doesn't seem right to me. Is there a Peek() method maybe?
    int value = S.Peek();

    Or at least:
    Code:
    int temp;
    S.Push((temp = S.Pop()));
    ...which technically violates the 'do not modify' rule, but by the time that statement ends it will be in the same condition it was before you started.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The main issue here is that we don't know how the Stack type you're dealing with is implemented, so there's only so many possibilities.

    1: B if the stack defaults to 10 elements and the missing semicolon is just a transcription error, C would work if the implementation contains a simple mistake, D would be the best way assuming Stack has a proper implementation.
    4: You output "Is Full", not "Full" as required.
    6: A is the most likely and the result of a sane = overload, if one exists. B and C are for broken = overloads. D if the = overload is disabled. E if = is even weirder.
    7: E is the most likely - again, this is assuming a sane implementation of operator =. But the mere fact that the question is asked means that 6D is definitely wrong. (You can't ask about what happens to the contents of S2 when the statement modifying it is illegal and won't compile in the first place.) Anyway, note that the destructor of S2 isn't even called for the statement. (Unless the = operator has an insane implementation.)
    8: Again, depending on the implementation it could be B or D.
    9: While '3' is an integer value, it's the integer value of the ASCII code of the '3' sign, not the actual integer 3. The correct answer is C.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stack and Queues questions
    By ee1215 in forum C++ Programming
    Replies: 12
    Last Post: 03-29-2011, 07:19 PM
  2. linked lists, stacks and queues
    By aniramg69 in forum C Programming
    Replies: 10
    Last Post: 11-29-2008, 10:58 AM
  3. Stacks and queues info
    By Emeighty in forum C++ Programming
    Replies: 4
    Last Post: 11-02-2008, 01:41 AM
  4. stacks and queues
    By j0hnb in forum C Programming
    Replies: 4
    Last Post: 04-16-2003, 09:44 PM
  5. using Stacks & Queues to compare Strings
    By eskimo083 in forum C++ Programming
    Replies: 1
    Last Post: 03-09-2003, 04:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21