If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

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.

Re: c++ exercises

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.

Re: c++ exercises

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.

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>>

Re: c++ exercises

Originally Posted by orabi

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.

Re: c++ exercises

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 mehttp://en.wiktionary.org/wiki/algorithmhttp://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.

Re: c++ exercises

Originally Posted by OReubens

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.

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" ?

Re: c++ exercises

Originally Posted by superbonzo

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.

Re: c++ exercises

Originally Posted by nuzzle

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.