Introduction To Computer Programming Exam Final B
Introduction To Computer Programming Exam Final B
Final Exam
December 9, 2014
General instructions:
Please wait to open this exam booklet until you are told to do so.
This examination booklet has 20 pages. You also have been issued a bubble sheet.
Write your name, university ID number and date, and sign your name below. Also,
write your name and ID number on your bubble sheet, and fill in the bubbles for your
ID.
The exam is open book and open notes, but is closed electronic device. The only
exception is that you may use an electronic device to display a PDF copy of the book
(all communication must be turned off and no other applications may be used). C
The exam is worth a total of 200 points (and 20% of your final grade). A
You have 2 hours to complete the exam. Be a smart test taker: if you get stuck on one
problem go on to the next. B
Use your bubble sheet to answer all multiple-choice questions. Make sure that the
question number and the bubble row number match when you are answering each
question. Use the provided space in this exam booklet to answer the coding questions.
C
On my honor, I affirm that I have neither given nor received inappropriate aid in the
completion of this exam.
Signature:
Name:
ID Number:
Date:
Question Points Score
Variables, Types and Conversions 16
Mathematical Operators 24
Conditionals and Logic 16
Methods 20
Objects and Classes 28
Loops, Arrays and Search 20
Nested Loops 12
Coding 64
Total: 200
2. (4 points) Given the following block of code. Which line contains an error?
1 i n t foo = 5 ;
2 i n t bar = 4 2 ;
3 d o u b l e foo = 3 . 5 ;
4 d o u b l e baz = 3 . 1 ;
3. (4 points) What type of data would you use to represent the current speed that a
baby is moving an assistive robot?
A. int B. boolean C. double D. String E. Answer not shown
4. (4 points) Given the following block of code. Which line contains an error?
1 i n t foo = 1 7 ;
2 d o u b l e bar = 2 . 7 ;
3 bar = foo * 2 ;
4 foo = bar / 2 ;
int a = 13;
int b = a + 2 * 3;
int c = b / 4 + 5;
double a = 1 8 . 5 ;
double b = a / 2 ;
int a = 11;
double b = a / 2 ;
double c = b * 2 ;
int a = 17;
int b = a − 3;
double c = b / 4. 0 + 3 ;
int j = 17;
int k = 4;
int a = 7;
int b = a / 2;
int c = b * 2;
int p = 49;
int q = 51;
int r = 53;
i f ( r / 5 == 9 )
{
System . out . println ( 1 ) ;
} else i f (p − q > r) {
System . out . println ( 2 ) ;
} e l s e i f ( q % 3 == 0 ) {
System . out . println ( 3 ) ;
} else {
System . out . println ( 4 ) ;
}
double m = 1 7 . 3 ;
double n = 2 . 7 1 ;
if (m > n)
{
System . out . println ( m − 3 . 3 ) ;
} else {
System . out . println ( n + 4 . 1 ) ;
}
int p = 41;
int q = 43;
int r = 47;
int s = 29;
int t = 31;
int u = 37;
p u b l i c s t a t i c i n t bar ( i n t t )
{
return t % 3;
}
p u b l i c s t a t i c i n t baz ( i n t a , i n t b )
{
a = a − 3;
b = a / 2;
return b ;
}
p u b l i c s t a t i c i n t foobar ( i n t j , i n t k )
{
return j + k ;
}
j = foobar ( j , k ) ;
k = foobar ( k , j ) ;
System . out . println ( foobar ( j , k ) ) ;
}
p u b l i c s t a t i c i n t m1 ( i n t a )
{
return a * 2;
}
p u b l i c s t a t i c i n t m2 ( i n t a )
{
r e t u r n m1 ( a ) + a / 2 ;
}
a = m2 ( a ) ;
a = m2 ( a ) ;
p u b l i c s t a t i c String s t r i n g M o d i f i e r ( String s )
{
r e t u r n s . toLowerCas e ( ) + s . toUpperCa se ( ) ;
}
p u b l i c s t a t i c String s t r i n g M o d i f i e r ( String s )
{
s = s . toUpperCas e ( ) ;
return s ;
}
1 p u b l i c c l a s s Point
2 {
3 p r i v a t e double x ;
4 p r i v a t e double y ;
5
6 p u b l i c Point ( d o u b l e xValue , d o u b l e yValue )
7 {
8 x = xValue ;
9 y = yValue ;
10 }
11
12 p u b l i c d o u b l e compute ( Point p )
13 {
14 r e t u r n Math . pow ( x − p . x , 2 ) + Math . pow ( y − p . y , 2 ) ;
15 }
16
17 p u b l i c Point combine ( Point p )
18 {
19 r e t u r n new Point ( p . x + x , p . y + y ) ;
20 }
21
22 p u b l i c String toString ( )
23 {
24 return ”( ” + x + ” , ” + y + ”) ” ;
25 }
26
27 p u b l i c s t a t i c v o i d main ( String [ ] args )
28 {
29 Point p1 = new Point ( 2 , 4 ) ;
30 Point p2 = new Point ( 4 , 7 ) ;
31
32 System . out . println ( p1 . compute ( p2 ) + p2 . compute ( p2 ) ) ;
33
34 System . out . println ( p2 ) ;
35
36 System . out . println ( p2 . combine ( p1 ) ) ;
37 }
38 }
25. (4 points) What type of variable would be used to store a temporary value used to
decide whether one circle is contained within another?
A. local B. class C. final D. instance E. Answer not shown
26. (4 points) What type of variable would be used to store the center of a circle object?
A. local B. class C. final D. instance E. Answer not shown
p u b l i c s t a t i c i n t foo ( i n t [ ] things , i n t a )
{
i n t top = 0 ;
i n t bottom = things . length ;
int k = 0;
w h i l e ( top != bottom )
{
i n t center = ( top+bottom ) / 2 ;
i f ( a == things [ center ] )
{
return k ;
} e l s e i f ( a > things [ center ] )
{
top = center +1;
} else
{
bottom = center ;
}
++k ;
}
return k ;
}
i n t [ ] things = { 2 , 1 , 4 } ;
i n t val = 0 ;
i n t [ ] things = { 3 , 8 , 1 , 1 } ;
i n t [ ] things = { 3 , 1 , 2 } ;
i n t val = 0 ;
p u b l i c c l a s s MyRectang l e {
// We assume t h a t th e s e g m e n ts o f th e r e c t a n g l e a r e e i t h e r
// h o r i z o n t a l o r v e r t i c a l
p r i v a t e double [ ] x ;
p r i v a t e double [ ] y ;
/* *
* x1 , y1 i s th e c o o r d i n a t e o f th e upper l e f t c o r n e r o f th e r e c t a n g l e
* x2 , y2 i s th e c o o r d i n a t e o f th e l o w e r r i g h t c o r n e r o f th e r e c t a n g l e
*/
p u b l i c MyRectang le ( d o u b l e x1 , d o u b l e y1 , d o u b l e x2 , d o u b l e y2 )
{
x = new d o u b l e [ ] { x1 , x2 } ;
y = new d o u b l e [ ] { y1 , y2 } ;
}
}
36. (16 points) Write the following class methods in the space below:
/* *
* @return th e a r e a o f th e r e c t a n g l e
*/
p u b l i c d o u b l e area ( ) {
/* *
* @return th e p e r i m e t e r o f th e r e c t a n g l e
*/
p u b l i c d o u b l e perimeter ( ) {
Using the above method, write a new method that takes as input a single array of
doubles and returns the same doubles in a sorted array. You may only use a single
for() loop.
Examples:
(2.4, 3.5, 7.1, 2.0, 9.8) → (2.0, 2.4, 3.5, 7.1, 9.8)
(1.1, 2.7, 5.8, 1.0) → (1.0, 1.1., 2.7, 5.8)
###
#######
##
######
###
####