multiple choice questions in string and array


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 1 of 3 123 LastLast
Results 1 to 15 of 37

Thread: multiple choice questions in string and array

  1. #1
    Join Date
    Nov 2007
    Posts
    3

    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.

  2. #2
    Join Date
    Oct 2005
    Location
    Maady
    Posts
    1,819
    and what's your suggestions to discuss them with you ?
    Programmer&Cracker CS
    MyBlog:Blog.Amahdy.com
    MyWebsite:www.Amahdy.com

  3. #3
    Join Date
    Nov 2007
    Posts
    3
    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.

  4. #4
    Join Date
    Oct 2005
    Location
    Maady
    Posts
    1,819
    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
    Programmer&Cracker CS
    MyBlog:Blog.Amahdy.com
    MyWebsite:www.Amahdy.com

  5. #5
    Join Date
    Nov 2007
    Posts
    3
    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.

  6. #6
    Join Date
    Oct 2005
    Location
    Maady
    Posts
    1,819
    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 .
    Programmer&Cracker CS
    MyBlog:Blog.Amahdy.com
    MyWebsite:www.Amahdy.com

  7. #7
    Join Date
    Dec 2003
    Posts
    3,366
    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.

  8. #8
    Join Date
    Oct 2005
    Location
    Maady
    Posts
    1,819
    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 !
    Programmer&Cracker CS
    MyBlog:Blog.Amahdy.com
    MyWebsite:www.Amahdy.com

  9. #9
    Join Date
    Dec 2003
    Posts
    3,366
    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.

  10. #10
    Join Date
    Oct 2005
    Location
    Maady
    Posts
    1,819
    Well jonnin you want to talk very abstractly, but for this mcq especially the answer of the last question is size-1 = 99 => C ; at least because the question has "list of integers" not pointers neither objects . and I agree with you nobody use this method of deleting and insertion in real system, that's why I asked the him

    "explain more how you resize the array and which are those two stpes for this resize operation"

    because if we want this type of operations we will at least use a LIST not an ARRAY ... I just wanted that he detect that we can't resize an array and once defined we can't change its size.
    Programmer&Cracker CS
    MyBlog:Blog.Amahdy.com
    MyWebsite:www.Amahdy.com

  11. #11
    Join Date
    Dec 2003
    Posts
    3,366
    Im sure you know this but you can resize a dynamic array, if you go back to C, using realloc. This is probably what the STL vectors do deep inside (or the assembly version to do the same). This is terribly slow, I never found a use for it because it is such a brute force operation, and I don't like that vectors do it behind your back (you have to keep an eye on them and manage them to prevent this penalty).

    And yes, I was giving the wrong answers intentionally just to make a point.

  12. #12
    Join Date
    Oct 2005
    Location
    Maady
    Posts
    1,819
    Thanks jonnin, hope that the OP has more info now ...
    Programmer&Cracker CS
    MyBlog:Blog.Amahdy.com
    MyWebsite:www.Amahdy.com

  13. #13
    Join Date
    Mar 2007
    Location
    Bangalore, India
    Posts
    247
    Regarding the 2nd question, all except e will work.
    ie, int p[int] is not accepted by the compiler. (I use gcc (g++) version 3.4.5., which is a standard C++ compiler)

    According to the C++ standard, they are all equivalent. Ie int p[50] will work just as fine as int p[100], even though it is better to use either int p[] or int *p. It will all be converted to pointer, because, according to the standard, you cannot pass an array to a function (in the sense that it will be sent as a pointer).

  14. #14
    Join Date
    Oct 2005
    Location
    Maady
    Posts
    1,819
    Both of p[50] and p[int] will give compile error, the first at the function calling and the second at the function definition.
    it will say can't convert from int[50] to int[100] or something similar.
    I just thing that p[int] is formaly accepted as the question ask like when I say something like this :

    int i = [int | INT | (int*)char | ...]
    this is like the formal expression which descripe i can have what in the right side.
    so p[int] is formally we say p can have a non specified int in the function like :
    p[E] , E belongs to int ... can be 0 , 1 , or 500 .. I'll choose exactly E=100 to solve this.
    but p[50] doesn't have a solution, I have specified that p is 50 element and can't by any way accept the A[100] .
    Programmer&Cracker CS
    MyBlog:Blog.Amahdy.com
    MyWebsite:www.Amahdy.com

  15. #15
    Join Date
    Mar 2007
    Location
    Bangalore, India
    Posts
    247

    Compiler should be standards compliant.

    Amahdy, please let me know which compiler you are using. I tried i with G++ (GCC) 3.4.5 (mingw special) for Windows. Your compiler might be an old one.


    Program:
    Code:
    #include <iostream>
    
    using namespace std;
    
    
    void fn1(int *p);
    void fn2(int p[100]);
    void fn3(int p[50]);
    //void fn4(int p[int]);
    void fn5(int p[]);
    
    
    int main()
    {
     int nums[100];
     nums[70] = 7070;
    
     fn1(nums);
     fn2(nums);
     fn3(nums);
     //fn4(nums);
     fn5(nums);
    
     return 0;
    }
    
    void fn1(int *p)
    {
     cout<<"void fn1(int *p): "<<p[70]<<endl;
    }
    
    void fn2(int p[100])
    {
     cout<<"void fn1(int p[100]): "<<p[70]<<endl;
    }
    
    void fn3(int p[50])
    {
     cout<<"void fn1(int p[50]): "<<p[70]<<endl;
    }
    
    //void fn4(int p[int]) {}
    
    void fn5(int p[])
    {
     cout<<"void fn1(int p[]): "<<p[70]<<endl;
    }

    Output:
    Code:
    C:\junk\tryouts>g++ t1.cpp -o t1.exe
    
    C:\junk\tryouts>t1
    void fn1(int *p): 7070
    void fn1(int p[100]): 7070
    void fn1(int p[50]): 7070
    void fn1(int p[]): 7070
    
    C:\junk\tryouts>
    According to the new C++ standards, an array declaration in a function should be converted into a pointer. (Also in C99 standards). The compilers which are compliant will not give an error, because all of them become pointer types. (You can check with a sizeof for the int p[100] within the function, it will give the sizeof a pointer, not int[100]. GCC doesn't even give you a warning for that matter.

Similar Threads

  1. string array in c++
    By emeric in forum C++
    Replies: 11
    Last Post: 07-16-2007, 10:36 PM
  2. verify local admin
    By Patrick Comeau in forum VB Classic
    Replies: 6
    Last Post: 03-22-2001, 11:50 PM
  3. SafeArrayCopy SLOWER than iterating string array!
    By Mark Alexander Bertenshaw in forum VB Classic
    Replies: 10
    Last Post: 06-16-2000, 06:34 AM
  4. SafeArrayCopy SLOWER than iterating string array!
    By Mark Alexander Bertenshaw in forum VB Classic
    Replies: 0
    Last Post: 06-12-2000, 09:15 PM
  5. Trying to print a PDF File from VB
    By Kunal Sharma in forum VB Classic
    Replies: 2
    Last Post: 04-25-2000, 04:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center