Fun exam questions

Discussion in 'Programming General' started by Sythe, Apr 25, 2010.

Fun exam questions
  1. Unread #1 - Apr 25, 2010 at 4:38 AM
  2. Sythe
    Joined:
    Apr 21, 2005
    Posts:
    8,071
    Referrals:
    467
    Sythe Gold:
    5,281
    Discord Unique ID:
    742989175824842802
    Discord Username:
    Sythe
    Dolan Duck Dolan Trump Supporting Business ???
    Poképedia
    Clefairy Jigglypuff
    Who did this to my freakin' car!
    Hell yeah boooi
    Tier 3 Prizebox Toast Wallet User
    I'm LAAAAAAAME Rust Player Mewtwo Mew Live Free or Die Poké Prizebox (42) Dat Boi

    Sythe Join our discord

    test

    Administrator Village Drunk

    Fun exam questions

    Some of you may know I've been a C programming tutor for a few years.

    If you're taking a C programming course at university these are some of the harder types of questions you might find on a final exam:

    Q1: Is the following valid C?
    Q2: How many times does the following code print "hello\n"?
    Q3: What does the following code do?
    Q4: Why is the following a bad idea?
    Q5: What would the following code do?
    Q6: What is the following code equivalent to?
    Q7: What is the type of foo in the following? Is it valid C?
    Q8: what is the return value of this function?
    Q9: What is the value of c after the following code is executed (assuming c is 2 before execution)?:
     
  3. Unread #2 - Apr 26, 2010 at 7:46 PM
  4. Lukef555
    Joined:
    Aug 16, 2008
    Posts:
    3,206
    Referrals:
    6
    Sythe Gold:
    0

    Lukef555 Grand Master
    Banned

    Fun exam questions

    Wow, these could definitly be helpful, ive been starting programming and was planning on taking some courses. Thanks for the cheat. :p
     
  5. Unread #3 - Apr 26, 2010 at 8:46 PM
  6. aznguy94
    Joined:
    Feb 11, 2007
    Posts:
    304
    Referrals:
    0
    Sythe Gold:
    0

    aznguy94 Forum Addict

    Fun exam questions

    c++ would be easier >.>
     
  7. Unread #4 - Apr 28, 2010 at 10:53 PM
  8. The Fat Controller
    Joined:
    Aug 16, 2007
    Posts:
    1,003
    Referrals:
    0
    Sythe Gold:
    1

    The Fat Controller Guru

    Fun exam questions

    4) looks like a fork bomb.
    5) will print 10? EDIT: 8, it assigned the 010 as an octal number?

    It must take a long time to know enough C to answer ones like Q9 in an exam hall :eek:
     
  9. Unread #5 - May 10, 2010 at 10:30 AM
  10. Sythe
    Joined:
    Apr 21, 2005
    Posts:
    8,071
    Referrals:
    467
    Sythe Gold:
    5,281
    Discord Unique ID:
    742989175824842802
    Discord Username:
    Sythe
    Dolan Duck Dolan Trump Supporting Business ???
    Poképedia
    Clefairy Jigglypuff
    Who did this to my freakin' car!
    Hell yeah boooi
    Tier 3 Prizebox Toast Wallet User
    I'm LAAAAAAAME Rust Player Mewtwo Mew Live Free or Die Poké Prizebox (42) Dat Boi

    Sythe Join our discord

    test

    Administrator Village Drunk

    Fun exam questions

    Correct, it is deferencing the address 'z' (the asciicode of z) and assigning it the value '010', which is octal for 8. The branch would always be taken because it is an assignment and an not equality operator; But in this case the if-statement is followed immediately by a semicolon, so the following statement is not part of any branch -- it is instead an independent code-block.

    You should take in an order of precedence table of operators if you are allowed. This might help you solve a question like 9.

    In this particular case I believe the language states the code is ambiguous and undefined.

    One might interpret it as:
    c += (c++) + (+c); // or
    c += (c) + (+(++c));
     
  11. Unread #6 - May 11, 2010 at 1:19 PM
  12. XeNoS
    Joined:
    May 6, 2010
    Posts:
    6
    Referrals:
    0
    Sythe Gold:
    0

    XeNoS Newcomer

    Fun exam questions

    1: No

    2: 99 times because it's not stated as less than or equal too. ( Also if it would've been a while loop, and the increment sign was in front of the output of the integer! )

    3: Not valid

    4: Not valid

    5: If z is a pointer to 010

    6: 0? A is a char with an integer, you couldn't compile

    7: Yes

    8: Idk

    9: C

    Idk if all of these are right, those were pretty tricky.
     
  13. Unread #7 - May 12, 2010 at 1:39 AM
  14. Sythe
    Joined:
    Apr 21, 2005
    Posts:
    8,071
    Referrals:
    467
    Sythe Gold:
    5,281
    Discord Unique ID:
    742989175824842802
    Discord Username:
    Sythe
    Dolan Duck Dolan Trump Supporting Business ???
    Poképedia
    Clefairy Jigglypuff
    Who did this to my freakin' car!
    Hell yeah boooi
    Tier 3 Prizebox Toast Wallet User
    I'm LAAAAAAAME Rust Player Mewtwo Mew Live Free or Die Poké Prizebox (42) Dat Boi

    Sythe Join our discord

    test

    Administrator Village Drunk

    Fun exam questions

    Wrong.

    Wrong. Look up #define macros.

    Wrong.

    Wrong.

    Wrong/Didn't answer the question.

    Wrong.

    Wrong/Didn't answer the question.

    Wrong.

    Wrong.

    You scored: 0/9.
     
  15. Unread #8 - May 13, 2010 at 5:51 AM
  16. XeNoS
    Joined:
    May 6, 2010
    Posts:
    6
    Referrals:
    0
    Sythe Gold:
    0

    XeNoS Newcomer

    Fun exam questions

    1: Yes?

    O.O
     
  17. Unread #9 - May 13, 2010 at 6:29 AM
  18. Sythe
    Joined:
    Apr 21, 2005
    Posts:
    8,071
    Referrals:
    467
    Sythe Gold:
    5,281
    Discord Unique ID:
    742989175824842802
    Discord Username:
    Sythe
    Dolan Duck Dolan Trump Supporting Business ???
    Poképedia
    Clefairy Jigglypuff
    Who did this to my freakin' car!
    Hell yeah boooi
    Tier 3 Prizebox Toast Wallet User
    I'm LAAAAAAAME Rust Player Mewtwo Mew Live Free or Die Poké Prizebox (42) Dat Boi

    Sythe Join our discord

    test

    Administrator Village Drunk

    Fun exam questions


    int main() {
    // define a pointer (called main) to a function that takes unknown arguments and returns an int.
    int (*main)();

    { // free standing codeblock.
    printf("hello world\n");

    return 0;
    }
    }
     
  19. Unread #10 - May 14, 2010 at 3:00 PM
  20. Govind
    Joined:
    Apr 22, 2005
    Posts:
    7,825
    Referrals:
    13
    Sythe Gold:
    23
    Prove it! Trole Tier 1 Prizebox Tortoise Penis Le Monkey UWotM8? Wait, do you not have an Archer rank? Potamus

    Govind The One Musketeer
    Mudkips Highly Respected Retired Administrator

    Fun exam questions

    (I promise I didn't look at the other posts in this thread)


    1. Yes.
    2. Infinite: you have a for(;;) with that macro.
    3. I don't know.
    4. It would eventually use up all the resources on your system because fork() spawns a new copy of the running executable, right?
    5. You have a semicolon after your if statement, so, um, the code block after it doesn't depend on that? Not to mention it's a single = sign not ==, so the if statement has no real test expression anyway. In the off-chance that you did this intentionally, I don't know what it's supposed to do.
    6. I give up.
    7. Function of type **void.
    8. Nothing, because those should be commas, not semicolons. EDIT: VS2008 doesn't compile that.
    9. c += c++ +(+c) = 2 += 2 + 2 = 6? I thought it was 7 at first but then I realized that if it was (c++) (++c) there would be no mathematical operation.
     
  21. Unread #11 - May 17, 2010 at 2:39 AM
  22. Sythe
    Joined:
    Apr 21, 2005
    Posts:
    8,071
    Referrals:
    467
    Sythe Gold:
    5,281
    Discord Unique ID:
    742989175824842802
    Discord Username:
    Sythe
    Dolan Duck Dolan Trump Supporting Business ???
    Poképedia
    Clefairy Jigglypuff
    Who did this to my freakin' car!
    Hell yeah boooi
    Tier 3 Prizebox Toast Wallet User
    I'm LAAAAAAAME Rust Player Mewtwo Mew Live Free or Die Poké Prizebox (42) Dat Boi

    Sythe Join our discord

    test

    Administrator Village Drunk

    Fun exam questions

    Correct.
    Correct.
    Wrong/Didn't answer.

    int main(void); //Prototype for main
    int(*__)()=main; // Function pointer called __ which points at main
    int(**_)()=&__; // Function pointer pointer called _ which points at __
    int main(void){
    (*_)(); // dereference _, and call the resulting function pointer. (Infinite recursion.)
    }

    Correct, fork bomb.

    Half mark, didn't answer the question but got halfway there.

    (It is intentional, and it's good you picked it up.)

    The code dereferences the ascii-code for 'z' and sets the memory there to octal 010. (decimal 8). It then proceeds to the unconnected codeblock, and prints the memory at the address which is the ascii-code of 'z', which we just set to 010, which is 8. So it prints 8.

    Wrong/didn't answer the question.

    The code is equivalent to the following:
    char a[10];
    int b = 5;
    a = 5;

    or

    char a[10];
    a[5] = 5;


    Wrong, it's a function pointer. I won't reveal what type of function pointer.

    Wrong.

    The last value or variable in a code-block is it's value if used in an expression. This is actually valid C but not all compilers support it. That function returns the octal value 011.

    It's ambiguous, it depends on the compiler. I'll give you this mark.

    Your score: 4.5/9.
     
  23. Unread #12 - May 17, 2010 at 6:47 PM
  24. Govind
    Joined:
    Apr 22, 2005
    Posts:
    7,825
    Referrals:
    13
    Sythe Gold:
    23
    Prove it! Trole Tier 1 Prizebox Tortoise Penis Le Monkey UWotM8? Wait, do you not have an Archer rank? Potamus

    Govind The One Musketeer
    Mudkips Highly Respected Retired Administrator

    Fun exam questions


    I can't believe I didn't see that one right away. Oh well...
     
  25. Unread #13 - May 24, 2010 at 5:29 AM
  26. vbpro
    Joined:
    Apr 20, 2010
    Posts:
    91
    Referrals:
    0
    Sythe Gold:
    0

    vbpro Member
    Banned

    Fun exam questions

    Thankyou! Im currently doing computer programming, year 12 and was looking as of what university exams would look like. One quick question, do you think by the time im in university they will still teach c? or just c++ and other?
     
  27. Unread #14 - May 25, 2010 at 12:19 PM
  28. Sythe
    Joined:
    Apr 21, 2005
    Posts:
    8,071
    Referrals:
    467
    Sythe Gold:
    5,281
    Discord Unique ID:
    742989175824842802
    Discord Username:
    Sythe
    Dolan Duck Dolan Trump Supporting Business ???
    Poképedia
    Clefairy Jigglypuff
    Who did this to my freakin' car!
    Hell yeah boooi
    Tier 3 Prizebox Toast Wallet User
    I'm LAAAAAAAME Rust Player Mewtwo Mew Live Free or Die Poké Prizebox (42) Dat Boi

    Sythe Join our discord

    test

    Administrator Village Drunk

    Fun exam questions


    Any respectable computer science or IT degree will contain at minimum one course in each C and Java programming. You might also get your hands on C++ depending on how good the university is.
     
  29. Unread #15 - Jul 11, 2010 at 6:21 PM
  30. halo2ownage8
    Joined:
    Jul 11, 2010
    Posts:
    7
    Referrals:
    0
    Sythe Gold:
    0

    halo2ownage8 Newcomer

    Fun exam questions

    This looks very difficult not even sure if i wanna major in computer science anymore :/
     
  31. Unread #16 - Jul 11, 2010 at 6:56 PM
  32. blindkilla
    Joined:
    Jun 22, 2005
    Posts:
    1,896
    Referrals:
    0
    Sythe Gold:
    6
    Discord Unique ID:
    282000633404456960
    Discord Username:
    sogord

    blindkilla Guru
    $25 USD Donor New

    Fun exam questions

    Don't let the syntax scare you off. Once you start learning the language and start understanding the basics and getting into the harder stuff, this will be nothing.
     
  33. Unread #17 - Sep 16, 2010 at 9:40 PM
  34. clawed
    Joined:
    Jan 29, 2010
    Posts:
    230
    Referrals:
    0
    Sythe Gold:
    0

    clawed Active Member

    Fun exam questions

    Pure giberish... What is the Owner talking about?
     
  35. Unread #18 - Sep 17, 2010 at 8:30 PM
  36. Kleenex
    Joined:
    Feb 12, 2008
    Posts:
    2,815
    Referrals:
    2
    Sythe Gold:
    0

    Kleenex Grand Master
    Banned

    Fun exam questions

    If you don't know a thing about C, don't post here.
     
  37. Unread #19 - Nov 18, 2010 at 2:05 PM
  38. Blunted xD
    Joined:
    Nov 18, 2010
    Posts:
    40
    Referrals:
    0
    Sythe Gold:
    2

    Blunted xD Member

    Fun exam questions

    I really need to stop slacking off and finally learn this.
     
  39. Unread #20 - Nov 19, 2010 at 9:57 AM
  40. blahbleh
    Joined:
    Nov 7, 2010
    Posts:
    179
    Referrals:
    0
    Sythe Gold:
    0

    blahbleh Active Member

    Fun exam questions

    Make sure you do not waste too much time for C because it is quite obsolete nowadays.
     
< Starting C++ | Anyone do Applescript? >

Users viewing this thread
1 guest


 
 
Adblock breaks this site