This exam is closed book, closed notes, closed classmates.
You have 180 minutes to complete it, though I expect if you are well prepared
you will need no more than 120 minutes.
Please put all answers on the test sheets. Make sure your answer is
clearly marked and easily distinguished from work you do to get to the
solution. You should show your work for partial credit.
This especially applies to the program execution simulations.
I showed you in class how to do this in a way that will assist you
in getting the correct answer, as well as allow partial credit.
When done place your exam face down on the lecture table up front.
Please print your name clearly below, and sign when you hand in
the exam, signifying your adherence to the honor code and attesting to
the work being yours alone.
Name (print CLEARLY) -------------------------------------------------------- Signed ---------------------------------------------------------------------
(a) < ul > < /ul > (b) < p > < /p > (c) < pre > < /pre > (d) < ol > < /ol > answer: __C_______________
(a) < ul > < /ul > (b) < p > < /p > (c) < pre > < /pre > (d) < ol > < /ol > answer: __A_______________
(a) in the CPU, get an instruction from memory, decode it, get data from
memory, compute and return the result, then do it all again
(b) the process by which a "for loop" computes on an array
(c) the process of getting a program from a source file, compiling it
to bytecode, then running the virtual machine to execute it
(d) the process of getting command line arguments into variables, then
passing them to a method or function call
answer: __A_____________
(a) in the head section (b) between the head section and the body section (c) right after the < h1 > header in the body section (d) at the end of the body section answer: __D_____________
(a) < title > < /title > (b) < br > < /br > (c) < table > < /table > (d) < head > < /head > answer: __A_______________
(a) strong variable typing (b) while loops (c) freedom from logical errors (d) functions (e) arrays answer: __A_____________
(a) a variable that is created when a method runs, and vanishes when
the method ends
(b) a variable declared in a class such that each object created from
that class shares a single "global" copy
(c) a variable that is created automatically by the compiler to contain
the name of a class at runtime
(d) a variable declared in a class such that each object created from
that class gets its own copy
answer: __B_____________
(a) a method used in compilers to implement mathematical division
for double length floating point numbers
(b) an approach to program writing that requires comments and code
to be in the same source file
(c) a programming technique in which the code for a function (method)
involves calling the function itself
(d) a problem solving strategy where the problem is too large to solve
directly so it is broken into smaller solvable problems
answer: __D_____________
(a) variables and assignment of values to them (b) command line arguments (c) loops... repetition of blocks of code (d) selection... conditional statements (e) expressions, and retrieving data values from storage (f) functions... named abstracted blocks of code answer: __B_____________
(a) inheritance saves code development by class reuse (b) information hiding with variables wrapped in classes (c) objects created dynamically when the program is run (d) strong typing ensures freedom from logical errors answer: __D_____________
10110111
answer: ________________________
(a) a variable declared in a class such that each object created from
that class gets its own copy
(b) a variable that is allowed to contain data that does not match
its type declaration
(c) a variable that is created when a method runs, and vanishes when
the method ends
(d) a variable declared in a class such that each object created from
that class shares a single "global" copy
answer: __A_____________
(a) a problem solving strategy where the problem is too large to solve
directly so it is broken into smaller solvable problems
(b) a method used in compilers to implement mathematical division
for double length floating point numbers
(c) a programming technique in which the code for a function (method)
involves calling the function itself
(d) an approach to program writing that requires comments and code
to be in the same source file
answer: __C_____________
final int MAX = 5;
int num = 4;
int k = 0; // loop index
while (k < MAX) {
num = num + k;
k++;
}
System.out.println(num);
answer: __14______________
class DemoConcepts {
int x = 0;
public int foo (int p) {
x = 2;
int x;
x = p;
p = 18;
return x;
}
public static void main (String[] args) {
int p = 0;
x = foo(24);
System.out.println("x: " + x+", p: "+p);
}
}
answer: ___ x: 24, p: 0 _______
int x = 3;
int f = 8;
if (x < 4) { x = x + 1; }
else if (x == 4) { x = x - 2 ; }
else if (x > 1) { x = x * 3; }
else { x = -8 ; }
if (f < 9) { f = f + 1; }
if (f == 8) { f = f - 2 ; }
if (f < 7) { f = f * 3; }
else { f = - f ; }
System.out.println("x: " + x + ", f: " + f);
answer: ___ x:4, f: -9 _______
public class NumCrunch {
public static int num = 50;
int k = 4;
NumCrunch() { k = num; num--; }
public int getK () { return k; }
public int getN () { return num; }
public double munch (double num) {
return num + num;
}
}
public class SomeThing {
public static void main (String[] args) {
NumCrunch nc1 = new NumCrunch();
NumCrunch nc2 = new NumCrunch();
NumCrunch nc3 = new NumCrunch();
nc1.munch(5);
nc2.munch(8);
System.out.println( nc2.getK()+", "+nc1.getN() );
}
}
answer: __ 49, 47 ___________
var i; // loop index
var flag = false;
for (i=0; i<5; i++) {
flag = !flag ;
}
alert(flag);
answer: __ true _________
-- create three double variables named vOne, vTwo, and vThree.
-- give these variables values from command line arguments.
-- determine which of the three variables contains the smallest value
and print out that smallest value (if two or more of the variables
happen to contain the same value, I don't care which one actually
has its value printed out).
However, the program does not work correctly. There is one line
that needs to be different. First indicate which line number is
incorrect, and then give the correct code so that the program
correctly computes the smallest value.
Use the space below to show your work in figuring out how the program works, for partial credit.
incorrect line #: __09 ___________
correct line: } else if (vTwo <= vOne && vTwo <= vThree) {
^^
logical AND
01: public class Smallest {
02: public static void main (String[] args) {
03: double vOne, vTwo, vThree;
04: vOne = Double.parseDouble(args[0]);
05: vTwo = Double.parseDouble(args[1]);
06: vThree = Double.parseDouble(args[2]);
07: if (vOne <= vTwo && vOne <= vThree) {
08: System.out.println("smallest value is " + vOne);
09: } else if (vTwo <= vOne || vTwo <= vThree) {
10: System.out.println("smallest value is " + vTwo);
11: } else if (vThree <= vOne && vThree <= vTwo) {
12: System.out.println("smallest value is " + vThree);
13: } else {
14: System.out.println("this case is impossible!!");
15: }
16: }
17: }
-- Create an array named A that contains integers; the array A
has length 25.
-- Fill the array with random integers between 0 and 100; the random
numbers do not have to be unique.
-- Then compute and print out the sum of the numbers in the array A.
However, the program does not work correctly. There is one line
that needs to be different. First indicate which line number is
incorrect, and then give the correct code so that the program
correctly computes the sum.
Use the space below to show your work in figuring out how the program works, for partial credit.
incorrect line #: __ 12 ___________
correct line: for (int k=0; k<MAX; k++) {
^
don't go beyond the end of the array
01: import java.util.*;
02: public class Summer {
03: public static void main (String[] args) {
04: final int MAX = 25;
05: final int TOP = 100;
06: int sum = 0;
07: int A[] = new int[MAX];
08: Random gen = new Random();
09: for (int i=0; i
- (10%)
For this problem you will write a single line of a program in Java.
A software engineer has written the program you see below; he told me
the program's behavior is as follows:
-- To read a single command line argument (an integer) and put
that value into a variable called N
-- Then compute and print the value of N! (N factorial).
Remember, factorial is defined as: n! = n * (n-1) * (n-2) * ... * 2 * 1
However, the program does not work correctly. There is one line
that needs to be different. First indicate which line number is
incorrect, and then give the correct code so that the program
correctly computes factorial.
Use the space below to show your work in figuring out how the program
works, for partial credit.
incorrect line #: __ 04 _________
correct line: int fac = 1;
^
to accumulate with *, you have to init to 1
01: public class Factorial {
02: public static void main (String[] args) {
03: int N;
04: int fac = 0;
05: N = Integer.parseInt(args[0]);
06: for (int k=1; k<=N; k++) {
07: fac = fac * k;
08: }
09: System.out.println(N + " factorial is " + fac);
10: }
11: }