Thread: Please check my work...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    37

    Please check my work...

    These are questions from my studyguide...and i have included my answers....
    Code:
    1.	Variables defined after the opening brace belong to the automatic storage class.  T
    
    2.	The statement   for(;5;)  is an easy way to make a loop execute only five times.      T
    
    3.	The while and for statements are virtually identical so that either can be used in place of the other with slight modification to the code.  F
    
    4.	The extern keyword is needed to reference a piece of data defined in another source file.  T
    
    5.	It is required that macro names be in upper case.  F
    
    6.	The NULL byte is automatically placed at the end of all arrays.  T
    
    7.	Arithmetic expressions in a macro should be surrounded by parentheses, even if that means enclosing the entire macro body.   F
    
    8.	The  return  statement must always be used when leaving a function.   T
    
    9.	The keyword  return()  can be used to return as many data items as you like from a function to the calling program.   T
    
    10.	Creating an array of 5 integers means that the largest subscript (also called index) that you can use is 4.  T
    Are these answers correct...and if not... will you help me understand why?

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    5 out of 10

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You have 3 correct. The first answer is false, because while they may default to automatic types, you can change that with the 'static' keyword.

    [edit]
    As we all know, if part of the answer is false, the whole thing is false. However, you'll probably get it wrong if you say it's false, due to the horrible wording of most all of these questions.
    [/edit]

    Quzah.
    Last edited by quzah; 12-17-2005 at 03:05 PM.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    37

    One More Question

    Code:
    11.	Assuming that   datalist[ ]  is a one-dimensional array of type int, which of the following refers to the value of the third element in the array?
    
    a. *(datalist+2)
    b.	*(datalist+4)         
    c.	datalist+4
    d.	datalist+2
    My answer for this is D
    However, i'm not sure if its B or D.... I believe it should be +2 because arrays start at 0.... Is my answer correct?

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    37
    so is it 5 correct or 3?

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    37
    Also, can someone give me a good schooling on the difference between an automatic and a static variable?

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Either way you're failing, so it doesn't really matter if it's 5 or 3.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    37
    well, i'm here to learn...I want to know why they are wrong... Since you don't want to give me the answers... I can't ask you to tell me which ones i got right. (Due to the nature of the questions: T or F) So what should i do to get help with these questions?

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    37
    Quote Originally Posted by quzah
    Either way you're failing, so it doesn't really matter if it's 5 or 3.


    Quzah.
    please keep in mind that this is only from my study guide...my final is on Monday... Thanks

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Here's an idea. Take them each one at a time, and with the question, write a small paragraph on why you think each one is the way you answered it. Then we can see where your logic is off, and it won't simply be a case of us giving you the right answers. This why you'll have to think about each question.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Nov 2005
    Posts
    37
    The first five....

    Code:
    1.	Variables defined after the opening brace belong to the automatic storage class.  F
    Already explained by Quzah but what is the difference between an automatic and a staic variable?

    Code:
    2.	The statement   for(;5;)  is an easy way to make a loop execute only five times.      T
    initialization; condition; increment

    Code:
    3.	The while and for statements are virtually identical so that either can be used in place of the other with slight modification to the code.  F

    From what i've written, its taken more than slight modification... but what may be slight to someone seems like a lot to me.



    Code:
    4.	The extern keyword is needed to reference a piece of data defined in another source file.  T
    After rereading this question...i don't know if it is "needed" but the defintion i got was "The variable or function may be defined in another source file, or later in the same file. Declarations of variables and functions at file scope are external by default"

    Code:
    5.	It is required that macro names be in upper case.  F
    After further research, this may be True...However, i could only find sources where they said that it is "good programming" but i could not find a source that says it is required?

    edited: to add question to number 1

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by viciousv322
    Code:
    2.	The statement   for(;5;)  is an easy way to make a
    loop execute only five times.      T
    initialization; condition; increment
    Your condition will never evaluate to false. It is like creating the infinite loop like:
    Code:
    while( 1 )
        ...do forever...
    Since the condition always evaluates to true, (anything non-zero is true), this loop will never end.

    Quote Originally Posted by viciousv322
    Code:
    3.	The while and for statements are virtually identical
    so that either can be used in place of the other with slight
    modification to the code.  F

    From what i've written, its taken more than slight modification... but what may be slight to someone seems like a lot to me.
    The changes are slight. It boils down to moving the incrementation and initialization around. Very minor changes in the grand scheme of things.
    Quote Originally Posted by viciousv322
    Code:
    4.	The extern keyword is needed to reference a
    piece of data defined in another source file.  T
    After rereading this question...i don't know if it is "needed" but the defintion i got was "The variable or function may be defined in another source file, or later in the same file. Declarations of variables and functions at file scope are external by default"
    To make a variable in one file visible in another, you reference it via 'extern'. So yes, it's needed.
    Quote Originally Posted by viciousv322
    Code:
    5.	It is required that macro names be in upper case.  F
    After further research, this may be True...However, i could only find sources where they said that it is "good programming" but i could not find a source that says it is required?
    No, it's not needed. The standard doesn't say you have to make your macros upper case. It's commonly done to denote that you are in fact using a macro, but it's not a requirement of the language.

    [edit]
    automatic variables are created at the beginning of the scope block they're defined in. They are not initialized upon creation. They are destroyed at the end of scope. You should not attempt to access them after they have gone out of scope, as they are destroyed and retain no value.

    static variables are created at the start of the program. They are initialized to zero at the time they are created. They only are destroyed at the end of the program. Their values are maintained even when their scope has ended.

    I'm sure someone else will have a few words to say, but that's the basics of it.
    [/edit]

    Quzah.
    Last edited by quzah; 12-17-2005 at 04:08 PM.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User
    Join Date
    Nov 2005
    Posts
    37
    Code:
    6.	The NULL byte is automatically placed at the end of all arrays.  T
    I was under the impression that null bytes were used to end strings of arrays, not sure if its ALL arrays...or if its automatic for that matter...


    Code:
    7.	Arithmetic expressions in a macro should be surrounded by parentheses, even if that means enclosing the entire macro body.   F
    I thought the parenthesis was only used to declare the order of the operations, but not needed for regular/ simple math such as 2+2. Are parenthesis required around the entire operation?

    Code:
    8.	The  return  statement must always be used when leaving a function.   T
    i thought this was a gimme question... am i overlooking/misunderstanding something?

    Code:
    9.	The keyword  return()  can be used to return as many data items as you like from a function to the calling program.   T
    not sure as i couldn't find a good source

    Code:
    10.	Creating an array of 5 integers means that the largest subscript (also called index) that you can use is 4.  T
    Arrays start at 0 so that would be 0,1,2,3,4 for the five integers?

  14. #14
    Registered User
    Join Date
    Nov 2005
    Posts
    37
    So what would be needed to make #2 a true statement? How would i be able to make it loop just 5 times?

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by viciousv322
    Code:
    6.	The NULL byte is automatically placed at the end of all arrays.  T
    I was under the impression that null bytes were used to end strings of arrays, not sure if its ALL arrays...or if its automatic for that matter...
    The null character is only used for character arrays which are to be used as strings. A string is zero or more characters followed by a null character. That's the only time a null character is used. You can initialize character arrays at creation time, or use string literals with pointers, and they'll automatically get a null character in this case. But not all arrays get them. Why would an array of doubles need a null character on the end?
    Quote Originally Posted by viciousv322
    Code:
    7.	Arithmetic expressions in a macro should be surrounded
    by parentheses, even if that means enclosing the entire macro body.   F
    I thought the parenthesis was only used to declare the order of the operations, but not needed for regular/ simple math such as 2+2. Are parenthesis required around the entire operation?
    Consider the following:
    Code:
    #define MULT(x,y) x * y
    Should be a simple multiplication macro, right? Pass it a few arguments, have it multiply them. What happens when I do this:
    Code:
    x = MULT( 2 + 3, 5 + 7 );
    What is x? Expand the macro:
    Code:
    x = 2 + 3 * 5 + 7;
    Not what you had in mind eh?
    Quote Originally Posted by viciousv322
    Code:
    8.	The  return  statement must always be used when leaving a function.   T
    i thought this was a gimme question... am i overlooking/misunderstanding something?
    What about a void function?
    Code:
    void foo( void )
    {
        ...do stuff...
        return; /* <-- not needed */
    }
    Quote Originally Posted by viciousv322
    Code:
    9.	The keyword  return()  can be used to return as many
    data items as you like from a function to the calling program.   T
    not sure as i couldn't find a good source
    You can only ever return one object at a time in a function. You can have multiple return points, but each one still can only return one thing.
    Quote Originally Posted by viciousv322
    Code:
    10.	Creating an array of 5 integers means that the largest subscript
    (also called index) that you can use is 4.  T
    Arrays start at 0 so that would be 0,1,2,3,4 for the five integers?
    This is poorly worded. Yes, you probably have it right. It depends really on their definition of use. Use as a point of reference? Then you'd be wrong. You are allowed to reference 1 past the end of the array. That is to say:
    Code:
    int array[5];
    ...
    if( ptr < array+5 ) /* make sure we haven't reached the end of the array... */
    That is a legal check. So, there I'm "using" in effect array[5]. However, if they mean use in the sense that you can actually check what value is stored there, and store something there, then you were right in saying true, that all you get is 0 ... 4.

    It's really dependant on how pedantic your teacher is. However, reading the wording, and the fact that you don't know most of these answers, I'm going to say your teacher is an idiot, or you just weren't paying attention. I'll assume for the sake of argument that you have been paying attention, and your teacher is lousy. In which case, I'd say "true" to this question.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. Code doesn't work: Check alphabetical order
    By motozarkov in forum C++ Programming
    Replies: 5
    Last Post: 04-27-2005, 02:53 PM
  3. Shouldn't this code work?
    By DeepFyre in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2004, 01:24 PM
  4. If you are employed as a programmer, please look
    By Flood Fighter in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 09-28-2004, 02:35 AM
  5. writing/reading from file produces double results.
    By stumon in forum C Programming
    Replies: 4
    Last Post: 03-20-2003, 04:01 PM