CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: c++ exercises

  1. #1
    Join Date
    Dec 2012
    Posts
    2

    c++ exercises

    hello there i have a c++ test in two weeks
    i'v got this sample test if u could just help me with the answers i'll be very thankfull


    1) Algorithm can be defined as _________________. (Choose one)) [1]

    a) A logical and concise list of steps required to solve a problem.
    b) A sequence of diagrams required to solve a problem.
    c) A concise list of steps required to solve a problem.

    2) _________ is the process of following one of two or more alternate paths of computations. (Choose one) [1.5]

    a) Branching. c) Loops
    b) Instructions. d) Algorithms.

    3) A program requires all the variables to be declared before they are used. (Choose one) [0.5]
    a) True. b) False.

    4) Which one of the following loop is ideally used when we know how many times the loop will be executed ? [1]
    a) For. c) Repeat...until.
    b) While.

    5) void main ()
    The above line of the program indicates the _____________ of a program. (Choose one) [1]
    a) Stop. c) Start.
    b) End. d) Break

    6) What will be the output of the following code segment? (Choose one)
    for(i=1; i<3; i++)
    {
    for(j=1; j<3; j++)
    {
    printf(“%d ”, i*j);
    }
    } [2]
    a) 1 1 1 2. c) 1 2 2 4.
    b) 1 2 2 3. d) 1 2 2 2.

    7) ______ statements can change the flow of program. (Choose one) [1]
    a) Logical. c) Unconditional.
    b) Conditional. d) Mathematical.

    8) For calculating the average marks for 100 students, the step that calculates the average for one student has to be repeated _______ times, if the loop starts with a value of 0. (Choose one) [1]
    a) 99. c) 101.
    b) 10. d) 100.

    9) void main( )
    {
    putchar(‘z’);
    putchar(70);
    }
    What is the output of the above code? (Choose one) [1.5]
    a) zF. c) 90F.
    b) z70. d) 9070.

    10) void main( )
    {
    putchar ( 'n' );
    }
    What is the output of the above code? (Choose one) [1]
    a)N b)’n’
    c)’N’ c)n

    11) After accepting a character, the ________ function waits for the Enter key to be pressed. (Choose one) [1]
    a) getch(). c) putchar().
    b) getchar(). d) putch().

    12) In a while loop, the condition is tested only after the body of the loop is executed at least once. [0.5]
    a) True. b) False.

    13) What will be the output of the following expression? (Choose one)
    i =10;
    if(i> 5 * 4)
    printf(“%d”, i) [1]
    a) None. c) 10.
    b) 1. d) 20.

    14) The following expression will evaluate to _____________. (Choose one)
    (5+9*3^2-4 > 10) AND (2+2^4-8/4 > 6) OR (3<6 AND 10 >11)) [2]
    a) 4. c) 6.
    b) True. d) False.

    15) If the first condition is false and the second condition is true, the result of the AND operator will be ______. (Choose one) [0.5]
    a) True. b) False.

    16) The result of which arithmetic operator will return 81 (Choose one)
    [1]
    a) 9+2. c) 9^2.
    b) 9/2. d) 9*2.

    17) What will be the output of the following code? (Choose one)
    int j;
    for(j=1; j<10; j+=2)
    printf(“%d ”,j); [1]
    a) 1 2 3 4 5 6 7 8 9. c) 1 3 5 7 9.
    b) 2 4 6 8. d) 1 2 4 6 8.

    18) What will be the value of *x? (Choose one)
    int a =20,b=50;
    int *x;
    x=&a;
    x=&b; [1]
    a) 20. c) a.
    b) 50. d) b.

    19) The index of an array starts at one. (Choose one) [0.5]
    a) True. b) False .

    20) The address operator is shown using the ________symbol (Choose one) [1.0]
    a) *. c) &.
    b) #. d) @.

    21) A value of 20 is to be assigned to the third element of an array. Which is the right method? (Choose one)
    a) Num[3]=20. c) Num[4]=20.
    b) Num[2]=20. d) [4]num=20.

    22) Identify the statement that produces the following output. (Choose one)
    1
    22
    333
    4444
    55555 [2.0]
    a) for(a = 1; a <= 5; a = a + 1)
    {
    for( b = 1; b <= 5; b = b + 1)
    printf("%d", b);
    printf("\n");
    }
    b) for( a = 1; a <= 5; a = a + 1)
    {
    for( b = 1; b <= a; b = b + 1)
    printf("%d", a);
    printf("\n");
    }
    c) for( a = 1; a <= 5; a = a + 1)
    {
    for( b = a; b <= 5; b = b + 1)
    printf("%d", b);
    printf("\n");
    }
    d) for( a = 1; a <= 5; a = a + 1)
    {
    for( b = 1; b < a; b = b + a)
    printf("%d", b);
    printf("\n");
    }

    23) A file pointer is essential for reading or writing files. (Choose one) [0.5]
    a) True. b) False .

    24) Fp=fopen(“c:\jak.txt”,”W”) in this Statement “W” Stands for ? (Choose one) [2.0]
    a) Open a text file for reading c) Create a text file for writing
    b) Append to a text file d) None.

    25) The function feof() returns true if the end of the file has been reached, otherwise it returns false (0). (Choose one) [1.0]
    a) True. b) False .

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: c++ exercises

    You go first

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

    Re: c++ exercises

    this list is AWFUL >.< No wonder we have to stomp out all this false beliefs when freshly graduated people get recruited.
    And if you submit the below to your teacher, yes, he'll probably object/argument his way out.


    in genral:
    if this is a C++ course, then please use proper C++. Don't teach kids to use the old C libs in C++.


    1) is a trick question
    I can guess that your teacher wants you to give answer 1, but that is incorrect. An algorithm does not have to be logical. And some very good algoritms in fact aren't. Some don't even make any sense unless applied to specific behaviour on a specific type of machine or environment.

    3) a "program" doesn't require any kind of variable declaration.
    The specifics of a programming language (which language here is unmentioned) may or may not have this requirement.
    if you assume C++ then "maybe", but it's still an awful question.

    4) Oh... C++ has repeat until loops now ? This is new.
    last I checked we had while and do-while. do-while comes close, but it's not the same.
    Don't teach incorrect or confusing names
    some languages implement the repeat until approach, some have the do-while approach they are similar, but not identical.

    5) void main() is not legal C++. Yes it compiles on some of the more popular compilers. No that does not make it legal C++. Can teachers STOP USING THIS PLEASE !!!

    6)
    Impossible to answer corerctly with the information given.
    a) when used as is, it doesn't compile C++ doesn't accept angled quotes
    b) answer depends on the types of i and j which are not given, one can more or less assume the intention is int, but who knows.
    c) the result will NOT print a . at the end which is present in each possible answer
    d) assuming the . in the answers merely ends each answer and is not part of the actual result. the correct answer isn't in there as there will be a space at the end of the answer and all of the possible answers here have the . affixed to the last digit.
    e) with all of the above failures, yes, you can sort of make out what the answer would be. it is still a silly question, nobody writes code like that, the ones that do get fired.

    7) In C++ there are 2 correct answers. In some other languages, the answer depends on whether or not optimisations are on. And in some other languages, there is only 1 answer.
    Yes, your teacher probably assumed there is only 1 answer possible.
    Code:
    bool b = (foo() && bar()) || woops();
    flow will be dependant on the results of the subexpressions.

    8) *** ?

    9) more void main(). Stop using it!
    a) more angled quotes
    b) can we assume putchar() from stdio.h ? ah well let's.

    10) do I need to mention the void main() thing. yes I do. Stop using it!
    a) at least the angled quotes are gone
    b) What is the point of this question in light of the preceding question 9 ? You're essentially attributing a whole 2.5 points to someone that knows what this does. if they don't know 9, then they can more or less guesstimate what it'll do by the answers in 10. And they can exclude half the answers in 10 by assuming one of the answers in 9 must be right.
    c) asking someone how a particular function works makes little sense, you can look that up on your library reference. It's much more sensible to ask questions that proove they understand how a program works rather than if they can memorize a reference (it is called a 'reference" for a reason, so you can refer to it. get it).

    11) none of these do.
    a) getchar() comes close but doesn't require the "after accepting a character" part
    b) see 10c

    12)
    ok, so... do you now refer to a while loop, or a do-while loop (see 4) ? This is why confusing names make for confusing questions.
    If the while is at the front or at the end... does that make it less of a while ?

    13)
    Confusing questions. the answer isn't in there.
    The teacher probably assumes a), but it won't actually output that.
    the possible answers make no visual indication of actual output and interpreted behaviour answers.
    Do I need to mention the angled quotes again. Oh, I just did.

    14)
    a) This isn't even valid C++ (or C). Unless maybe if you have a
    Code:
    #define AND && 
    #define OR ||
    somewhere at the top of your code. but then, we're making assumptions, they could have been defined entirely different, or not at all.
    b) even with the above defines in place, it still won't compile, there's an extra ) at the end.
    c) even with a and b solved. the answer is still tricky. it requires an "abnormal" proficiency in operator precedence behaviour. Programmers that write code like that in my company get fired. In expressions like that you want to explicitely add the parenthesis to make it clear what you wanted.
    d) In light of the failure in question 16) are we to assume the proper C/C++ functioning of the ^ operator (xor) or the assumed 'power' behaviour indicated by question 16 ?

    15) there is no AND operator. There's an && operator. and an &= operator, and an & operator though.

    16) none of these.
    C++ does not have a 'power' operator. There might be a 81 answer in some languages that do.
    Teacher probably assumed 9^2 returns 81, it doesn't it returns 11. (the ^ operator is an xor).

    20) there is no address operator, there is an address-of operator though.
    yes a lot of people tend to use the name "address operator", that doesn't make it right. Use the proper terminology when in a teaching environment. Don't teach the uneducated the wrong things.

    21) either ask 21 or ask 19 don't ask both.

    22) Code like that at our company gets a firm slap on the wrist.

    23) what is a file pointer ? Did you mean a FILE pointer ?
    please teach C++ (c++ streams) not C.
    are you assuming all the available C/C++ libs ? or only the buffered ones ? open() vs fopen() ?
    (this is just a bad question in so many ways).

    24)
    a) this has a compile error. "\j" is not a valid escape sequence, I guess you probably meant "c:\\jak.txt"
    b) "W" does not stand for anything, this isn't valid/standard syntax. Yes, some libraries accept W as an alias for w. not all of them do. don't use it.

    25)
    feof() never returns true or false. it returns an int. This int can be either zero or nonzero. Yes you can (automatically) convert this zero or nonzero int to a bool.
    don't teach kids this returns true or false, it doesn't. Just because one thing auto-casts to something else does not make them equal. If you teach proper C++ rather than C on a C++ compiler, the difference would probably be more obvious.


    I give your teacher a D in C++ question making.
    Last edited by OReubens; December 7th, 2012 at 07:00 AM.

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: c++ exercises

    I agree. My first thought when I saw that was you need to find another class, but I didn't have the enthusiasm you did to explain why.

  5. #5
    Join Date
    May 2009
    Posts
    2,413

    Re: c++ exercises

    Quote Originally Posted by OReubens View Post
    An algorithm does not have to be logical. And some very good algoritms in fact aren't. Some don't even make any sense unless applied to specific behaviour on a specific type of machine or environment.
    Of course an algorithm must be logical; It must be consistent, valid, complete and sound. If you come up with an algorithm that doesn't make sense it's not much of an algorithm, is it?

    I find many of your objections questionable and some are even off the mark because you're assuming there must be a 100% correct alternative. That's not so. You're supposed to identify the best alternative among the choises. A 100% correct alternative may not be present or even exist.

    All in all I think this is a fair quiz for those who attended the course.
    Last edited by nuzzle; December 8th, 2012 at 03:19 AM.

  6. #6
    Join Date
    Dec 2012
    Posts
    2

    Re: c++ exercises

    thanks guys for the tip
    i'v already answerd this sample test
    i just wanna compare my answers with the right answers
    because i'm pretty sure that 99% of my answers are wrong
    so please if anybody knows the right answers tell me
    that will help me alot with studying>>

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: c++ exercises

    Quote Originally Posted by orabi View Post
    thanks guys for the tip
    i'v already answerd this sample test
    i just wanna compare my answers with the right answers
    because i'm pretty sure that 99% of my answers are wrong
    so please if anybody knows the right answers tell me
    that will help me alot with studying>>
    We don't do homework. You post your answers and we'll comment on them.

  8. #8
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: c++ exercises

    Quote Originally Posted by nuzzle View Post
    Of course an algorithm must be logical; It must be consistent, valid, complete and sound. If you come up with an algorithm that doesn't make sense it's not much of an algorithm, is it?
    And I stick by my premise that an algorithm does not HAVE to be logical. Yes, most of them are, but some aren't.
    The purpose of an algorithm is to have consistent behaviour, if you can do that in what seems illogical, then that's perfectly fine. Maybe not ideal, but it's still an algorithm.
    Sometimes the illogical/nonideal algorithm is merely a shortcut.

    Suppose you wanted a kettle of boiled water.
    And you want an algoritm for that.
    - take kettle
    - fill with water
    - put on the stove
    - turn on the stove
    - wait for water to boil
    - turn off stove
    - remove kettle

    Now... what if you wanted to account for the fact that the kettle might already contain water, or the stove may already be on. You could properly test for that, but it's not uncommon for programming to do something similar to
    - empty kettle
    - turn off stove
    - call above algorithm
    it's "illogical", but it's still a valid algorithm.



    If you look at the algoritms (or even the code) of most of the lock-free containers, you'll probably even think "this can't possibly work', and yet they do because of how CPU's behave, and the fact that some of those algorithms rely on hardware exceptions.

    Turn it around, you can design on paper seemingly perfectly logical algorithms for lock free access that "should work", but when actually running on a computer, they fail, and you can't even logically explain why they fail in the way they do. (been there). Among others: lock free approaches that are attempting to build around 2 or more atomic compare/exchanges just don't work.



    an algorithm is a step by step breakdown of how you're solving a problem. given a certain input it should always result in a certain output.
    although you would usually want it to be logical, it does not HAVE to be, sometimes there are good reasons to not follow established logic.
    You do not have to believe me, but other sources agree with me
    http://en.wiktionary.org/wiki/algorithm
    http://oxforddictionaries.com/defini...hm?q=algorithm
    Neither of those use 'logical' in their definition. My own language dictionary makes no mention of logicality either.




    I find many of your objections questionable and some are even off the mark because you're assuming there must be a 100% correct alternative. That's not so. You're supposed to identify the best alternative among the choises. A 100% correct alternative may not be present or even exist.
    It is a multiple choice questionnaire.
    In that case, the answer is required to be in there. And the question should be sufficiently clear that it's not possible to mistake what is being asked. If there is a "none of these answers is right" option, then... "maybe.

    If these were open ended questions with a "what is wrong with this piece of code"... then that is a different matter.
    so yes, I do expect the answers to be 100%.
    I don't agree that "the closest answer" should be a valid response.
    If you ask questions about code, and those snippets of code don't even compile, then yes, I have my objections about that.


    All in all I think this is a fair quiz for those who attended the course.
    it isn't.
    If this was a C++ course, then they have not learned C++. They have learned C (with objects). it's obvious it was C++ because some questions allude to it.
    They are being asked to memorize how (old C lib for that matter) functions work
    worse, they are expected to know how functions work that in their entire living career, will probably never ever need to use again (C input routines). I don't call that particularly "good" questions.

    A good exam questionnaire will try to establish if you have managed to learn the bits of knowledge that you will need if you decide to proceed on a further career, or that will be referenced to in future classes or side classes. The majority of this questionnaire fails to do this.
    If I need to hire a C++ programmer, I'm not going to ask them how putchar() works, or what the difference between getchar() and getch() is.


    The really important matters about C++ aren't even being asked AT ALL.
    So no, I don't agree it's a fair quiz.
    If it was a quiz on C... Then... it gets a little closer, but it still fails to ask the important questions about C.

  9. #9
    Join Date
    May 2009
    Posts
    2,413

    Re: c++ exercises

    Quote Originally Posted by OReubens View Post
    And I stick by my premise that an algorithm does not HAVE to be logical. Yes, most of them are, but some aren't.
    The purpose of an algorithm is to have consistent behaviour, if you can do that in what seems illogical, then that's perfectly fine. Maybe not ideal, but it's still an algorithm.
    Sometimes the illogical/nonideal algorithm is merely a shortcut.
    Whether an algorithm is logical is a property of the algorithm. It's not about how it appears to someone who lacks full information about it. This misconception is the recurring theme of your examples. The algorithms appear illogical at first sight but then you supply the missing pieces and voila! They all turn out to be logical indeed.

    Logic is at the very core of algorithms. Without logic an algorithm lacks meaning, except maybe as a joke.

    It is a multiple choice questionnaire.
    In that case, the answer is required to be in there.
    You're assuming that one multiple choise alternative must be 100% correct. That's not cut in stone. Several, even all, alternatives may be correct but to a varying degree and the task is to select which applies the best.

    This false premise renders many of your objections moot.

    The really important matters about C++ aren't even being asked AT ALL.
    So no, I don't agree it's a fair quiz.
    If it was a quiz on C... Then... it gets a little closer, but it still fails to ask the important questions about C.
    I agree that the quiz doesn't cover all important aspects of C++ but do you think that's the objective? I don't. To me it looks more like a simple test of who listened up during a course and I think it works fine for that purpose.

    Many of your objections are questionable (like your claim that algorithms don't have to be logical). And many objections are off the mark (because they're based on the false premise that exactly one multiple choise alternative has to be 100% correct). And finally I think you're exaggerating the importance of the quiz. Criticism is fine but it should be relevant and fair.
    Last edited by nuzzle; December 11th, 2012 at 03:50 AM.

  10. #10
    Join Date
    Oct 2008
    Posts
    1,456

    Re: c++ exercises

    just for curiosity, what's the pedagogical purpose or convenience in filling a quiz with things like "void main", wrong terminology ( AND,^ ?? ), wrong references ( eof(), 'W' ?? ) etc ... all these errors could have been avoided without sacrificing clarity or conflicting with the quiz form in any way. According to your reasoning, "a simple test of who listened up" during, say, an english course full of basic grammar errors should be considered "fair" ?

  11. #11
    Join Date
    May 2009
    Posts
    2,413

    Re: c++ exercises

    Quote Originally Posted by superbonzo View Post
    just for curiosity,
    Well, I just think it's a fair quiz. It's not as pretentious as the annotated version by OReubens. Besides it was just a test quiz indicating what to expect later. Hopefully the real thing will be somewhat better worked out.

    If the quiz were a grade-determining final exam for a C++ course at a top university I would think differently. Then I would consider it completely inadequate. But it isn't, it's just a classroom quiz. You have higher expectations on a three-star Michelin Guide resturant than on your local pub so to speak.

  12. #12
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: c++ exercises

    Quote Originally Posted by nuzzle View Post
    Logic is at the very core of algorithms. Without logic an algorithm lacks meaning, except maybe as a joke.

    Now you are changing the words. logic and logical are not the same.
    Because you see algorithms as describing a function in a programming language you require the algoritm to be made of logic blocks. And yes, I agree there. But an algorithm does not have to be for a programming language, a description on how to make a certain cake can equally be an algorithm.

    But the algoritm as a whole does not need to be logical. Even if it is made of logic constructs.


    You're assuming that one multiple choise alternative must be 100% correct. That's not cut in stone. Several, even all, alternatives may be correct but to a varying degree and the task is to select which applies the best.

    This false premise renders many of your objections moot.
    I strongly disagree here. A multiple choice question is supposed to have an unbiased right answer listed.
    If the questionnaire explicitely mentions this as possibilities either as a totality for the questionnaire or per question, it could be a multi-answer ("there may be more than one possible answer"), it could have no answer ("there may be no correct answer"). But without such prior statement, I find that an unacceptable methodology.

    If an answer is allowed to be "close", then I would expect this to be listed in the question as well "what is the closest value that blablabla... ?".

    You are implying it would be perfectly valid for a elementary school exam on math to have a question like follows:
    How much is 2+2 ?
    A) 0 B) 1 C) 3 D) 6


    Or well... Can you please tell me what would be "the closest answer" to question 16 ?
    and please enlighten us on how you derived at that conclusion.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured