Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
2008 BHCSI Introduction to Java
Test #3 (100 points) Solutions
7/25/08
(Note: Assume that java.util.* is imported for all programs.)
1) (6 pts) What is the output of the following program?
public class Q1 {
public static void main(String[] args) {
int x = 2, y, z;
y = 3+4*x;
z = (y+x)%7;
x = x*z/3;
System.out.println("x = "+x);
System.out.println("y = "+y);
System.out.println("z = "+z);
}
}
x = 4
y = 11
z = 6 (Grading 2 pts each)
2) (12 pts) In each of the following questions, assume that age is an integer variable that
stores how old a person is. Based on the question, write the appropriate if statement to
produce the desired output.
a) Write an if statement that prints out "You are an adult." (without the
quotes) if the person in question is 21 years or older.
if (age >= 21) (2 pts – 1 cond, 1 print)
System.out.println("You are an adult.");
b) Write an if statement that prints out "You get a cheap haircut." if
the person in question is a young child (12 or younger) or a senior citizen (60
or older.)
if (age <= 12 || age >=60) (5 pts – 4 cond, 1 prnt)
System.out.println("You get a cheap haircut.");
c) Write an if statement that prints out "You should be working." if
the person in question is in between 22 and 60 years old, inclusive.
if (age >= 22 && age <= 60) (5 pts – 4 cond, 1 prnt)
System.out.println("You get a cheap haircut.");
3) (10 pts) What is the output of the following program?
public class Q3 {
public static void main(String[] args) {
int x = 6;
for (int i=3; i<=7; i++) {
x = 2*x - i;
System.out.println(x);
}
}
}
9
14
23
40
73
(2 pts per response)
4) (15 pts) Write a program below that reads in a set of integers from the file
"grades.txt" and prints out the average of those integers. In particular, continue
reading integers from the file until -1 is read in. When calculating the average, don't
include -1. (For example, if the contents of the file were 3 4 7 2 -1, then your program
should print out 4.)
public class Ave {
public static void main(String[] args) {
Scanner fin = new Scanner(new File("grades.txt"));
int grade = fin.nextInt();
int sum = 0, numGrades = 0;
while (grade != -1) {
sum += grade;
numGrades++;
grade = fin.nextInt();
}
System.out.println("Average = "+(1.0*sum)/numGrades);
}
} (Grading: 2
2
2
1
pts scanner, 2 pts init vars, 3 pts loop,
pts add accum var, 2 pts count grades,
pt read grade in loop, 2 pts print ave –
pt math, 1 pt double division)
5) (15 pts) You wrote a program that printed out various patterns of stars. Write a
method that prints out a parallelogram of stars. For example, a parallelogram of stars that
has 4 rows with 7 stars each looks like this:
*******
*******
*******
*******
Your method will take in two pieces of information: the number of rows and the number
of stars on each row. Fill in the method prototype below:
public static void printPara(int numRows, int starsPerRow)
{
int i,j; (1 pt var dec)
for (i=0; i<numRows; i++) { (4 pts)
for (j=0; j<i; j++) (4 pts for spaces)
System.out.print(" ");
for (j=0; j<starsPerRow; j++) (4 pts stars)
System.out.print("*");
System.out.println(); (2 pts go next line)
}
}
6) (5 pts) Write a single method call that prints the following:
****
****
****
printPara(3,4) (1 pt func name, 2 pts each param)
7) (5 pts) What is wrong with the following segment of code?
Scanner stdin = new Scanner(System.in);
String name = stdin.next();
if (name == "Bob");
System.out.println("Hi Bob!");
Strings can't be compared using ==, need a method, like
equals(). (Grading – use your judgement)
8) (10 pts) Write a method that returns n!. Remember, that n! = 1x2x3x…xn. The method
prototype is given for you below.
public static int factorial(int n) {
int answer = 1;
(3 pts)
for (int i=1; i<=n; i++) (5 pts)
answer = answer*i;
(5 pts)
return answer;
(2 pts)
}
9) (10 pts) Write a method that takes in no parameters and prompts the user to enter an
integer and then returns that integer. Fill in the method prototype below:
public static int getMenuChoice() {
Scanner stdin = new Scanner(System.in); (3 pts)
System.out.println("Enter a number."); (3 pts)
return stdin.nextInt(); (2 pts for reading, 2 for return)
}
10) (9 pts) Explain what each of the following Java compiler/run-time errors means:
a) cannot find symbol method readLine()
The system doesn't think readLine() is defined. Most likely, you are calling the
method on something other than a Scanner object.
b) Exception in thread "main" java.util.InputMismatchException
You tried to read in one type of information, but got a different type. For example,
this happens when you use nextInt() but the value read in is a double.
c) possible loss of precision
You are storing something more precise into a variable that has less precision,
potentially. This might happen when you assign an int to an expression that could
evaluate to a double.
Grading: 3 pts each, use judgement
11) (3 pts) What computer language is named after mathematician Blaise Pascal? (Hint:
There is a triangle with the same name!)
Pascal (Give to everyone)