Module 5 Flashcards

1
Q

Which of the following loops would print out a list of even numbers ranging from 1 through 10?

Formatting impossible, See print version

A

Formatting impossible, See print version

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

What are the two main components of an iterative loop in Java?

A) A subordinate scope of program code.
B) Its opening and closing round parentheses.
C) The evaluation of a boolean expression yielding either true or false.
F) It’s opening and closing curly brackets.

A

A) C)

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

Which of the following are valid elementary conditional clauses?

Formatting Error - see text

A

Formatting Error see text

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

Please select the expression or expressions below which identify the purpose of a ! sign in a conditional clause

A) Terminates an action statement.
B) Specifies the negation of an elementary equals operator.
C) Precedes an if statement
D) Reverses the logical sense of a conditional clause.

A

B) D)
A and C are incorrect. B refers to the elementary conditional operator !=, and D refers to the compound conditional operator !.

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

Please select all of the clauses below which indicate an aspect of the if statement

A) A conditional statement
B) Followed by one or more action clauses
C) Can alter the flow of the program execution
D) Capable of assigning a value
E) Followed by an open round parenthesis

A

A) B) C) E)

The if statement cannot assign a value. All other Feedback: statements are true.

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

Which Java iterative procedure(s) may never execute its subscope?

A) for
B) do/while
C) do until
D) while

A

A) D)

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

Which of the following Java statements will tell if the two dogs defined in this scenario have the same name?
String dogname1 = new (“Rover”);
String dogname2 = new (“Rover”);

A) if (dogname1 == dogname2)
B) if (dogname1.equals (dogname2))
C) if (dogname1[0] == dogname2 [0])
D) if (dogname1.length() == dogname2.length())

A

B)

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

Which iterative procedure in Java always executes the subscope of code at least once?

A) while
B) for
C) do…while
D) do until

A

C)

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

Please identify all of the conditional operators which occur in the list below.

A) >=
B) =
C) +
D) !=
E) &&

A

A) D)

! is the compound Not operator, && is the compound AND operator, and >= is the elementary greater-than-or-equals operator. + is the arithmetic addition operator and = is the assignment operator.

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

What will be printed by the following Java program?

public class Scope_of_i
{
public static void main (String [] args)
{
int i;
for (i=1; i<3; i++)
{
System.out.print (i);
}
System.out.print (i+1);
}

A) 121
B) 124
C) 1224
D) This program would not compile

A

B)

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

What will the value left in i be after the following Java code is executed?
int i=0;
for (int j = 0; j < 5; j = j + 2)
i += j;

A) 0
B) 15
C) 6
D) 10

A

C)

The loop will execute three times when j is 0, 2, 4. Each time through the loop j is added to i so the sum will be 6.

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

What is the outcome of the following code?
int a = 5, b = 10, sum = 0;
if ((a++ == 5) && (b++ == 10)) {
sum = a + b;
}
System.out.println(a + “,” + b + “,” + sum);

A) 5, 10, 17
B) 6, 11, 17
C) 5, 10, 15
D) 5, 10, 0

A

B)

Since the ++ is post increment, if test evaluates to true. At this point, a and b are increments resulting in 6, 11, and the sum as 17

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

Which of the following is a valid header of the Java for statement?

A) for (double i = 1; i<=5; i++)
B) for (int i=1; i>=5; i–)
C) for (int i = 5; i>0; i–)
D) for (int i>1; i=5; i++)
E) for (int i = 1; i <=5; i++)

A

C) E)

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

Which of the following statements are true regarding the Java do…while statement ?

A) The subscope of the loop is not executed following the do keyword.
B) The do keyword appears at the top of the loop and the while at the bottom.
C) Testing is done prior to entering the subscope of the loop.
D) Testing is done following the subscope of the loop.
E) The subscope of the loop is executed once before testing to continue the iteration.

A

B) D) E)

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

What will be printed by the following code?

int x = 12;
if ( x < 5 || x > 15 )
System.out.println (“1”);
else if ( x > 5 && x < 10 )
System.out.println (“2”);
else
System.out.println (“3”);

A) 3
B) 1
C) 2
D) Nothing
E) 1 2

A

A)

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

What is the outcome of the following code?
int x = 10, y = 20;
switch (x) {
case 5: y = 10;
case 10: y = 20;
case 15: y = 30;
}
System.out.println(y);

A) 20
B) 30
C) 10
D) None of the given choices

A

C)

17
Q

Which of the following are true about this switch statement?

int x =1; int y=5;
switch (p){
case 1: System.out.print(x + y);
case 2: System.out.print(x - y);
break;
case 3: System.out.print(x / y);
case 4: System.out.print(x * y);
break;
default: System.out.print(“That’s not a valid choice.”);
}

A) If p = 5, the output would be: That’s not a valid choice.
B) If p = 3, the output would be: 0.2 5
C) If p = 2, the output would be: -4
D) If p = 4, the output would be: 5
E) If p = 1, the output would be: 6

A

A) C) D)

18
Q

Which of the following are true about the output of the following code?
for (int i = 1; i <=3; i++){
for (int j = i; j>0; j–){
System.out.println(“j = “ + j);
}
System.out.println(“i = “ + i);
}

A) The last line of output is: i=3
B) The first line of output is: j=1
C) The first line of output is: i=1
D) The first line of output is: j=3
E) The second line of output is: i=1

A

A) B) E)

19
Q

Which of the following are true about the following code?
for (int i = 1; i <=5; i++){
for (int j = 1; j<=i; j++){
System.out.println(i + “, “ + j); }
5 out of 5 points
}

A) The inner loop will execute a total of 15 times.
B) The print statement will execute in total the same number of times as the outer loop.
C) The outer loop will execute 5 times.
D) The inner loop will execute a total of 5 times.
E) The outer loop will execute 15 times.

A

A) C)

20
Q

Which of the following are true regarding the do…while iterative loop in Java?

A) The do…while loop is the only iterative loop which requires two keywords to manage it.
B) The do…while loop is the only type of iterative loop in which a semicolon immediately follows the closing round parentheses which enclose its conditional clause.
C) It is possible for the do…while loop to not execute its code in the body of the loop if the condition starts off as false.
D) The conditional clause which governs the execution of a do…while loop is a standard conditional clause like any which occur in other conditional statements.

A

A) B) D)

The do…while loop always executes once before checking its conditional clause at the end of the first loop iteration.

21
Q

What is the outcome of the following code? int count = 0, sum = 0;
while (count <= 5) {
sum = sum + count;
}
System.out.println(sum);

A) 15
B) 10
C) 20
D) None of the given choices
E) 0

A

D)

The while statement goes into an infinite loop as count is never incremented.

22
Q

Which of the following are valid while loop headers?

A) while (num = > 6)
B) while (num != -1)
C) while (num = 5)
D) while (num <=5)
E) while (num > 5)

A

B) D) E)

23
Q

What is the outcome of the following code?
int a = 5, b = 10, sum = 0;
if ((a++ == 5) || (b++ == 10)) {
sum = a + b;
}
System.out.println(a + “,” + b + “,” + sum);

A) 5, 10, 0
B) 6, 10, 0
C) 5, 10, 17
D) 6, 10, 16

A

D)

In the OR case, since the first part is true, the second part is not evaluated. Only a is incremented to 6, b remains 10, hence the sum is 16.

24
Q

How do we determine if sport1 is the same as sport2?
String sport1 = new String (“Soccer”);
String sport2 = new String (“Tennis”);

A) if (“Soccer” = “Tennis”)
B) if (“Soccer” == “Tennis”)
C) if (sport1.equals (sport2))
D) if (sport1 == sport2)

A

C)

The equals method is the only valid way shown to compare the two strings Soccer and Tennis. sport1 and sport2 contain the address of the String objects in memory, not the strings themselves.

25
Q

What is true about the output of the following code?
for (int i = 1; i < 5; i++){
for (int j = 2; j>0; j–) {
System.out.print(j);
}
}

A) The last line of output will be: 2121212121
B) There will be one line of output.
C) There will be 8 lines of output.
D) There will be 10 lines of output.
E) The first line of output will be: 21212121

A

B) E)

The print statement inside the inner loop will only generate one line of output because it is a print statement not a println statement. The inner loop will execute 2 times per outer loop execution, printing 21 for each outer loop iteration.

26
Q

In what sense are iterative procedures repetitious?

A) Iterative procedures are repetitious in that they process the same subordinate scope of program code again and again until a condition is fulfilled.
B) Iterative procedures are repetitious in that they contain programming statements.
C) Iterative procedures are repetitious in that their subordinate scopes terminate with a closing curly bracket.
D) Iterative procedures are repetitious in that they include conditional clauses.

A

A)

27
Q

Please select the name or names applied to Java programming material which is enclosed within round parentheses.

A) Logical expression
B) Parameter list
C) Scope
D) Conditional clause
E) statement

A

B) D)

Conditional clauses and parameter lists are generally enclosed within round parentheses. Scopes are enclosed within curly brackets { }, and statements are generally terminated by semicolons.

28
Q

What happens if the break statement is left out of a case section in a switch statement in Java?

A) You will get a compiler error
B) Once the last statement in the case section is executed control will flow to the default case.
C) Once the last statement in the case section is executed control will flow out of the switch statement
D) Once the last statement in the case section is executed control will flow into the next case section
E) You will get a runtime error

A

D)

29
Q

What is the outcome of the following code?
int a = 5, b = 10, sum = 0;
if ((++a == 5) && (b++ == 10)){
sum = a + b;
}

A) 5, 10, 0
B) 6, 10, 0
C) 5, 10, 17
D) 6, 11, 17

A

B)

Since the ++ is pre increment, the first part of the test evaluates to false. The second part is never evaluated. Only a is changed to 6 in this case.