Looping & Random Numbers Flashcards

1
Q

A structure that allows repeated execution of a block of statements.

A

Loop

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

What must be evaluated before the loop body executes?

A

A Boolean Expression

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

In order for the loop body to execute, a Boolean expression must be evaluated to be what?

A

The Boolean expression must be true.

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

A block of statements. It can be a single statement, or a block of statements between curly braces.

A

Loop Body

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

What are the two categories of loops?

A
  1. Conditional Loop
  2. Count-controlled Loop
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

This category of loops executes as long as a particular condition exists.

A

Conditional Loop

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

This category of loops repeats a specific number of times.

A

Count-controlled Loop

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

What are the three elements of a count-controlled loop?

A
  1. Initialization - Initialize a control variable to a starting value.
  2. Increment/Decrement - Update the control variable during each iteration.
  3. Condition - Test the control variable by comparing it to a maximum value. When the control variable reaches its maximum value, the loop terminates.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the ++ operator called?

A

Increment

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

What is the – called?

A

Decrement

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

What do the increment and decrement operators do?

A

They add and subtract one from their operands.

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

What does it mean to increment a value?

A

To increase the value by one.

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

What does it mean to decrement a value?

A

To decrease the value by one.

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

x = 6;
System.out.print(x++);

What would the output be? What would the new value of x be?

A

Output: 6
New Value of x: 7

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

x = 6;
System.out.print(++x);

What would the output be? What would the new value of x be?

A

Output: 7
New value of x: 7

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

Used to iterate a part of the program repeatedly until the specified Boolean condition is true. As soon as the Boolean condition becomes false, the loop automatically stops.

A

While Loop

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

This loop is also considered as a repeating if statement.

A

While Loop

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

What are the two important parts of a while loop?

A
  1. A boolean expression that is tested for a true or false value
  2. A statement or block of statements that is repeated as long as the expression is true.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

This loop is known as a pretest loop, which means it tests its expression before each iteration.

A

While Loop

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

What would the output be?

int number = 6;
while (number <= 5) {
System.out.println(“Hello”);
number++;
}

A

This program would have no output since the condition is false.

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

This loop does not have a way of stopping, it continues to repeat until the program is interrupted.

A

Infinite Loops

22
Q

int number = 1;
while (number <= 5){
System.out.println(“Hello”);
}

What will happen to the loop in this program?

A

The loop in this program will repeat infinitely and will not be terminated since the condition is always true since the value of the number is always 1

23
Q

What will happen if you put a semicolon after the first condition?

int number = 1;
while (number <= 5);{
System.out.println(“Hello”)
x++
}

A

An infinite loop will be created.

24
Q

This loop is a posttest loop, which means its Boolean expression is tested after each iteration.

A

Do-while Loop

25
Q

This loop looks something like an inverted while loop.

A

Do-while Loop

26
Q

What does the do-while loop look like?

A

An inverted while loop

27
Q

What is the syntax for do-while loops?

A

do {
Statement/s;
} while (BooleanExpression);

28
Q

What is the syntax for while loops?

A

while (BooleanExpression) {
Statement/s;
}

29
Q

“The do-while loop must be terminated with a semicolon.”

Is the statement above TRUE or FALSE?

A

TRUE

30
Q

What is the difference between the while loop and do-while loop?

A

The do-while loop always performs at least one iteration, even if the boolean expression is false to begin with.

31
Q

int num= 6;
while(num<=5) {
System.out.println(“Hello World”);
num++;
}

Will the loop be executed?

A

Loop will not be executed since the condition is False.

32
Q

Explain what happens below.

num= 6;
do {
System.out.println(“Hello World”);
num++;
}while(num<=5);

A

The loop will print Hello world at least once, then the loop will terminate because the condition is false.

33
Q

What are some examples of uses for random numbers in programming tasks?

A
  • Commonly used in games
  • Useful in simulation programs
  • Useful in statistical programs that must randomly select data for analysis
  • Commonly used in computer security to encrypt sensitive data
34
Q

What is the syntax for importing the Random class?

A

import java.util.Random;

35
Q

What is the syntax for creating an object from the random class?

A

Random randomNumbers = new Random();

Where randomNumbers is the name of the object.

36
Q

This Random class method returns the next random number as a double. The number will be within the range of 0.0 through 1.0.

A

nextDouble()

37
Q

This Random class method returns the next random number as a float. The number will be within the range of 0.0 through 1.0

A

nextFloat()

38
Q

This Random class method returns the next random number as an int. The number will be within the range of an int, which is −2,147,483,648 to +2,147,483,648

A

nextInt()

39
Q

This Random class method accepts an integer argument, n. It returns a random number as an int. The number will be within the range of 0 through n.

A

nextInt(int n)

40
Q

This loop is the most used one out of the three loops

A

For Loop

41
Q

This loop is ideal for performing a known number of iterations

A

For Loop

42
Q

In this loop, the three elements of the loop can be found in one line.

A

For Loop

43
Q

What is the syntax for for loops?

A

for (initialization; condition; increment/decrement) {
Statement/s;
}

44
Q

What are the steps of executing a for loop?

A

Step 1. Perform the initialization expression
Step 2. Evaluate the test expression. If it is true, go to Step 3. Otherwise, terminate the loop.
Step 3. Execute the body of the loop.
Step 4. Perform the update expression, then go back to step 2.

45
Q

What happens if you put a semicolon at the end of the for clause before the loop body?

A

The semicolon signifies the end of the loop prematurely.

46
Q

What would the output be?

int x;
for (x=1; x<=5; x++) {
System.out.print(x + “ “);
}

A

Output: 1 2 3 4 5

47
Q

What would the output be?

int x;
for (x=1; x<=5; x++); {
System.out.print(x + “ “);
}

A

Output: 6

The loop is terminated prematurely because of the semicolon at the end of the for clause.

48
Q

A sum of numbers that accumulates with each iteration of a loop.

A

Running Total

49
Q

This is the variable used to keep the running total.

A

Accumulator

50
Q

What type of loop is shown below?

int num, count=0;
for(num=10; num<=15; num++){
if (num % 2== 0)
count++;
}

A

For loop

51
Q

What type of loop is shown below?

int x= 1;
Scanner input = new Scanner(System.in);
while(x<=5) {
System.out.print(“Input a number “ + x + “ “);
num = input.nextInt();
x++;
}

A

While loop

52
Q

What type of loop is shown below?

int num, x=1;
char ch;
Scanner input = new Scanner(System.in);
do {
System.out.print(“Input a number “);
num = input.nextInt();
System.out.print(“Input again? “);
ch = input.next().charAt(0);
} while(ch==’y’ || ch==’Y’);

A

Do-while Loop