Professional Documents
Culture Documents
CS101 Fall 2013 Midterm I Solution
CS101 Fall 2013 Midterm I Solution
1. (3+3+4
points)
Write
down
the
output
for
the
following
code
fragments.
a.
in t n = 1;
wh ile (n < 10) {
print(n);
n += 2;
}
13579
b.
in t x = 1;
in t y = 10;
in t z = 3;
10101
01010
10101
01010
CS
101
Computer
Programming
-
Midterm
Exam
I
P a g e
|
3
/
8
2. (4+4
points)
Write
down
the
outputs
of
the
following
programs.
a.
int i = 0; 1
3
5
7
9
11
13
15
17
19
int j = 1; -9
while (j < 20) {
print(j+" ");
21
j = j+1;
i--;
j++;
}
println();
println(i);
println(j);
b.
for (int i = 4; i <= 12; i++) { 4
is
div
by
2
if(i%2==0){ 5
is
div
by
5
println(i+" is divisible by 2");
}else if(i%3==0){
6
is
div
by
2
println(i+" is divisible by 3"); 8
is
div
by
2
} 9
is
div
by
3
if(i%5==0){ 10
is
div
by
2
println(i+" is divisible by 5"); 10
is
div
by
5
}
12
is
div
by
2
}
CS
101
Computer
Programming
-
Midterm
Exam
I
P a g e
|
4
/
8
pu bli c vo id run(){
fo r(in t n=readInt("n? "); n>0; n=n/10){
print(n%10);
}
}
This
program
gets
a
number
from
the
user,
then
it
prints
the
digits
of
the
number
in
reverse
order.
CS
101
Computer
Programming
-
Midterm
Exam
I
P a g e
|
5
/
8
4. (4
+
4
points)
Convert
the
following
for
statements
to
the
corresponding
while
statements.
a)
fo r(in t i=0; i < n; i++){
println(i*i*i);
}
in t i=0;
wh ile (i < n){
println(i*i*i);
i++;
}
b)
fo r(in t n=readInt("n? "); n > 0; n=n/10){
println(n%10);
}
in t n=readInt("n? ");
wh ile (n > 0) {
println(n%10);
n=n/10;
}
5. (6
points)
The
following
method
min
takes
two
integer
numbers
and
returns
the
smallest
of
these
numbers.
Using
this
method,
implement
another
method
named
min3
that
takes
three
integer
numbers
and
returns
the
smallest
of
these
numbers.
pu bli c in t min(in t a, in t b) {
if (a < b) re tur n a;
el se re tur n b;
}
pu bli c i nt min3(in t a, in t b, in t c) {
re tur n min(a, min(b,c));
}
CS
101
Computer
Programming
-
Midterm
Exam
I
P a g e
|
6
/
8
6. (18
points)
Write
a
ConsoleProgram
that
reads
an
integer
number
x
and
a
digit
d
from
the
user.
Then,
it
checks
if
x
contains
d
or
not.
Sample
outputs
of
the
program
are
shown
below.
You
may
assume
the
user
will
always
enter
a
proper
digit
value
for
d.
import acm.program.*;
CS
101
Computer
Programming
-
Midterm
Exam
I
P a g e
|
7
/
8
7. (25
points)
Write
a
ConsoleProgram
that
reads
two
integer
numbers
x
and
y
from
the
user.
Then,
it
prints
the
digits
of
x
that
are
not
included
in
y.
Sample
outputs
of
the
program
are
shown
below.
import acm.program.*;
CS
101
Computer
Programming
-
Midterm
Exam
I
P a g e
|
8
/
8
8. (17
points)
Write
a
ConsoleProgram
that
reads
an
integer
number
n
and
then
prints
the
digits
of
n
in
ascending
order.
You
may
NOT
use
features
we
havent
covered
yet,
such
as
strings
or
arrays,
to
solve
this
problem.
Hint:
To
solve
this
problem,
you
will
need
to
repeatedly
find
and
remove
the
smallest
digit
in
the
number.
This
requires
finding
not
only
the
value
of
the
smallest
digit
but
also
its
position.
For
instance,
suppose
the
number
is
381254.
Smallest
digit
is
1,
and
it
is
at
the
1000s
position.
Removing
the
smallest
digit
can
be
done
by
some
division
and
modular
arithmetic,
such
as:
((381254
/
1000)
/
10)
*
1000
+
381254
%
1000
=
38254
import acm.program.*;
while(n > 0) {
// find the smallest digit and its index (from right)
int smallestDigit = 10;
int positionOfSmallestDigit = 1;
int positionalValue = 1;
int tempN = n;
while(tempN > 0) {
int digit = tempN % 10;
if (digit < smallestDigit) {
smallestDigit = digit;
positionOfSmallestDigit = positionalValue;
}
positionalValue *= 10;
tempN /= 10;
}
print(smallestDigit);
}
}