Study Questions #4 - 50-85 Flashcards

1
Q
Show the output produced by the following program fragment.
int i = 1;
int j = 2;
i = + - + - + - j;
printf("%d %d\n", i, j);
A

-2 2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
Show the output produced by the following program fragment.
int i = 1;
int j = 2;
i = - + - + - ++ j;
printf("%d %d\n", i, j);
A

-3 3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
Show the output produced by the following program fragment.
int i = 1;
int j = 2;
i = + - + - + -- j;
printf("%d %d\n", i, j);
A

1 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Show the output produced by the following program fragment.
int i = 1, j = 2;
int k = 3, m = 4;
i += j - (k *= m++);
printf(“%d %d %d %d\n”, i++, ++j, k–, –m);

A

-9 3 12 4

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Show the output produced by the following program fragment.
int i = 1, j = 2, k = 3, m = 4;
i *= –j * (k *= –m);
printf(“%d %d %d %d\n”, i++, ++j, k–, –m);

A

9 2 9 2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Show the output produced by the following program fragment.
int i = 1, j = 2;
int k = 3, m = 4;
i %= j++ % (k += –m);
printf(“%d %d %d %d\n”, i++, ++j, k–, –m);

A

1 4 6 2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Show the output produced by the following program fragment.
int i = 1, j = 2;
int k = 3, m = 4;
i *= j / - (k -= ++m);
printf(“%d %d %d %d\n”, i++, ++j, k–, –m);

A

1 3 -2 4

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

If the value of n is 4 and m is 5, will the value of ++(n * m) be 21? Explain your answer.

A

++(n * m) = 21. The 4x5 will be evaluated first and then the ++ will be added after (the brackets has precedence).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

In which order the following operators will be executed:

1. i j k

A

Will be executed from left to right*.

  1. The less than will be evaluated before the two logical or’s.
  2. The less than will be evaluated before the two logical and’s.
  3. Less than, and, or
  4. Less than, left to right.
  5. Left to right.
  6. less than, equal, and
  7. less than, not equal, or
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Show the output produced by the following program fragment.
int i = 2, j = 3, k = i * j == 6;
printf(“%d\n”, k);

A

1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Show the output produced by the following program fragment.
int i = 5, j = 10, k = 1;
printf(“%d\n”, k > i < j);

A

1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Show the output produced by the following program fragment.
int i = 3, j = 2, k = 1;
printf(“%d\n”, i < j == j < k);

A

1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Show the output produced by the following program fragment.
int i = 3, j = 4, k = 5;
printf(“%d\n”, i % j + i < k);

A

0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Show the output produced by the following program fragment.
int i = 3, j = 4, k = 5;
printf(“%d\n”, i % j + i < k );

A

0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Show the output produced by the following program fragment.
int i = 3, j = 4, k = 5;
printf(“%d\n”, k = i * j == 6 );

A

0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Show the output produced by the following program fragment.
int i = 3, j = 4, k = 5;
printf(“%d\n”, i < j == k < i );

A

0

17
Q

Show the output produced by the following program fragment.
int i = 3, j = 4, k = 5;
printf(“%d\n”, k > i < j );

A

1

18
Q

Show the output produced by the following program fragment.
int i = 3, j = 4, k = 5;
printf(“%d\n”, i < j || ++ j < k );
printf(“%d %d %d\n”, i, j, k );

A

1

3 4 5

19
Q

Show the output produced by the following program fragment.
int i = 7, j = 8, k = 9;
printf(“%d\n”, i – 7 && j ++ < k );
printf(“%d %d %d\n”, i, j, k );

A

Error. The i-7 is not legal. It is undefined.

20
Q

Show the output produced by the following program fragment.
int i = 7, j = 8, k = 9;
printf(“%d\n”, ( i = j ) || ( j = k ));
printf(“%d %d %d\n”, i, j, k );

A

1

8 8 9

21
Q

Show the output produced by the following program fragment.
int i = 1, j = 1, k = 1;
printf(“%d\n”, ++i || ++ j && ++ k );
printf(“%d %d %d\n”, i, j, k );

A

1

2 1 1

22
Q

Show the output produced by the following program fragment. Assume that i, j, and k are int variables and you
compile the segment using C99. What will be the produced output if i, j, and k are bool?
i = 0; j = 1; k = 2;
printf(“%d %d %d\n”, i, j, k);
printf(“%d %d %d\n”, i–, j–, k–);
printf(“%d %d %d\n”, i, j, k);

A

int 0 1 2
0 1 2
-1 0 1
Bool i – not allowed for bool.

23
Q

Show the output produced by the following program fragment. Assume that i, j, and k are int variables and you
compile the segment using C99. What will be the produced output if i, j, and k are bool?
i = 0; j = 1; k = 2;
printf(“%d %d %d\n”, i, j, k);
printf(“%d %d %d\n”, i++, j++, k++);
printf(“%d %d %d\n”, i, j, k);

A

int 0 1 2 bool 0 1 1
0 1 2 0 1 1
1 2 3 1 1 1

24
Q

Show the output produced by the following program fragment. Assume that i, j, and k are int variables and you
compile the segment using C99. What will be the produced output if i, j, and k are bool?
i = 0; j = 1; k = 2;
printf(“%d %d %d\n”, i, j, k);
printf(“%d %d %d\n”, –i, –j, –k);
printf(“%d %d %d\n”, i, j, k);

A

int 0 1 2
-1 0 1
-1 0 1
Bool –i not allowed for bool.

25
Q

Show the output produced by the following program fragment. Assume that i, j, and k are int variables and you
compile the segment using C99. What will be the produced output if i, j, and k are bool?
i = 0; j = 1; k = 2;
printf(“%d %d %d\n”, i, j, k);
printf(“%d %d %d\n”, ++i, ++j, ++k);
printf(“%d %d %d\n”, i, j, k);

A

int 0 1 2 bool 0 1 1
1 2 3 1 1 1
1 2 3 1 1 1

26
Q

Is the following if statement legal?
if (n >= 1 );
If so, what does it do when n is equal to 5 and when n is equal 15?

A

There is no syntax error but there is a logical error. When n=5 it prints “n is between 1 and 10”. When n=15 it also prints “n is between 1 and 10”. As there is no && between the two evaluations and the second expression doesn’t have the n before the evaluation.

27
Q

What does the following statement print if i has the value of 17? What does it print if i has the value 31?
printf(“%d\n”, ++i >= 0 ? ++i : –i);

A

17 = 19. 31 = 32

28
Q

What does the following statement print if i has the value of 17? What does it print if i has the value 31?
printf(“%d\n”, ++i >= 0 ? ++i : (i-=10));

A

When 17, it prints 19. When 31, it prints 33.

29
Q

The following if statement is unnecessarily complicated. Simplify it as much as possible. (Hint: the entire statement
can be replaced by a single assignment.)
if( age >= 13)
if( age

A

if ( age

30
Q

Convert the following conditional expression to traditional if statement
i = j < k ? l > m ? 1 : 2 : n == p ? 3 : 4;

A

Convert i = j < k ? l > m ? 1 : 2 : n == p ? 3 : 4;

31
Q
What value is assigned to fee by the if statement when speed is 75?
if(speed > 35)
fee = 20.0;
else if(speed > 50)
fee = 40.00;
else if(speed > 75)
fee = 60.00;
A

40

32
Q
What value is assigned to fee by the if statement when speed is 75?
if(speed > 75)
fee = 60.0;
else if(speed > 50)
fee = 40.00;
else if(speed > 35)
fee = 20.00;
A

40

33
Q

Which if statement in the above two questions seems reasonable?

A

I am unsure what this question is looking for

34
Q
What output does the following program fragment produce? (Assume that i is an integer variable)
i = 1;
switch (i % 3)
{
case 0: printf("zero");
case 1: printf("one");
case 2: printf("two");
}
A

onetwo

35
Q
What will be printed by this switch statement if the value of color is 'R'?
switch(color)
{
case 'R':
printf("red\n");
case 'B':
printf("blue\n");
case 'G':
printf("green\n");
}
A

red
Blue
Green ( no break statement)

36
Q
What output line(s) are displayed by the statements that follow when grade is 'I'? When grade is 'B'? and
When grade is 'b'?
switch(grade)
{
case 'A':
points = 4;
break;
case 'B':
points = 3;
break;
case 'C':
points = 2;
break;
case 'D':
points = 1;
break;
case 'E':
case 'I':
case 'W':
points = 0;
}
if(points > 0)
printf("Passed, points earned = %d\n", points);
else
printf("Failed, no points earned\n");
A

“Failed, no points earned”