CS 0401 Spring 2010
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
2)
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) We can categorize Java instance methods into 3 groups: constructors, ____________________________ and __________________________
True / False: Indicate whether each of the following statements is true or false. For false statements explain WHY they are false.
1)
2)
The following
3)
The scope of
4) If there are no statements in the loop body of a for loop, the loop will either have no iterations or will be an infinite loop.
5) In order to effectively use some new class, Foo, I need to know what instance variables Foo has and how they are manipulated.
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"
}