What is true in this Perl loop mechanism?
foreach (@words) {....}
Front
C. both A & B
Back
loop
select
when x =>
accept pickup;
or
when y =>
accept dropoff;
or
terminate;
end select;
end loop;
It is an error if a selective accept statement in Ada has a guard on each
of its alternatives and all the guards evaluate to false.
Front
T
Back
What is the output of this Fortran code?
i = 3
j = 3
10 PRINT "x"
15 PRINT "y"
i = i - 1
IF (i .gt. 0) GOTO 15 // .gt. is "greater than"
j = j - 1
IF (j .gt. 0) GOTO 10
Front
C. xyyyxyxy
Back
The following two control structures in C share the same semantics.
// #1
k = 1;
loop:
if (k > 10) then goto out
k = k + 1;
goto loop
out:
// #2
for (k = 1; k <=10; k++)
;
Front
T
Back
Select the statements that are true of C's multiple selection statement (the switch).
Front
A. C supports implicit fall through
B. there is a mechanism for a default choice
C. the branch is explicit
Back
C's switch statement does not have an implicit break thus supports fall through (also called flow through). What will be printed when this C code is executed for num = 2?
switch (num) {
case 1: printf("one ");
case 2: printf("two ");
case 3: printf("three ");
default: printf("whatever ");
}
Front
B. two three whatever
Back
Assume the following legal statement in the C language, where '=' is assignment and 'i' can be any numeric variable type. What is true?
while (i = 5) {
i--;
}
Front
C. the loop will execute forever
Back
Select the true statements regarding break, continue and goto statements.
Front
A. they are all unconditional control statements
D. they all require a corresponding label
Back
What is true concerning user-controlled loop control structures?
Front
A. You can jump out from anywhere inside the loop body.
C. You can jump in anywhere inside the loop body.
D. This type of control is implemented by unconditional branches.
Back
What does the following C code do? (X has been initialized with ints)
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (X[i][j] != 0)
goto foo;
}
printf ("Found it at: %d ", i);
break;
foo:
}
Front
A. finds the first row that contains all zeros
D. uses a goto to exit the loop prematurely at the first instance of 0
Back
The following two control structures in C share the same semantics.
int i;
int j = 0;
// #1
for (i = 0; i < 3; i++) {
switch (j+2) {
case 3:
case 2: j--; break;
case 0: j++; break;
default: j = 0;
}
if (j > 0) break;
j = 3 - i;
}
// #2
i = 0;
while (i < 3) {
if ((j+2 == 3) || (j+2 == 2))
j--;
else if (j+2 == 0)
j++;
else
j = 0;
if (j > 0)
break;
j = 3 - i;
i++;
}
Front
T
Back
Reading values from a file of unknown size until you hit EOF must be solved with what type of looping control mechanism?
Front
B. logically controlled
Back
The following C code will accomplish what?
while (i < n) {
flag = 0;
for (j = 0; j < n; j++) {
if (X[i][j] < 0) // assume X contains integers
flag = 1;
}
if (flag == 1) {
printf ("value: %d ", i);
i = n ;
}
i++;
}
Front
C. both A and B will occur
Back
What is true about the default enumeration value option in a switch?
Front
A. all unrepresented enumeration values will be caught by this option
B. this option should be omitted only if all possible enumeration values have been tested
C. it can lead to programmer laziness
Back
This pseudo-code describes the control structure of a pre-test loop.
start
if (ctrl_expr)
do loop body
goto start
Front
T
Back
This function is tail-recursive.
int stuff (int n) {
if (n == 0) return 1
return n * stuff (n-1);
}
Front
F
Back
What is always true in counter-controlled loops regardless of the language?
Front
C. The loop variable is evaluated no more than once per iteration.
Back
loop
select
when x =>
accept pickup;
or
when y =>
accept dropoff;
or
terminate;
end select;
end loop;
Which statement is true concerning the Ada code?
Front
A. x and y are both evaluated when the select statement is hit
Back
This code is an example of a dangling else. Under C syntax which 'if' is attached to the 'else'? (C++,Java and C# share the same semantics)
if (sum == 0) // first
if (count == 0) // second
result = 0;
else result = 1;
Front
B. the second if
Back
loop
select
when x =>
accept pickup;
or
when y =>
accept dropoff;
or
terminate;
end select;
end loop;
The terminate call occurs when
Front
C. both A & B
Back
Which of the following cannot be accomplished without the use of an
unconditional labeled jump; i.e., a goto?
Front
B. exiting a loop at somewhere other than the bottom or top
Back
Assume this is a legal statement in your language, where '=' is assignment and 'i' can be any numeric variable type. What do you know about the language?
while (i = floor(x))
Front
A. a condition statement may be any arithmetic expression
Back
What is true in the output of the three chunks of code below for n = 4?
// Chunk #1
void junk1 (int n) {
print n;
if (n > 0)
junk1 (n-1);
}
// Chunk #2
void junk2 (int n) {
if (n > 0)
junk2 (n-1);
print n;
}
// Chunk #3
while (n > 0) {
print n;
n--;
}
Front
D. the output is different in all cases
Back
What is displayed in this C code if num is -1 and sum is 1 to start with?
1 while (sum) {
2 if (num < 0) {
3 break;
4 }
5 sum--;
7 }
8 printf("Sum Value: %d",sum);
Front
A. 1
Back
What does it mean to use negative logic in a control structure?
Front
B. control is released when the condition becomes true
Back
The syntax of C#'s switch statement is shown below:
switch (expression) {
case constant-expression:
statement
jump-statement
[default:
statement
jump-statement]
}
What can you conclude about C#'s form of switch statement?
Front
C. both A and B are true.
Back
Reading values from an array of integers on unknown size until you hit a flag (say -999) can be solved with what type of looping control mechanism?
Front
B. logically controlled
Back
An implicit branch in a multiple selection statement
Front
A. ensures that only one selection will be executed.
B. eliminates the need for a break statement.
C. prevents fall through.