Download Chapter 2 Test Review New ANS

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

Name mangling wikipedia , lookup

Falcon (programming language) wikipedia , lookup

String literal wikipedia , lookup

Go (programming language) wikipedia , lookup

Scala (programming language) wikipedia , lookup

One-pass compiler wikipedia , lookup

Java syntax wikipedia , lookup

Java (programming language) wikipedia , lookup

CAL Actor Language wikipedia , lookup

Java performance wikipedia , lookup

C syntax wikipedia , lookup

C Sharp syntax wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

Transcript
Ch 2 T Rev 12-13 page 1
Intro to Programming in Java
Chapter 2 Test Review
Name ___________________________
.
Part 1 – Multiple Choice, T/F, Fill-In
1. a. What is the output of the following segment of code?
pulic class Question
{
public static void main(String[ ] args)
{
System.out.print("Hello");
System.out.println("Mary " + “John");
System.out.println("How are" + "you today");
}
}
b. Add a "reasonable" comment that describes the purpose of this program.
2. Write the code to output the text "Java Essentials", including the quotation marks, to the screen.
3.
Name the numeric primitive types in the java language.
4.
What is the value of z if we execute the following assignment statement?
double z = 4 / 8;
5.
What is the value of y and z if we execute the following assignment statements?
a. int z = 30 / 10.0;
b. double y = 14/6.0;
Ch 2 T Rev 12-13 page 2
6.
If x is an int and y is a double, will x = y generate a compile time error? Why or why not?
7.
Give the order that the operators will be applied based on operator precedence for the following
assignment statement.
a = e -f / d * (a + b);
8.
What is the result of 45/30/6 in java?
9.
What is output with the statement System.out.println(x+y); if x and y are int values
where x=7 and y=3?
10. What is output with the statement System.out.println(""+x+y); if x and y are int
values where x=7 and y=3?
11. Which library package would you import to use the class Random?
12. Assume that x is a double that stores 0.362491. To output this value as 36%, you could use the
NumberFormat class with NumberFormat nf = NumberFormat.getPercentInstance(
). Write a statement that outputs x as 36%.
Ch 2 T Rev 12-13 page 3
13. a.
What does the class Questions (defined below) compute? (What is its purpose?)
import java.util.Scanner;
public class Questions
{
public static void main(String[ ] args)
{
Scanner in = new Scanner(System.in);
int x, y, z;
double average;
System.out.println("Enter an integer value");
x = in.nextInt( );
System.out.println("Enter another integer value");
y = in.next.Int( );
System.out.println("Enter a third integer value");
z = in.nextInt( );
average = (x + y + z) / 3;
System.out.println("The result of my calculation is " +
average);
}
}
b. What is output if x = 1, y = 2, z = 2?
14. Write two different statements that assign "GREAT" to a String variable x.
15. If x is the String "Good Work", then what will be output by the statement
System.out.println(x.toUpperCase().toLowerCase()).
16. If String name = "Mrs. Skidmore"; then what is the output for the command
System.out.println( name.length())?
Ch 2 T Rev 12-13 page 4
17. What value will a variable of type boolean store?.
18. In Java, are ‘a’ and ‘A’ considered to be the same values?
19. Name at least 2 of the classes we have used that are part of standard Java.
20. Which library do you need to import to use NumberFormat and DecimalFormat?
21. What are some of the Random class methods? What do they return?
22. What is the value of (4 * 3 – 6) / 5 ?
23. Name at least four reserved words in Java.
24. a.
b.
What is a Java variable?
Which keyword (reserved word) is used to be sure this variable will not change its value?
25. Write a set of instructions to prompt the user for an int value using the Scanner class and assign
it to the variable int num1; then prompt the user for a double value and assign it to the variable
double num2.
Ch 2 T Rev 12-13 page 5
26. When might you use a boolean variable; a double variable; a char variable; a String
variable; an int variable?
27. Review the gas mileage program we did in class. Know how to write the statements that compute
the gas mileage assuming the other values have already been input as int values.
28. How do you output a “ \” or “ (quotation marks) in a Java String? Write a println statement
that does that.
29
What do we call the "=" symbol in Java? How do we use it?
Ch 2 T Rev 12-13 page 6
Part 2: Programming Part
You will be asked to write 3 Java programs.
•
The first one will ask you to prompt the user for his/her name then output a greeting message
using that name but print it in all capital letters. (Hint: you will need to use the toUpperCase()
method of the String class.)
•
The second one will ask you to write a program that asks for the number of people attending an
event and will prompt the user for the number of people attending as well as the cost of the
event. You will then let the computer compute the total cost for the group using
NumberFormat and getCurrencyInstance(). The code for the formatting will be
provided.
The third program will be to calculate output using / and % similar to the one you did with minutes and
seconds