Week 1
1) What best defines a "programming language"?
- It allows us to control a computer.
- It allows us to make a calculation.
- It allows to execute a program.
- It allows us to express an algorithm.
2) Select the valid identifiers from the following list. State why each of the others is not valid.
- 123
- $50
- 50dollars
- abs
- hunter
- @60cents
- a_b
- #define
3) We learned about basic programs such as Hello World!
How can we modify this to print a whole message?
Let's print two lines:
- First: Hello, my name is [YOUR NAME HERE].
- Second: How are you?
4) Write a Java program that creates TWO integer variables to the values of your choice (e.g. 4 and 5, 12 and 10, etc.) and computes and prints the following:
- the sum of the two numbers
- the difference between the two numbers
- the product of the two numbers
- the quotient of the two numbers
5) Answer the following:
- What is the result of the following Java expression: 5 + 6 * 3 ?
- How does the order of operations affect the evaluation of the following expression: 4 + 5 * 2 - 3 ?
- Evaluate the following Java expression: 10 / 2 + 3 * 5 - 4
-
What is the output of the following code snippet?
int a = 2 + 3 * 4 - 1; System.out.println("Result: " + a);
- Rewrite the following expression using parentheses to explicitly define the order of operations: 8 - 2 * 4 / 2 + 3
-
What is the value of the variable 'b' after executing the following
code snippet?
int b = 10; b += 2 * 3 - 1;
6) Trace the following program:
public static void main(String [] args) {
int a, b, c, d, e, f, g;
a = 10;
b = 27;
System.out.println("a is " + a + ", b is " + b);
c = a + b;
System.out.println("a+b is " + c);
d = a - b;
System.out.println("a-b is " + d);
e = a+b*3;
System.out.println("a+b*3 is " + e);
f = b / 2;
System.out.println("b/2 is " + f);
g = b % 10;
System.out.println("b%10 is " + g);
}
7) Write a Java program to print the area and perimeter of a rectangle.
- initialize two integer variables:
- one for length
- one for width
- compute the formula for area: a = wl
- compute the formula for perimeter: p = 2(l+w)
- print and label the results
8) Write a Java program to convert temperature from Fahrenheit to Celsius degrees.
9) What will be printed by the following program?
public static void main(String [] args) {
int w, x;
double y;
w = -9;
x = Math.abs(w + 5);
y = Math.sqrt(x);
System.out.printf("%d %d %.2f%n", w,x,y);
}
10) Assuming that the variable x has already been declared and assigned a value, is the following assignment legal? If not why?
11) Write a Java program that initializes the radius of a circle and computes and prints the circle's area and circumference (perimeter). (Limit each result to a precision of 3 decimal places)
- Formula for area: πr^2
- Formula for circumference (perimeter): 2πr
12) Write a Java expression that would compute the following: