CS7 Exam 1 Practice Questions
Here are some good problems from the book (JBD) to try:
- Ch1: p.11-12 (1, 3, 10), p 13 (7b)
- Ch2: p. 48-49 (4, 5, 6, 7, 13, 19, 25), p.49-50 (4, 6, 7,
11)
- Ch3: p. 86-87 (5, 6, 7, 9, 10), p.89 (4)
And here are a few more. Assume all programs compile correctly
(any errors are unintentional) unless stated otherwise.
1. Determine the output:
class HelloWorld {
public static void main (String[] args) {
int a=0, b=1;
System.out.println(a + " " + b);
a += b*3;
System.out.println(a + " " + b);
--b;
System.out.println(a + " " + b);
System.out.println(a + " " + --b);
}
}
2. Determine the output
// assume a 3 is entered when prompted
import tio.*;
class HelloWorld {
public static void main (String[] args) {
String greeting = "Hello";
int num;
System.out.println(greeting + " Please enter a number: ");
num = Console.in.readInt();
if (num < 5)
System.out.print("That is just fine... ");
else
System.out.print("That is a great number... ");
System.out.println("for me to poop on!");
}
}
3. Write java declarations for the following:
- an integer named "girth" initialized to -1.
- an unitialized String intended to hold someone's middle name.
- three doubles named x1, x2, and y1 initialized to 0.5, 10.5, and
100.5 respectively.
4. Write a java program that reads in two integers and prints
their product, sum, and average.
5. Write a program that reads a single integer then prints "YES"
if the number is even and divisible by 4, and a "NO" otherwise.