Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

multiple choice questions in string and array

The followings are interesting and tricky multiple choices questions in C++ . I want to share and discuss these questions in forum.
1) Which of the following expression is true?
a. 3 < 3
b. 3 < 3.0
c. string("ABLE") == string("able")
d. string("aBlE") < string("AbLe")
e. string("able") != string("ABLE")

2) An array declared int A[100] is to be passed as a parameter to a function. Which of the following may not be used to declare the function's formal parameter?
a. int *P
b. int P[]
c. int P[50]
d. int P[100]
e. int P[int]

3) If a list contains an array declared with int A[100]. What will be the value of Size when the list is full?
a. -1
b. 0
c. 99
d. 100
e. 101

4) Consider an unordered list of integers which includes an array and a Size value. The Size value equals 100. What is the minimum number of assignment operations needed in order to delete (or remove) the data stored in location 1of the list's array? In other word, when the work is completed, Size will be 99 and the contents of the array will be adjusted appropriately.
a. 1
b. 2
c. 99
d. 100
e. 101

Hope to hear from members in the forum.
[1251 byte] By [booker] at [2007-11-11 11:58:42]
# 1 Re: multiple choice questions in string and array
and what's your suggestions to discuss them with you ?
Amahdy at 2007-11-11 20:55:35 >
# 2 Re: multiple choice questions in string and array
My answer is 1) c ; 2) e ; 3) d ; 4) 2.
However, I am sure about question 1 and 4. Please check if there is something wrong.
Could someone tell me what the criteria to compare two strings.
booker at 2007-11-11 20:56:35 >
# 3 Re: multiple choice questions in string and array
ok for the computer "A" is different from "a" .. if it wasn't different then how can i write here Aa and you can see it like that exactly ?
this was for the first question.

for the second :
when we declare "int A[100]" then we need 100 version of A denoted by A[0] up to A[99] .. then we need at least 100 destinations in the function arguments .. check the methods used to pass some array to the function and which of them can't accept those 100 A's

for the third:
ok i told the answer in the previous question ... size is how many elements are used ... when it's full it should have all the elements used so what's their count ?

for the fourth:
he wants here to move back the array elements one by one, to remove the first element so how much assignments are needed do u think ?

hope that any of those hints helps you
Amahdy at 2007-11-11 20:57:39 >
# 4 Re: multiple choice questions in string and array
Thank you for your hints, Amahdy.
For 1), I checked on the computer and it seems that string("a")> string("A"). Therefore, the answer would be e.
For 2), I have never seen int P[int]. Could you please tell me whether it is a legal declaration or not?
For 3), I believe the answer will be 100?
For 4), as Amahdy's hints, first i have to "delete" the integer at location 1. Then i have to move back the array elements one by one (99 steps). Finally, i have to resize the array from 100 to 99. So I have totally 101 assigments? Please check for me.
booker at 2007-11-11 20:58:40 >
# 5 Re: multiple choice questions in string and array
good;
1 is correct ..

2 .. he is asking about the formal not the correct syntax .. so what's semanticly not correct .... of course the array indexes are always integer values and this p[int] has no scence .. but formaly correct .. as if i tell him p is an array with an integer index and i haven't specified this index value.

3 correct too

4 explain more how you resize the array and which are those two stpes for this resize operation to see if your answer is correct or no .
Amahdy at 2007-11-11 20:59:40 >
# 6 Re: multiple choice questions in string and array
E C C A

On the first one, 3 < 3.0 potentially but not always, due to floating point roundoff 3.0 might actually be 2.99999999999999999999999 or something. However E is always true.

for the last one, you need a single memcpy command to do the assignment, which is the proper way to perform this task.

The size is 99, if you count from zero, which is the C++ standard. It is 100 if you count from 1, which is the non-techinal / user method.

[int] is ugly, and redundant, but [50] will never work. [] is ugly too, because it will not accept a pointer only an array (I think? or was this a compiler problem? ). An * always works, array or pointer does not matter. [100] is iffy, you can use it to force the caller to send an exact match but its far less flexible approach, I hate this type of babysitting of the user.
jonnin at 2007-11-11 21:00:46 >
# 7 Re: multiple choice questions in string and array
jonnin:
I think he is asking about the size, how many elemnts, they r 100 elemnts from 0 to 99 ... but the last index is 99.

for the last one, he is talking about arrays ... and also he want the number of assignment operations, memcopy is a function call not assignment or if we look deeper we don't know exactly the number of assignments are there in this function definition !
Amahdy at 2007-11-11 21:01:44 >
# 8 Re: multiple choice questions in string and array
So lets assume you had a class instead, so memcpy is not allowed now... then you have to copy as many as size -1 times or an average of size/2 items every time you do a "dumb" insert or delete. However, for a class in an array doing these sorts of things, you would make the main array an array of pointers to the class objects (and perhaps maintain a seperate array of class objects of which you can point to). Then you are back to memcpy to shuffle the pointer array around when you need to re-order it... which is back to extremely fast copying instead of very slow copying, which no one would actually do in a real system.
jonnin at 2007-11-11 21:02:42 >