Chapter 5 Conditionals and Loops Flashcards

1
Q

What is meant by the flow of control through a program?

A

The flow of control through a program determines the program statements that will be executed on a given run of the program.

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

What type of conditions are conditionals and loops based on?

A

Each conditional and loop is based on a boolean condition that evaluates to either true or false.

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

What are the equality operators? The relational operators? The logical
operators?

A

The equality operators are equal (==) and not equal (!=). The relational
operators are less than (),
and greater than or equal to (>=). The logical operators are not (!), and
(&&), and or (||).

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

Given the following declarations, what is the value of each of the
listed boolean expressions?

int value1 = 5, value2 = 10;
boolean done = true;

a. value1 <= value2
b. (value1 + 5) >= value2
c. value1 < value2 / 2
d. value2 != value1
e. !(value1 == value2)
f. (value1 < value2) || done
g. (value1 > value2) || done
h. (value1 < value2) && !done
i. done || !done
j. ((value1 > value2) || done) && (!done || (value2 >
value1) )

A

Assuming the given declarations, the values are: a. true, b. true,
c. false, d. true, e. true, f. true, g. true, h. false, i. true, j. true

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

What is a truth table?

A

A truth table is a table that shows all possible results of a boolean expression, given all possible combinations of variables and conditions.

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

Assuming done is a boolean variable and value is an int variable, create a truth table for the expression:

(value > 0 ) || !done

A

The truth table is:

value > 0  done  !done  (value > 0) || !done
true           true    false    true
true           false   true     true
false          true    false    false
false          false   true     true
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Assuming c1 and c2 are boolean variables, create a truth table for the
expression:

(c1 && !c2) || (!c1 && c2)

A

See book 5.7.

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

What output is produced by the following code fragment given the assumptions below?

if (num1 < num2)
   System.out.print(" red ");
if ((num1 + 5) < num2)
    System.out.print(" white ");
else
    System.out.print(" blue ");
System.out.println(" yellow ");

a. Assuming the value of num1 is 2 and the value of num2 is 10?
b. Assuming the value of num1 is 10 and the value of num2 is 2?
c. Assuming the value of num1 is 2 and the value of num2 is 2?

A

Based on the given assumptions, the output would be:

a. red white yellow
b. blue yellow
c. blue yellow

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

How do block statements help us in the construction of conditionals?

A

A block statement groups several statements together. We use them to define the body of an if statement or loop when we want to do multiple things based on the boolean condition.

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

What is a nested if statement?

A

A nested if occurs when the statement inside an if or else clause is an if statement. A nested if lets the programmer make a series of decisions. Similarly, a nested loop is a loop within a loop.

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

For each assumption, what output is produced by the following code
fragment?

if (num1 >= num2)
{
   System.out.print(" red ");
   System.out.print(" orange ");
}
if ((num1 + 5) >= num2)
    System.out.print(" white ");
else
if ((num1 + 10) >= num2)
{
     System.out.print(" black ");
     System.out.print(" blue ");
}
else
    System.out.print(" yellow ");
System.out.println(" green ");

a. Assuming the value of num1 is 5 and the value of num2 is 4?
b. Assuming the value of num1 is 5 and the value of num2 is 12?
c. Assuming the value of num1 is 5 and the value of num2 is 27?

A

Based on the given assumptions, the output would be:

a. red orange white yellow
b. black blue green
c. yellow green

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

Write an expression that will print a message based on the value of
the int variable named temperature. If temperature is equal to or less
than 50, it prints “It is cool.” on one line and “Dress warmly.” on the
next. If temperature is greater than 80, it prints “It is warm.” on one
line and “Dress coolly.” on the next. If temperature is in between 50
and 80, it prints “It is pleasant.” on one line and “Dress pleasantly.”
on the next.

A
if (temperature <= 50)
	 {
             System.out.println("It is cool.");
             System.out.println("Dress warmly.");
	 }
         else
               if (temperature > 80)
               {
                    System.out.println("It is warm.");
                    System.out.println("Dress cooly.");
               }
               else
              {
                System.out.println("It is pleasant.");
                System.out.println("Dress pleasantly.");
             }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Why must we be careful when comparing floating point values for
equality?

A

Because they are stored internally as binary numbers, comparing floating point values for exact equality will be true only if they are the same bit-by-bit. It’s better to use a reasonable tolerance value and consider the difference between the two values

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

How do we compare strings for equality?

A

We compare strings for equality using the equals method of the String class, which returns a boolean result. The compareTo method of the String class can also be used to compare strings. It returns a positive, 0, or negative integer result depending on the relationship between the two strings.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
Write an equals method for the Die class of Section 4.2. The method
should return true if the Die object it is invoked on has the same
facevalue as the Die object passed as a parameter, otherwise it should
return false
A
//------------------------------------------------------
// Returns true if this Die equals die, otherwise
// returns false.
//------------------------------------------------------
public boolean equals(Die die)
{
return (this.faceValue == die.faceValue);
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Assume the String variables s1 and s2 have been initialized. Write an
expression that prints out the two strings on separate lines in lexicographic order.

A

if (s1.compareTo(s2) < 0)
System.out.println(s1 + “\n” + s2);
else
System.out.println(s2 + “\n” + s1);

17
Q

What is an infinite loop? Specifically, what causes it?

A

An infinite loop is a repetition statement that never terminates. Specifically, the body of the loop never causes the condition to become false

18
Q

What output is produced by the following code fragment?

int low = 0, high = 10;
while (low < high)
{
System.out.println(low);
low++;
}
A

The output is the integers 0 through 9, printed one integer per line.

19
Q

What output is produced by the following code fragment?

int low = 10, high = 0;
while (low <= high)
{
System.out.println(low);
low++;
}
A

The loop is not entered, so there is no output.

20
Q

What output is produced by the following code fragment?

int low = 0, high = 10;
while (low <= high)
{
System.out.println(low);
high = high − low;
}
A

Since the value of high always remains larger than the value of low, the code loops continuously, producing many lines of zeros, until the program is terminated.

21
Q

What output is produced by the following code fragment?

int low = 0, high = 10, mid;
while (low <= high)
{
   mid = low;
  while (mid <= high)
  {
     System.out.print(mid + " ");
     mid++;
  }
 System.out.println();
 low++;
}
A

The output is:

0 1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
2 3 4 5 6 7 8 9 10
3 4 5 6 7 8 9 10
4 5 6 7 8 9 10
5 6 7 8 9 10
6 7 8 9 10
7 8 9 10
8 9 10
9 10
10
22
Q

Assume the int variable value has been initialized to a positive integer. Write a while loop that prints all of the positive divisors of value.
For example, if value is 28, it prints divisors of 28: 1 2 4 7 14 28

A
int count = 1;
System.out.print("divisors of " + value + ":");
while (count <= value)
{
    if ((value % count) == 0)
         System.out.print(" " + count);
    count++;
}
23
Q

Assume the int variable value has been initialized to a positive integer. Write a while loop that prints all of the positive divisors of each
number from one to value. For example, if value is 4, it prints

divisors of 1: 1
divisors of 2: 1 2
divisors of 3: 1 3
divisors of 4: 1 2 4

A
int count1 = 1, count2;
while (count1 <= value)
{
       System.out.print("divisors of " + count1 + ":");
       count2 = 1;
       while (count2 <= count1)
       {
           if ((count1 % count2) == 0)
                 System.out.print(" " + count2);
          count2++;
       }
       System.out.println();
       count1++;
}
24
Q

Devise statements that create each of the following Scanner objects.

a. One for interactive input, which reads from System.in.
b. One that reads from the file “info.dat”.
c. One that reads from the String variable infoString.

A

a. Scanner user = new Scanner(System.in);
b. Scanner infoFileScan = new Scanner(new
File(“info.dat”));
c. Scanner infoStringScan = new Scanner(infoString);

25
Q

Assume the Scanner object fileScan has been initialized to read from
a file. Write a while loop that calculates the average number of characters per line of the file.

A

The following code prints out the average number of characters per line:

int numChars = 0;
int numLines = 0;
String holdLine;

// Read and process each line of the file
while (fileScan.hasNext())
{
        numLines++;
        holdLine = fileScan.nextLine();
        numChars += holdLine.length();
}
System.out.println ((double)numChars/numLines);
26
Q

What are the advantages of using an ArrayList object?

A

An ArrayList stores and manages multiple objects at one time. It allows you to access the objects by a numeric index and keeps the indexes of its objects continuous as they are added and removed. An ArrayList dynamically increases its capacity as needed.

27
Q

What type of elements does an ArrayList hold?

A

An ArrayList generally holds references to the Object class, which means that it can hold any type of object at all (this is discussed further in Chapter 8). A specific type of element can and should be specified in the ArrayList declaration to restrict the type of objects
that can be added and eliminate the need to cast the type when extracted.

28
Q

Write a declaration for a variable named dice that is an ArrayList of
Die objects.

A

ArrayList dice = new ArrayList();

29
Q

What output is produced by the following code fragment?

ArrayList names = new ArrayList();
names.add("Andy");
names.add("Betty");
names.add(1, "Chet");
names.add(1, "Don");
names.remove(2);
System.out.println(names);
A

[Andy, Don, Betty]