topic 1 java review test
TRANSCRIPT
-
7/21/2019 Topic 1 Java Review Test
1/12
Topic Test
Topic 1 - Java Review
Multiple Choice. Pick the best response to each question.
1. What is the output of the following coe seg!ent"
int x;
x = 1 + 3 * 2;
x = x * 2;System.out.println(x);
#. 7 $. 14 C. 16 %. 28 &. 'one of these
(. What is the output of the following coe seg!ent"
int x = 7;
int y = 3;
y++;
x--;
int z = x * y;
System.out.println(x + + y + + z);
#. 8 2 16 $. 7 3 21 C. 6 4 1! %. 6 4 24 &. 'one of these
). What is the output of the following coe seg!ent"
int x = 14;
int y = 4;
int z = x " y;
System.out.println(z);
#. 3 $. 3.# C. 4 %. co!pile error &. 'one of these
*. What is the output of the following coe seg!ent"
int x = 3;
$ou%le & = 4.!;
$ou%le % = x " &;
System.out.println(%);
#. ! $. !.7# C. 1 %. co!pile error &. 'one of these
+. What is the output of the following coe seg!ent"
$ou%le & = 3!.!;
$ou%le % = 8.!;
int x = (int)( & " % );
System.out.println(x);
#. 4 $. 3.7# C. 3 %. co!pile error &. 'one of these
Topic TestTopic 1 - Java Review
1
-
7/21/2019 Topic 1 Java Review Test
2/12
Topic Test
Topic 1 - Java Review
,. What is the output of the following coe seg!ent"
int x = 12;
int y = x;
x++;
x += #;System.out.println(x + + y);
#. 18 12 $. 18 18 C. 12 18 %. 18 13 &. 'one of these
or questions an / consier !etho &$o.
pu%li' oi$ &$o(int x int y)
x = y * 2;
y = x 3;
System.out.println( x + + y );
,
. What is the output of the following coe seg!ent"
&$o(2 1);
#. 2 1 $. 2 -1 C. 1 2 %. 1 -2 &. 'one of these
/. What is the output of the following coe seg!ent"
int & = 3;
int % = 2;
&$o(& %);
System.out.println( & + + % );
#. 3 ! 3 !
$. 3 ! 3 2
C. 4 1 4 1
%. 4 1 2 3
&. 'one of these
0. Consier the following coe seg!ent
int x;
int y;
""'o$e to initi&lize x &n$ y
i( x + y ! )
""%o$y o i st&tement
Which of the following are equivalent to the boolean e2pression x + y !"
3. x ! // y !
33. x ! 00 y !
333. x y
#. 3 onl4 $. 33 onl4 C. 333 onl4 %. 3 an 333 onl4 &. 'one of these
Topic TestTopic 1 - Java Review
(
-
7/21/2019 Topic 1 Java Review Test
3/12
Topic Test
Topic 1 - Java Review
or questions 15 6 1) consier the following !ethos
pu%li' int tempest(int x int y)
return (x + y) " 2;
,
pu%li' int tempest(int x)
return x * x + 1;
,
pu%li' int sre( int x )
int y = tempest(x - 1 x * 3);
int result = !;
i( (y 2) == ! )
result = y " 2;
else
result = y;
return result;
,
15. What is the output of the following coe seg!ent"
System.out.println( tempest(# 3) );
#. # 3 $. x y C. 1! %. 4 &. 'one of these
11. What is the output of the following coe seg!ent"
System.out.println( tempest(3) );
#. 'othing $. 3 C. 3.! %. co!pile error &. 'one of these
1(. What is the output of the following coe seg!ent"
System.out.println( tempest( tempest(2) tempest(24) );
#. 'othing $. 1! C. 4 %. co!pile error &. 'one of these
1). What is the output of the following coe seg!ent"
System.out.println( sre(#) );
#. 4 $. # C. %. 1! &. 'one of these
Topic TestTopic 1 - Java Review
)
-
7/21/2019 Topic 1 Java Review Test
4/12
Topic Test
Topic 1 - Java Review
1*. Consier the following coe seg!ent
int x;
int y;
"" 'o$e to initi&lize x &n$ y
i( x y )System.out.println(x);
else i( y x )
System.out.println(y);
else
System.out.println(x &n$ y);
3f the above seg!ent is e2ecute one ti!e how !an4 ti!es coul the println !etho be calle"
#. 5 $. 1 C. ( %. unknown &. 'one of these
1+. What is the output of the following coe seg!ent"
int x = 7!;
i( x ! )
System.out.print(&);
i( x 1! )
System.out.print(%);
i( x 1!! )
System.out.print(');
i( x ! // x 5 1!! )
System.out.print($);
#. &%$ $. & C. &% %. &%'$ &. 'one of these
1,. Consier the following coe seg!ent
int x;
"" 'o$e to initi&lize x
i( x 5 ! 00 x 1!!)
System.out.print(out o r&ne);
Which of the following values of 2 will cause the !essage 7out o r&ne7 to be printe"
. !
. 1!!
. 1#!
#. 3 onl4 $. 33 onl4 C. 333 onl4 %. 3 an 33 onl4 &. 'one of these
Topic TestTopic 1 - Java Review
*
-
7/21/2019 Topic 1 Java Review Test
5/12
Topic Test
Topic 1 - Java Review
1. What is the output of the following coe seg!ent"
int tot&l = !;
in&l int 9: = 1!;
or(int i = !; i 5 9:; i++)
tot&l++;,
System.out.println( tot&l );
#. unknown $. ! C. %. 1! &. 'one of these
1/. What is the output of the following coe seg!ent"
int tot&l = !;
in&l int 9: = 1!;
or(int i = !; i 5 9:; i++)
or(int = !; 5 9:; ++)
tot&l++;
,,
System.out.println( tot&l );
#. 1! $. 1!! C. ! %. &. 'one of these
10. What is the output of the following coe seg!ent"
int tot&l = !;
in&l int 9: = #;
or(int i = !; i 5 9:; i++)
or(int = 9:; i; --)
tot&l++;,
,
System.out.println( tot&l );
#. ## $. 1# C. 12! %. #! &. 'one of these
(5. What is the output of the following coe seg!ent"
int x = 8;
int 'ount = !;
ile( x
-
7/21/2019 Topic 1 Java Review Test
6/12
Topic Test
Topic 1 - Java Review
(1. 3n the coe seg!ent fro! question (5 assu!e the first line is change to
int x = 3;
What is the output of the following coe seg!ent assu!ing nothing else is change"
#. . the loop never ter!inates $. 8 C. 6 %. 7 &. 'one of these
((. 3n the coe seg!ent fro! question (5 which of the following are logicall4 equivalent to the
while loop8s boolean e2pression9 x list = ne int#>;
or(int i = !; i 5 list.lent; i++)
listi> = i * 3;
,
System.out.println( list3> );
#. 3 $. 0 C. 12 %. ! &. 'one of these
(*. What is the output of the following coe seg!ent"
int> list = # 2 12 ! 11 2,;
System.out.println( list list1> + list3> > );
#. 12 $. 2 C. ! %. #n #rra43ne2:ut:f$ouns&2ception occurs &. 'one of these
(+. Consier the following coe seg!ent
int> list;
"" 'o$e to initi&lize list
or(int i = !; i 5 2!; i++)
listi> = #;,
When will the above coe result in an #rra43ne2:ut:f$ouns&2ception"
#. alwa4s $. never C. when listhas (5 or
!ore ele!ents
%. when listhas fewer
than (5 ele!ents
&. 'one of these
Topic TestTopic 1 - Java Review
,
-
7/21/2019 Topic 1 Java Review Test
7/12
Topic Test
Topic 1 - Java Review
(,. Consier the following coe seg!ent
int> list;
int temp;
"" 'o$e to initi&lize list
or(int i = !; i 5 list.lent; i++)
temp = listi>;
listi> = list list.lent i>;
list list.lent i > = temp;
,
#ssu!e listis initiale to hol the following values
5 1 ( ) *
11 ) +
What are the contents of listafter the coe seg!ent e2ecutes"
#.
+ ) 11
$.
11 ) +
C.
11 ) +
%.
+ + + + +
&. 'one of these
(. What is the output of the following coe seg!ent"
Strin s1 = on;
Strin s2 = ?orns;
Strin s3 = s1 + s2;
System.out.println( s3 );
#. on $. ?orns C. on+?orns %. s3 &. 'one of these
(/. What is the output of the following coe seg!ent"
Strin s1 = @/9;
int x = #;
int y = 1;
Strin s3 = s1 + x + y;
System.out.println( s3 );
#. @/9#1 $. @/96 C. @/9xy %. co!pile error &. 'one of these
Topic TestTopic 1 - Java Review
-
7/21/2019 Topic 1 Java Review Test
8/12
Topic Test
Topic 1 - Java Review
(0. What is the output of the following coe seg!ent"
Strin s1 = @/9;
int x = #;
int y = 1;
Strin s3 = x + y + s1;
System.out.println( s3 );
#. #1@/9 $. 6@/9 C. xy@/9 %. co!pile error &. 'one of these
)5. What is the output of the following coe seg!ent"
Strin s1 = A&nt&sti';
Strin s2 = s1.su%Strin(!3);
System.out.println( s2 );
#. A&nt&sti' $. A&nt C. A&n %. runti!e error &. 'one of these
Topic TestTopic 1 - Java Review
/
-
7/21/2019 Topic 1 Java Review Test
9/12
Topic Test
Topic 1 - Java Review
ree Response ;uestions
1. Write a !etho that has a single integer para!eter greater than 5. The !etho returns -1 if the
para!eter is 7eficient79 5 if it is 7perfect79 an 1 if it is 7e2cessive.7
# perfect nu!ber is one that is equal to the su! of its factors e2cluing itself.
The factors of , are 19(9)9,. &2cluing itself the su! of ,8s factors are 1 < ( = ) = ,.
, is a perfect nu!ber.
# nu!ber is eficient if the su! of its factors e2cluing itself are less than the nu!ber. / is
eficient because its factors are 19(9*9/. &2cluing itself the su! of /8s factors are 1 < ( < * = .
/ is eficient.
# nu!ber is e2cessive if the su! of its factors e2cluing itself are greater than the nu!ber. 1( is
e2cessive because its factors are 19 (9 )9 *9 ,9 1(.
&2cluing itself the su! of 1(8s factors are 1 < ( < ) < * < , = 1,.
1( is e2cessive.
pu%li' int num%er:ype(int num)
"" preB num !
"" postB return -1 i nums is $ei'ient ! i it is pere't or 1
"" i it ex'essie
Topic TestTopic 1 - Java Review
0
-
7/21/2019 Topic 1 Java Review Test
10/12
Topic Test
Topic 1 - Java Review
Topic TestTopic 1 - Java Review
15
-
7/21/2019 Topic 1 Java Review Test
11/12
Topic Test
Topic 1 - Java Review
(. Consier an arra4 of ints that represents !easure!ents of a person8s bloo sugar level over a
perio of ti!e. #ssu!e reaings are taken ever4 1+ !inutes. The reaings are store in the arra4
in the orer the4 are taken. Write a !etho that returns the length of the longest consecutive
series of reaings that are greater than or equal to so!e threshol value.
5 1 ( ) * + , / 0 15 11
1+5 1(+ 1+5 1++ 1*+ 1*5 1)+ 1(+ 1,5 1,+ 1++ 1(+
or e2a!ple9 assu!e the threshol is sent an argu!ent equal to 1+5. #n4 reaings greater than or
equal to the value of the threshol are consiere high. >our !etho !ust fin the length of the
longest consecutive series of reaings that are greater than or equal to the threshol. 3n the above
e2a!ple there are three series of reaings greater than or equal to the threshol9 ?5@9 ?(-)@9 ?/-15@.
The longest of these is three reaings so the !etho !ust return ). 3f no reaings are greater than
or equal to the threshol then the !etho !ust return 5.
Co!plete the following !etho
pu%li' int lonestStre&C(int> re&$ins int tresol$)
"" preB re&$ins
-
7/21/2019 Topic 1 Java Review Test
12/12
Topic Test
Topic 1 - Java Review
Topic TestTopic 1 - Java Review
1(