CECS 174                          EXAMINATION #2  SAMPLE

Multiple choice

1.. Which of the following is a relational operator?
a. =
b. ==
c. !
d. &&
 
Answer:   b

3. Which of the following is not a logical operator?
a. !
b. ||
c. !=
d. &&

Answer:  d

4. Suppose x is 5 and y is 7. What is the value of the following expression?

(x != 7) && (x <= y)

a. false
b. true
c. This is an invalid expression in C++
d. None of these
 
Answer:  b

5. Suppose that x is an int variable. Which of the following expression always evaluates to true?
a. (x > 0) || ( x <= 0)
b. (x > 0) || (x == 0)
c. (x > 0) && ( x <= 0)
d. (x > 0) && (x == 0)
 
Answer:  a

6. What is the output of the following C++ code?
x = 0;
if(x < 0)
System.out.prinntln("One ");
System.out.prinntln("Two ");
System.out.prinntln("Three");

a. Two
b. Two Three
c. One Two Three
d. None of these
 
Answer: b

7. After the execution of the following code, what is the value of sum?

sum = 0;
num = 10;
if(num > 0)
sum = sum + 10;
else
if(num > 5)
sum = num + 15;

a. 0
b. 10
c. 20
d. 25

Answer: b
 

8. What is the output of the following code fragment?

x = 10;
if(x > 15)
x = 0;
System.out.println(x);
else
System.out.println(x+5);

a. 0
b. 5
c. 10
d. None of these

Answer: d

Part II 

1. Circle 0 if the expression evaluates to 0 (false), or 1 (true) if the expression evaluates to 1 (true).
    Answers in bold

    int count = 0, limit = 10;

    0    1    1.  ! (count = = 12)                          

    0    1    2.  ( 5 && 7) + (!6)                         

    0    1    3.    !( ((count < 10 ) | | (x < y)) &&  (count > = 0) )      

    0    1    4.    (limit < 20)  | | (limit/count) > 7)                                      

    0    1    5.   !count

2. Circle 0 if the expression evaluates to 0 (false), or 1 (true) if the expression evaluates to 1 (true).
    [ 1/2 point each; 5 points total] Answers in bold and underline

    int x = 2, y = 3;

    0    1    1.  x = = y                                         0     1    6.   x >= y

    0    1    2.  x  &&   (y < 10)                           0     1    7.   !x  || (y < 10)

       1    3.  !5                                                 0     1    8.   x < 10  &&  y > -10     

    0    1    4.  x  !=  y                                         0     1    9.   x  - 2

    0       5.  x < 0   &&  y < 9  ||  x > 0             0    1     10.  !(!5)

 

3. Assuming X is 7, and Y is 12, indicate by circling the T or F if each of the following relational expressions is true or false:

    a.     X = = 5              T     F

    b.    7 <= (X + 2)      T     F

    c.    2 * X < Y            T   F

    d.    Y  + 3  ! = X       T    F

4. For each expression, find values for x and y that makes it true.

    a.     x > 10  ||  y ! = 5

   Answer:  x is 20 and y is 6

    b.     x > 10 && y < x + 4

    Answer:  x  is 11 and y is 0

5. Rewrite the following part of the program that it uses only one if statement. If it is not possible to rewrite it using only one if, explain why. [ 2 points]

    if ( x > 12)                                                                                         Answer
        if ( x < 100) 
           System.out.println("Number is in range");                                     if ( x > 12  &&  x < 100 )
                                                                                                                   System.out.println("Number is in range");     

6. Write  Java statements that will 

    a.    display  x>y if x is greater than y and display y>=x otherwise.

   Answer :    if (x > y)
                        cout<<"x>y"<<endl;
                    else
                        cout<<"y>=x"<<endl;

7.  Write a Java expression that determines x is greater than10 and less than 20?

Answer 10 < x && x < 20

8. Write a Java expression that determines num is less than 0 or num is greater than 50

Answer num < 10 || num > 50

 

9. What value will the variable x has after executing  

    x =6;
    if ( k < 10)
        if (k < 5)
          x =7;
   else
    x = 8;

    if k has the value

    a.  9

    Answer:   8

    b.  -2

    Answer:    7

10. Consider the following section of a program:

    if ( x > y )
        if (z > 100)
             x = y;
       else 
             y  =   z;
    else
        if ( z <= 100)
             y = x;
       else
             z = y;
    System.out.println(" x + " " + y + " " + z);

What is displayed when x  = 10, y  =  20, and z  = 75?

Answer:        10  10   75

What is displayed when x  = 20, y = 10, and z = 75?

Answer        20  75   75

11. Which of the following  correctly lists the sequence of statements that Java executes, given the code fragment: [ 2 points]

  1. int x =1, y =4;
  2. if ( x >=2)
  3. x = 7;
  4. y = 9;
  5. x = 8;

a.  1,2,3,4,5    b.  1,2,5   c.  1,2,4,5    d.  1,2,3,5  e. none of these above

Answer (Select a, b,c,d or e)
                       c

12. Given the following code snippet:

     int myInt = 3;
     if (myInt < 5)
          if (myInt < 3)
               System.out.println(“< 3”);
     else
          if (myInt > 2)
               System.out.println(“>2”);
     else
          System.out.println(“Other”);

     What will appear in the standard output? [ 3 points ]
    
     Answer:     >2

13. [6 points] Write a Java statement which displays the following pattern on the screen.

******
******
******
******

Your solution must meet the following requirements:
  - Can not use more than one statement   System.out.println()
  - Can not use more than one statement   System.out.print("*")
  - Can not use more than two for loops

Answer

 int a=4,b=6;
for(int i=0;i<a;i++)
{ for(int j=0;j < b;j++)
System.out.print("*");
System.out.println();

14. Write a program that is using loop statements to display an output below. [5 points]

1
12
123
1234
12345
123456
1234567
12345678
123456789
12345678910
1234567891011
123456789101112
12345678910111213
1234567891011121314
123456789101112131415
12345678910111213141516
1234567891011121314151617
123456789101112131415161718
12345678910111213141516171819
1234567891011121314151617181920

Answer:

public class Test
{ public static void main (String [] agrs )
    { for (int i =1; i <= 20; i ++)
       { for (int j = 1; j <= i; j++)
            System.out.print(j);
           System.out.println("");;
       }
    }
}