Like Tree5Likes

Google Written Test question...!

This is a discussion on Google Written Test question...! within the General Discussions forums, part of the Community Boards category; I saw this on a facebook group. Many members quickly confirmed that this is indeed from a Google Written test ...

  1. #1
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498

    Google Written Test question...!

    I saw this on a facebook group.
    Many members quickly confirmed that this is indeed from a Google Written test they had appeared for.
    If they said it was from an interview, it would be easy to say that this was a trick question.
    But in a written test without extra options, what are people supossed to do when there is a known undefined behaviour/segfault/etc?

    I've heard stories about non-tech people conducting interviews which are obviously technical in nature...but this takes it a step further.

    And after seeing Google being praised as a developers' heaven in the media for a few years..this seems strange for me.

    Code:
    void f(char *x) 
    {
     
         x++;
     
         *x = ‘a’;
     
    }
     
    int main() 
    {
     
         char *str = "hello";
     
         f(str);
     
         printf("%s", str);
     
    }
     
     a) hello
     
     b) allo
     
     c) hallo
     
     d) empty string
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  2. #2
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    1,008
    The answer is c, right? Or is it an empty string?

  3. #3
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by MutantJohn View Post
    The answer is c, right? Or is it an empty string?

    You totally missed the point.
    There is no answer because you can not modify a string literal.
    It may or may not reside in the read only section of the executable.

    Question 1.32

    For example, it gives me a segfault.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by MutantJohn View Post
    The answer is c, right? Or is it an empty string?
    I hope that's a joke!
    manasij7479 likes this.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by manasij7479 View Post
    I saw this on a facebook group.
    Many members quickly confirmed that this is indeed from a Google Written test they had appeared for.
    It seems unlikely to me that "many members" would know that. More likely they heard the rumor before.

    Although it's obviously undefined behavior, it's not necessarily a segfault as it could work on some systems.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,408
    Quote Originally Posted by manasij7479
    But in a written test without extra options, what are people supossed to do when there is a known undefined behaviour/segfault/etc?
    Inform the relevant people that the test question is wrong. After all, tests can have bugs too.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Right...only one explicitly said that. So, many was an exaggeration.

    And by relevant people, you mean the invigilators ?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  8. #8
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    1,008
    Would the answer have been c if the question was written as,
    Code:
    char a[6] = "hello";
    
    char *b = a;
    That way you actually have memory, I do believe, to modify. Or am I completely wrong? Gah, I'm so bad at tests.
    manasij7479 likes this.

  9. #9
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    1,008
    Alright, I just ran this code :

    Code:
    #include <stdio.h>
    
    
    void f(char *x) 
    {
      
         x++;
      
         *x = 'a';
      
    }
      
    int main(void) 
    {
      
         char test[6] = "hello";
    
    
         char *str = test;
      
         f(str);
      
         printf("%s\n", str);
      
    
    
         return 0;
    }
    and the output is :hallo

    Therefore, the answer is C!!!!!

    Ha ha ha! I can totally work for Google now. Even if that question was on a test, don't assume it's valid code. Just pretend and if you had to do the pointer arithmetic, would you get it right?

  10. #10
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,597
    Besides modifying a literal, notice that main is not given an explicit return value.
    I don't think this is Google's mistake though. If this indeed from Google byte-for-byte, they probably introduced these "errors" just so they could give points to any interviewees that would catch them, considering a sufficiently liberal compiler would make that program perfectly runnable.
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  11. #11
    Registered User
    Join Date
    Oct 2006
    Posts
    2,292
    Quote Originally Posted by Yarin View Post
    Besides modifying a literal, notice that main is not given an explicit return value.
    not sure about C, but in C++, not returning anything from main is defined to be the same as returning 0.
    SirPrattlepod likes this.
    Code:
    namespace life
    {
        const bool change = true;
    }

  12. #12
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by Yarin View Post
    Besides modifying a literal, notice that main is not given an explicit return value.
    Although horrible, C99 (and I think C89) do not require an explicit return in main

    5.1.2.2.3 Program termination
    1 If the return type of the main function is a type compatible with int, a return from the
    initial call to the main function is equivalent to calling the exit function with the value
    returned by the main function as its argument;10) reaching the } that terminates the
    main function returns a value of 0
    . If the return type is not compatible with int, the
    termination status returned to the host environment is unspecified.
    (bold is mine)

  13. #13
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,412
    Quote Originally Posted by laserlight View Post
    Inform the relevant people that the test question is wrong. After all, tests can have bugs too.
    Or can be purposely misleading, no?

    I wish I could remember all the details of the last question on a 3 question exam in preparation for an interview for a software development company in Ireland. It included a quite misleading question. The code was shown as a test case and we were supposed to implement the function(s) which were meant to perform a recursive sum of the elements in the input array. The problem was unsolvable and points were awarded for pointing out all the flaws, which included:

    1) The test case wasn't atomic.
    2) The recursive function was expecting a single element as input.
    3) The helper function return value violated the non-destructive requirement for the input array.
    4) and a couple more...

    My only beef with it was that pointing out 1) was less awarded than the others.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  14. #14
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,412
    Quote Originally Posted by SirPrattlepod View Post
    Although horrible, C99 (and I think C89) do not require an explicit return in main
    I don't do the same reading of 5.1.2.2.3

    What I read in there is that you can omit the return statement. Not the return type. Void is not compatible with int.

    EDIT: Nevermind. I both misread your answer and who you were answering to.
    Last edited by Mario F.; 10-25-2013 at 05:48 PM.
    SirPrattlepod likes this.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  15. #15
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by Mario F. View Post
    I don't do the same reading of 5.1.2.2.3

    What I read in there is that you can omit the return statement. Not the return type. Void is not compatible with int.
    That's what I said...? The original code is int main(void) and omits the return statement, which is fine so far as complying with the standard (we're talking about the return value)

    Edit: perhaps my wording was rushed and contained assumptions based on previous comments; but, yes, I was indeed referring to the return statement (and the value returned if it's omitted) and not the return type.
    Mario F. likes this.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. General question why must of the servers written in c and not c++ ?
    By umen242 in forum Networking/Device Communication
    Replies: 3
    Last Post: 06-26-2008, 04:17 AM
  2. Reading Google Sketchup (.skp) or Google Earth (.kmz)
    By Zeeshan in forum Game Programming
    Replies: 9
    Last Post: 03-07-2008, 04:25 PM
  3. Question of test c++
    By endrzer in forum C++ Programming
    Replies: 8
    Last Post: 01-31-2008, 09:07 AM
  4. test question
    By dudinka in forum C Programming
    Replies: 3
    Last Post: 06-23-2005, 07:59 PM

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