# Thread: Some C test questions: challenge

1. 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. 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.

3. Why exactly was the answer 'd' in that previous question?? I got 'c'. Can someone please explain this?

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;
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.

5. 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);```

6. 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): bit level hacking?

7. I guess the result is '19' because 'break' is missing
Ok, nevermind, I overlooked the missing break's...

8. 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. 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");
}```

10. Here is another.

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

11. zdude,

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

Mr. C.

12. 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

13. That's why I like the question.

Mr. C

14. 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

15. 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.