Home

Test 2 Guide

You will see some questions like the following. The test is on paper, closed-notes. The material covered is keyboard input, mouse input, arrays and loops. Earlier subjects (colors, animation, variables and types, etc.) are not specifically addressed but you should know about them anyway.

Keyboard input

Write code that you would put in place of the ... to make the following if work (i.e., so the message is printed at the right times):

void keyPressed()
{
  if(...)
  {
    println("You pressed the 'w' key!");
  }
}

Write a keyPressed() function that prints the message "Invalid key", using println() as above, whenever a key other than w, a, s, or d is pressed.

Mouse input

Write code for the ... that draws a circle (diameter 10) centered on the mouse pointer (so that the circle follows the mouse):

void draw()
{
  ellipse(...);
}

Finish this code so that the integer variables x and y are both set to 0 when a mouse button is clicked anywhere inside the top-left quarter (quadrant) of the screen:

void mouseClicked()
{
  ...
}

Arrays

Suppose we have an array with 97 values. What is the first array "position" or "index"? What is the last index?

Know how types (int, float, String, etc.) work with arrays. Specifically, suppose we have this code:

int j;
String s;
float[] ww;
String[] t;
float r;
int[] z;

You might see questions like the following. Note that we can answer these questions even though we don't know which values any of these variables contain or how many values are in the arrays.

Suppose I want to create an array of 1000 float values. Write the necessary code to define such an array with the name myvals and set all the values in the array to 12.55.

Write code that creates an array of 2000 integers, called oddeven, and sets the values in "even" array positions (0, 2, ...) to 1 and "odd" array positions (1, 3, ...) to 0. For example, oddeven[18] is 1 and oddeven[19] is 0. (Note that you can determine if a number is even by checking if its remainder, after dividing by 2, is equal to 0.)

Loops

Write a for loop that draws 100 ellipses (I don't care about the dimensions or locations of the ellipses).

What is the value of sum after this loop is finished?

int sum = 0;
for(int q = -5; q < 10; q--)
{
  sum += q;
  q = q + 3;
}
CSE 1211 material by Joshua Eckroth is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Source code for this website available at GitHub.