Review Questions and Excercise
|
|
Here are some review questions and exercises to test your knowledge and exercises to give you practice in Java programming.
Review Questions
- What is the difference between System.out.println and System.out.print?
- Where does a Java program begin execution?
- Why do escape sequences exist? Can you think of another way to handle these characters?
- What is the proper format of variable and literal names? Give 3 examples.
- How do you create new variables to be used? (Hint: 'declare' would be a better word)
- What are variables and literals used for?
- What are the primitive variables? Give an example of each.
- What is the order of precedence, for arithmetic operators?
- Why use brackets, to separate statements?
- What would be the result of the following casts?
- Given a long data type named value equal to 50:
(int)value;
(long)25.7
(long)5.5E16
(float)1.23e240
(int)2500000000L
(double)67890000
(double)600L
- What is the length of the following Strings?
- "Pretty short String"
- "\tTab and\nNewline"
- "\\\\slashes!"
Exercises
- There are several syntax errors in the following program. Find the errors, fix them, and then try running the program.
public Class BrokenProgram { public static void main(String args[]) { int value1; int value2; int result; value1 = 25; value2 = 5;
result = value1 / value2 System.out.println("When you divide " value1 " by " value2 ", the result is: " result); } }
- You have been asked to look at this code. The manager says "The greatest mind in the world wrote this program. However, for some reason it's not outputting the correct sum." The manager says the sum should equal 20. The values are correct at value1 = 11 and value2 = 9. What's the problem?
public class Wrong_Sum { public static void main(String[] args) { int value1 = 10; int value2 = 10; int sum; sum = value1++ + --value2; System.out.println("value1 = " + value1 + ", value2 = " + value2); System.out.println("sum = " + sum); } }
- Write a program that asks the user to
- enter the price of a drink
- enter the price of a sandwich
The program should then
- sum the cost of these two items
- add GST and PST to the sum
- print the total cost with taxes included
- We want to write a program that stores the phrase X+Y=Z in a String, where and X and Y are variables and Z is the sum of those to variables. It should then output the string to the screen. You are given the following code:
public class StringCreator { public static void main(String []args) { int x=10; int y=21;
String result=???
System.out.println(result); } }
Fill in the missing assignment statement for the String result.
|