CSC 211 Midterm Review

Note, The following is to give you an idea as to the type of questions you will be asked. It does not represent the actual exam nor is it intended to be complete. You need to know the material in the above mentioned sections, lecture notes, labs and sample code worked out in class.
I will not provide answers to these. I will set up a forum at the discussion board for you to discuss these. Most of these can be answered by making up small programs to test the statements. You should work these out first with pencil and paper, then turn to the computer. Please use the discussion board. Since you will not be handing these in you can post solutions.

  1. Which of the following are valid Identifiers ? For the ones that are not, explain a) coolHandLuke b) cool_hand_luke c) cool hand luke d) 157RivesideAvenue e) int f) intValue h) _myScore

  2. True or False, if false explain.

    Given x = 7, y = 3, z = 2

    x + y * z

    is the same as

    (x + y ) * z

  3. For each of the following, what is the value of z ?
    Given a = 2, b = 3, d = 6 a) z = d/2 - d/3 b) z = d % c - a c) z = c % b * a;

  4. Which of the following statements are incorrect? Explain !
    Given:

    int b;
    double c;
    final int x = 12;

    1) b = Math.sqrt(16); 2) c = 1000; 3) x += 12;

  5. The following class definition has EXACTLY four errors, find them public CLASS MATH { public square ( base ) { return base * base ; }

  6. An instance method named greetings will print a greeting to the console, hence does not return a value. Is the following an acceptable definition for that method ? public greetings() { System.out.println("Welcome"); }

  7. Write a simple if statement that prints the word true to the console if an integer variable x is not equal to 10.

  8. Correct the error for each of the following. a) if( y > 10) z = 3; y = 9; else w = 2; ============================================== b) if ( z = 7 ) y = 7; else y = 8; ============================================== c) if ( x == 0 ) then z+= 12; =============================================== d) if ( x < 0 ) status = "negative"; else if ( x > 0 ) status = "positive"; else ( x == 0 ) status = "zero"; ================================================= e) int x = 0 while ( x <= 4 ) { y = x; --x; } ================================================== f) int x = 4; while ( x != 14 );; x += 1; ================================================== g) int x = 15; while ( x > 12 ) System.out.println( x ); --x; ==================================================

  9. For each one, what is output to the console ? a) int x = 17; if( x >= 7 ) System.out.println("x is greater than 7 "); else System.out.println("x is " + x ); System.out.println("x is less that 7 "); ========================================================= b) int x = 0; while ( x <= 4 ) { System.out.println("x is " + x ); x += 2; } =========================================================== c) for (int i = 0; i < 5; i++) System.out.println("i is " + i ); System.out.println("Looping Again ?"); ============================================================ d) for(int j = 0; j <= 10; j += 2) System.out.println("j is " + j);

  10. Explain the difference between a while statement and a do-while

  11. Write a complete Java program that uses a pop up window to get an integer from the user. If the integer is bigger than 1000 print out to the console "big" else print "small"

    Before you answer 11 - 14, review lab 5 question 7.

  12. Write a method called findMin that accepts two integer parameters and returns the smaller of the two.

  13. Write a method called average that accepts four integer parameters and returns their average as a floating point value.

  14. Write a method called computeTax that accepts one floating point parameter that represents your income and returns the tax you owe based on this simple schedule. 0 to 100000 5% 100000 to 200000 10% over 200000 15% less than 0 0%

  15. Write a method called isEven that accepts one integer and returns true if the integer is even false otherwise.

  16. Use the class definition to answer the following quastions. public class Rectangle { private int length; private int width; public Rectangle(int l, int w){ length = l; width = w; } public int computeArea(){ return length * width; } public int getLength(){ return length; } public int getWidth(){ return width; } }

  17. Design a class named Book with the following specification Instance variables: - title - price Constructor: - one to create a book object with title and price; Methods: - Accessor methods, one for each instance variable - A method to change the price.

  18. Create an object of type Book and assign it to a reference variable called myBook. Give any title and price.

  19. Lab 4 questions 1 - 5

  20. Lab 5 questions 1 - 7.