Final exams pt 2 Flashcards
(18 cards)
Give the 4 assignment operators
- addition and assign: +=
- subtraction and assign: -=
- multiply and assign: *=
- divide and assign: /=
List the 5 basic math operators
- addition: +
- subtraction: -
- multiplication: *
- division: /
- remainder division: %
Write a variable to store the result of the following expressions:
2x + 3y − z
int result = 2 * x + 3 * y - z;
Write a variable to store the result of the following expressions:
(2×3) /4
float result = (2 * 3) / 4;
Write a variable to store the result of the following expressions:
The remainder of 4/3
int remainder = 4 % 3;
Write a line of code that will add 7 to a variable and store the result in the same variable.
1) int num = num + 7
2) int num += 7;
. What is the shortest line of code to increment the value of a variable by 1?
var++; or ++var;
What is the shortest line of code to decrement the value of a variable by 1?
var–; or –var;
What units are the screen measured in?
Pixels
Give 3 examples of a graphics command. Write a command to show the syntax for each.
1) Rectangle: rect (x, y, w, h);
2) ellipse: ellipse (x, y, w, h);
3) triangle: triangle (x1, y1, x2, y2, x3, y3);
4) line (x1, y1, x2, y2);
Describe the function of a for loop in 1 sentence
A for loop will repeat a section of code for a specified number of times.
What 3 things must be defined in a for loop?
*The counter starting value
* The ending value, specified as a condition which is true as long as the loop needs to
continue
* The increment/decrement value for the counter variable
Write code for a for loop that will:
loop 10 times, starting at 0, increasing by 1
for (int i = 0; i < 10; i++)
Write code for a for loop that will:
loop 20 times, counting by 2s
for(int i = 1; i < 20; i+=2);
count backwards from 100 down to 1 by 3s
for (int i = 100; i > 1; i -= 3)
Write code for a for loop that will
sum up all the numbers from 1 to 100
int sum = 0;
for (int i = 1; i <= 100; i++)
sum = sum + i;
Write code for a for loop that will
find the product of every odd number from -51 to 51
int product = 1;
for (int i = -51; i <= 51; i += 2)
product *= i;
Write code for a for loop that will
subtract all the even numbers from 60 to 30 from 8192
int sum = 8192
for (int i = 60; i >= 30; i -= 2)
sum = sum - i;