Computer Science PAP : Chapter 5 Exam

Ace your homework & exams now with Quizwiz!

What is the benefit of using loop control structures with graphics?

A large number of graphics features can easily be displayed for some interesting output effects.

Most conditional statements will require what?

A logical or Boolean operator

What kind of operator is required to make an expression evaluate to true or false?

A logical or Boolean operator

Give a real life situation where fixed iteration is not practical.

A loop that repeats until a password is entered

What is a conditional statement?

A statement that is true or false

Explain Multiple-Way Selection.

A variable value is compared against multiple values. When a match occurs, the statement with the match is executed.

Where must the loop counter be initialized when using a while loop?

Before the loop condition is used

What would you need to change in program Java0507.java if Joe wanted the message displayed 1000 times?

Change the value in the loop condition

While program Java0505.java compiles and executes, its output is strange. How does program Java0506.java fix the problem?

It uses the break keyword.

Repetition is also called . . .

Iteration and looping

What are 2 synonyms for Repetition?

Iteration and looping

What is known about equivalent Boolean expressions?

The truth tables are identical

What do Selection and Repetition control structures have in common?

They both use conditional statements.

Consider the following program segment. if (education >= 16 || experience >= 5) System.out.println("You are hired") else System.out.println("You are not hired"); *A compound statement with two conditions has four possible combinations.* In how many of these combinations is somebody hired?

Three

The while loop structure requires a statement inside the loop body that was not necessary with the for loop. What is the purpose of this statement?

To give an opportunity for the loop condition variable to change.

True or False: If your structure does not use default, then you do not need a break after the last case.

True

True or False: In a switch structure, multiple cases can yield the same outcome.

True

How do you control multiple program statements with one-way or two-way selection?

Use braces

How do you repeat multiple statements with the for loop structure?

Use braces

What is the essence of understanding, and using, control structures?

Use the correct control structure for the correct purpose.

When is the default statement executed?

When no match is found.

What symbol does Java use for a logical "not" ?

!

What symbol does Java use for a logical "xor" ?

!=

What symbol does Java use for a logical "and" ?

&&

List the 6 relational operators.

Equals, not equals, greater than, less than, greater than or equal, and less than or equal. == > < >= <= !=

True or False: Break must always be used with default.

False

True or False: One problem with the switch command is that each case can only control a single program statement.

False

Which data type only has two possible values?

boolean

Which commands can be used with "switch"?

case, break, default

What statement is used with if for two-way selection?

else

A for loop needs a ________________. The technical name for this is __________________________________________________.

- counter - Loop Control Variable ( LCV )

What are the types of loop structures?

- fixed repetition ( for ) - conditional repetition ( while ) - do . . . while

What are the types of selection structures?

- one way selection ( if ) - two way selection ( else ) - multiple way selection ( switch )

Java does not use "=" to test for equality? What does it use instead?

==

In a switch structure, what symbol separates the selection constant from the program statement that needs to be performed if a match is found.

A colon ( : )

What kind of operator is "="?

An assignment operator

What are 2 synonyms for Selection?

Conditional branching or decision making

for(j = 1; j <= 100; j++) : What does the j <= 100 mean?

Continue to execute the loop as long as this statement is true.

A for loop is not well suited for something like entering a password. Both the while loop and the do...while loop would work in this case. If you were writing a password program, which loop structure would you chose? Explain the reasoning behind your answer.

I would chose the do...while loop since it is guaranteed to always execute at least once. Even if someone knows the password and will get it correct on his/her first try, he/she still needs to enter it that one time.

In one-way selection, when does the program flow branch off?

If the condition is true

for(j = 1; j <= 100; j++) : What does the j++ mean?

Increment the loop control variable by 1 for each iteration.

The switch structure was not used in program Java0519.java because of what 2 limitations?

It cannot handle ranges, and cannot be used with real numbers.

for(j = 1; j <= 100; j++) : What does the j = 1 mean?

It initializes the loop control variable

What kind of loop is the while loop?

It is a pre-condition loop

Programs in what computer language require control structures?

Java and all other program languages

Explain how Multi-way Selection is accomplished in Java.

Multi-way selection is accomplished with the switch command which uses a selection variable with a value. The selection variable value is compared with a group of designated case values. When a match is found, the statements following the case value are executed.

Is a compound condition limited to 2 conditions?

No

Must the loop counter in a for statement be an int?

No

Does Multi-way Selection use a conditional statement?

Not a conditional statement with a relational operator. It does use a switch statement

Assume the condition for a do...while loop is already false, even before the loop begins. Furthermore, assume nothing in the loop will ever make the condition change to true. How many times will the loop execute?

Once

What are the 3 types of Selection?

One-way, two-way and multiple-way

What is the only thing checked in a while loop?

Only the loop condition is checked.

All conditions must be placed inside what?

Parentheses

How do you control multiple program statements with an "if", "for", or "while" control structure?

Put the statements inside braces "{}"

Explain Repetition.

Repetition repeats the same statement or statements as long as some condition is valid.

What are the 3 general types of control structures?

Simple Selection Repetition

Which control structure is used in the most with graphics programs in this chapter?

The for loop

Which loop structure would be good if you wanted to display 50 rectangles on the screen?

The for loop

Why is the do...while loop called a post-condition loop?

The loop condition comes after the loop body.

What must happen inside a while loop?

The loop counter must change

What does the nextInt method do?

The nextInt method "reads" in an int value from the keyboard.

In fixed iteration, what does fixed mean?

The number of times that the body of the loop structure executed.

In two-way selection, what happens if the condition is false?

The program executes the else statement.

In one-way selection, what happens if the condition is false?

The program flow continues without change in program sequence.

What does program flow follow?

The sequence of written program statements

How do you control multiple statements with switch?

With switch, controlling multiple lines is done in the same way as controlling a single line because you can put as many programming statements as you wish between the case and break statements.

Why is a prompt necessary for program input?

Without the prompt the program user has no clue what is happening and certainly does not know what type of input is required.

With switch, can multiple cases yield the same result?

Yes

What is the main concern in using graphics commands with control structures?

You must know which coordinate values stay fixed and which one change.

Assume the condition for a while loop is already false, even before the loop begins. How many times will the loop execute?

Zero

In an "and" statement, if one is false then . . .

all of it is false

In an "or" statment, if one is true then . . .

all of it is true

Selection control structures use a special _____________ statement.

condition

The two-way selection control structure uses the _____________ keyword.

else

What should you do when a computer science program becomes too complex?

focus on smaller, less complex parts

The fixed repetition control structure uses the _________ keyword

for

Which repetition control structures are covered on the AP® Computer Science Exam?

for and while

Rewrite this program segment as a single program statement: if (gpa >= 90) honorStudent = true; else honorStudent = true;

honorStudent = gpa >= 90;

The one-way selection control structure uses the __________ keyword.

if

Write the Java statement that will do this action: If your grade is 70 or better, print "You passed!" otherwise print "You failed!"

if (grade >= 70) System.out.println("You passed"); else System.out.println("You failed");

Which selection control structures are covered on the AP® Computer Science Exam?

if and if...else

Why does the boolean data type exist?

it adds readability

Why is a do...while loop structure frequently used for input protection?

it guarantees that the loop body is executed at least once

If a type of loop structure is inside the braces of another loop structure, then it is called . . .

nested looping

If a type of selection structure is inside the braces of another selection structure, then it is called . . .

nested selection

What new Java statements are required for nesting?

nesting does not require any new statements

Consider the following program segment. Assume only upper-case letters can be entered. do { System.out.print("\nEnter Your Gender. ===>> "); gender = input.nextLine().charAt(0); System.out.println(); if ((gender != 'M' || gender != 'F')) { System.out.println("Invalid Gender Entered: " + gender); System.out.println("You must enter M or F."); } } while (!(gender == 'M' || gender == 'F')); Will the program segment above properly protect against erroneous input?

no, if ((gender != 'M' || gender != 'F'))

Describe De Morgan's law.

not (A or B) = not A and not B not(A and B) = not A or not B

Consider the following program segment. if (education >= 16 && experience >= 5) System.out.println("You are hired") else System.out.println("You are not hired"); There are again four possible combinations. In how many of these combinations is somebody hired?

one

Which data types work with "switch"?

only char, int, and String

What is nesting?

placing one control structure inside the another control structure

Besides selection, what other control structures can be nested?

repetition

The multiple-way selection control structure uses the ___________ keyword.

switch

Which loop structure is probably the easiest for nesting?

the for loop

What library is the Scanner class located in?

the java.util library

If A is true, what is the value of A or ((B and C) or (A or C) and (A and B)) ?

true

What are the 2 possible values of the boolean data type?

true and false

The conditional repetition control structure uses the ____________ keyword.

while

Consider the following program segment. Assume only upper-case letters can be entered. do { System.out.print("\nEnter Your Gender. ===>> "); gender = input.nextLine().charAt(0); System.out.println(); if (!(gender == 'M' || gender == 'F')) { System.out.println("Invalid Gender Entered: " + gender); System.out.println("You must enter M or F."); } } while (!(gender == 'M' || gender == 'F')); Will the program segment above properly protect against erroneous input?

yes, if (!(gender == 'M' || gender == 'F'))

What symbol does Java use for a logical "or" ?

||


Related study sets

Food Handler Class - All information

View Set