CSC121 TEST 1

February 28, 2007

 

NAME:__________________________________________________________

 

Multiple Choice Section (4 points each, 60 total points). Each question has only one correct answer. Please circle the letter corresponding to the correct answer.

1. What is the data type of the result of the following three expressions: (3 + 5) / 7; (3 + 5) / (float) 7; (float) ((3 + 5) / 7);
    A. int, float, double
    B. int, float, float
    C. int, double, double
    D. int, double, float
    E. None of these

2. Which of the following statement is true of a variable?
    A. It is a name for a memory location
    B. It sometimes have a data type
    C. its value can’t be changed 
    D. All of the above name

3. What is the output for the code:
    int x=2, y=3;
    if(x*y*x*y%30 = = 5)
        System.out.println("x + y = " + "(x+y)");
    else
       System.out.println("x + y ="  + (x+y));
    A. x + y = (x+y)
    B. x + y = 23
    C. 2 + 3 = (2+3)
    D. x + y = 5

4. The value of the expression 5 % 10 <= 5 is
    A. 3     B. 3.5     C. 4     D. 5    E. true   F. false

5. The value of the expression 10 – 2 * 4 - 2 is
    A. 0     B. 4     C. 6     D. 8   E. 10   F. 16 G. 30

6. The value of the expression 10 – 2 % 4 - 2 is
    A. 0     B. 4     C. 6     D. 8   E. 10   F. 16 G. 30

7. The value of the expression 10 – 2 / 4 - 2 is
    A. 0     B. 4     C. 6     D. 8   E. 10   F. 16 G. 30

8. The value of the expression 5 / 10 * 2 is
    A. 0     B. .25     C. 1     D.     2     E. None of these

9. The value of the expression (double) 5 / 10 * 2 is
    A. 0.0     B. .25     C. 1.0     D. 2.0     E. None of these

10. Which of these is the reserved word used to create (instantiate) objects in Java?
    A. static     B. void     C. object     D. new     E. class

11. A missing semicolon is an example of a
    A. logic error     B. run-time error     C. syntax error     D. A, B, and C

12. How many times will the string “YES” be printed?

for (int count=1; count <= 10; count++)

{

      for (int count2=1; count2 <= 5; count2++)

   {

      System.out.println ("YES");

   }

}

A. 1.0     B. 2     C. 36     D. 50     E. None of these

13. Which of the following is not a relational expression?

    A. num > 5
    B. p == true && q == false
    C. num + 5 <= 20
    D. All of the above

 

14. In which order will the six operators in the following expression be evaluated?

     A. 1  2  3  4  5  6

     B. 2  4  5  1  3  6

     C. 4  5  2  1  3  6

     D. 4  2  5  1  3  6

     E. None of the above

 

15. Given the declarations/assignments int num = 4, val = 2, count = 5, sum = 10; what value is printed for this if statement?

     if(count < val || sum < num)

            System.out.println(sum);

    else

            System.out.println(count);

    A. true
    B. false
    C. 10
    D. 5

 


Short Answer Section (5 points each, 40 total points).

16. Write an if-statement to print the value of sum if balance is greater than or equal to 100.

 

 

 

 

17. Write an if-statement to add MAX to sum and print the value of sum if num + 5 is less than the difference between val and num.

 

 

 

 

 

 

 

 

 

 

18. Write a while loop to add all the integers between 77 and 275 that are divisible by both 9 and 11.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


19. Write a for loop to add all the integers between 77 and 275 that are divisible by both 9 and 11.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

20. Transform the following while loop into an equivalent for (that produces the same output).

int num = 1;

while(num < 20)

{

      num++;

      System.out.println(num);

}

 


21. What is wrong with the following code fragment? Rewrite it so that it produces correct output.

if (total == MAX)

     if(total < sum)

            System.out.println(“total == MAX and < sum”);

else

     System.out.println(“total is not equal to MAX”);

 

 

 

 

 

 

 

 

 

 

 

22. What output is produced by the following code fragment?

int num = 87, max = 25;

if (num <= max*2)

      System.out.println(“apple”);

      System.out.println(“orange”);

System.out.println(“pear”);

 

 

 

 

 

 

 

 

 

 

 

 

 

23. What output is produced by the following code fragment?

int num = 0, max = 20;

while(num < max)

{

      System.out.println(num);

      num += 4;

}