Name one type of error and describe when it occurs.
Syntax error: at compilation
Runtime error: at runtime
Logic error: after runtime
What will the following statement print out?
String x = "hello";
if (x.length < 10) {
x = x + "world";
}
System.out.println(x);
helloworld
What operator is used to check equality in Java?
==
Name a class that Java offers.
Math, Character, Scanner, String, etc.
Name a keyword (there are two) that starts a loop.
While, for
Declare a constant, social security number, as 99999999.
final int SOCIAL_SECURITY_NUMBER = 9999999;
Write a for loop that prints out your name 10 times.
for (int i = 0; i < 10; i++) {
System.out.println(myname);
}
What data type holds the result of a comparison?
Boolean (True or False)
List three functions of the String class and describe what they do.
charAt(), substring(), equals(), length(), concat(), eqaulsIgnoreCase(), contains(), etc.
How many times will the following loop repeat?
for (int i = 1; i <= 5; i++) { System.out.println(i); }
Binary Language
Assembly Language, Bytecode
Java, C#, Python, etc.
What is wrong with the following loop?
int i = 0;
while (i < 1) {
System.out.println("I'm failing this exam");
i--;
}
The loop repeats forever.
And also a bad attitude.
Describe the symbols in JAVA SYNTAX of the three main logical operators.
1. AND = '&&'
2. OR = '||'
3. NOT = '!'
Write a statement that finds a random number between 1 and 50.
int num = (int)(Math.random() * 50) + 1;
What is a sentinel value?
A specific value (typically inputted from the user), that terminates a loop when it occurs.
Write the Java statement using printf that has 10 spaces after "Exam2"
System.out.printf("%8s", "Exam2");
Write a program that validates a phone number (A String). The number must contain only digits and must be a length of 10.
if (phone.length() != 10) {
valid = false;
} else {
for (int i = 0; i < phone.length(); i++) {
char ch = phone.charAt(i);
if (!Character.isDigit(ch)) {
valid = false;
break;
}
}
}
System.out.println(valid);
If x = 2, y = 5, and z = 1, what is the result of:
!(x >= z) || ((y != 2) && (z > 0))
TRUE
Using Character class functions, find if the fourth character of the string x is an upper case letter.
Character.isUpperCase(x.charAt(3));
What are the three parts to any loop? (The loop continuation-condition)
1. initialization of the loop control variable
2. the test or comparison done on the loop control variable
3. modification of the loop control variable
Describe what happens when Java source code is compiled and run on the JVM.
1. Source code is complied by the Java Compiler.
2. Compiler generates a .class file or a bytecode executable file.
3. The JVM executes the code so that it can be run on any machine.
The following code is meant to calculate the sum of all even numbers entered by the user until they type -1. However, it doesn’t work correctly. Identify what’s wrong and show the corrected version.
Scanner input = new Scanner(System.in);
int num = 0;
int sum = 0;
while (num != -1) {
if (num % 2 == 0)
sum += num;
num = input.nextInt();
}
System.out.println("Sum = " + sum);
Problem: The user’s first input is never read before the loop starts, so num stays 0 and the first real input is skipped.
Solution: put a num = input.nextInt(); before the loop.
Write a statement that will print out if a number is even or odd, and if the number is divisible by 5.
if (x % 0 == 1) {
System.out.println("Number is odd");
if (x % 5 == 0) {
System.out.println("Number is divisibile by 5"); }
}
else {
System.out.println("Number is even");
if (x % 5 == 0) {
System.out.println("Number is divisibile by 5"); }
}
String name = "averyspears";
String firstname = name.substring(0, 4);
String lastname = name.substring(5);
String formattedName =
Character.toUpperCase(firstname.charAt(0)) + firstname.substring(1) + " " +
Character.toUpperCase(lastname.charAt(0)) + lastname.substring(1);
Write a loop that averages the numbers 13 through 21 and prints the result.
int sum = 0;
for (i = 13; i < 22; i++) {
sum += i;
}
System.out.println(sum/9);