Remark: this is copied from last quarter. It will change with time, so keep looking back.
Chapter 1 is mostly tested using true/false, short answer or multiple choice questions. Your best bet is to read the text carefully and to follow some of the links. Some definitions come up, as do some questions involving who did what in what time period.
There is always something involving file names and how to compile or run a program from the command line. (pg. 13)
FYI: anything covered in the labs or homework is fair game.
Write the following mathematical expressions in Java syntax. You may assume that a, b, c, and x have already been declared.

![]()
Evaluate the following Java expressions:
6 - 4 / 3
6 - 4 + 3
6 - (4 + 3)
3 + 7 % 4
Math.abs(-4)
Math.floor(-3.1)
Actually, the last two in the list above and the square root in the previous part are all really chapter 3 questions, but they've often been tested together.
Some questions may use the compound (or shorthand) assignment operators. (pg. 23) +=, -+, *=, /+, %=
What output does the following program generate?
public class T1Q2
{
public static void main(String [] args)
{
int n = 5;
System.out.println("First line, n = " + n);
n += 3;
System.out.println("Second line, n = " + n);
n *= 2;
System.out.println("Third line, n = " + n);
n -= 4;
System.out.println("Fourth line, n = " + n);
n %= 6;
System.out.println("Fifth line, n = " + n);
}
}
Another typical question asks you to write some code similar
to what you've done in the lab and homework using the
java.util.Scanner class to read in some data and
perform a computation.
You'll need to understand how the concatenation operator +
works with String instances and primitives. (pg. 16-17)
Some information questions often ask which of a collection of groups of characters constitute legal Java identifiers. (pg.13-14)
Some typical questions involve the difference between
print and println.
Information questions often check to see if you know the primitive types andreserved words. See documentation.
I can't really ask you to write an applet on a test, but you should at least be aware that they are programs meant to be run from within a browser.
What output does the following program generate?
public class TestQ1
{
public static void main(String [] args)
{
String str = new String("aBcDeF");
int cLoc = str.indexOf("c");
String lower = str.toLowerCase();
String rest = str.substring(4);
System.out.println("str = " + str);
System.out.println("cLoc = " + cLoc);
System.out.println("str.charAt(3) = " + str.charAt(3));
System.out.println("lower = " + lower);
System.out.println("rest = " + rest);
}
}
Fill in the blank below, so that the following code would assign a
pseudorandom integer from 1 through 100, inclusive, to num.
Random rand = new Random(); // you get this for free int num = _____________________________________________________;
See page 40.
What output does the following program generate?
import static java.lang.System.out;
import static java.text.NumberFormat.*;
import static java.util.Locale.*;
import java.text.*;
public class T2Q5
{
public static void main(String [] args)
{
long longNum = 3L;
NumberFormat pct = getPercentInstance();
NumberFormat cash = getCurrencyInstance(US);
DecimalFormat fmt1 = new DecimalFormat("0.0#");
DecimalFormat fmt2 = new DecimalFormat("00");
out.println("a.) " + pct.format(2.1));
out.println("b.) " + cash.format(2.1));
out.println("c.) " + fmt1.format(0.111));
out.println("d.) " + fmt1.format(0.1));
out.println("e.) " + fmt2.format(longNum));
}
}
See pages 42-44
Evaluation of expressions or converting an expression from mathematical
to Java syntax can be expected, but with the Math class
included (pages 41-42 and look at the APIs.) Be sure to
understand the usage of the following methods and fields:
PI
E
abs
ceil
floor
max
min
pow
round
sqrt
There may be something involving the wrapper classes 44-46.
Make absolutely sure that you understand every line of code in
the
Multiply and
UseMultiply
examples. If you understand how objects communicate, the course will
make sense to you. If you don't, it might as well be in Martian.
Since String is so important, it would be wise to
review its methods.
Below is a typical question used to see if the student understands how to use objects.
Consider the following class:
public class A {
private int a;
public A(int initValue) { a = initValue; }
public int getA() { return(a); }
public void setA(int updateValue) { a = updateValue; }
}
1.)(9) Fill in the blanks so that the following program performs the actions indicated in the comments.
public class Quiz3
{
public static void main(String [] args)
{
// Create an instance of A called objectA that stores
// the int value 3
A objectA = ______________________
// Display the value stored by objectA
System.out.println("objectA contains " + __________________
// Change the value stored by objectA to the integer 7
_____________________________
// Display the value stored by objectA
System.out.println("objectA contains " + __________________
}
}
See the book for examples of writing methods. In addition to
Multiply, take a look at
MyInt and
Die.
In order to see it you understand these, try two labs from last Summer.
In the first one, the class
D.java is a working class and your task is to fill in the
blanks to get the client
UseD.java to work correctly. Put them in the same directory
and try to compile them both. If they compile and make sense when they
run, you've done the calls properly.
The second lab does things in the opposite order. The class C.java is incomplete. Your job
is to implement the methods so that agree with the comments. When you
compile both it and the client
UseC.java
in the same directory, then it should work properly.
Look at the exercises on page 96 for sample questions involving logical expressions and logical operators.
You will be expected to understand how one can
break down a String into tokens.
There may be some information questions on the GUI techniques we've covered, but I won't ask you to write any code.
Consider the following code fragment:
String st1 = "x", st2 = "x", st3 = "y";
int ans1 = st1.compareTo(st2);
int ans2 = st2.compareTo(st3);
int ans3 = st3.compareTo(st2);
Check one in each part:
ans1 is positive____ negative____ zero____
ans2 is positive____ negative____ zero____
ans3 is positive____ negative____ zero____
What output does the following program generate?
public class Loops
{
public static void main(String [] args)
{
int n = 1;
System.out.println("Before first loop, n = " + n);
while(n > 5) {
System.out.println("In first loop, n = " + n);
n++;
}
System.out.println("After first loop, n = " + n);
do {
System.out.println("In second loop, n = " + n);
n++;
} while(n > 5);
System.out.println("After second loop, n = " + n);
}
}
What output does the following program generate?
public class Grader
{
public static void main(String [] args)
{
char grade;
int score = 102;
while (score >= 50) {
System.out.print("Score: " + score);
if(score >= 90) {
grade = 'A';
} else if(score >= 80) {
grade = 'B';
} else if(score >= 70) {
grade = 'C';
} else if(score >= 60) {
grade = 'D';
} else {
grade = 'F';
}
System.out.println(" Grade: " + grade);
score -= 7; // update the score
} // of the while
} // of main
} // of class
what output does the following program generate?
public class Loops
{
public static void main(String [] args)
{
System.out.println("a.)"); // label me!
int n = 0;
while(n < 5) {
System.out.print(n + " ");
n += 2;
}
System.out.println();
System.out.println("After loop, n = " + n);
System.out.println("b.)"); // label me!
for(char ch = 'a'; ch <= 'd'; ch++) {
System.out.print(ch);
}
System.out.println();
System.out.println("c.)"); // label me!
n = 0;
do {
System.out.println("n = " + n);
n--;
} while(n > 0);
System.out.println("After loop, n = " + n);
}
}
Write a program that displays all of the even integers from 2 through 1000, one to a line. Hint: I don't give you a lot of room for this, so a loop is in order.
You will be asked to write a static function or method,
that could be called from the main.
You should understand the difference between mutable and
immutable classes (page 105). Mutable classes are ones that allow
the user to change instance data stored by an object after that object
has been created. This is usually done with mutator methods like the
setDbl() method in quiz 3. Immutable classes are not mutable.
You will certainly see questions that test whether you understand the difference between passing a primitive by value (pages 101-102) and a reference to an object (pages 104-105).
There are always some questions to see if you understand how functions return values to the calling method.
Write a public static function isMultOf3
that returns true if its int argument
n is a multiple of 3 and returns false
otherwise.
Look at exercises on pages 112 and 113 in the text.
What output does the following program generate?
public class FunctionCalls
{
public static void main(String [] args)
{
int n = 5, m = 6;
System.out.println("Before calls: n = " + n + " m = " + m);
System.out.println("foo(" + n + "," + m + ") = " + foo(n,m));
System.out.println("bar(" + n + "," + m + ") = " + bar(n,m));
System.out.println("After calls: n = " + n + " m = " + m);
}
public static int foo(int a, int b)
{
return(a+b*2);
}
public static int bar(int a, int b)
{
a += 3;
b--;
return(a+b);
}
}
What output does the following program generate?
public class Value
{
public static void main(String [] args)
{
int n = 5;
System.out.println("Before call: n = " + n);
foo(n);
System.out.println("After call: n = " + n);
}
public static void foo(int k)
{
System.out.println("in foo: k = " + k);
k *= 3;
System.out.println("in foo: k = " + k);
}
}
Write implementations for the getValue,
toString and setValue methods.
All methods in this class are public and none of
them are static.
public class MyInt
{
// the value this object manages
private int value;
/**
* Constructor for objects of class MyInt
* @param initValue the value passed to the constructor
*/
public MyInt(int initValue)
{
value = initValue;
}
/**
* getValue returns the value stored in this object
* @return the value stored in this object
*/
// Write the implementation here:
/**
* setValue updates the value stored in this object
* @param newValue the new value to be stored by this object
*/
// Write the implementation here:
/**
* toString overloads the toString method from class Object
* returns a String representation of the value stored in his object
* @return a String representation of the value stored in his object
*/
// Write the implementation here:
}
For this question, you may assume that your answer to the previous
question is correct, and that all methods work as the documentation
suggests. What output does the following program generate? You may assume
that it is in the same directory as the MyInt program above.
public class Reference
{
public static void main(String [] args)
{
MyInt mine = new MyInt(4);
System.out.println("Before call, mine contains " + mine.getValue());
foo(mine);
System.out.println("After call, mine contains " + mine.getValue());
}
public static void foo(MyInt parm)
{
System.out.println("In foo, parm contains " + parm.getValue());
parm.setValue(19);
System.out.println("In foo, parm contains " + parm.getValue());
}
}
A similar problem that combines the two ways to pass something follows:
import static java.lang.System.out;
public class PassBoth
{
public static void main(String [] args)
{
// Create new instance of MyInt containing 3
MyInt obj = new MyInt(3);
int prim = 3;
out.println("obj " + obj.getValue() + " prim " + prim);
foo(obj);
bar(prim);
out.println("obj " + obj.getValue() + " prim " + prim);
}
public static void foo(MyInt parm)
{
out.println("In foo:");
out.println("parm contains " + parm.getValue());
parm.setValue(8);
out.println("parm contains " + parm.getValue());
}
public static void bar(int parm)
{
out.println("In bar:");
out.println("parm contains " + parm);
parm = 8;
out.println("parm contains " + parm);
}
}
Material from chapter 7 can only be in the form of information
questions. You should have a basic idea of what layout managers do,
and which ones we've looked at, but I don't expect you to write code
using them in a closed book test. You should be aware, for example,
of the difference between BoxLayout,
FlowLayout and BorderLayout, in terms of how
they place Components in a window, but not the details of
how to call the constructor.
Questions involving arrays, would most likely involve legal indices and access methods. These could involve either information questions or display the output questions. An example program for the latter type of question follows:
import static java.lang.System.out;
public class Arr
{
public static void main(String [] args)
{
int [] a = {5, 4, 3, 2, 1};
for(int n = 0; n < a.length; n++) {
out.println("a[" + n + "] = " + a[n]);
}
}
}
Last modified: 10/18/09, WKA