Test 2 Flashcards Preview

CS 130 > Test 2 > Flashcards

Flashcards in Test 2 Deck (60):
1

T or F? In an && operation, if the first operand is true the result is true.

False

2

T or F? In an && operation, if the first operand is false the result is false.

True

3

T or F? In an || operation, if the first operand is true the result is true.

True

4

T or F? In an && operation, if both operands are true the result is true.

True

5

T or F? In an || operation, if the first operand is false the result is false.

False

6

T or F? In the evaluation of a logical expression, ! has priority over the && and || operations.

True

7

T or F? In the evaluation of a logical expression, parentheses have higher priority than logical operators.

True

8

T or F? In the evaluation of a logical expression, arithmetic operators have higher priority than relational and logical operators.

True

9

T or F? In the evaluation of a relational expression, relational operators have lower priority than logical operators.

False

10

Given the following logical values, expr1 =t true, expr2 = false, expr3 = true, expr4 = false, evaluate the following logical expressions. expr1 && expr2 || expr3

True

11

Given the following logical values, expr1 =t true, expr2 = false, expr3 = true, expr4 = false, evaluate the following logical expressions. expr1 || expr2 && expr3

True

12

Given the following logical values, expr1 =t true, expr2 = false, expr3 = true, expr4 = false, evaluate the following logical expressions. !expr2 && expr1 || expr4

True

13

Given the following logical values, expr1 =t true, expr2 = false, expr3 = true, expr4 = false, evaluate the following logical expressions. !expr2 && (expr1 || expr3)

True

14

Given the following logical values, expr1 =t true, expr2 = false, expr3 = true, expr4 = false, evaluate the following logical expressions. !(expr1 && expr3) || expr1

True

15

Given the following values for the variables, a =10, b = 5, c = 12, and d = 8, evaluate the following relational and logical expressions. a > b || b < c &amp;&amp; d > b

True

16

Given the following values for the variables, a =10, b = 5, c = 12, and d = 8, evaluate the following relational and logical expressions. !(a < b &amp;&amp; c >d) || c < d

True

17

Given the following values for the variables, a =10, b = 5, c = 12, and d = 8, evaluate the following relational and logical expressions. a <= b || !b < c &amp;&amp; d > a

False

18

Given the following values for the variables, a =10, b = 5, c = 12, and d = 8, evaluate the following relational and logical expressions. a > c &amp;&amp; !d < c || (a > b &amp;&amp; b

True

19

Given the following values for the variables, a = 10, b = 15, c = 10, and d = 25, evaluate the following relational expressions. a >= c

True

20

Given the following values for the variables, a = 10, b = 15, c = 10, and d = 25, evaluate the following relational expressions. b < d

True

21

Given the following values for the variables, a = 10, b = 15, c = 10, and d = 25, evaluate the following relational expressions. a * b / 2 >= c + d / 5 + 2

True

22

Given the following values for the variables, a = 10, b = 15, c = 10, and d = 25, evaluate the following relational expressions. a - b <= c + d - 4

True

23

Write code for the following selection structure flowchart.

Q image thumb

if(c1)

{

     if(c2)

          stmt3;

     else

          stmt2;

}

else

       stmt1;

24

T/F? A relational expression assigns values to the operands, which make it true.

False

25

T/F? When a relational operator has two characters such as >= or <= or != there should not be any spaces between them.

True

26

T/F? In a relational expression with arithmetic operations, arithmetic operations are performed before the relaitonal operations.

True

27

T/F? A relational expression may compare operands of any data type.

False

28

T/F? Relational expressions are used to make logical choices in branching control structures. 

True

29

T/F? Relational expressions evaluate to 0 or 1.

True

30

T/F? Relational expressions are also called conditions.

True

31

var1(false) && var2(false)

False

32

var1(false) && var2(true)

False

33

var1(true) && var2(false)

False

34

var1(true) && var2(true)

True

35

var1(false) || var2(false)

False

36

var1(false) || var2(true)

True

37

var1(true) || var2(false)

True

38

var1(true) || var2(true)

True

39

Write the flowcharts and the code for the selection structure for the statement:

If x is greater than 25, add 20 to y.

if(x > 25)

    y += 20;

 

40

Write the flowcharts and the code for the selection structure for the statement:

If z is less than 15, add x to y.

if(z < 15)

    y += x;

41

Write the flowcharts and the code for the selection structure for the statement:

If n is 40, input values to l, m, and r.

if(n == 40)

   scanf(" %d %d %d %d", &l, &m, &r)

42

Write the flowcharts and the code for the selection structure for the statement:

If p is greater than or equal to r, increment r by 1.

if(p >= r)

   r++;

43

Write the segment of C code for the following:

If the compression stress is greater than 50,000, execute the following code.

printf("stress =  %8d\n", stress);

printf("This stress si greater 50000\n");

if(stress > 50000)

{

printf("stress = %8d\n", stress);

}

44

Write the segment of C code for the following:

If the number of cars sold per month by XYZ Motor Company is greater than 10,000, add a 5% bonus to employees' monthly paychecks.

if(sales > 10000)

    pay = pay + 0.05 * pay;

45

Write the segment of C code for the following:

If the monthly sales of an employee exceeds $50,000, award a 10% bonus, if it exceeds $100,000 award a 15% bonus, if it exceeds $150,000, award a 20% bonus. If it is less than $10,000 there will be no bonus. 

if(sales > 150000)

    pay = pay + 0.20 * pay;

else

{

    if(sales > 100000)

         pay = pay + .15 * pay;

    else

    {

        if (sales > 50000)

            pay = pay + .10 * pay;

    }

}

 

46

T/F?

In a for loop the initialization, condition, and the increment are all in the header.

True

47

T/F?

A for loop can count by 10.

True

48

T/F?

In a for loop the index can be incremented or decremented.

True

49

T/F?

for loops can be nested one inside the other.

True

50

T/F?

When two for loops are nested, the loop variables myst be distinct.

True

51

T/F?

In a for loop stement, more than one variable can be initialized, by using a comma (,) operator.

True

52

What is the error?

for(i = 0; i < 10; i++);

should not be a semicolon at the end

53

What is the error?

for(i = 10; i < 0; i++)

loop will not execute as termination condition is false

54

What is the error?

for(i = 0; i < 10; i--)

loop will not terminate as termination condition is larger than initial value and index i is being decremented

55

What is the error?

for(i = 0; i < 10, i++)

"," where there should be a ","

56

What is the error?

for(i = 0; i < 10; i++)

    for(i = 0; i < 10; i++)

nested inner and outer loops use the same index variable

57

What is the error?

for(i = 0; i < 10; i++)

    for(j = 0;  j < 8; j++)

        for(i = 0; i < 5; i ++)

nested inner and outer loops use the same index variable

58

What is printed in the following simple for loops?

for(i = 0; i < 4; i++)

    printf("Inside for loop i = %d\n", i);

printf("Outside for loop i = %d\n", i);

Inside for loop i = 0

Inside for loop i = 1

Inside for loop i = 2

Inside for loop i = 3

Outside for loop i = 4

59

What is printed in the following simple for loops?

for(i = 5; i > 0; i-)

    printf("i = %d\n", i);

printf("Outside for loop i = %d\n", i);

i = 5

i = 4

i = 3

i = 2

i = 1

Outside for loop i = 0

60

What is printed in the following simple for loops?

for(i = 0, sum = 0; i < 4; i++)

{

    sum += i;

    printf("Inside loop i = %d\n", i);

}

printf("Outside loop i = %d\n", i);

printf("Sum = %d\n", sum);

 

Inside loop i = 0

Inside loop i = 1

Inside loop i = 2

Inside loop i = 3

Outside loop i = 4

Sum = 6