Thread: Some C test questions: challenge

  1. #31
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Thumbs up

    Moi you are right- Check before giving the answers.

    I meant I posted two question. I need to finish Java.

    Also, check spelling

    Mr. C.

  2. #32
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    29 d. both | and || are binary operators.....
    bool operator | (arg1,arg2)
    bool operator || (arg1,arg2)

    both take 2 arguments.

    21 c. you can play with the bits in a floating point number with bitwise operators with some creative casting but not directly.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #33
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Why exactly was the answer 'd' in that previous question?? I got 'c'. Can someone please explain this?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #34
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    //radius = (radius++) + (z++)

    radius = radius + z;
    radius = radius + 1;
    z = z++;
    OK, so that's how you cook the books to get 48

    But that's only one possible interpretation of that expression.

    And this maybe, is how moi got 47 with
    > lcc for windoze v2.5 output radius=47

    Code:
    //radius = (radius++) + (z++)
    int temp1 = radius;      // save old value before incrementing it
    int temp2 = z;
    radius = radius + 1;     // increment radius (ie radius++)
    z = z+1;                 // increment z (ie z++)   z=z++ is broken 
    radius = temp1 + temp2;  // calculate the sum
    Oh no!, the increment of radius is lost because it got written to store before the sum did.

    Here's another thread where the poster managed to dig a big hole around expressions with multiple side effects and undefined behaviour.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #35
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166
    Originally posted by Sebastiani
    Why exactly was the answer 'd' in that previous question?? I got 'c'. Can someone please explain this?
    Code:
    // general form of the switch statement
    
    switch (expression)
    {
        case template_1: statement(s); break;
        case template_2: statement(s); break;
        ...
        case template_n: statement(s); break;
        default: statement(s);
    }
    I guess the result is '19' because 'break' is missing at the end of each statement. If a match is found between the expression and one of the templates, the statement that follows the case label is executed. After that execution is transferred to the first statement following the switch statement's closing brackets. But as a result of the missing 'break', the statements of the following case labels are executed too!
    Code:
    int num;
    int alpha = 10;
    
    scanf(“%d\n”, &num);
    switch (num)
    {
        case 3  : alpha++;
        case 4  : alpha = alpha + 2;
        case 8  : alpha = alpha + 3;
        default : alpha = alpha + 4;
    }
    printf(“%d\n”, alpha);
    Last edited by Sargnagel; 09-08-2002 at 05:12 AM.

  6. #36
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by Mister C


    Code:
    21. The bitwise operators can be used to manipulate the bits of variables of type __________.
    a) float
    b) double
    c) long
    d) long double
    Mr. C
    i agree that it is poor form, but one can manipulate the bits of a float (even though in this particular example the bitwise operators are not used, they could be): http://cboard.cprogramming.com/showt...threadid=24350
    hello, internet!

  7. #37
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    I guess the result is '19' because 'break' is missing
    Ok, nevermind, I overlooked the missing break's...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  8. #38
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    I forgot to post the two answers to the the last two answers but they were D - Switch and C- for the bitwise. Thank you salem about the discussion of the prefix/postfix question. Clarification and being correct-is important. l left out a few steps.


    Anyway, Here are two more problems. Once again I would like to see more of your best questions.

    Code:
    Which of the following is not a valid operation on a structure?
    a) Assigning structure variables to structure variables of the same type.
    b) Taking the address of a structure variable.
    c) Using the sizeof operator to determine the size of a structure variable.
    d) Comparing structures of the same type with relational operators.
    Code:
    31Operator ##
    a) concatenates two tokens in a macro definition.
    b) is a relational operator. 
    c) is the conditional-compilation operator.
    d) is the symbolic-constant operator.

    I will check back later with the correct answers.

    Mr. C.

  9. #39
    Registered User zdude's Avatar
    Join Date
    Sep 2002
    Posts
    32
    You could do a question about the binary operator ^^, what it is or what would happen if this was executed:

    Code:
    bool a = TRUE, b = TRUE;
    
    if (a ^^ b){
        printf("TRUE");
    } else {
        printf("FALSE");
    }
    Those who live by the sword get shot by those who don't.

    I can C.

    Compiler: gcc

  10. #40
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    Here is another.

    Code:
    Evaluate (00001000 & 11000101) ^ (11110000)
    (a) 00111101
    (b) 11000000
    (c) 00111101
    (d) 11110000
    Mr. C

  11. #41
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    zdude,

    I like that one. I will use it. Although bool is not in the C language.

    Mr. C.

  12. #42
    Registered User Dr. Bebop's Avatar
    Join Date
    Sep 2002
    Posts
    96
    There is no ^^ operator in C, there also isn't a bool data type or TRUE and FALSE macros unless you define them yourself. I have heard of people wondering about a logical exclusive OR which would probably be ^^, but I also remember them saying that the reason it wasn't created is because there's no point. I think you meant the binary XOR operator, which is just one ^.
    Code:
    #include <stdio.h>
    
    #define TRUE (1==1)
    #define FALSE !TRUE
    
    typedef int bool;
    
    int main() {
    	bool a = TRUE, b = TRUE;
    
    	if (a ^ b) {
    		printf("TRUE\n");
    	} else {
    		printf("FALSE\n");
    	}
    
    	return 0;
    }
    Bebop
    Processing error: Stupidity detected.
    ------------------------------
    Dr. Bebop
    Windows XP Professional Ed.
    Microsoft Visual Studio 6

  13. #43
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    That's why I like the question.

    Mr. C

  14. #44
    Registered User Dr. Bebop's Avatar
    Join Date
    Sep 2002
    Posts
    96
    There's no point in using trick questions unless you want your students to miss them. The last time I checked people didn't go to school to pass or fail, they go to learn. There's no practical application for knowing that ^^ doesn't exist, so it's a stupid question, you don't get anything from having it.

    Bebop
    Processing error: Stupidity detected.
    ------------------------------
    Dr. Bebop
    Windows XP Professional Ed.
    Microsoft Visual Studio 6

  15. #45
    Registered User zdude's Avatar
    Join Date
    Sep 2002
    Posts
    32
    I think that since this is programming it might be a little more important than in other subjects, because of the precision required. Never try to argue semantics with a computer.
    Those who live by the sword get shot by those who don't.

    I can C.

    Compiler: gcc

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie needs help..
    By xpress urself in forum C++ Programming
    Replies: 3
    Last Post: 07-26-2007, 07:22 PM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. My C++ test is COMING!!...
    By [Z-D] in forum C++ Programming
    Replies: 52
    Last Post: 12-01-2006, 08:02 PM
  4. undefined reference
    By 3saul in forum Linux Programming
    Replies: 12
    Last Post: 08-23-2006, 05:28 PM
  5. Bitwise Test Questions
    By Mister C in forum C Programming
    Replies: 9
    Last Post: 11-27-2002, 06:06 PM