CSc
2010,
Fall
2013
Final
Exam
-‐
Practice
Problems
for
Java
n-‐
SOLUTIONS
MULTIPLE
CHOICE
QUESTIONS
(1)
Which
of
the
following
reserved
words
in
Java
is
used
to
create
an
instance
of
a
class?
a)
class
b)
public
c)
public
or
private,
either
could
be
used
d)
import
e)
new
(2)
If
a
method
does
not
have
a
return
statement,
then
a)
it
will
produce
a
syntax
error
when
compiled
b)
it
must
be
a
void
method
c)
it
can
not
be
called
from
outside
the
class
that
defined
the
method
d)
it
must
be
defined
to
be
a
public
method
e)
it
must
be
an
int,
double,
or
String
method
(3)
What
is
the
value
of
the
String
S
after
the
following
line?
String S = (new String("arach")).substring(0,2) +
(new String("nophobia")).substring(3);
(a)
"arachobia"
(b)
"arnophobia"
(c)
"arhobia"
(d)
"rachobia"
(4)
The
method
twist
is
defined
as
follows:
public void twist(String[] w) {
String temp = w[0].substring(0, 1);
w[0] = w[1].substring(0, 1) + w[0].substring(1);
w[1] = temp + w[1].substring(1);
}
What
is
the
output
of
the
following
code
segment?
String[] words = {"HOW", "NEAT"};
twist(words):
System.out.println(words[0] + " " + words[1]);
(a) NOW NOW
(b) HOW HOW
(c) NOW HOW
(d) HOW NEAT
(e) NOW HEAT
(5)
What
is
the
output
of
the
following
code?
THIS
TOPIC
NOT
COVERED
IN
CLASS
–
PLEASE
IGNORE
String barb = "BARBARA";
scramble(barb);
System.out.println(barb);
The
method
scramble
is
defined
as
follows:
public static String scramble(String str) {
if (str.length() >= 2){
int n = str.length() / 2;
str = scramble(str.substring(n)) + str.substring(0, n);
}
return str;
}
(1)
BARBARA
(b)
ARBABAR
(c)
AABAR
(d)
ARBABARB
(e)
ARABARBARB
(6)
What
are
the
values
in
arr
after
the
following
statements
are
executed?
int[] arr = {1, 1, 0, 0, 0};
for (int i = 2; i < arr.length; i++)
arr[i] = arr[i-1] + arr[i-2];
(a)
11011
(b)
11210
(c)
11222
(d)
11235
(e)
11248
(7)
Given
double x = 5, y = 2;
What
is
the
value
of
m
after
the
following
statement
is
executed?
int m = (int)(x + y + x / y - x * y - x / (10 * y));
(a)
-‐1
(b)
-‐0.75
(c)
-‐0.5
(d)
0
(e)
1
(8)
What
is
the
value
of
sum
after
the
following
code
segment
is
executed?
int p = 3, q = 1, sum = 0;
while (p <= 10) {
sum += p % q;
p++;
q++;
}
(a)
0
(b)
10
(c)
12
(d)
14
(e)
52
(9)
Consider
the
following
method:
THIS
TOPIC
NOT
COVERED
IN
CLASS
–
PLEASE
IGNORE
public int goFigure(int x) {
if (x < 100)
x = goFigure(x + 10);
return (x - 1);
}
What
does
goFigure(60)
return?
(a)
59
(b)
69
(c)
95
(d)
99
(e)
109
(10)
What
are
the
values
of
m
and
n
after
the
following
code
runs?
int m = 20, n = 2, temp;
for (int count = 1; count <= 20; count++) {
temp = m;
m = n + count;
n = temp - count;
}
(a)
m
=
230
n
=
-‐208
(b)
m
=
30
n
=
-‐8
(c)
m
=
12
n
=
-‐10
(d)
m
=
-‐12
n
=
8
(e)
m
=
-‐190
n
=
212
Show
the
EXACT
output
the
following
programs
generate:
(1)
public class e21 {
public static void main (String args[]) {
int num = 4;
printNumbers(num);
}
private static void printNumbers(int n) {
for (int i=1; i <= n; i++) {
for (int k=1; k <= 2*(i-1)+1; k++)
System.out.print(i);
System.out.println();
}
}
}
1
222
33333
4444444
(2)
public class f5 {
public static void main(String args[]) {
int m=63,n=0;
for (;m>0; n++,m/=2);
System.out.println("n = " + n);
}
}
n = 6
PROGRAMS
TO
WRITE:
(1)
Write
a
class
method
that
takes
as
input
a
String
object
whose
value
is
an
English
sentence.
The
method
should
return
the
last
word
in
the
sentence.
You
may
assume
that
the
sentence
contains
words
separated
by
one
space
and
the
sentence
terminates
with
a
period.
For
example
the
following
would
be
a
valid
sentence:
"The
cat
ate
the
rat."
Make
sure
that
the
period
at
the
end
of
the
sentence
is
not
included
in
the
result
that
is
returned
by
the
method.
private static String getLastWord(String str) {
str=str.substring(str.lastIndexOf(' ')+1, str.length()-1);
return str;
}
Write
a
main
method
that
reads
a
sentence
from
the
keyboard,
calls
the
above
method
to
get
the
last
word,
and
prints
the
last
word
to
the
screen.
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a sentence: ");
String sentence = sc.nextLine();
System.out.println(getLastWord(sentence));
}
(2)
Write
a
static
method
named
vowelCount
that
accepts
a
String
as
a
parameter
and
produces
and
returns
an
array
of
integers
representing
the
counts
of
each
vowel
in
the
String.
The
array
returned
by
your
method
should
hold
5
elements:
the
first
is
the
count
of
A’s,
the
second
is
the
count
of
E’s,
the
third
I’s,
the
fourth
O’s,
and
the
fifth
U’s.
Assume
that
the
string
contains
no
uppercase
letters.
For
example,
the
call
vowelCount("i
think,
therefore
i
am");
should
return
the
array
{1,
3,
3,
1,
0}.
private static int[] vowelCount(String str) {
int[] arr = { 0, 0, 0, 0, 0 };
for (int i = 0; i < str.length(); i++)
if (str.charAt(i) == ‘a’)
arr[0]++;
else if (str.charAt(i) == ‘e’)
arr[1]++;
else if (str.charAt(i) == ‘i’)
arr[2]++;
else if (str.charAt(i) == ‘o’)
arr[3]++;
else if (str.charAt(i) == ‘u’)
arr[4]++;
return arr;
}
(3)
Write
a
method
named
minGap
that
accepts
an
integer
array
as
a
parameter
and
returns
the
minimum
'gap'
between
adjacent
values
in
the
array.
The
gap
between
two
adjacent
values
in
a
array
is
defined
as
the
second
value
minus
the
first
value.
For
example,
suppose
a
variable
called
array
is
an
array
of
integers
that
stores
the
following
sequence
of
values.
int[] array = {1, 3, 6, 7, 12};
The
first
gap
is
2
(3
-‐
1),
the
second
gap
is
3
(6
-‐
3),
the
third
gap
is
1
(7
-‐
6)
and
the
fourth
gap
is
5
(12
-‐
7).
Thus,
the
call
of
minGap(array)
should
return
1
because
that
is
the
smallest
gap
in
the
array.
Notice
that
the
minimum
gap
could
be
a
negative
number.
For
example,
if
array
stores
the
following
sequence
of
values:
{3,
5,
11,
4,
8}
The
gaps
would
be
computed
as
2
(5
-‐
3),
6
(11
-‐
5),
-‐7
(4
-‐
11),
and
4
(8
-‐
4).
Of
these
values,
-‐7
is
the
smallest,
so
it
would
be
returned.
This
gap
information
can
be
helpful
for
determining
other
properties
of
the
array.
For
example,
if
the
minimum
gap
is
greater
than
or
equal
to
0,
then
you
know
the
array
is
in
sorted
(nondecreasing)
order.
If
the
gap
is
greater
than
0,
then
you
know
the
array
is
both
sorted
and
unique
(strictly
increasing).
If
you
are
passed
an
array
with
fewer
than
2
elements,
you
should
return
0.
private static int minGap(int[] value) {
if (value.length < 2)
return 0;
int min = value[1]-value[0];
for(int i=1;i<value.length-1;i++)
if(min>value[i+1]-value[i])
min = value[i+1]-value[i];
return min;
}
(4)
Write
a
method
named
toBinary
that
accepts
an
integer
as
a
parameter
and
returns
a
string
of
that
number's
representation
in
binary.
For
example,
the
call
of
toBinary(42)
should
return
"101010".
We
will
assume
a
positive
parameter.
private static String toBinary(int num) {
if (num == 0)
return "0";
String str="";
while (num != 0) {
str = (num%2)+str;
num /= 2;
}
return str;
}