CS 0401 Fall 2011
Exam 1 Practice
Questions
These
are some questions that demonstrate the types of questions that will be on the
midterm exam. Take a look at them
and give each a good try before looking at the
solutions. Note that the
actual exam will not have this exact amount of questions and the questions of
each type may not be in proportion to those shown here. Also remember that the topics on the
actual exam will cover all of the material covered thus far in the term. Refer to the study
guide for more information on preparing for the exam.
Fill in the Blanks: Fill in the
most appropriate word / phrase in each blank
1) The words class, while and if are examples of Java
____________________________ – they have a special meaning in the program
and cannot be used in any other way.
2) Java variables fall into two
categories – _____________________________, which are simple values
stored directly in the memory location associated with the variable, and
________________________________, which store addresses of objects stored
elsewhere in memory.
3) If boolean variable A = true, B = false and C = true,
then the expression A && B || C equals ___________________________________.
4) Java _______________________
methods are not associated with any
object, whereas Java ________________________ methods are part of objects and
can only be called through an object.
5) The two general ways of
accessing a Java array are _________________________ (accessing an arbitrary location)
and _____________________________ (going through the entire array).
True / False: Indicate whether
each of the following statements is true or false. For false statements explain WHY they are false.
1) Java class names must begin with
an uppercase letter, while variable names must begin with a lower case letter.
2) The following Java statement is
legal: float
X = 3.5;
3) The scope of Java method
variables is the point in the method that they are declared up to the end
of the block in which they are declared.
4) In order to effectively use some
new class, Foo, I need to know what instance
variables Foo has and how they are manipulated.
5) The declaration int [] A;
creates an array of integers.
Short Answers:
Define / discuss / explain each of the following in detail.
1)
We
discussed compiler/syntax errors and
logic errors. Explain what each is and how they
differ. Also give an example of
each.
2)
One
of the debugging issues that we discussed is the infinite loop. Explain
what this is and explain how a while loop can be an infinite loop. Be specific.
3)
We
said in lecture that object-oriented programming consists of 3 primary
ideas. List these ideas and briefly define each.
Trace
1)
Give
all output produced by the execution
of the Java program below. Clearly
mark your output by drawing a box around it. Show your work for partial credit.
|
public class
pracTrace1 { public
static void
loopy(int n) { int i
= 1; while (i
< n) { for (int j = 1; j <= i; j++) { System.out.print(i + "," + j + "
: "); } i += 2; System.out.println(); } System.out.println(); } public
static void
doopy(int
n) { int i
= n; do { System.out.println(i + " "); i += 1; i = i
/ 2; } while (i
> 1); System.out.println(); } public
static void
main(String [] args) { loopy(8); doopy(27); } } |
Coding
1)
Write
a single complete Java program that does the following:
a)
Prompts
the user to enter a positive integer and reads it in. Any numbers 0 or less should be rejected and the user must
re-enter.
b)
Prompts
the user to enter a positive integer larger than the first and reads it
in. Any numbers less than or equal
to the first integer should be rejected and the user must re-enter
A static method
with the header below must be called to implement a) and b) above. You must write the body of this method
so that it works as required.
public
static int getInteger(Scanner
s, int lowerBound)
c)
Prints
out the two numbers that were input by the user.
For example, see the run below:
|
Enter an integer > 0 -3 Enter an integer > 0 0 Enter an integer > 0 4 Enter an integer > 4 4 Enter an integer > 4 8 Your numbers are 4 and 8 |
2)
Consider
the main program below. Complete
the class TVClass so that the program will run as
shown with the output as shown. Be careful!
public class
pracCode2
{
public
static
void
main(String [] args)
{
TVClass myTV = new
TVClass("Sony");
myTV.setResolution(TVClass.HD);
myTV.setSize(34);
myTV.type = "Tube";
System.out.println(myTV);
}
}
Output:
|
Brand:
Sony Size:
34 Type:
Tube Resolution:
HDTV |
public class
TVClass
{
// Strings for resolution: STAN
= "Standard"
// ED
= "EDTV"
// HD
= "HDTV"
}