CS 116 Proficiency Exam / Lab 01
Instructions
We will be going over parts I-III together in class — your only submission for this lab will be your solutions to part IV, which should be committed and pushed to your BitBucket repository by the lab due date.
Unlike future labs, there is no grading rubric for the programming problems on this exam — we're just interested in seeing how you approach the questions, and will use submissions as a gauge for how/what material is covered in class (the machine problems will be given a nominal point value of 5 points each, however, contingent on submission and reasonable effort).
I. Definitions / Short answer
- Give an example of a reference type.
- What does it mean to say that method arguments are passed-by-value?
- Why would you declare a method
static
? Why would you not? - Give an example of some
static
methods you've used from the Java library. - What keyword is used to allocate an instance of a class (aka an object)?
- What is the purpose of an
equals
method? - What is the primary difference between a
while
loop anddo-while
loop? - Under what circumstances are
for
loops best used? - Is a
switch
statement always a suitable replacement forif/else
statements? Why or why not? - What distinguishes "service" classes from "client" classes?
II. Evaluation
The questions in this section are based on the following declarations:
int x=5, y=10, z=15;
What do the following expressions evaluate to?
x + 2 * z - y
(x / y) * y
y % (z / x)
((x < y) && (x >= z)) || (y < z)
x++ + y
++z
III. Multiple Choice
- Which of the following is a syntactically correct constructor
declaration for a class named
Foo
?a.
public new() { ... }
b.public Foo() { ... }
c.public void Foo() { ... }
d.public int Foo() { ... }
Questions 8 and 9 are based on the following code:
class Container { int val; public Container(int v) { val = v; } } public class Swapper { public static void swapInts(int x, int y) { int tmp = x; x = y; y = tmp; } public static void swapObjs(Container c1, Container c2) { int tmp = c1.val; c1.val = c2.val; c2.val = tmp; } public static void main(String[] args) { int a = 5, b = 10; Container c = new Container(5), d = new Container(10); swapInts(a, b); // L1 swapObjs(c, d); // L2 } }
- Following the execution of the line marked
L1
, what are the values of the variablesa
andb
(in that order)?a. 5 and 10
b. 10 and 5
c. 5 and 5
d. 10 and 10 - Following the execution of the line marked
L2
, what are the values ofc.val
andd.val
(in that order)?a. 5 and 10
b. 10 and 5
c. 5 and 5
d. 10 and 10 - Which of the following lines correctly declares and allocates an array of
10 integers?
a.
int iarr = new [10] int;
b.int iarr[] = new int(10);
c.int[] iarr = new int[10];
d.int iarr[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
IV. Machine Problems
- Write a program that implements a "guessing game" — it should work
as follows:
- when started, it will generate and save a "secret" number between 0 and
100 (you should use
java.util.Random
'snextInt
method to generate a random number) - the user will then be prompted to enter a guess; if the guess is
incorrect, one of the following will be output by the program:
- "Nope!", if this is the player's first guess
- "Warmer", if the guess is closer to the secret than the previous guess
- "Colder", if the guess is further away from the secret than the previous guess
if the guess is correct, the program should just print out a congratulatory message and terminate.
Your program should be saved in a file named "GuessingGame.java".
- when started, it will generate and save a "secret" number between 0 and
100 (you should use
- Write a program that reads in 10 separate positive integers and prints out
their mode — i.e., the most frequently occuring number. You can assume
that there will never be a tie. E.g., with input
1 2 5 1 2 2 3 5 2 3
, the output should be2
, as it appears the most times. Your program should be saved in a file named "Mode.java".