Computer Science Quiz

Computer Science Quiz -...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Name:


______________________________________
 Student
Number:


________________
 
 APSC
160
(or
equivalent)
completed
in
(circle
one):
Spring

/ 

Fall

/

Summer

of

___________

(year)
 
 
 CPSC
260:
Preparedness
Test
 This
test
cons ists
of
multip le
choice
and
short
answer
questions.

For
mu ltiple
choice
qu estions
circle
th e
 single
be st
ans wer.

For
short
ans wer
qu estions,
pleas e
write
your
ans wer
in
th e
space
provided.
 Q1.


 Suppose
 that
 var1
 an d
 var2
 are
 variables
 of
 typ e
 int.
 
 Which
 of
 the
 followin g
 code
 segmen ts
 s waps 
 the
va lu e
of
th ese
two
variables?
 (a)
 (b)
 var1 = var2; var2 = var1;
 int temp = var1; var2 = var1; var1 = temp;
 int temp = var1; var1 = var2; var2 = temp; int temp1 = var1; int temp2 = var2; temp1 = temp2; var2 = var1;
 var1 <-> var2;
 Answer:
_____C____
 
 For
ea ch
of
th e
n et
two
ques tions ,
assu me
tha t
th e
following
variable
d eclarations
ha ve
b een
mad e:
 int a = 14; int b = 3; double c = 2.5; Q2.
 What
va lu e
is 
assigned
to
th e
variable
d
b y
the
following
sta tement?
 double d = c + a * b; 
 
 
 
 
 
 
 
 
 Answer:
____44.5___
 (c) 
(d)
 
(e) 
 Q3.
 What
va lu e
is 
assigned
to
th e
variable
e
b y
the
following
sta tement?
 double e = b / a; Answer:
_0.0

‐OR‐

0____
 CPSC
260
Preparedn ess
Test
 
 page
2
of
12
 Q4.
 Suppose
that
variables
“t”
is 
a
variab le
tha t
has
a 
va lu e
wh ich
evalua tes 
to
true 
and
“f”
is 
a
variab le
 that
has
a 
va lu e
which
evalua tes 
to
false .

Wh ich 
on e
of
th e
following
expressions
eva luates
to
false?
 (a)
 (b)
 (c)
 (d)
 (e) 
 t && !f
 !t && f
 t || f
 !(t && f)
 t || ( !f && !t )
 Remind er:
 
&&





means 

“and ”
 

||






means
“or”
 


!








means
“not”
 The
norma l
preced en ce
rules
for
th e
evaluation
of
mathema tical
 expressions
app ly,
th erefore
!
is
eva luated 
before
||
a nd
&&
 Note:
in
future
qu izzes
you
will
not
ha ve
th is
remind er!
 Answer:

___B___
 Q5.
 Consid er
th e
following
poorly
indented
cod e
s egment:
 
 int q; int a = -3; int b = 9; if( a < 0 || b > 0 ) q = 1; else q = 2; a = 0; 
 What
are
th e
valu es 
of
a,
b
and 
q
after
th is
cod e
s egment
has
executed?
 Answer:

a
has
t he
value 
__0__,

b
has
t he
value
__9__
and
q
has
t he
value 
__1__
 Q6.
 Consid er
th e
following
cod e
s egment:
 int j = 1; int k = 0; while( j < 5 && k < 4 ) { k = k + j; j++; } printf( "j = %d, k = %d", j, k ); Comp lete
the
lin e
b elow
to
show
what
is
printed
wh en 
this 
cod e
s egment
runs.
 Answer: j = __4__, k = __6__ CPSC
260
Preparedn ess
Test
 Q7.
 Consid er
th e
following
cod e
s egment:
 int i; int j; int count1 = 0; int count2 = 0; for( i = 0; i < 5; i++ ) { count1++; for( j = 1; j < 6; j++ ) count2++; } 
 page
3
of
12
 // note that “i” starts at 0 // note that “j” starts at 1 What
va lu es
do
count1
and 
coun t2
ha ve
after
this 
cod e
s egment
has
executed?
 Answer:


count1
=
___5____




count2
=
____25___
 
 Q8.
 Consid er
th e
following
cod e
s egment:
 int data = { 1, 2, 4, 8, 16, 32, 64 }; int sum = 0; int index = 1; while( index < 5 ) { sum += data[ index ]; index++; } What
is
th e
valu e
of
sum
after
this
cod e
s egment
has
executed?
 Answer:
____30_____
 
 CPSC
260
Preparedn ess
Test
 Q9.
 Consid er
th e
following
cod e
fun ction:
 
 page
4
of
12
 int doSomething( int data, int numEls, int someVal ) { int index; int found = -1; for( index = 0; index < numEls; index++ ) if( data[ index ] == someVal ) found = index; return found; } 
 Which
of
th e
followin g
b est
d es crib es
the
likely
intend ed
purp ose
of
this
fu nction?
 
(a)
 If
 someVal
 is
 conta in ed
 in
 the
 first
 numEls
 entries
 of
 th e
 arra y
 data,
 it
 returns
 th e
 valu e
 someVal,
oth erwis e
it
returns
-1.
 (b)




If
 someVal
is
 conta in ed
in
 th e
firs t
numEls
 en tries
of
th e
arra y
data,
it
returns
th e
 ind ex
of
th e
 first
s lot
a t
which
someVal
is
found ,
otherwise
it
retu rns
-1.
 (c)

 If
 someVal
 is
 con tain ed
 in
 th e
 first
 numEls
 entries
 of
 the
 array
 data,
 it
 returns
 th e
 ind ex
 of
 th e
 last
s lot
a t
which
someVal
is
found ,
otherwise
it
retu rns
-1.
 (d)
 It
 returns
 true
 if
 someVal
 is
 found
 in
 th e
 first
 numEls
 en tries
 of
 th e
 array
 data
 and
 fals e
 otherwise.
 Answer:
____C_____
 CPSC
260
Preparedn ess
Test
 Q10.
 Consid er
th e
following
cod e
s egment:
 #include <stdio.h> void myFunc( int a, int b ); int main( void ) { int a = 4; int b = 6; 
 page
5
of
12
 myFunc( a, b ); printf( "In main a = %d b = %d\n", a, b ); return 0; } void myFunc( { a=a+ b=bprintf( } 
 
 Answer:
 
 In
m ain





a
=
___4___
b
=
____6____
 
 In
m yFunc

a
=
___7___
b
=
___1____
 int a, int b ) 3; 5; "In myFunc a = %d b = %d\n", a, b ); Fill
in
th e
blan ks
b elow
to
show
what
th e
ou tput
to
th e
screen 
when 
th is
program
runs.
 CPSC
260
Preparedn ess
Test
 
 Q11.
 Consid er
th e
following
cod e
s egment:
 #in clud e
<s tdio.h >
 #d efin e
SIZE
4

/*
s ize
of
arra y
*/
 
 void 
process(
int
data);
 
 int
main( 
void 
)
 {
 
 int
da ta[SIZE]
=
{
5,
‐1,
2,
20
};
 
 int
ind ex;
 
 
 
 process( 
data
);
 
 
 for(
ind ex
=
0;
ind ex
<
SIZE;
index++
)
 
 
 printf(
"%d 
",
data[
ind ex
]
);
 
 
 return 
0;
 }
 
 void 
process(
int
data)
 {
 
 int
ind ex;
 
 
 for(
ind ex
=
0;
ind ex
<
SIZE;
index++
)
 
 
 data[
ind ex
]
=
0;
 }
 
 
 
 
 
 page
6
of
12
 Fill
in
th e
blan ks
b elow
to
show
what
is
output
to
th e
s creen
wh en
this 
progra m
ru ns?
 Answer:

__0__

__0__

__0__

__0__
 
 CPSC
260
Preparedn ess
Test
 
 
 page
7
of
12
 Q12.
 Below
 are
 lin es
 of
 code,
 ord ered
 b y
 len gth,
 that
 wh en
 rearranged
 can
 produce
 a
 sn ipp et
 of
 cod e
 that,
 upon
execu tion ,
 will
result
in
 th e
variable
“min”
con taining
 the
 minimum
 va lu e
in
the
arra y
“x”.

Assu me
 that
a ll
variables
are
d eclared
as
“in t”
and 
that
array
“x”
con tains
SIZE
elements
of
typ e
int.
 1. 2. 3. 4. 5. 6. 7. } i++; i = 1; min = x[0]; min = x[i]; if (x[i] < min) while (i < SIZE) { Select
 from
 above,
 in 
 correct
 ord er,
 th e
 lin es
 of
 code
 need ed
 to
 prod uce
 th e
 snipp et
 of
 cod e
 tha t
 will 
 result
 in 
 “min”
 con tain ing
 th e
 min imu m
 valu e
 from
 the
 array
 “x”.
 Th e
 nu mb er
 of
 lin es
 in
 the
 tab le 
 corresponds
 exa ctly
 to
 th e
 nu mb er
 of
 lin es
 of
 cod e
 required
 to
 ach ieve
 the
 specified
 tas k. Each
 lin e
 given
above
is
us ed
exactly
on ce.
 
 Line number from abov e ( You must provide the line number ) 
 3

‐OR‐
4
 
 4

‐OR‐

3
 
 7
 
 6
 
 5
 
 2
 
 1
 
 i
=
1; 
 min
=
x[0]; 
 while 
(i
<
SIZE)
{ 
 




if
(x[i] 
<
min) 
 









min
=
x[i] ; 
 




I++; 
 } The actual line of code from above (optional, for your convenience) 
 CPSC
260
Preparedn ess
Test
 
 page
8
of
12
 The
 n ext
 3
 qu estions
 (on
 th e
 next
 pa ge)
 refer
 to
 programs
 that
 read
 in
 positive
 integers
 provid ed
 by
 a 
 user.
 
 Th e
 us er
 ma y
 provid e
 as
 many
 integers
 as
 he/s he
 likes ,
 up
 to
 th e
 limit
 of
 200
 in tegers.
 The
 us er
 indicates
 that
 he/sh e
 has
 finish ed
 enterin g
 th e
 positive
 integers ,
 desp ite
 having
 entered
 less
 than
 200
 integers,
b y
en terin g
a
negative
in teger.

The
progra m
prints
all
th e
nu mbers
 entered
by
th e
user,
except 
 for
th e
final
negative
numb er.

A
ps eud o
cod e
vers ion
of
th e
program
looks 
like
th is:
 Read a number while less then 200 integers have been read and the last number read is not negative do Output the last number read Read another number end of the while loop On
th e
n ext
page,
answer
 th e
three
qu estions
 that
d is cuss
how
such 
a
program
 might
b e
modified
to
do
 thin gs
oth er
than
jus t
printing
out
numb ers
that
have
been
read
in.
 CPSC
260
Preparedn ess
Test
 
 page
9
of
12
 Q13.
 Suppose
 this
progra m
was
 mod ified
 to
ou tput
the
a verage
 of
all
th e
pos itive
 integers
and 
th e
nu mb er
of
 positive
in tegers
that
 exceed ed
th e
average.

The
cod e
could
b e
mod ified
b y
eith er
chan gin g
th e
existing
 code
and /or
add ing
extra
code.
Th e
simp les t
chan ge
to
th e
program
to
ach ieve
this
wou ld
be:
 (a)
Not
requ ire
an 
array
or
an
add itiona l
loop.
 (b)
Requ ire
an
arra y
bu t
no
additiona l
loop .
 (c)
Not
require
an
arra y
but
at
least
one
additiona l
loop
wou ld
b e
req uired .
 (d)
Requ ire
an
arra y
and 
at
least
on e
a dditiona l
loop .

 (e)
Non e
of
th e
above
 Answer:
____D_____
 Q14.
 Suppose
 the
 progra m
 given 
 in
 the
 ab ove
 ps eudo
 cod e
 was
 instead
 mod ified
 to
 outpu t
 th e
 nu mb er
 of
 positive
integers
entered
b y
the
us er.

Th e
s implest
ch ange
to
th e
progra m
to
ach ieve
this 
would:
 (a)
Not
requ ire
an 
array
or
an
add itiona l
loop.
 (b)
Requ ire
an
arra y
bu t
no
additiona l
loop .
 (c)
Not
require
an
arra y
but
at
least
one
additiona l
loop
wou ld
b e
req uired .
 (d)
Requ ire
an
arra y
and 
at
least
on e
a dditiona l
loop
 (e)
Non e
of
th e
above
 Answer:
_____A____
 Q15.
 Suppose
 th e
 program
 given
 in
 the
 ps eudo
 cod e
 above
 was
 instead
 modified
 to
 outpu t
 in
 revers e
 ord er
 all
 the
 positive
 integers
 entered .
 
 For
 exa mple,
 if
 2
 5
 3
 6
 4
 was
 entered,
 th en
 th e
 mod ified
 progra m
 wou ld
ou tput
4
6
3
5
2.
Th e
s implest
chan ge
to
th e
program
to
a chieve
th is
wou ld:
 (a)
Not
requ ire
an 
array
or
an
add itiona l
loop.
 (b)
Requ ire
an
arra y
bu t
no
additiona l
loop .
 (c)
Not
require
an
arra y
but
at
least
one
additiona l
loop
wou ld
b e
req uired .
 (d)
Requ irean
arra y
and 
at
least
on e
a dditiona l
loop
 (e)
Non e
of
th e
above
 Answer:
_____D____
 CPSC
260
Preparedn ess
Test
 
 page
10
of
12
 Q16.
 Suppose
 that
 array1
 and 
 array2
 are
 arrays
 of
 typ e
 int
 and
 tha t
 th e
 s ymbolic
 constan t
 SIZE
 has 
 been
defin ed
to
repres ent
th e
size
of
thes e
arra ys
(they
both
ha ve
th e
same
size) .

Also,
suppos e
that
an 
 integer
 variable
 i has
 b een
 d eclared.
 
 Which
 TWO
 of
 the
 followin g
 cod e
 s egments
 resu lts
 in
 array2
 becoming
a
cop y
of
array1?


 









HINT:
 
 All
 of
 th e
 incorrect
 options
 fail
 b ecaus e
 th ey
 attempt
 to
 access
 an
 elemen t
 outs id e
 th e
 bounds
 of
 the
array.
 
 (a)
 (a)

i = -1; while( i < (SIZE -1) (a)
 ){ (b)

i = 0; while( i < SIZE ) { ++i; array2[ i ] = array1[ i ]; (a)
 }
 (c)

i = -1; while( i < (SIZE-1) ) { ++i; array2[ i ] = array1[ i ]; (a)
 }
 (d)

i = 0; while( i < SIZE ) { ++i }
 
 array2[ i ] = array1[ i ]; ++i; ++i }
 array2[ i ] = array1[ i ]; ++i; Answer:

___A___


and


____D___
 CPSC
260
Preparedn ess
Test
 
 
 page
11
of
12
 Q17.
 Suppose
 tha t
 an
 array
 array1
 has
 b een
 declared
 a nd
 is
 of
 typ e
 int
 and
 that
 the
 s ymb olic
 cons tant
 SIZE
 has
 been
 d efin ed
 to
 represen t
 th e
 size
 of
 this
 array.
 Which
 one
 of
 th e
 followin g
 pieces
 of
 cod e
 results
 in
 a
 new
 array
 array2 b ecoming
 a
 cop y
 of
 array1?
 Tha t
 is,
 which
 of
 th e
 followin g
 pieces
 of
 code
is 
eq uivalent
to
th e
correct
cod e
in
th e
previous 
question?
 (a)
 int array2[SIZE]; array2 = array1;
 (b)
 int array2[ SIZE ] = array1;
 (c)
 int array2[ SIZE ] (d)

 All
of
th e
above
 (e) 
 Non e
of
th e
above
 
 Answer:
_____E____
 
 = { array1 };
 CPSC
260
Preparedn ess
Test
 
 page
12
of
12
 The
 n ext
 tw o
 qu estions
 refer
 to
 the
 followin g
 scenario.
 N
 d ifferen t
 positive
 integers
 are
 stored
 in
 th e
 first
 N
 positions
 of
 an
 arra y.
 
 Th e
 firs t
 unus ed
 pos ition
 in
 th e
 arra y
 is
 indicated
 b y
 th e
 va lue
 ‐1
 b ein g
 stored
 in
 th at
 place.

Th e
followin g
p icture
illus trates
a
p ossib le
array:
 
 The
array
 
 Q18.
 Suppos e
 we
 wa nt
 to
 search
 throu gh
 th e
 array
 to
 find
 the
 position
 of
 a
 positive
 integer
 “x”.
 
 We
 start
 by
 lookin g
 at
 th e
 leftmost
 elemen t
 of
 th e
 arra y
 and
 we
 move
 righ t
 until
 we
 find
 “x”
 or
 ‐1.
 
 To
 ach ieve
 th is 
 functionality
th e
n ew
cod e
wou ld:
 (a)
Not
requ ire
an 
additiona l
arra y,
or
a
loop 
(no
arra y,
no
loop).
 (b)
Requ ire
an
add itional
array
but
no
loop
(arra y
bu t
n o
loop) .
 (c)
 Not
 require
 an 
 additiona l
 arra y,
 but
 a t
 least
 on e
 additional
 loop
 wou ld
 b e
 requ ired 
 (loop
 but
 no
 array).
 (d)
Requ ire
an
add itional
array
and
at
least
on e
add itional
loop
(arra y
and 
loop) .
 (e)
Non e
of
th e
above
 Answer:
_____C____
 
 Q19.
 Now
 consid er
 th e
 s ituation
 wh ere
 we
 have
 on e
 array
 with 
 N
 integers
 in
 it
 and
 one
 with
 2N
 integers .
 We
 know
 that
 th e
nu mb er
“x”
 is
rand omly
located
a t
some
pos ition
 in
b oth
arra ys.
(Note
th e
position 
might
 be
 different
 in
 each
 arra y.)
 
 On
 a verage
 th e
 ratio
 of
 the
 amount
 of
 time
 ( i.e
 th e
 nu mb er
 of
 ins tructions 
 executed)
it
ta kes
to
locate
“x”
in 
th e
arra y
of
size
2N
wh en
compared
to
th e
arra y
of
s ize
N
is:

 (a)
 1
(Th e
sa me
amou nt
of
time
is
n eed ed)
 (b)
 2
(Twice
as
much
time
is
n eeded 
to
find 
“x”
in
the
arra y
or
size
2N)
 (c)
 3
(Three
times
as 
mu ch
time
is 
need ed 
to
find
“x”
in
th e
arra y
of
size
2N)
 (d)

 4
(Four
times 
as
much
time
is
n eeded 
to
find 
“x”
in
the
array
of
size
2N)
 (e)



8
(Eight
times
as 
mu ch
time
is
need ed
to
find
“x”
in
th e
arra y
of
s ize
2N)
 
 Answer:
____B_____
 
 12
 
 34
 
 89
 
 18
 
 21
 
 ‐1
 
 ??
 ...
View Full Document

This note was uploaded on 02/02/2010 for the course CPSC CPSC 260 taught by Professor Kelloggbooth during the Spring '10 term at UBC.

Ask a homework question - tutors are online