Midterm Practice Questions

Short Questions

What are the purpose of code comments?

What are data types?

What is an algorithm?

What is an iteration?

What do each of these operators do?

  • &&
  •  
  • +=
  • ++

When do you use a single equal sign and when do you use a double? (= versus ==)

What are three ways to write “subtract 1 from x” ?

What is the difference between a class and an object?

Review the following code. What is the value of b after running this code?

boolean b = false;
b = !b;

Example exam questions

1 What does the following code print out?

int counter = 1;
int num = 10;
while (counter < num){
  println(counter);
  counter++;
}

2 What does the following code print out?

int counter = 1;
int num = 10;
while (counter < num){
  println(counter);
  counter--;
}

3 Write a loop to print odd numbers from 7 to 77.







4 What is wrong with the following code and how would you fix it?

int c = random(20,100);

5 Given the following function findAverage(), create a new variable myAverage and set it equal to a function call that returns the average of the numbers 5, 85, 125.

float findAverage(float a, float b, float c){
  return (a+b+c)/3
}


6 Write out in pseudocode (aka in simple english) the steps to code the throw of a regular die.

7 What is the difference between a local and global variable?

8 I want to pick a name from a hat. Given the following list of names, write out in code how I could select a random name from the list.

String[] names = {"Harrison","Gabe","Gustavo","Mary","Iz","Anthony","Michael","Fiona","Karoline","Isaiah","Sean","David","Sam","Heavan"};