DOC

Java Final Review

By Christine Morales,2014-04-24 13:37
14 views 0
Java Final Review

CP1 Final Review Name_________KEY____________________

    12172008

    1. To do well on the final, you need to understand its vocabulary. Give a short definition of each of the

    following words.

    a. return value the value that a function array int [] nums={5,-1,7};

    returns nums[-1] and nums[3] would both

    b. declaration creating a variable (“mailbox”), create out-of-bounds index exceptions like int num; j. (square) brackets []

    c. initialization setting a variable equal to it's k. element (of an array) One of the values in an first value, like num = 4; array, for example, 5 is an element of nums

    d. modulus finds the remainder of integer l. Relational operator > >= < <= == !=

    division m. Logical operator && || ! e. Assignment operator = f. data type the type of variable, like int, n. void means “nothing” as in boolean, float, etc. the function returns nothing g. block of code the code between an open and o. Literal not a variable: “Hello”, 'x', 5 closing curly brace and 17.2 are all literals. h. (curly) braces {} p. class a plan or blueprint used to create i. out-of-bounds index an index that is too big objectsor small for an array. For example, for the

    2. What ends every Java statement? A semi-colon

3. Evaluate the following expressions.

    a. (5 > 3) || (5 != 5) && ( 2 <= 2) true

    b. (5 > 3) && (5 != 5) false

    c. (5 > 3) && (5 != 5) || ( 2 <= 2) true

    d. (5 > 3) || ( 2 <= 2) true

    e. ((5 > 3) || (5 != 5)) && ( 2 <= 2) true

    f. !(5 > 3) false

4. Given the following if/else:

    if(nA == nB)

    {

     nX = 10;

    }

    else if(nC == nD)

    {

     nX = 55;

    }

    else

    {

     nX = 100;

    }

    what is the value of nX under the following conditions?

    a. nA = 5, nB = 1, nC = 100, nD = 50 100

    b. nA = 5, nB = 5, nC = 0, nD = 0 10

    c. nA = 0, nB = 0, nC = 5, nD = 0 10

    d. nA = 0, nB = 5, nC = 5, nD = 5 55

    e. nA = 0, nB = 100, nC = 5, nD = 5 55

5. Given the following logical expression:

    (nA == nB) && (nA != nC)

    evaluate the expression under the following conditions:

    a. nA = 0, nB = 0, nC = 5 true

    b. nA = 1, nB = 50, nC = 100 false

    c. nA = 0, nB = 5, nC = 5 false 6. Complete the following program that produces an animation of a ball bouncing around the screen. The

    ball should be initialized to start at the center of the screen moving down and to the left.

     __Ball___ bob; void setup() { bob = new Ball(); size(300,300); } void draw() { background(0,0,0); bob.move(); bob.bounce(); bob.show(); } class Ball { int x,y; boolean up,right; Ball() { __x = 150; _____; //NOT int x = 150;! __y = 150; ______; ___ up = false; ____; ____right = false; ________; } void move() { if(__right == true ____) //NOTE DOUBLE EQUALS! { x++; } else { x--; } if(_____up == true_______) { y--; } else { y++; } } void bounce() { if( ___ x >= 300____ ) { ___right = false _____; //NOTE SINGLE EQUALS! } if(___x <= 0____ ) { _____right = true _____; } if(____ y >= 300___________ ) { ____ up = true _______; } if(_____y <= 0_____ )

     { ____up = false _____; } } void show() { ellipse(x,y,20,20); } }

    7. Complete the following code fragment so that it displays the sum of all the multiples of 3 from 1 to num.

    (Assume that num has already been declared and initialized)

     int sum=0;

     for (int i = 1; i <= num; i++)

     {

     //if i is a multiple of 3

     if (__ i % 3 == 0 __)

     {

     sum = sum + i;

     }

     }

     println(sum);

    8. What is the output of the following code? What is the output of the following code? void setup()

    {

     for(int i = 1; i <= 3; i++)

     {

     mystery();

     }

    }

    void mystery()

    {

     for(char letter = 'a'; letter <= 'c'; letter++)

     {

     print(letter+", ");

     }

     println(); //ends the line

    }

a, b, c,

    a, b, c,

    a, b, c,

9. What is the output of the following code? void setup()

    {

     for(int i = 1; i <= 3; i++)

     {

     mystery(i);

     }

    }

    void mystery(int num)

    {

     for(char letter = 'a'; letter <= 'c'; letter++)

     {

     println(letter+", "+num);

     }

    }

    a, 1

    b, 1

    c, 1

    a, 2

    b, 2

    c, 2

    a, 3

    b, 3

    c, 3

    10. What is the output of the following code? void setup()

    {

     for(int i = 1; i <= 3; i++)

     {

     for(char letter = 'a'; letter <= 'c'; letter++)

     {

     println(letter + ", " + i);

     }

     }

    }

    a, 1

    b, 1

    c, 1

    a, 2

    b, 2

    c, 2

    a, 3

    b, 3

    c, 3

    11. Explain what “Java is case sensitive” means. Java thinks lowercase and uppercase are different, so void Setup() is not the same as void setup()

12. Finish the following program so that fahrenheit is correctly converted to celsius:

    float fahrenheit = 212;

    float celsius = (__fahrenheit__ - _ 32__ )* float(_5)/_9;

13. What is the output of the following program? int num1 = 7;

    int num2 = 14;

    println(num1/num2); 0

    println(float(num1)/num2); 0.5 println(float(num1/num2)); 0

14. What style or type of programming uses a class to model what an object has and does?

    OOP or Object Oriented Programming

    15. Every processing program that uses functions is suppose to have both setup() and draw()

    methods, but only one of them is really required. Which one is it? setup()

    16. Find the output: int num = 10; while(num > 0) {

     num = num /3;

     println(num); }

    3

    1

    0

    17. Find the output: int num = 10; while(num > 0) {

     num = num /3; }

    println(num); 0

Report this document

For any questions or suggestions please email
cust-service@docsford.com