C++

This is a discussion on C++ within the C++ Programming forums, part of the General Programming Boards category; Pls help to check if my answer is correct or not. Q.1 Given the following definiton: char s[10], *p = ...

  1. #1
    nt259
    Guest

    C++

    Pls help to check if my answer is correct or not.
    Q.1
    Given the following definiton:
    char s[10], *p = s;

    Which of the following statement is valid and legal:

    (a) p = s + 5 ;
    (b) s = [ p + s ];
    (c) s[2] = p[12];
    (d) *p = s

    MY ANSWER IS : D

    Q2.

    Given the following code segment:

    char str[] = "Hello";
    char *ptr;
    ptr = &str[1];

    After executed the above codes, what is the value of
    *( ptr + 3 )?

    (a) 'o'
    (b) '\0'
    (c) indeterminate
    (d) address of 'o'
    MY ANSWER IS : D

    Q3.

    Given the prototype for the constructor for a student class is

    Student ( const int idNum = 0,
    const double height = 1.7 );

    The definition
    Student Joshua(3); is to...

    (a) define a student with idNum 0 and height 1.7
    (b) define a student with idNum 3 and height 1.7
    (c) define a student with idNum 0 and height 3
    (d) is illegal
    MY ANSWER IS : B

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Here's what I think :

    1. a
    2. a
    3. b

  3. #3
    ! |-| /-\ +3 1337 Yawgmoth's Avatar
    Join Date
    Dec 2002
    Posts
    187
    This sounds a lot like homework.

    Oh, and nice title. Without it, I would have never guessed this topic was on C++.
    L33t sp3@k sux0rz (uZ it t@k3s 10 m1|\|ut3s 2 tr@nzl@te 1 \/\/0rd & th3n j00 h@\/3 2 g3t p@$t d@ m1zpelli|\|gz, @tr0(i0u$ gr@mm@r @|\|d 1n(0/\/\pr3#3|\|$1bl3 $l@|\|g. 1t p\/\/33nz j00!!

    Speling is my faverit sujekt

    I am a signature virus. Add me to your signature so that I may multiply.

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Re: C++

    Originally posted by nt259
    Pls help to check if my answer is correct or not.
    Q.1
    Given the following definiton:
    char s[10], *p = s;

    Which of the following statement is valid and legal:

    (a) p = s + 5 ;
    (b) s = [ p + s ];
    (c) s[2] = p[12];
    (d) *p = s

    MY ANSWER IS : D
    I disagree. p is a char* and s is a char[] (basically a char*). By dereferencing p, you're doing the assignment char = char*, which is illegal (might work if you have a bad compiler).
    b) is obviously wrong since there is no such syntax in C.
    a) is correct, since both sides of the assignment have the same type.
    c) is also correct, though it is trying to access an element which is out of bounds which may cause a crash.

    Q2.

    Given the following code segment:

    char str[] = "Hello";
    char *ptr;
    ptr = &str[1];

    After executed the above codes, what is the value of
    *( ptr + 3 )?

    (a) 'o'
    (b) '\0'
    (c) indeterminate
    (d) address of 'o'
    MY ANSWER IS : D
    str holds the adress to the 'H', meaning str[1] holds the adress to the 'e'. 3 letters ahead lies the 'o'.
    The answer is a).

    Q3.
    Given the prototype for the constructor for a student class is

    Student ( const int idNum = 0,
    const double height = 1.7 );

    The definition
    Student Joshua(3); is to...

    (a) define a student with idNum 0 and height 1.7
    (b) define a student with idNum 3 and height 1.7
    (c) define a student with idNum 0 and height 3
    (d) is illegal
    MY ANSWER IS : B
    b) is correct. The first parameter gets the passed value and the second uses the default value.

    (Can't believe I'm doing someone's homework... Must be really bored...)
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109

    Re: Re: C++

    Originally posted by Magos
    (Can't believe I'm doing someone's homework... Must be really bored...)
    must be...

    but then again, he did try...

  6. #6
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    My answers are:
    Q1) a
    Q2) a
    Q3) b
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Popular pages Recent additions subscribe to a feed

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