Download 5 points

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
NOTE: This exam was intended for a one hour attempt with extra time if needed. I have removed a few
questions to better fit our time.
What is your name? _______________________________
What section are you in? ___________________________
Do not look at the questions until you are told you may do so. Answer all questions to the best of your ability.
An answer that is unreadable will be marked wrong. On questions requiring one or more sentences for the
answer, assume your audience is a fellow student who needs the answer explained to them.
You may NOT use a calculator on this test/exam. Do not leave any answer in the form of an equation, even
expressing the answer as some number to a power, unless the question explicitly indicates you may do so.
All tests should have questions 1 through 8. Make sure your test has all 8 questions.
You may NOT discuss the test with anyone who may be taking the test/exam at a later time/date.
Page 2:
__________________ (18 points)
Page 3:
__Question 3 was omitted_(5 points)
Page 4:
__________________(14 points)
Page 5:
__________________(20 points)
Page 6:
__________________(10 points)
Page 7:
__________________(10 points)
Page 8:
__________________(20 points)
SUB-TOTAL:
TOTAL:
___________________(102 points total)
___________________ (of 100%)
I have not rendered or received unauthorized assistance in taking this examination.
_____________________________________________________
Question 1 (18 points)
Use Wages.java from the Reference Page to complete these questions.
a) List all of the primitive data types in Wages.java.
b) List all of the reference data types in Wages.java.
c) List all of the primitive variables in Wages.java.
d) List all of the constants (of any kind) in Wages.java.
e) List all of the primitive literals found in Wages.java.
f) List the line numbers in Wages.java of all statements containing boolean expressions.
g) List all of the operators present in Wages.java. (List only once if an operator is used more than
once.)
h) List the line numbers of all statements in Wages.java that declare primitive variables.
i) List the line numbers of all statements in Wages.java that contain a constructor.
Question 2 (5 points)
Classify the following as R (run-time error), C (compile-time error), or L (logic error).
____ A java statement that tries to assign a float value into an int variable.
____ A program that attempts to divide by zero.
____ A program that has more open curly braces (‘{‘) than closed curly braces (‘}’).
____ A program containing the expression (var1 + var2 / var3) that is incorrect because the author
wanted the addition to be performed first.
____ A program that uses a nextInt() method to read a value, but the user types the value as “abc”.
Question 4 (14 points)
Some of the questions below ask you to write a statement, others an expression. You must answer
with the entity the question asks for. DO NOT write a declaration statement unless the question
explicitly asks you to do so.
a) Write a java statement that declares myInt to be an int variable.
b) Write a java statement that declares the String constant BLUE with a value “I am blue
today”.
c) Write a java statement that declares stdIn to be a Scanner object.
d) Write a java statement that assigns the value 3.14159 to the float variable pi.
e) Write a java statement that increases the value in the int variable yourInt by 5.
f) Write a java expression that yields a boolean value that indicates if the int variable jj is
odd.
g) Write a java statement that assigns the int variable len the length of String variable
str.
Question 5 (20 points)
What data type and value do the following java expressions evaluate to? If an expression would
result in a syntax error (cannot be evaluated) write the word “INVALID” in place of the answer.
Use the following variable declarations:
int i5 = 5;
int i6 = 6;
d3 = 3.0;
double
Data Type Value
i5 + (i6 % 4)
a.
!( 5 == (( 5 / 2) * 2))
b.
((i5 < 3) && (i5 != i6))
c.
true && (3 == (6/2) == 1)
d.
true || ((24 % i6) <= 0)
e.
i6 / i5
f.
(5 % 3) + (8 / 16)
g.
i5 % i6
h.
!(true || (i6 < i5))
i.
true && !(i6 < i5)
j.
Question 6 (10 points)
What is output by the following Java code fragment?
// beginning of code fragment
int abc;
int myInt;
abc = 25;
if (abc == 0)
abc = 95;
else
abc = 82;
myInt = 13;
if (abc <= 90 || myInt == 0 || abc < myInt)
myInt = 50;
else
{
System.out.printf(“Number is %d.\n”, myInt);
if ( abc == 82)
myInt = abc / 2;
System.out.printf(“Number is %d.\n”, myInt);
}
if (abc != 95 && myInt < abc)
System.out.printf(“Done.\n”);
System.out.printf(“Finally.\n”);
// end of code fragment
Output:
Question 7 (10 points)
The following program fragment asks the user for their age. You must add code to output a specific message
based on the age using on the chart below:
Age criteria
If the age is more than 55
If the age is between 12 and 55
inclusively
If the age is less than 12
Message to print
“Senior discount\n”
“Regular price\n”
“Chile free\n”
import java.util.Scanner;
public class AgeCalculator
{
public static void main (String args[])
{
int myAge;
Scanner stdIn;
stdIn = new Scanner(System.in);
System.out.printf(“Enter your age to determine your discount: “);
myAge = stdIn.nextInt();
// put the rest of your code here.
}
}
Question 8 (20 points)
The following program has been partially written, but you need to add the following functionality.
Professor Smith has the following grading scheme for his Basketweaving 101 course that is only offered credit/no credit. A
student’s grade is based on the following criteria:
A student receives credit if they attended is 45 classes regardless of final average.
A student who does not have 45 classes attended can receive credit if their final average is above 65 or if their final project
is graded 85 or higher.
You must calculate whether or not the student receives credit for the course based on the input values. You will then print
the results with either the message, “Credit” or “No Credit”, depending on the result of the calculation.
import java.util.Scanner;
public class BasketWeaving
{
public static void main(String[] argv)
{
int
attendance;
double
finalAverage;
double
projectGrade;
Scanner stdIn;
stdIn = new Scanner(System.in);
System.out.printf(“Number of classes attended? “);
attendance = stdIn.readInt();
System.out.printf(“\nFinal average? “);
finalAverage = stdIn.readDouble();
System.out.printf(“\nProject grade? ? “);
projectGrade = stdIn.readInt();
// put the rest of the code here.
}
}
Reference page.
Wages.java – Use to complete question number 1.
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
import java.util.Scanner;
public class Wages
{
public static void main (String[] args)
{
final double RATE = 8.25;
final int
STANDARD = 40;
int
double
Scanner
hours;
pay;
keyboard;
keyboard = new Scanner (System.in);
pay = 0.0;
System.out.print ("Enter the number of hours worked: ");
hours = keyboard.nextInt();
System.out.println ();
if (hours > STANDARD)
pay = STANDARD * RATE + (hours - STANDARD) * (RATE * 1.5);
else
pay = hours * RATE;
System.out.printf("Gross earnings:
}
}
$%6.2f\n", pay);