Download Solutions

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
Sample Test #1 Solutions
(Note: Assume that java.util.* is imported for all programs.)
1) (9 pts) What do each of the following expressions evaluate to in Java? (Hint:
remember to think about the difference between integer and real division.)
a) 5 - 2 = 3
d) 7 * 3 = 21
g) 29 % 9 = 2
b) 10 - 4 = 6
e) 21/4 = 5
h) 17%18 = 17
c) 8 / 2 = 4
f) 17/4.0 = 4.25
i) 15/26 = 0
2) (12 pts) What do each of the following expressions evaluate to in Java? (Hint: Make
sure to follow order of operations?)
a) 2 + 4 * 6 – 8 = 18
b) 9/4 + 6%10 = 8
c) 16 - 2*(4 + 14/4) = 2
d) 12.0/8 - 4*(12%(2+4)) = 1.5
3) (4 pts) Circle the LEGAL Java variable names from the set below
student
class
new position
Scanner
4) (5 pts) Write a single println statement that produces the following output:
This summer camp rocks!
Well at least “I” think so.
System.out.println(“This Summer Camp rocks!\n Well at least
\" I \” think so");
5) (6 pts) Write the two Java statements necessary to read in a string from the keyboard
into the variable country. You may assume that a Scanner object named input is
already declared to read from the keyboard.
String country;
country = input.next()
6) (8 pts) What do each of the following boolean expressions evaluate to in Java? (Hint:
All answers will be either true or false.)
a) (6-3) == (12%4)
true
c) 3*(14%7) > 5 false
b) !((5 * 6) >= 20)
false
d) 23 <= (13% 21 *2) true
7) (14 pts) It takes 2 cups of pasta sauce to make a medium pizza. A medium pizza has 8
slices and can feed 4 hungry 3rd graders. Complete the program below so that it prompts
a soccer mum for the number of cups of pasta sauce that she has in her kitchen and prints
out the number of slices of pizza that she can make and how many hungry 3rd graders that
she can feed. Assume that she can only have an integral number of cups of pasta sauce
and she always makes an integral number of pizzas.
public class BakePizza
{
final int cupsPerPizza = 2;
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int cupsSauce;
int num3Graders;
System.out.println("How many cups of pasta sauce do
you have?");
cupsSauce = input.nextInt() ; (2)
numSlices = (amtSauce / cupsPerPizza)
num3Graders = numSlices /2;
(2)
(6)
}
}
* 8;
(4)
System.out.println("With “ + cupsSauce + “cups of
pasta sauce, you can make “ + numSlices + “slices of
pizza, to feed “ + num3Graders + “ hungry 3rd
graders”.);
8) (6 pts) What is the output produced by the following segment of code?
int num1,num2,num3;
num1 = 8;
num2 = 4;
num3 = 3;
if (2*num1 == num3)
System.out.print("1");
else if (num3%num2 < (num1 – num2))
System.out.print("2");
else
System.out.print("3");
System.out.println(“123”);
2123
9) (4 pts) What is the output produced by the following segment of code below? Circle
the correct choice:
No output
A
B
AB
int c = 2, d = 1;
if (c < d)
{
if ((2*c > 3*d) && !(c < d))
System.out.print("A");
else
System.out.print("B");
}
Note: This is the matching-else problem. The else matches
the inner if. Since the outer if is false, there's no else
for it, so nothing gets printed.
10) (15 pts) Review the following program and fix the 5 syntax errors
/* Sum.java
KungFu Panda
Program prompts for two numbers and returns their sum */
public class Sum
{
public static void Main(String[] args)
{
Scannner input = new Scanner(System.in);
int num1 = input.nextInt();
double num2 = input.nextInt();
double sum = num1 + num2;
System.Out.print(“The sum of ”+ num1 );
System.out.println(“ and ” + num2 + “is” + sum);
}
}