Final exams pt 2 Flashcards

(18 cards)

1
Q

Give the 4 assignment operators

A
  • addition and assign: +=
  • subtraction and assign: -=
  • multiply and assign: *=
  • divide and assign: /=
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
1
Q

List the 5 basic math operators

A
  • addition: +
  • subtraction: -
  • multiplication: *
  • division: /
  • remainder division: %
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Write a variable to store the result of the following expressions:
2x + 3y − z

A

int result = 2 * x + 3 * y - z;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Write a variable to store the result of the following expressions:
(2×3) /4

A

float result = (2 * 3) / 4;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Write a variable to store the result of the following expressions:
The remainder of 4/3

A

int remainder = 4 % 3;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Write a line of code that will add 7 to a variable and store the result in the same variable.

A

1) int num = num + 7
2) int num += 7;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

. What is the shortest line of code to increment the value of a variable by 1?

A

var++; or ++var;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the shortest line of code to decrement the value of a variable by 1?

A

var–; or –var;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What units are the screen measured in?

A

Pixels

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Give 3 examples of a graphics command. Write a command to show the syntax for each.

A

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);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Describe the function of a for loop in 1 sentence

A

A for loop will repeat a section of code for a specified number of times.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What 3 things must be defined in a for loop?

A

*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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Write code for a for loop that will:
loop 10 times, starting at 0, increasing by 1

A

for (int i = 0; i < 10; i++)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Write code for a for loop that will:
loop 20 times, counting by 2s

A

for(int i = 1; i < 20; i+=2);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

count backwards from 100 down to 1 by 3s

A

for (int i = 100; i > 1; i -= 3)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Write code for a for loop that will
sum up all the numbers from 1 to 100

A

int sum = 0;
for (int i = 1; i <= 100; i++)
sum = sum + i;

16
Q

Write code for a for loop that will
find the product of every odd number from -51 to 51

A

int product = 1;
for (int i = -51; i <= 51; i += 2)
product *= i;

17
Q

Write code for a for loop that will
subtract all the even numbers from 60 to 30 from 8192

A

int sum = 8192
for (int i = 60; i >= 30; i -= 2)
sum = sum - i;